X=[1,2,3,4,5,6]; %X矩阵
Y=X.^3-4.*X; %Y矩阵
N=6 %总共六个结点
f=zeros(N,N); %建立一个N维零方阵
for k = 1 : N
f(k,1)=Y(1,k); %先给第一列赋值Y
end
for i = 2:N %列
for k = i:N %行
f(k,i)=(f(k,i-1)-f(k-1,i-1))/(X(k)-X(k-i+1));
%这个X(k-i+1)要仔细琢磨一下其余的直接抄公式
end
end
syms x;
y=Y(1)+f(2,2)*(x-1)+f(3,3)*(x-1)*(x-2)+f(4,4)*(x-1)*(x-2)*(x-3);
再举一个例子
X=[0.4,0.5,0.6,0.7,0.8];
Y=[-0.916291,-0.693147,-0.510826,-0.357765,-0.223144];
N=5
f=zeros(N,N);
for k = 1 : N
f(k,1)=Y(1,k);
end
for i = 2:N %列
for k = i:N %行
f(k,i)=(f(k,i-1)-f(k-1,i-1))/(X(k)-X(k-i+1));
end
end
x=0.54;
y=Y(1)+f(2,2)(x-0.4)+f(3,3)(x-0.4)(x-0.5)+f(4,4)(x-0.4)(x-0.5)(x-0.6)+…
f(5,5)(x-0.4)(x-0.5)(x-0.6)(x-0.7);