学号:5专业:电气工程题目:对于函数,进行拉格朗日插值。,按等距节点求分段线性插值,把和插值多项式的曲线画在同一张图上进行比较。观察Lagrange插值及数值积分中的分段性插值。f.m:functionf=f(x)f=1./(1+x.^2);endLagrange.mfunctiony=Lagrange(x0,y0,x);n=length(x0);m=length(x);fori=1:mz=x(i);s=0.0;fork=1:np=1.0;forj=1:nifj~=kp=p*(z-x0(j))/(x0(k)-x0(j));endends=p*y0(k)+s;endy(i)=s;End拉格朗日插值的曲线:x=[-5:1:5];y=1./(1+x.^2);x0=[-5:0.001:5];y0=Lagrange(x,y,x0);y1=1./(1+x0.^2);plot(x0,y0,'b')holdonplot(x0,y1,'r')使用龙格现象观察分段性插值的曲线:symsyxlx;y=1/(1+x^2);x0=-5:1:5;y0=zeros(1,length(x0));fori=1:11x=x0(i);y0(i)=eval(y);endM=-5:0.01:5;y1=zeros(1,length(M));n=1;fori=2:11forx=-5:0.01:5ifx<x0(i)&&x>=x0(i-1)lx(1)=y0(i-1)*(x-x0(i))/(x0(i-1)-x0(i));lx(2)=y0(i)*(x-x0(i-1))/(x0(i)-x0(i-1));y1(n)=lx(1)+lx(2);n=n+1;endendendezplot(y,[-5,5])holdonA=-5:0.01:5;plot(A,y1,'r');分别运行两个文件后的得到两个图形曲线:拉格朗日插值曲线分段插值曲线