1、昆明理工大学工科研究生数值分析上机实验报告专业:材料加工工程姓名:王允良学号:2013230074课题五 函数插值方法一、问题提出对于给定的一元函数 的n+1个节点值 。试用Lagrange公式求其插值多项式或分段二次Lagrange插值多项式。数据如下:(1)0.40.550.650.800.951.050.410750.578150.696750.901.001.25382求五次Lagrange多项式L,和分段三次插值多项式,计算 的值。 (2) 12345670.3680.1350.0500.0180.0070.0020.001 试构造Lagrange多项式L,计算的值。 结果0.165
2、299 0.00213348二、要求1、 利用Lagrange插值公式 编写出插值多项式程序;2、 给出插值多项式或分段三次插值多项式的表达式;3、 根据节点选取原则,对问题(2)用三点插值或二点插值,其结果如何;4、 对此插值问题用Newton插值多项式其结果如何。三、目的和意义1、 学会常用的插值方法,求函数的近似表达式,以解决其它实际问题;2、 明确插值多项式和分段插值多项式各自的优缺点;3、 熟悉插值方法的程序编制;4、 如果绘出插值函数的曲线,观察其光滑性。四、结构程序设计1、 利用Lagrange插值公式 编写出插值多项式程序;Lagrange插值多项式-function f=La
3、grange(x,y,x0)syms t;if(length(x)=length(y) n=length(x);else disp(x和y维数不等!); return;endf=0.0;for(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; simplify(f); if(i=n) if(nargin=3) f=subs(f,t,x0); else f=collect(f); f=vpa(f,6); end endend-2、给出插
4、值多项式或分段三次插值多项式的表达式;(1)f = 121.972*t-377.255*t2+572.567*t3-422.750*t4+121.626*t5-15.0845(2)f = -.968350*t+.441858*t2-.117542*t3+.185833e-1*t4-.160833e-2*t5+.583333e-4*t6+.995000运算结果(1) x=0.4,0.55,0.65,0.80,0.95,1.05y=0.41075,0.57815,0.69675,0.90,1.00,1.25382x = 0.4000 0.5500 0.6500 0.8000 0.9500 1.05
5、00y = 0.4108 0.5782 0.6967 0.9000 1.0000 1.2538 f=Lagrange(x,y) f = 121.972*t-377.255*t2+572.567*t3-422.750*t4+121.626*t5-15.0845 f=Lagrange(x,y,0.596)f = 0.6257 f=Lagrange(x,y,0.99)f = 1.0542-运算结果(2) x=1,2,3,4,5,6,7x = 1 2 3 4 5 6 7 y=0.368,0.135,0.050,0.018,0.007,0.002,0.001y = 0.3680 0.1350 0.050
6、0 0.0180 0.0070 0.0020 0.0010 f=Lagrange(x,y) f = -.968350*t+.441858*t2-.117542*t3+.185833e-1*t4-.160833e-2*t5+.583333e-4*t6+.995000 f=Lagrange(x,y,1.8)f = 0.1648 f=Lagrange(x,y,6.15)f = 0.0013-3、根据节点选取原则,对问题(2)用三点插值或二点插值,其结果如何;- 三点插值 x=1,2,3x = 1 2 3 y=0.368,0.135,0.050y = 0.3680 0.1350 0.0500 f=La
7、grange(x,y) f = .740000e-1*t2-.455000*t+.749000 f=Lagrange(x,y,1.8)f = 0.1698 x=5,6,7x = 5 6 7 y=0.007,0.002,0.001y = 0.0070 0.0020 0.0010 f=Lagrange(x,y) f = .200000e-2*t2-.270000e-1*t+.920000e-1 f=Lagrange(x,y,6.15)f = 0.0016- 二点插值 x=1,2x = 1 2 y=0.368,0.315y = 0.3680 0.3150 f=Lagrange(x,y) f = -.
8、530000e-1*t+.421000 f=Lagrange(x,y,1.8)f = 0.3256 x=6,7x = 6 7 y=0.002,0.001y = 0.0020 0.0010 f=Lagrange(x,y) f = -.100000e-2*t+.800000e-2 f=Lagrange(x,y,6.15)f = 0.00194、对此插值问题用Newton插值多项式其结果如何。(1)牛顿前插值-function f=Newtonfoward(x,y,x0)syms t;if(length(x)=length(y) n=length(x); c(1:n)=0.0;else disp(x
9、和y维数不等!); renturn;endf=y(1);y1=0;xx=linspace(x(1),x(n),(x(2)-x(1);if(xx=x) disp(节点之间不是等距!); returnendfor(i=1:n-1) for(j=1:n-i) y1(j)=y(j+1)-y(j); end c(i)=y1(1); l=t; for(k=1:i-1) l=l*(t-k); end; f=f+c(i)*l/factorial(i); simplify(f); y=y1; if(i=n-1) if(nargin=3) f=subs(f,t,(x0-x(1)/(x(2)-x(1); else
10、f=collect(f); f=vpa(f,6); end endend-运行结果 x=0.4,0.55,0.65,0.80,0.95,1.05x = 0.4000 0.5500 0.6500 0.8000 0.9500 1.0500 y=0.41075,0.57815,0.69675,0.90,1.00,1.25382y = 0.4108 0.5782 0.6967 0.9000 1.0000 1.2538 f=Newtonfoward(x,y) f = .410750+.469885*t-.557710*t2+.326089*t3-.772496e-1*t4+.638600e-2*t5 f
11、=Newtonfoward(x,y,0.596)f = 0.5991 f=Newtonfoward(x,y,0.99)f = 0.9962 - x=1,2,3,4,5,6,7x = 1 2 3 4 5 6 7 y=0.368,0.135,0.050,0.018,0.007,0.002,0.001y = Columns 1 through 6 0.3680 0.1350 0.0500 0.0180 0.0070 0.0020 Column 7 0.0010 f=Newtonfoward(x,y) f = .368000-.370617*t+.185525*t2-.581250e-1*t3+.11
12、4167e-1*t4-.125833e-2*t5+.583333e-4*t6 f=Newtonfoward(x,y,1.8)f = 0.1648 f=Newtonfoward(x,y,6.15)f =0.0013(2)牛顿后插值-function f=Newtonback(x,y,x0)syms t;if(length(x)=length(y) n=length(x); c(1:n)=0.0;else disp(x和y维数不等!); renturn;endf=y(n);y1=0;xx=linspace(x(1),x(n),(x(2)-x(1);if(xx=x) disp(节点之间不是等距!);
13、 returnendfor(i=1:n-1) for(j=i+1:n) y1(j)=y(j)-y(j-1); end c(i)=y1(n); l=t; for(k=1:i-1) l=l*(t+k); end; f=f+c(i)*l/factorial(i); simplify(f); y=y1; if(i=n-1) if(nargin=3) f=subs(f,t,(x0-x(n)/(x(2)-x(1); else f=collect(f); f=vpa(f,6); end endend-运行结果 x=0.4,0.55,0.65,0.80,0.95,1.05x = 0.4000 0.5500 0
14、.6500 0.8000 0.9500 1.0500 y=0.41075,0.57815,0.69675,0.90,1.00,1.25382y = 0.4108 0.5782 0.6967 0.9000 1.0000 1.2538 f=Newtonback(x,y) f = 1.25382+.680926*t+.728690*t2+.377598*t3+.824004e-1*t4+.638600e-2*t5 f=Newtonback(x,y,0.596)f = 0.6917 f=Newtonback(x,y,0.99)f = 1.0759- x=1,2,3,4,5,6,7x = 1 2 3 4
15、 5 6 7 y=0.368,0.135,0.050,0.018,0.007,0.002,0.001y = Columns 1 through 6 0.3680 0.1350 0.0500 0.0180 0.0070 0.0020 Column 7 0.0010 f=Newtonback(x,y) f = .100000e-2+.978333e-2*t+.212750e-1*t2+.148750e-1*t3+.516667e-2*t4+.841667e-3*t5+.583333e-4*t6 f=Newtonback(x,y,1.8)f = 0.1648 f=Newtonback(x,y,6.1
16、5)f = 0.0013-五、结果讨论和分析(1)0.40.550.650.800.951.050.410750.578150.696750.901.001.25382求五次Lagrange多项式L计算 的值。 (2) 12345670.3680.1350.0500.0180.0070.0020.001 试构造Lagrange多项式L,计算的值。 结果0.165299 0.00213348不同插值方法结果汇总:结 题目果插值方法(1)(2)f(0.596)f(0.99)f(1.8)f(6.15)题中给定值-0.1652990.00213348Langrange0.62571.05420.164
17、80.0013三点插值-0.16980.0016二点插值-0.32560.0019Newtonfoward0.59910.99620.16480.0013Newtonback0.69171.07590.16480.0013绘出插值函数的曲线,观察其光滑性1. Langrange 圆圈,5次插值;菱形,3点插值程序x=0.4,0.55,0.65,0.80,0.95,1.05;y=0.41075,0.57815,0.69675,0.90,1.00,1.25382;p1=polyfit(x,y,5);y1=polyval(p1,x);p2=polyfit(x,y,3);y2=polyval(p2,x
18、);plot(x,y1,-o,x,y2,-d)2. Langrange 圆圈,6次插值;菱形,3点插值 x=1,2,3,4,5,6,7y=0.368,0.135,0.050,0.018,0.007,0.002,0.001p1=polyfit(x,y,6);y1=polyval(p1,x);p2=polyfit(x,y,3);y2=polyval(p2,x);plot(x,y1,-o,x,y2,-d)讨论分析:拉格朗日插值法一、方法原理n次拉格朗日插值多项式为:Ln(x)=y0l0(x)+y1l1(x)+y2l2(x)+ynln(x)n=2时,为线性插值或两点插值,L1(x)=y0(x-x1)/
19、(x0-x1)+ y1(x-x0)/(x1-x0)=y0+(y1-x0)(x-x0)/(x1-x0)n=3时,称为二次插值或抛物线插值,精度相对高些L2(x)=y0(x-x1)(x-x2)/(x0-x1)/(x0-x2)+y1(x-x0)(x-x2)/(x1-x0)/(x1-x2)+y2(x-x0)(x-x1)/(x2-x0)/(x2-x1)二、主要思路用构造方法来插值对节点xi(i=0,1,n)中任一点xk(0=k=n)作一n 次多项式lk(xk),使它在该点上取值为1,而在其余点xi(i=0,1,k-1,k+1,n)上为0,则插值多项式为Ln(x)=y0l0(x)+y1l1(x)+y2l2
20、(x)+ynln(x)上式表明:n 个点xi(i=0,1,k-1,k+1,n)都是lk(x)的零点。可求得lk三实验分析(1)n=2时,为两点插值,即线性插值,拟合曲线柔顺性差;(2)n=3时,为三点插值,即抛物线插值,拟合曲线柔顺性稍好;(3)在没有出现龙格现象时,插值次数高所得结果会更精确,三点插值比二点插值的曲线更光滑,常用的是线性插值和抛物线插值,由图中可看出显然,抛物线精度相对高些。牛顿插值法一、方法原理及基本思路在拉格朗日插值方法中,若增加一个节点数据,其插值的多项式需重新计算。若构造一个插值多项式Nn(x),只需对Nn-1(x)作简单修正(如增加某项)即可得到,这样计算方便。利用
21、牛顿插值公式,当增加一个节点时,只需在后面多计算一项,而前面的计算仍有用;另一方面Nn(x)的各项系数恰好又是各阶差商,而各阶差商可用差商公式来计算。由线性代数知,对任何一个不高n次的多项式P(x)=b0b1xb2x2bnxn (幂基) 也可将其写成P(x)=a0a1(xx0)a2(xx0) (xx1)an(xx0) (xxn-1)其中ai为系数,xi为给定节点,可由求出ai 一般情况下,牛顿插值多项式Nn(x)可写成:Nn(x)= a0a1(xx0)a2(xx0) (xx1)an(xx0) (xxn-1)。只需求出系数ai,即可得到插值多项式。二实验分析(1)从结果列表中可以看出,插值点的个数、精度、插值点的选择都会影响实验的结果;与拉格朗日插值结果相比,所得精度相当甚至更好。(2)通常选择与插值点最接近的节点,可以提高精度;在可以计算出结果的情况下,插值点越多,结果越精确。(3)牛顿前插值与后插值计算结果相当。六、总结 通过本次实验,在查阅和学习了大量资料后,最终按照题目要求编出程序并得出较符合预期的实验结果。在此过程中,我不仅学会了使用matlab软件,同时在对比各种方法得出的结果中对于拉格朗日插值和牛顿插值法也有了更加深入的理解,收获挺大。不足之处在于还未能绘出各种方法计算结果的比较曲线来,以后还应多多学习。