% newtdd.mm文件
function [c, v] = f(x, y)
n = length(x);
x = x(:);
v = [y(:) zeros(n, n-1)];
for i = 2:n
v(i:end, i) = (v(i:end, i-1)-v(i-1:end-1, i-1))./(x(i:end)-x(1:end-i+1));
end
c = diag(v);
end
% M文件
n = 4; % 这里只取4个点,则插值成三次函数
x = linspace(-5, 5, n);
xx = linspace(-5, 5, 100)';
func = {@(x)5./(1+x.^2), @(x)atan(x), @(x)x./(1+x.^4)};
for fi = 1:3
y = func{fi}(x);
[c v] = f(x, y)
yy = c(n);
for i = n-1:-1:1
yy = yy.*(xx-x(i)) + c(i);
end
subplot(1, 3, fi);
plot(x, y, 'o', xx, [func{fi}(xx) yy]);
end
结果如示: