① 你好,可能幫我用matlab編個牛頓插值法的程序嗎題目大概是知道5個點,用牛頓插值法求某一點的值
可否貼一下具體數據點,這里給出一個測試代碼,希望有所幫助。代碼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