Here is another way to describe the matlab code
We look directly at the code, the detailed theorem please find yourself.
The code has a.mlx filename extension
x= [8.1, 8.3, 8.6, 8.7];y= [16.94410, 17.56492, 18.50515, 18.82091];%%初始散点
[p,D]=newt(x,y)
simplify(p)%%化简
t=linspace(8.1,8.7,100);%%生成等距节点
b=subs(p,t)
plot(t,b)%%绘制曲线图
hold on
scatter(x,y)%%绘制散点图
function [p,D]=newt(x1,y1)
%D:均差表
%p:插值多项式
syms x %%定义符号变量,进行符号运算,方便求插值表达式
N=length(x1);%%计算x1数组的数据数量
D=zeros(N,N);%%初始化差商表
D(1:N,1)=y1';%%差商表第一列赋值
%%计算差商表
for k=2:N
for i=k:N
D(i,k)=(D(i,k-1)-D(i-1,k-1))/(x1(i)-x1(i+1-k));
end
end
p=D(1,1);%%插值多项式初始化
for k=2:N
t=1;
for j=1:k-1
t=t*(x-x1(j));
end
p=D(k,k)*t+p;
end
end
*Running result
!!The data of b is not completely captured, please run it yourself to see the detailed results!!
Please correct any mistakes. Thank you!!