数值分析之牛顿插值(MATLAB)
x1=input('输入节点坐标x=')
y=input('输入节点坐标函数值f(x)=')
x2=input('输入所要计算的节点x2=')
syms x
n=length(x1);
%%%%%%%%%%%%%%%%%%%%差商的求法
for i=2:n
f1(i,1)=(y(i)-y(i-1))/(x1(i)-x1(i-1));
end
for i=2:n
for j=i+1:n
f1(j,i)=(f1(j,i-1)-f1(j-1,i-1))/(x1(j)-x1(j-i));
end
end
f1=[y',f1]% 输出带0阶差商的差商表格
%%%%%%%%%%%%%%%%%%%%%%%%%%Newton插值函数
Newton=f1(1,1);
for i=2:n
tt=1;
for j=1:i-1
tt=tt*(x-x1(j));
end
Newton=Newton+f1(i,i)*tt;
end
fprintf('Newton插值函数为n')
expand(Newton)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
x=x2;
p=eval(Newton);
fprintf('Newton插值函数在所求点x2的函数值为n')
p