計算方法實驗三
一、實驗名稱:
實驗三、插值逼近
二、實驗目的:
1. 掌握Lagrange插值、Newton插值的概念;
2. 編寫程序?qū)崿F(xiàn);
3. 觀察Runge現(xiàn)象。
三、實驗內(nèi)容及要求:
(a)取,以等分節(jié)點為插值節(jié)點,構(gòu)造Lagrange插值公式,用程序計算各等分區(qū)間中點處的值,列表顯示(給出插值多項式及函數(shù)在等分區(qū)間中點處值),并分別作出和插值函數(shù)的圖形(將5個圖形畫在同一張圖里,并用不同顏色表示,四個插值多項式及一
個圖形);
(b)取,以等分節(jié)點為插值節(jié)點,構(gòu)造牛頓插值函數(shù),用程序計算各等分區(qū)間中點處的值,列表顯示(給出插值多項式及函數(shù)在等分區(qū)間中點處值),并分別作出和插值函數(shù)的圖形(將5個圖形畫在同一張圖里,并用不同顏色表示,四個插值多項式及一個圖形);
(c)取,在剖分的基礎上簡歷分片線性Lagrange插值函數(shù),用程序計算各等分區(qū)間中點處的值,列表顯示(給出插值多項式及函數(shù)在等分區(qū)間中點處值),并分別作出和插值函數(shù)的圖形(將5個圖形畫在同一張圖里,并用不同顏色表示,四個插值多項式及一個圖形);
2.實現(xiàn)課本39頁例2.12,取,分別作出和插值函數(shù)的圖形(將5個圖形畫在同一張圖里,并用不同顏色表示,四個插值多項式及一個圖形);觀察Runge現(xiàn)象。
三、實驗步驟(或記錄)
1、(a)
插值多項式及函數(shù)在等分區(qū)間中點處值????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? ???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? ????????????????????????
Y???? 0.038461538?? 0.042440318?? 0.047058824?? 0.052459016?? 0.058823529?? 0.066390041?? 0.075471698??????? 0.086486486?? 0.1?? 0.116788321?? 0.137931034?? 0.164948454?? 0.2?? 0.246153846?? 0.307692308??????? 0.390243902?? 0.5?? 0.64 0.8?? 0.941176471?? 1????? 0.941176471?? 0.8?? 0.64 0.5?? 0.390243902?? 0.307692308??????? 0.246153846?? 0.2?? 0.164948454?? 0.137931034?? 0.116788321?? 0.1?? 0.086486486?? 0.075471698??????? 0.066390041?? 0.058823529?? 0.052459016?? 0.047058824?? 0.042440318?? 0.038461538
y1??? -0.048076923 0.321153846?? 0.567307692?? 0.321153846?? -0.048076923??????????????????????????????????????????????????????????? ???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? ?????????????????????????????????????????
y2??? 1.57872099???? -0.226196289 0.253755457?? 0.235346591?? 0.84340743???? 0.84340743???? 0.235346591??????? 0.253755457?? -0.226196289 1.57872099????????????????????????????????????????????????????????????????????????????????????????????????????????????????? ??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
y3??? -39.95244903 3.4549578?????? -0.447051961 0.202422616?? 0.080659993?? 0.17976263???? 0.238445934??????? 0.395093054?? 0.636755336?? 0.94249038???? 0.94249038???? 0.636755336?? 0.395093054?? 0.238445934??????? 0.17976263???? 0.080659993?? 0.202422616?? -0.447051961 3.4549578?????? -39.95244903?????????????????????????????????? ?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
y4??? -57409.17974 2287.728499?? -156.169717?? 15.42434982?? -1.939791158 0.39938813???? 0.014910807??????? 0.10858616???? 0.103537161?? 0.128151723?? 0.150063036?? 0.181523667?? 0.221349033?? 0.274733128??????? 0.345913865?? 0.441399666?? 0.566357999?? 0.719110364?? 0.876706771?? 0.984617272?? 0.984617272??????? 0.876706771?? 0.719110364?? 0.566357999?? 0.441399666?? 0.345913865?? 0.274733128?? 0.221349033??????? 0.181523667?? 0.150063036?? 0.128151723?? 0.103537161?? 0.10858616???? 0.014910807?? 0.39938813???? -1.939791158?? 15.42434982?? -156.169717?? 2287.728499?? -57409.17974
%構(gòu)造lagrange插值函數(shù),參考:https://blog.csdn.net/qq_44692057/article/details/108314856
? function y=lagr(X,Y,x)
? n=length(X);
? m=length(x);
? for i=1:m
? ??? z=x(i);
? ??? s=0;
? ??? for k=1:n
? ??????? p=1;
? ???? ???for j=1:n
? ??????????? if j~=k
? ??????????????? p=p*(z-X(j))/(X(k)-X(j));
? ??????????? end
? ??????? end
? ??????? s=p*Y(k)+s;
? ??? end
? ??? y(i)=s;
? end
?
? %輸入已知量(第2次a問)
? clear;
? n=[5 10 20 40];
? for a=1:4
? b=10/n(a);
? i=1:n(a)+1;
? j=-5:b:5;
? x=[i;j];
? for k=1:n(a)+1
? ??? ????X(k)=x(2,k)
? ??????? Y(k)=1/(1+X(k)*X(k));
? ??? end
? for l=1:n(a)
? ??? x0(l)=(x(2,l)+x(2,l+1))/2;
? end
? if a==1
? ??? y1=lagr(X,Y,x0);
? ??? x1=x0;
? elseif a==2
? ?????? ?????y2=lagr(X,Y,x0);
? ?????? ?????x2=x0;
? elseif a==3
? ?? ?????y3=lagr(X,Y,x0);
? ?? ?????x3=x0;
? elseif a==4
? ?? ?????y4=lagr(X,Y,x0);
? ?? ?????x4=x0;
?
? end
?
? end
? %畫圖比較精確值
? figure(1)
? xc=[-5:0.05:5]
? plot(xc,1./(1+xc.*xc),'k')
? hold on
? plot(x1,y1,'r')
? hold on
? plot(x2,y2,'y')
? hold on
? plot(x3,y3,'b')
? hold on
? plot(x4,y4,'g')
? hold off
? figure(2)
? xc=[-5:0.05:5]
? plot(xc,1./(1+xc.*xc),'k')
? hold on
? plot(x1,y1,'r')
? hold on
? plot(x2,y2,'y')
? hold on
? plot(x3,y3,'b')
? hold on
? plot(x4,y4,'g')
? hold off
? axis([-5,5,-1,1])
? 得出圖像

