你好同学,写好newton,一样招呼:
function v = newton(x,y,u)
n = numel(y);
p = ones(size(u));
v = y(1)*p;
for i = 2:n
y = diff(y)./(diff(x)*(i-1));
p = p.*(u-x(1));
v = v + p*y(1);
x = x(2:end);
end
end
主函数
clc;clear
x = [4,9,16,25,36,49,64,81,100];
y = [2,3,4,5,6,7,8,9,10];
% 第(1)题
u = 1:1:100;
for i = 2:9
subplot(2,4,i-1)
plot(x,y,'ro'); hold on
v = newton(x(1:i),y(1:i),u);
plot(u,v,'b-');
title([num2str(i-1),'阶Newton'])
end
% 第(2)题
u = 10:10:200;
vn = arrayfun(@(i)['n=',num2str(i-1)],2:9,'uniform',0);
rn = arrayfun(@(i)['x=',num2str(u(i))],1:numel(u),'uniform',0);
T = table('Size',[numel(u),8],'VariableTypes',repmat("double",1,8),'variablenames',vn,'rownames',rn);
format short
for i = 2:9
v = newton(x(1:i),y(1:i),u);
T{:,i-1} = v(:);
end
T