资源描述:
《拉格朗日差值、牛顿插值以及三次样条插值的matlab实现》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、%Lagrange插值clearclc%-----------------------------n=10;%结点个数lb=-1;%下界ub=1;%上界step=0.01;%作图点步长%-----------------------------%原始函数图形x0=lb:step:ub;y0=1./(1+25*x0.^2);plot(x0,y0,'r-');holdon%-----------------------------%插值函数fori=1:n+1xi(i)=lb+(ub-lb)*(i-1)/n;yi(i)=1/(
2、1+25*xi(i)^2);end%------------------------------count=1;forx=lb:step:ubfl=0;%--------------------------%求出pn(xk)fork=1:n+1up=1;dn=1;%----------------------%求出f(xk)fori=1:n+1ifk~=iup=up*(x-xi(i));dn=dn*(xi(k)-xi(i));endend%----------------------fl=fl+yi(k)*up/dn;en
3、dpn(count)=fl;%--------------------------fi(count)=1/(1+25*x^2);%求原函数的值count=count+1;end%------------------------------%L插值函数图x=lb:step:ub;plot(x,pn,'g--')%------------------------------num=(ub-lb)/step+1;fori=1:nump_f(i)=pn(i)-fi(i);endcenter=fix(num/2);scale=fix
4、(num/10);a=center-scale;b=center+scale;disp'pn(i)-fi(i)的值为:'p_f(a:b)%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%clearallclc%Newton迭代法求解极小值点%=====================================disp'几点说明:'disp'1.程序中的函数采用课本P102例
5、3.3.2。'disp'2.只需改变所需要求的函数和初始点的值。'disp'==================================================='%=====================================%定义函数disp'函数f(x)为:'symsx1x2f=(x1-2)^4+(x1-2)^2*x2^2+(x2+1)^2disp'初始点的值:'x0=[1;1]%=====================================%求函数的梯度和海色阵disp'函数
6、f的梯度:'g=jacobian(f,[x1;x2])disp'函数f的Hesse矩阵:'G=jacobian([g(1);g(2)],[x1,x2])%=====================================%定义迭代的最大次数n=10;%=====================================%一些初始值的计算g0=subs(g,{x1,x2},{x0(1),x0(2)})';G0=subs(G,{x1,x2},{x0(1),x0(2)});f0=subs(f,{x1,x2},{x0
7、(1),x0(2)});%=====================================%迭代点集合x和函数值F的初始化x=zeros(2,n);F=zeros(1,n);%运用Newton方程解出下一近似值x(:,1)=x0-inv(G0)*g0;%注:用点乘有误A=x(:,1);F(1)=subs(f,{x1,x2},{A(1),A(2)});%=====================================%定义误差初始值为1deta=1;i=1;%循环用求出的近似解迭代Newton方程%求出下
8、一个近似解,并在规定的误差范围内whiledeta>=1e-10&i