(b)
%構(gòu)造newton插值參考https://zhuanlan.zhihu.com/p/34883522
? function? ? N? = Newton( x,y,t )
? syms?? p ? ;?? %定義符號變量
? N = y(1);
? dd = 0;
? dxs = 1;
? n = length(x);
? for(i = 1:n-1)
? ??? ? for(j = i+1:n)
? ?? ?????dd(j) = (y(j)-y(i))/(x(j)-x(i));
? ??? end
? ??? temp1(i) = dd(i+1);
? ??? dxs = dxs*(p-x(i));
? ??? N = N + temp1(i)*dxs;
? ??? y = dd;
? end
? ? simplify(N);
? %以上為計算部分,下面是輸出規(guī)則;
? if(nargin == 2)
? ??? N = subs(N,'p','x');
? ??? N = collect(N);
? ??? N = vpa(N,4);
? else
? ??? m = length(t);
? ??? for i = ? 1:m
? ? ?????temp(i) = subs(N,'p',t(i));
? ??? end
? ??? N = temp;
? end
?
? %輸入已知量(第2次a問)
? clear;
? n=[5 10 20 40];
? for a=1:4
? b=10/n(a);
? i=1:n(a)+1;
? j=-5:b:5;
? x=[i;j];
? for k=1:n(a)+1
? ??????? X(k)=x(2,k)
? ??????? Y(k)=1/(1+X(k)*X(k));
? ??? end
? for l=1:n(a)
? ??? x0(l)=(x(2,l)+x(2,l+1))/2;
? end
? if a==1
? ??? y1=Newton(X,Y,x0);
? ??? x1=x0;
? elseif a==2
? ?????? ?????y2=Newton(X,Y,x0);
? ?????? ?????x2=x0;
? elseif a==3
? ?? ?????y3=Newton(X,Y,x0);
? ?? ?????x3=x0;
? elseif a==4
? ?? ?????y4=Newton(X,Y,x0);
? ?? ?????x4=x0;
?
? end
?
? end
? %畫圖比較精確值
? figure(1)
? xc=[-5:0.05:5]
? plot(xc,1./(1+xc.*xc),'k')
? hold on
? plot(x1,y1,'r')
? hold on
? plot(x2,y2,'y')
? hold on
? plot(x3,y3,'b')
? hold on
? plot(x4,y4,'g')
? hold off
? figure(2)
? xc=[-5:0.05:5]
? plot(xc,1./(1+xc.*xc),'k')
? hold on
? plot(x1,y1,'r')
? hold on
? plot(x2,y2,'y')
? hold on
? plot(x3,y3,'b')
? hold on
? plot(x4,y4,'g')
? hold off
? axis([-5,5,-1,1])
? 得出圖像

