1、实验四、插值法插值法是函数逼近的一种重要方法,它是数值积分、微分方程数值解等数值计算的基础与工具,其中多项式插值是最常用和最基本的方法。拉格朗日插值多项式的优点是表达式简单明确,形式对称,便于记忆,它的缺点是如果想要增加插值节点,公式必须整个改变,这就增加了计算工作量。而牛顿插值多项式对此做了改进,当增加一个节点时只需在原牛顿插值多项式基础上增加一项,此时原有的项无需改变,从而达到节省计算次数、节约存储单元、应用较少节点达到应有精度的目的。一、实验目的1、理解插值的基本概念,掌握各种插值方法,包括拉格朗日插值和牛顿插值等,注意其不同特点;2、通过实验进一步理解并掌握各种插值的基本算法。二、Ma
2、tlab 命令和程序命令 poly:创建一个向量,其分量为一个多项式的系数,该多项式具有给定的根。命令 polyval:求多项式的值,命令 conv: 创建一个向量,其分量为一个多项式的系数,该多项式是另外两个多项式的积polyval(C,2) P=poly(2)P=1 -2Q=poly(3)Q=1 -3 conv(P,Q)ans=1 -5 6 polyval(P,2)ans=01、拉格朗日插值( 基于 N+1 个点 ,计算拉格朗日多项式)function C,L=lagran(X,Y)%input -X is a vector that contains a list of abscissa
3、s% Y is a vector that contains a list of ordinates%output-C is a matrix that contains the coefficient of the lagrane% interplatory polynomial% - L is a matrix that contains the Lagrange coefficent polynomialsw=length(X);n=w-1;L=zeros(w,w);%Form the Lagrange coefficient polynomialsfor k=1:n+1V=1;for
4、j=1:n+1if k=jV=conv(V,poly(X(j)/(X(k)-X(j);endendL(k,:)=V;end%Determine the coefficiants of the Lagrange interpolating polynomialC=Y*L;2、牛顿插值function C,D,Newton=newpoly(X,Y,p)%Input -X is a vector that contains a list of abscissas% -Y is a vector that contains a list of ordinates% -p is the %Output
5、-C is a vector that contains the coefficents of % the Newton interpolatory polynomia% -D is the divided-difference table% -Newton is the value of Newton interplatory polynomia in pn=length(X);0()nkLxflx)D=zeros(n,n);D(:,1)=Y;%Use formula to form the divided-difference tablefor j=2:nfor k=j:nD(k,j)=(
6、D(k,j-1)-D(k-1,j-1)/(X(k)-X(k-j+1);endend%Determine the coefficient fo the newton interpolating polynomialC=D(n,n);for k=(n-1):-1:1C=conv(C,poly(X(k);m=length(C);C(m)=C(m)+D(k,k);End%Determine the value of the newton interpolating polynomial at pNewton=D(n,n);for k=(n-1):-1:1Newton=Newton*(p-X(k)+D(
7、k,k);End三、实验任务1、已知函数表0.56160 0.56280 0.56401 0.56521ix0.82741 0.82659 0.82577 0.82495iy用二次拉格朗日插值多项式求 时的函数近似值。563.0x解:题目要求我们做二次拉格朗日插值多项式,选取三组数字,选最接近x=0.5635 的三个数字为0.56280 0.56401 0.56521ix0.82659 0.82577 0.82495iy在 MATLAB 中输入程序: X= 0.56160 0.56280 0.56401 0.56521; Y= 0.82741 0.82659 0.82577 0.82495;
8、C,L=lagran(X,Y)C =1.0e+03 *-1.2982 2.1943 -1.2370 0.2334L =1.0e+08 *-0.9578 1.6207 -0.9141 0.17182.8577 -4.8319 2.7233 -0.5116-2.8577 4.8284 -2.7194 0.51050.9578 -1.6172 0.9102 -0.1708 polyval(C,0.5635)ans =0.82612、已知函数表1 3 2 ix1 2 -1iy用牛顿插值多项式求和 2(1.5)N解:在命令窗口输入:X=1 3 2; Y=1 2 -1; p=1.5; C,D,Newton=newpoly(X,Y,p)C =2.5000 -9.5000 8.0000D =1.0000 0 02.0000 0.5000 0-1.0000 3.0000 2.5000Newton =-0.6250 四、 实验总结:这次实验我们主要学习了拉格朗日插值法和牛顿插值法。通过学习插值法求近似值,使我们我们对这一概念初步熟悉,同时对在理论课上学到的三次样条插值法有了更深的了解,这一方法使近似值的拟合曲线更加光滑,误差更小。插值法就是这一个方法的基础。