第三章习题及参考答案
解答:
>> p=[1 -1 -1];
>> roots(p)
ans =
-0.6180
1.6180
解答:
取n=5,m=61
>> x=linspace(0,2*pi,5); y=sin(x);
>> xi=linspace(0,2*pi,61);
>> y0=sin(xi);
>> y1=interp1(x,y,xi);
>> y2=interp1(x,y,xi,'spline');
>> plot(xi,y0,'o',xi,y1,xi,y2,'-.');
>> subplot(2,1,1); plot(xi,y1-y0);grid on
>> subplot(2,1,2); plot(xi,y2-y0);grid on
分段线性和三次样条插值方法与精确值之差取n=11,m=61
>> x=linspace(0,2*pi,11); y=sin(x);
>> xi=linspace(0,2*pi,61);
>> y0=sin(xi);
>> y1=interp1(x,y,xi);
>> y2=interp1(x,y,xi,'spline');
>> plot(xi,y0,'o',xi,y1,xi,y2,'-.');
>> subplot(2,1,1); plot(xi,y1-y0);grid on
>> subplot(2,1,2); plot(xi,y2-y0);grid on
分段线性和三次样条插值方法与精确值之差
解答:
>> x=[0,300,600,1000,1500,2000];
>> y=[0.9689,0.9322,0.8969,0.8519,0.7989,0.7491]; >> xi=0:100:2000;
>> y0=1.0332*exp(-(xi+500)/7756);
>> y1=interp1(x,y,xi,'spline');
>> p3=polyfit(x,y,3);
>> y3=polyval(p3,xi);
>> subplot(2,1,1);plot(xi,y0,'o',xi,y1,xi,y3,'-.');
>> subplot(2,1,2);plot(xi,y1-y0,xi,y3-y0);grid on
插值和拟合方法相比较,都合理,误差也相近。
解答:
梯形法积分
>> x=-3:0.01:3;
>> y=exp(-x.^2/2);
>> z=trapz(x,y)/(2*pi)
z =
0.3979
辛普森积分
>> z=quad('exp(-x.^2/2)',-3,3)/(2*pi)
z =
0.3979
积分区间改为-5~5:
梯形法积分
>> x=-5:0.01:5;
>> y=exp(-x.^2/2);
>> z=trapz(x,y)/(2*pi)
z =
0.3989
辛普森积分
>> z=quad('exp(-x.^2/2)',-5,5)/(2*pi)
z =
0.3989
积分区间改变了,两种积分的结果依然相同。
梯形积分中改变x的维数为2维数组
>>x(1,:)=-5:0.01:5
>> x(2,:)=-5:0.01:5
>> y=exp(-x.^2/2);
>> z=trapz(x,y)/(2*pi)
??? Error using ==> trapz
LENGTH(X) must equal the length of the first non-singleton dimension of Y.
结论参考教材第82页。
解答:
>> x=linspace(0,1,4);
>> y=x./(x.^2+4);
>> t=cumsum(y)*(1-0)/(4-1);
>> z1=t(end)
>> z2=trapz(x,y)
>> z3=quad('x./(x.^2+4)',0,1)
>> z4=quadl('x./(x.^2+4)',0,1)
z1 =
0.1437。