牛顿插值
disp(Newton_fun(2010));
x=linspace(1994,2010);
y=zeros(1,100);
for i=1:100
y(i)=Newton_fun(x(i));
end
plot(x,y);
function sum= Newton_fun(x)
newton=ones(10);
X=[1994,1995,1996,1997,1998,1999,2000,2001,2002,2003];
Y=[67.052,68.008,69.803,72.024,73.400,72.063,74.669,74.487,74.065,76.777];
x;
for i=1:10
newton(i,1)=Y(i);
end
for j=2:10
for i=j:10
a=newton(i,j-1)-newton(i-1,j-1);
b=X(i)-X(i-j+1);
newton(i,j)=a/b;
end
end
sum=newton(1,1);
for i=2:10
product=1;
for j=1:(i-1)
product=product*(x-X(j));
end
sum=sum+product*newton(i,i);
end
end
拉格朗日插值法
clear;
X=[25,40,50,60];
Y=[95,75,63,54];
x=70,sum=0;
for i=1:4
product1=1;
product2=1;
for j=1:4
if i==j
else
product1=product1*(x-X(j));
end
end
for j=1:4
if i==j
else
product2=product2*(X(i)-X(j));
end
end
sum=sum+(product1/product2)*Y(i);
end
fprintf('%.1f\n',sum)