跪求matlab中求牛顿插值法的编程,要测试过可以的

2024-12-05 07:43:47
推荐回答(1个)
回答1:

做个测试,希望有所帮助。代码function main()clcx=[0.40,0.55,0.65,0.80,0.90,1.05];y=[0.41075,0.57815,0.69675,0.88811,1.02652,1.25382];xhat=0.596;[yhat,dy,cout]=newtint(x,y,xhat);figure; hold on; box on;plot(x, y, 'r-*');plot(xhat, yhat, 'ko'); function [yhat,dy,cout]=newtint(x,y,xhat) %牛顿插值n=length(y);if length(x)~=n error('x和y不一致');endc=y(:);for j=2:n %计算差商矩阵 for i=n:-1:j c(i)=(c(i)-c(i-1))/(x(i)-x(i-j+1)); endendyhat=c(n);for i=(n-1):-1:1 %构造插值多项式 yhat=yhat.*(xhat-x(i))+c(i);endif nargout>1 yn2=c(n-1); for i=n-2:-1:1 yn2=yn2.*(xhat-x(i))+c(i); end dy=yhat-yn2; if nargout>2, cout=c;endend 结果