当前位置:文档之家› 实验四插值法

实验四插值法

实验四、插值法
插值法是函数逼近的一种重要方法,它是数值积分、微分方程数值解等数值计算的基础与工具,其中多项式插值是最常用和最基本的方法。

拉格朗日插值多项式的优点是表达式简单明确,形式对称,便于记忆,它的缺点是如果想要增加插值节点,公式必须整个改变,这就增加了计算工作量。

而牛顿插值多项式对此做了改进,当增加一个节点时只需在原牛顿插值多项式基础上增加一项,此时原有的项无需改变,从而达到节省计算次数、节约存储单元、应用较少节点达到应有精度的目的。

一、实验目的
1、理解插值的基本概念,掌握各种插值方法,包括拉格朗日插值和牛顿插值等,注意其不同特点;
2、通过实验进一步理解并掌握各种插值的基本算法。

二、Matlab命令和程序
命令poly:创建一个向量,其分量为一个多项式的系数,该多项式具有给定的根。

命令polyval:求多项式的值,
命令 conv: 创建一个向量,其分量为一个多项式的系数,该多项式是另外两个多项式的积
polyval(C,2>
>> P=poly(2>
P=1 -2
Q=poly(3>
Q=1 -3
>> conv(P,Q>
ans=
1 -5 6
>> polyval(P,2>
ans=
1、拉格朗日插值( 基于N+1个点,计算拉格朗日多项式>
function [C,L]=lagran(X,Y>
%input --X is a vector that contains a list of
abscissasb5E2RGbCAP
% Y is a vector that contains a list of
ordinatesp1EanqFDPw
%output--C is a matrix that contains the coefficient of the lagraneDXDiTa9E3d
% interplatory polynomial
% -- L is a matrix that contains the Lagrange
coefficent polynomialsRTCrpUDGiT
w=length(X>。

n=w-1。

L=zeros(w,w>。

%Form the Lagrange coefficient polynomials
for k=1:n+1
V=1。

for j=1:n+1
if k~=j
V=conv(V,poly(X(j>>>/(X(k>-X(j>>。

end
end
L(k,:>=V。

end
%Determine the coefficiants of the Lagrange interpolating polynomial5PCzVD7HxA
C=Y*L。

2、牛顿插值
function [C,D,Newton]=newpoly(X,Y,p>
%Input -X is a vector that contains a list of abscissasjLBHrnAILg
% -Y is a vector that contains a list of ordinatesxHAQX74J0X
% -p is the
%Output -C is a vector that contains the coefficents of LDAYtRyKfE
% the Newton interpolatory polynomia
% -D is the divided-difference table
% -Newton is the value of Newton interplatory polynomia in pZzz6ZB2Ltk
n=length(X>。

D=zeros(n,n>。

D(:,1>=Y'。

%Use formula to form the divided-difference table
for j=2:n
for k=j:n
D(k,j>=(D(k,j-1>-D(k-1,j-1>>/(X(k>-X(k-j+1>>。

end
end
%Determine the coefficient fo the newton interpolating polynomialdvzfvkwMI1
C=D(n,n>。

for k=(n-1>:-1:1
C=conv(C,poly(X(k>>>。

m=length(C>。

C(m>=C(m>+D(k,k>。

End
%Determine the valueof the newton interpolating polynomial at prqyn14ZNXI
Newton=D(n,n>。

for k=(n-1>:-1:1
Newton=Newton*(p-X(k>>+D(k,k>。

End
三、实验任务
1、已知函数表
0.56160 0.56280 0.56401 0.56521
0.82741 0.82659 0.82577 0.82495
用二次拉格朗日插值多项式求时的函数近似值。

解:题目要求我们做二次拉格朗日插值多项式,选取三组数字,选最接近x=0.5635的三个数字为
0.56280 0.56401 0.56521
0.82659 0.82577 0.82495
在MATLAB中输入程序:
>> X=[ 0.56160 0.56280 0.56401 0.56521]。

>> Y=[ 0.82741 0.82659 0.82577 0.82495]。

>> [C,L]=lagran(X,Y>
C =
1.0e+03 *
-1.2982 2.1943 -1.2370 0.2334
L =
1.0e+08 *
-0.9578 1.6207 -0.9141 0.1718
2.8577 -4.8319 2.7233 -0.5116
-2.8577 4.8284 -2.7194 0.5105
0.9578 -1.6172 0.9102 -0.1708
>> polyval(C,0.5635>
ans =
0.8261
2、已知函数表
1 3 2
1 2 -1
用牛顿插值多项式求和
解:在命令窗口输入:
>>X=[1 3 2]。

>> Y=[1 2 -1]。

>> p=1.5。

>> [C,D,Newton]=newpoly(X,Y,p>
C =
2.5000 -9.5000 8.0000
D =
1.0000 0 0
2.0000 0.5000 0
-1.0000 3.0000 2.5000
Newton =
-0.6250
四、实验总结:
这次实验我们主要学习了拉格朗日插值法和牛顿插值法。

通过学习插值法求近似值,使我们我们对这一概念初步熟悉,同时对在理论课上学到的三次样条插值法有了更深的了解,这一方法使近似值的拟合曲线更加光滑,误差更小。

插值法就是这一个方法的基础。

EmxvxOtOco
申明:
所有资料为本人收集整理,仅限个人学习使用,勿做商业用途。

相关主题