数值实验报告_实验三数值实验报告
实验题三
多项式插值的振荡现象
问题提出
考虑一个固定区间上用插值逼近一个函数。显然拉格朗日插值中使用的节点越多,插值多项式的次数就越高。我们自然关心插值多项式的次数增加时,是否也更加靠近被逼近的函数。龙格(Runge)给出的一个例子是极著名并富有启发性的。设区间[-1,1]上函数
考虑的一个等距划分,分点为
则拉格朗日插值多项式为
其中
是次拉格朗日插值基函数。
实验内容
第一小题
选择不断增大的分点数目,画出原函数及插值多项式函数在上的图像,比较并分析实验结果。
(1)MATLAB代码
:
function f_n=lag_interp(xn,yn,x0)
%Lagrange Interpolation
%
%Type 1: y=lag_interp(xn,yn,x0)
%If x is input, then the function returns a numerical value Ln(x0), which
%is equal to vpa(subs(lag_interp(xn,yn),x,x0)).
%
%Type 2: y=lag_interp(xn,yn)
%If x is not input, the the function returns an symbolic function Ln(x).
%Safeguards for input arguments
[dim1,dim]=size(xn);
if dim1~=1
error('xn must be a line vector.')
end
if dim<=1
error('There must be at least 2 points.')
end
[dim1,dim2]=size(yn);
if dim1~=1
error('yn must be a line vector.')
end
if dim2~=dim
error('The dimention of xn and yn must agree.')
end
%Langrange interpolation
syms x;
f_result=0;
for i=1:dim
l=1;
for j=1:dim
if i~=j
l=l*(x-xn(j))/(xn(i)-xn(j));
end
end
f_result=f_result+yn(i)*l;
end
if nargin==2
%If x is not input, the the function returns an symbolic function Ln(x).
f_n=f_result;
elseif nargin==3
%If x is input, then the function returns a numerical value Ln(x0).
f_n=vpa(subs(f_result,x,x0));
end
:
function xn=gen_eqpts(left,right,n)
%Returns an n-dimentional line vector of equidistant points on interval [left, right]
%
%Type: xn=gen_eqpts(left,right,n)
%
%'left' is the lower boundary of the interval.
%'right' is the lower boundary of the interval.
%'n' is the number of equidistant points. n must be an integer greater or equal to 2.
%This m file is coded by Steve Zhao. For reference only.
%
%The author will not be responsible for any potential consequences that may
%caused by this file.
%
%(c)2016 3g Studio. All rights reserved
%Safeguards for in
数值实验报告 实验三 来自淘豆网m.daumloan.com转载请标明出处.