資源描述:
《matlab在數(shù)值分析中的應(yīng)用》由會(huì)員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在學(xué)術(shù)論文-天天文庫(kù)。
1、1牛頓插值代碼%1-1homeworkofsomeone(ID)%創(chuàng)建插值原始數(shù)據(jù)x,yx=-1:0.2:1;y=1./(1+9.*x.^2);%讀取插值維數(shù)n=length(x);%定義需要的變量c(1:n)=0.0;symst;f=y(1);y1=0;l=1;%循環(huán)生成插值多項(xiàng)式for(i=1:n-1)for(j=i+1:n)y1(j)=(y(j)-y(i))/(x(j)-x(i));endc(i)=y1(i+1);l=l*(t-x(i));f=f+c(i)*l;simplify(f);y=y1;%化簡(jiǎn)f=collect(f);f=vpa(f,6);end%輸出ff
2、%畫圖symp;q=1/(1+9*p^2);ezplot(q,[-1,1])%標(biāo)題holdonezplot(f,[-1,1])title('牛頓插值someone(ID)')牛頓插值運(yùn)行截圖:三次樣條插值代碼%1-1homeworkofsomeone(ID)%創(chuàng)建插值原始數(shù)據(jù)x0,y0x0=-1:0.2:1;y0=1./(1+9.*x0.^2);yy=spline(x0,y0);%讀取插值維數(shù)n=length(x0);%創(chuàng)建符號(hào)變量xsymsxfori=1:n-1y=yy.coefs(i,1)*(x-x0(i))^3+yy.coefs(i,2)*(x-x0(i))^2+
3、yy.coefs(i,3)*(x-x0(i))^1+yy.coefs(i,4);%因三次樣條插值與原圖過(guò)于相似,為避免重合,分列在兩張圖里面subplot(2,1,1)ezplot(y,[x0(i),x0(i+1)])title('someone(ID)三次樣條')holdonaxis([-1101])endy=1/(1+9*x^2);subplot(2,1,2)ezplot(y,[-1,1]);title('someone(ID)三次樣條')三次樣條插值截圖因三次樣條插值與原圖過(guò)于相似,為避免重合,分列在兩張圖里面2二次勒讓德逼近代碼%2homeworkofsomeo
4、ne(ID)%定義逼近原始函數(shù)%創(chuàng)建符號(hào)變量xsymsx;%定義目標(biāo)函數(shù),并作等值變換使得自變量的區(qū)間為[-1,1]azf=sin(0.5*pi+0.5*pi*x);%定義逼近次數(shù)n=2;%定義Legender多項(xiàng)式f1=[1x1.5*x^2-0.5];%通過(guò)循環(huán)依次計(jì)算系數(shù)fori=1:n+1g(i)=f1(i)*f;temp(i)=int(g(i),-1,1);a(i)=(2*i-1)*temp(i)/2;end%累加得出插值多項(xiàng)式pf=0;fori=1:n+1pf=pf+a(i)*f1(i);end%化簡(jiǎn),取六位有效數(shù)字pf=simplify(pf);pf=vpa
5、(pf,6);ezplot(pf,[0,1])holdonezplot(f,[0,1])title('someone(ID)逼近')二次勒讓德逼近截圖3-1牛頓插值代碼:%3-1homeworkofsomeone(ID)%創(chuàng)建插值原始數(shù)據(jù)x,yx=-5:5;y=1./(1+x.^2);%讀取插值維數(shù)n=length(x);%定義需要的變量c(1:n)=0.0;symst;f=y(1);y1=0;l=1;%循環(huán)生成插值多項(xiàng)式for(i=1:n-1)for(j=i+1:n)y1(j)=(y(j)-y(i))/(x(j)-x(i));endc(i)=y1(i+1);l=l*(
6、t-x(i));f=f+c(i)*l;simplify(f);y=y1;%化簡(jiǎn)f=collect(f);f=vpa(f,6);end%畫圖symsp;q=1/(1+p^2);ezplot(q,[-5,5])%標(biāo)題holdonezplot(f,[-5,5])title('牛頓插值someone(ID)')牛頓插值截屏:3-2拉格朗日插值代碼:%3-2homeworkofsomeone(ID)%創(chuàng)建插值原始數(shù)據(jù)p=0:20;x=5*cos((2*p+1)*pi/42);y=1./(x.^2+1);%創(chuàng)建符號(hào)變量symst;n=length(x);f=0.0;%循環(huán)生成基函數(shù)
7、,分兩段以繞過(guò)xi=xjfor(i=1:n)l=y(i);for(j=1:i-1)l=l*(t-x(j))/(x(i)-x(j));end;for(j=i+1:n)l=l*(t-x(j))/(x(i)-x(j));end;f=f+l;%化簡(jiǎn)simplify(f);end;%將插值多項(xiàng)式展開(kāi)f=collect(f);%將插值多項(xiàng)式的系數(shù)化成6位精度的小數(shù)f=vpa(f,6);%輸出ff%畫圖ezplot(f,[-5,5]);%標(biāo)題title('拉格朗日someone(ID)')holdonsymss;q=1/(s^2+1);ezplot(q