資源描述:
《拉格朗日插值、牛頓插值的matlab代碼》由會員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在行業(yè)資料-天天文庫。
1、實(shí)驗(yàn)五多項(xiàng)式插值逼近信息與計(jì)算科學(xué)金融崔振威201002034031一、實(shí)驗(yàn)?zāi)康模豪窭嗜詹逯岛团nD插值的數(shù)值實(shí)現(xiàn)二、實(shí)驗(yàn)內(nèi)容:p171.1、p178.1、龍格現(xiàn)象數(shù)值實(shí)現(xiàn)三、實(shí)驗(yàn)要求:1、根據(jù)所給題目構(gòu)造相應(yīng)的插值多項(xiàng)式,2、編程實(shí)現(xiàn)兩類插值多項(xiàng)式的計(jì)算3、試分析多項(xiàng)式插值造成龍格現(xiàn)象的原因主程序1、拉格朗日function[c,l]=lagran(x,y)%c為多項(xiàng)式函數(shù)輸出的系數(shù)%l為矩陣的系數(shù)多項(xiàng)式%x為橫坐標(biāo)上的坐標(biāo)向量%y為縱坐標(biāo)上的坐標(biāo)向量w=length(x);n=w-1;l=zeros(w,w);fork=1:n+1v=1;forj=1:n+1ifk~=jv=co
2、nv(v,poly(x(j)))/(x(k)-x(j))%對多項(xiàng)式做卷積運(yùn)算endendl(k,:)=v;endc=y*l;牛頓插值多項(xiàng)式主程序function[p2,z]=newTon(x,y,t)%輸入?yún)?shù)中x,y為元素個數(shù)相等的向量%t為插入的定點(diǎn)%p2為所求得的牛頓插值多項(xiàng)式%z為利用多項(xiàng)式所得的t的函數(shù)值。n=length(x);chaS(1)=y(1);fori=2:nx1=x;y1=y;x1(i+1:n)=[];y1(i+1:n)=[];n1=length(x1);s1=0;forj=1:n1t1=1;fork=1:n1ifk==j%如果相等則跳出循環(huán)continue
3、;elset1=t1*(x1(j)-x1(k));endends1=s1+y1(j)/t1;endchaS(i)=s1;endb(1,:)=[zeros(1,n-1)chaS(1)];cl=cell(1,n-1);%cell定義了一個矩陣fori=2:nu1=1;forj=1:i-1u1=conv(u1,[1-x(j)]);%conv()用于多項(xiàng)式乘法、矩陣乘法cl{i-1}=u1;endcl{i-1}=chaS(i)*cl{i-1};b(i,:)=[zeros(1,n-i),cl{i-1}];endp2=b(1,:);forj=2:np2=p2+b(j,:);endiflengt
4、h(t)==1rm=0;fori=1:nrm=rm+p2(i)*t^(n-i);endz=rm;elsek1=length(t);rm=zeros(1,k1);forj=1:k1fori=1:nrm(j)=rm(j)+p2(i)*t(j)^(n-i);endz=rm;endendplot(t,z,'y',x,y,'*r')%輸出牛頓插值多項(xiàng)式的函數(shù)圖p171.1(a)、f(x)=ex解:在matlab窗口中輸入:>>x=[00.20.40.60.81];>>y=[exp(0)exp(0.2)exp(0.4)exp(0.6)exp(0.8)exp(1)]y=1.00001.22141
5、.49181.82212.22552.7183>>[c,l]=lagran(x,y)可以得出輸出結(jié)果為:c=0.01390.03490.17040.49911.00011.0000l=-26.041778.1250-88.541746.8750-11.41671.0000130.2083-364.5833369.7917-160.416725.00000-260.4167677.0833-614.5833222.9167-25.00000260.4167-625.0000510.4167-162.500016.66670-130.2083286.4583-213.541763.54
6、17-6.2500026.0417-52.083336.4583-10.41671.00000由輸出結(jié)果可以的出:P(x)的系數(shù)分別為:a0=0.0139a1=0.0349a2=0.1704a3=0.4991a4=1.0001a5=1.0000(a)、f(x)=sin(x)解:在matlab窗口中輸入:>>x=[00.20.40.60.81];>>y=[sin(0)sin(0.2)sin(0.4)sin(0.6)sin(0.8)sin(1)];>>[c,l]=lagran(x,y)可以得出輸出結(jié)果為:c=0.00730.0016-0.16760.00021.00000l=-26.0
7、41778.1250-88.541746.8750-11.41671.0000130.2083-364.5833369.7917-160.416725.00000-260.4167677.0833-614.5833222.9167-25.00000260.4167-625.0000510.4167-162.500016.66670-130.2083286.4583-213.541763.5417-6.2500026.0417-52.083336.4583-10.416