插值法
2010年10月24日
11:16
牛顿插值与拉格朗日插值ployfit实现多项式回归matlab的m文件:
Function yint=newtint(x,y,xx)
%newtint:newton interpolating polynomial
%yint=newtint(x,y,xx):uses an (n-1)-order newton
%to determine a value of the dependent variable (yint)
%at a given value of the independent variable,xx.
%input:
%x=independent variable
%y=dependent variable
%xx=value of independent variable at which Interpolation is calculated
% output:
% yint=interpolated value of dependent variable
%compute the finite divided differences in the form of a
%difference table
n=length(x);
If length(y)~=n,error('x and y must be same length');
End
b=zeros(n,n);
%assign dependent variables to the first column of b.
b(:,1)=y(:);%the(:)ensures that y is a column vector.
For j=2:n
For i=1:n-j+1
b(I,j)=(b(i+1,j-1)-b(I,j-1))/(x(i+j-1)-x(i));
End
End
% use the finite divided differences to interpolate
Xt=1;
Yint=b(1,1);
For j=1:n-1
xt=xt*(xx-x(j));
Yint=yint+b(1,j+1)*xt;
End
-------------------------------------------------------------------------------------------------------------
拉格朗日插值方法m文件的编写
Function yint =lagrange(x,y,xx)
%lagrange:lagrange interpolating polynomial
%yint=lagrange(x,y,xx):uses an (n-1)-order
% to determine a value of the dependent variable (yint) at
% a given value of the independent variable ,xx.
%input:
% x=independent variable
%y=dependent variable
%xx=value of independent variable at which the interpolation is calculated
%output:
% yint=interpolated value of dependent variable
n=length(x);
If length(y)~=n,error('x and y must be same length');
End
s=0;
For i=1:n
Product=y(i);
For j=1:n
If i~=j
Product =product*(xx-x(j))/(x(i)-x(j));
End
分区 新分区 1 的第 1 页
End
End
s=s+product;
End
Yint=s;
分区 新分区 1 的第 2 页