Lagrange插值
对给定的n个插值点x1,x2,x3…xn(xi的值互不相同)及对应的函数值y1,y2,y3…yn,利用构造的n-1次Lagrange插值多项式,则对插值区间内任意的x的函数值y可通过下式求得:
Lagrange函数matlab: Lagrange.m
function y=Lagrange(x0,y0,x)
ii=1:length(x0);
y=zeros(size(x));
for i=ii
ij=find(ii~=i);
y1=1;
for j=1:length(ij)
y1=y1.*(x-x0(ij(j)));
end
y=y+y1*y0(i)/prod(x0(i)-x0(ij));
end
Hermite插值
满足在在节点的函数值相等,同时要求导数值也相等
已知n个插值点和对应的函数值,及一阶导数,则对插值区间内任意x的函数值y的插值公式:
Hermite函数matlab: Hermite.m
function y=hermite(x0,y0,y1,x)
n=length(x0);
m=length(x);
for k=1:m
yy=0.0;
for i=1:n
h=1.0;
a=0.0;
for j=1:n
if j~=i
h=h*((x(k)-x0(j)))/(x0(i)-x0(j))^2;
a=1/(x0(i)-x0(j))+a;
end
end
yy=yy+h*((x0(i)-x(k))*(2*a*y0(i)-y0(i)+y0(i)));
end
y(k)=yy;
end
请发表评论