(c)
%構(gòu)造分段線性插值函數(shù)
? function y=fenduan(X,Y,x)
? n=length(X);
? m=length(x);
? for j=1:m
? ??? for i=1:n-1
? ?? ?????if x(j)>X(i)&&x(j)<=X(i+1)
? ??? ????????y(j)=((x(j)-X(i+1))/(X(i)-X(i+1)))*Y(i)+(((x(j)-X(i))/(X(i+1)-X(i)))*Y(i+1));
? ?? ?????end
? ??? end
? end
?
? %第二次c問
? clear;
? n=[5 10 20 40];
? for a=1:4
? b=10/n(a);
? i=1:n(a)+1;
? j=-5:b:5;
? x=[i;j];
? for k=1:n(a)+1
? ??????? X(k)=x(2,k)
? ??????? Y(k)=1/(1+X(k)*X(k));
? ??? end
? for l=1:n(a)
? ??? x0(l)=(x(2,l)+x(2,l+1))/2;
? end
? if a==1
? ??? y1=fenduan(X,Y,x0);
? ??? x1=x0;
? elseif a==2
? ?????? ?????y2=fenduan(X,Y,x0);
? ?????? ?????x2=x0;
? elseif a==3
? ?? ?????y3=fenduan(X,Y,x0);
? ?? ?????x3=x0;
? elseif a==4
? ?? ?????y4=fenduan(X,Y,x0);
? ?? ?????x4=x0;
?
? end
?
? end
? %畫圖比較精確值
? xc=[-5:0.05:5]
? plot(xc,1./(1+xc.*xc),'k')
? hold on
? plot(x1,y1,'r')
? hold on
? plot(x2,y2,'y')
? hold on
? plot(x3,y3,'b')
? hold on
? plot(x4,y4,'g')
? hold off
得出圖像

2、
%d2
? clear;
? n=[4 6 8 10];
? x0=[-1:0.005:1];
? for a=1:4
? b=2/n(a);
? X=-1:b:1;
? for i=1:n(a)+1
? Y(i)=1/(1+25*X(i)*X(i))
? end
? if a==1
? ??? y1=lagr(X,Y,x0);
? elseif a==2
? ??????????? y2=lagr(X,Y,x0);
? elseif a==3
? ??????? y3=lagr(X,Y,x0);
? elseif a==4
? ??????? y4=lagr(X,Y,x0);
? end
? end
? %畫圖比較精確值
? plot(x0,1./(1+25*x0.*x0),'k')
? hold on
? plot(x0,y1,'r')
? hold on
? plot(x0,y2,'y')
? hold on
? plot(x0,y3,'b')
? hold on
? plot(x0,y4,'g')
? hold off
得出圖像
