Langrage和Newton插值法的matlab实现.doc
《Langrage和Newton插值法的matlab实现.doc》由会员分享,可在线阅读,更多相关《Langrage和Newton插值法的matlab实现.doc(11页珍藏版)》请在冰豆网上搜索。
仅供参考
1.已知数据如下:
0.2
0.4
0.6
0.8
1.0
0.98
0.92
0.81
0.64
0.38
(1)用MATLAB语言编写按Langrage插值法和Newton插值法计算插值的程序,对以上数据进行插值;
(2)利用MATLAB在第一个图中画出离散数据及插值函数曲线。
(1.1)langrage插值法编程实现
symsx
x0=[0.2,0.4,0.6,0.8,1.0];
y0=[0.98,0.92,0.81,0.64,0.38];
fori=1:
5
a=1;
forj=1:
5
ifj~=i
a=expand(a*(x-x0(j)));
end
end
b=1;
fork=1:
5
ifk~=i
b=b*(x0(i)-x0(k));
end
end
A(i)=expand(a/b);
end
L=0;
forp=1:
5
L=L+y0(p)*A(p);
end
L
L=
-25/48*x^4+5/6*x^3-53/48*x^2+23/120*x+49/50
(1.2)Newton插值程序实现
clearall
clc
symsx
x0=[0.2,0.4,0.6,0.8,1.0];
y0=[0.98,0.92,0.81,0.64,0.38];
fork=1:
5
fori=1:
k
a=1;
b=0;
forj=1:
k
ifj~=i
a=a*(x0(i)-x0(j));
end
end
b=b+y0(i)/a;
end
A(k)=b;
end
B=[1,(x-x0
(1)),(x-x0
(1))*(x-x0
(2)),(x-x0
(1))*(x-x0
(2))*(x-x0(3)),(x-x0
(1))*(x-x0
(2))*(x-x0(3))*(x-x0(4))];
L1=A.*B;
l=0;
form=1:
5
l=l+L1(m);
end
L=expand(l)
L=
61/100+13/30*x+383/48*x^2-155/24*x^3+475/48*x^4
(2)画图
x0=[0.2,0.4,0.6,0.8,1.0];
y0=[0.98,0.92,0.81,0.64,0.38];
subplot(1,2,1);
plot(x0
(1),y0
(1),'+r',x0
(2),y0
(2),'+r',x0(3),y0(3),'+r',x0(4),y0(4),'+r',x0(5),y0(5),'+r')
x=0:
0.05:
1;
y=-25/48.*x.^4+5/6.*x.^3-53/48.*x.^2+23/120.*x+49/50;
subplot(1,2,2);
plot(x,y)
2.给定函数,利用上题编好的Langrage插值程序(或Newton插值程序),分别取3个,5个、9个、11个等距节点作多项式插值,分别画出插值函数及原函数的图形,以验证Runge现象、分析插值多项式的收敛性。
取三个节点如下
clear
clc
x0=0:
0.5:
1;
y0=1./(1+25.*x0.^2);
symsx
fori=1:
3
a=1;
forj=1:
3
ifj~=i
a=expand(a*(x-x0(j)));
end
end
b=1;
fork=1:
3
ifk~=i
b=b*(x0(i)-x0(k));
end
end
A(i)=expand(a/b);
end
L=0;
forp=1:
3
L=L+y0(p)*A(p);
end
L
L=
575/377*x^2-1875/754*x+1
x1=0:
0.0001:
1;
y1=1./(1+25.*x1.^2);
y2=575/377.*x1.^2-1875/754.*x1+1;
plot(x1,y1,'+r')
holdon
plot(x1,y2,'*k')
取五个节点如下
clear
clc
x0=0:
0.25:
1;
y0=1./(1+25.*x0.^2);
symsx
fori=1:
5
a=1;
forj=1:
5
ifj~=i
a=expand(a*(x-x0(j)));
end
end
b=1;
fork=1:
5
ifk~=i
b=b*(x0(i)-x0(k));
end
end
A(i)=expand(a/b);
end
L=0;
forp=1:
5
L=L+y0(p)*A(p);
end
L
L=
1570000/3725137*x^4-9375000/3725137*x^3+16996575/3725137*x^2-25546875/7450274*x+1
x1=0:
0.0001:
1;
y1=1./(1+25.*x1.^2);
y2=1570000/3725137.*x1.^4-9375000/3725137.*x1.^3+16996575/3725137.*x1.^2-25546875/7450274.*x1+1;
plot(x1,y1,'+r')
holdon
plot(x1,y2,'*k')
取九个节点
clear
clc
x0=0:
0.125:
1;
y0=1./(1+25.*x0.^2);
symsx
fori=1:
9
a=1;
forj=1:
9
ifj~=i
a=expand(a*(x-x0(j)));
end
end
b=1;
fork=1:
9
ifk~=i
b=b*(x0(i)-x0(k));
end
end
A(i)=expand(a/b);
end
L=0;
forp=1:
9
L=L+y0(p)*A(p);
end
L
L=
-745631513600000000/6545742033698309*x^8+3419841600000000000/6545742033698309*x^7-6621592639456000000/6545742033698309*x^6+7019299350000000000/6545742033698309*x^5-4393156359065510000/6545742033698309*x^4+1603771386328125000/6545742033698309*x^3-22515465371294825/503518617976793*x^2+7750485791015625/13091484067396618*x+1
x1=0:
0.0001:
1;
y1=1./(1+25.*x1.^2);
y2=-745631513600000000/6545742033698309.*x1.^8+3419841600000000000/6545742033698309.*x1.^7-6621592639456000000/6545742033698309.*x1.^6+7019299350000000000/6545742033698309.*x1.^5-4393156359065510000/6545742033698309.*x1.^4+1603771386328125000/6545742033698309.*x1.^3-22515465371294825/503518617976793.*x1.^2+7750485791015625/13091484067396618.*x1+1;
plot(x1,y1,'--r')
holdon
plot(x1,y2,'k')
取十一个节点
clear
clc
x0=0:
0.1:
1;
y0=1./(1+25.*x0.^2);
symsx
fori=1:
11
a=1;
forj=1:
11
ifj~=i
a=expand(a*(x-x0(j)));
end
end
b=1;
fork=1:
11
ifk~=i
b=b*(x0(i)-x0(k));
end
end
A(i)=expand(a/b);
end
L=0;
forp=1:
11
L=L+y0(p)*A(p);
end
L
L=
-3424659989996187952765255410847243578373715179152933334243989076419413866221274679613493494711144683493498122577559208945836334877091233965270312991850496/53178433436436854112137751234603968902040519205586379256501744820282895654320795897063686873618885213871681425479462777404544150039401268463093187392345*x^10+121086192503439730397482523520885664132124539598838340051093380960893394307010980404143358507570389043992652515804277667665850493797050376702379508889550848/265892167182184270560688756173019844510202596027931896282508724101414478271603979485318434368094426069358407127397313887022720750197006342315465936961725*x^9-1866684313118720862370761857357873725727309899525470177959305327610184009984336016654604586537474647225602036909009509629462337871679343044326927998528782336/1329460835910921352803443780865099222551012980139659481412543620507072391358019897426592171840472130346792035636986569435113603750985031711577329684808625*x^8+2361180753817157490601164359984945296081740861682440608420596372826539195107329413948261865424198108462767092910060184301539989010355832103758391330174140416/949614882793515252002