matlab数值分析程序--高等数学,数值代数的matlab实现-文字版, matlab电子书, 和matlab 有关的电子书:

6.5.1 高次插值的振荡和MATLAB程序

6.5.1  高次插值的振荡和MATLAB程序

例6.5.1  作下列函数在指定区间上的次拉格朗日插值多项式 的图形,并讨论插值多项式的次数与误差的关系.

(1);(2).

解  将计算次拉格朗日插值多项式的值的MATLAB程序保存名为lagr1.m的M文件.

function y=lagr1(x0,y0,x)

n=length(x0); m=length(x);

for i=1:m

    z=x(i);s=0.0;

for k=1:n

       p=1.0;

for j=1:n

if j~=k           

p=p*(z-x0(j))/(x0(k)-x0(j));

         end

      end

      s=p*y0(k)+s;

   end

   y(i)=s;

end

(1)现提供作次拉格朗日插值多项式的图形的MATLAB程序,保存名为 Li1.m 的M文件

m=150; x=-pi:2*pi/(m-1): pi;

y=tan(cos((3^(1/2)+sin(2*x))./(3+4*x.^2)));

plot(x,y,'k-'),

gtext('y=tan(cos((sqrt(3)+sin(2x))/(3+4x^2)))'),pause

n=3; x0=-pi:2*pi/(3-1):pi; y0=tan(cos((3^(1/2)+sin(2*x0))./(3+4*x0.^2)));

y1=lagr1(x0,y0,x);hold on,

plot(x,y1,'g<'),gtext('n=2'),pause,hold off

n=5; x0=-pi:2*pi/(n-1):pi; y0=tan(cos((3^(1/2)+sin(2*x0))./(3+4*x0.^2)));

y2=lagr1(x0,y0,x);hold on,

plot(x,y2,'b:'),gtext('n=4'),pause,hold off

n=7; x0=-pi:2*pi/(n-1):pi; y0=tan(cos((3^(1/2)+sin(2*x0))./(3+4*x0.^2)));

y3=lagr1(x0,y0,x);hold on,

plot(x,y3,'rp'),gtext('n=6'),pause,hold off

n=9; x0=-pi:2*pi/(n-1):pi; y0=tan(cos((3^(1/2)+sin(2*x0))./(3+4*x0.^2)));

y4=lagr1(x0,y0,x);hold on,

plot(x,y4,'m*'),gtext('n=8'),pause,hold off

n=11; x0=-pi:2*pi/(n-1):pi; y0=tan(cos((3^(1/2)+sin(2*x0))./(3+4*x0.^2)));

y5=lagr1(x0,y0,x);hold on,

plot(x,y5,'g:'),gtext('n=10')

title('高次拉格朗日插值的振荡')

在MATLAB工作窗口输入名为 Li1.m的M文件的文件名

>> Li1.m

回车运行后,便会逐次画出在区间上的次拉格朗日插值多项式 的图形(略).

 

(2)在MATLAB工作窗口输入程序

m=101; x=-5:10/(m-1):5; y=1./(1+x.^2); z=0*x;plot(x,z,'r',x,y,'k-'),

gtext('y=1/(1+x^2)'),pause

n=3; x0=-5:10/(n-1):5; y0=1./(1+x0.^2); y1=lagr1(x0,y0,x);hold on,

plot(x,y1,'g'),gtext('n=2'),pause,hold off

n=5; x0=-5:10/(n-1):5; y0=1./(1+x0.^2); y2=lagr1(x0,y0,x);hold on,

plot(x,y2,'b:'),gtext('n=4'),pause,hold off

n=7; x0=-5:10/(n-1):5; y0=1./(1+x0.^2); y3=lagr1(x0,y0,x);hold on,

plot(x,y3,'r'),gtext('n=6'),pause,hold off

n=9; x0=-5:10/(n-1):5; y0=1./(1+x0.^2); y4=lagr1(x0,y0,x);hold on,

plot(x,y4,'r:'),gtext('n=8'),pause,hold off

n=11; x0=-5:10/(n-1):5; y0=1./(1+x0.^2); y5=lagr1(x0,y0,x);hold on,

plot(x,y5,'m'),gtext('n=10')

title('高次拉格朗日插值的振荡')

回车运行后,便会逐次画出在区间上的次拉格朗日插值多项式 的图形(略).

欢迎转载,转载请注明来自一手册:http://yishouce.com/book/3/3060501.html
友情链接It题库(ittiku.com)| 版权归yishouce.com所有| 友链等可联系 admin#yishouce.com|粤ICP备16001685号-1