1、MATLAB作业4参考答案MATLAB作业4参考答案MATLAB作业四参考答案 2yt,,sin(103)1、 用在(0,3)区间内生成一组较稀疏的数据,并用一维数据插值的方法对给出的数据进行曲线拟合,并将结果与理论曲线相比较。 【求解】类似于上面的例子可以用几乎一致的语句得出样本数据和插值效果。 t=0:0.2:3; y=sin(10*t.2+3); plot(t,y,o) ezplot(sin(10*t2+3),0,3); hold on x1=0:0.001:3; y1=interp1(t,y,x1,spline); plot(x1,y1) 由于曲线本身变换太大所以在目前选定的样本点下是
2、不可能得出理想插值效果的因为样 本数据提供的信息量不够。为了得到好的插值效果必须增大样本数据的信息量对本例来 说必须在快变化区域减小样本点的步长。 hold off t=0:0.1:1,1.1:0.04:3; y=sin(10*t.2+3); plot(t,y,o) ezplot(sin(10*t2+3),0,3); hold on x1=0:0.001:3; y1=interp1(t,y,x1,spline); plot(x1,y1) 241,xy22fxyexyxy,,(,)sin()2、 用原型函数生成一组网络数据或随机数据,分别拟合出曲3xy,3面,并和原曲面进行比较。 【求解】由下面
3、的语句可以直接生成一组网格数据用下面语句还可以还绘制出给定样本点是三维表面图。 x,y=meshgrid(0.2:0.2:2); z=exp(-x.2-y.4).*sin(x.*y.2+x.2.*y)./(3*x.3+y); surf(x,y,z) 选择新的密集网格则可以通过二元插值得出插值曲面。对比插值结果和新网格下的函数值精确解则可以绘制出绝对插值误差曲面。由插值结果可见精度是令人满意的。 x1,y1=meshgrid(0.2:0.02:2); z1=interp2(x,y,z,x1,y1,spline); surf(x1,y1,z1) z0=exp(-x1.2-y1.4).*sin(x1
4、.*y1.2+x1.2.*y1)./(3*x1.3+y1); surf(x1,y1,abs(z1-z0) 现在假设已知的样本点不是网格形式分布的而是随机分布的则可以用下面语句生成样本点得出分布的二维、三维示意图。 x=0.2+1.8*rand(400,1); y=0.2+1.8*rand(400,1); % 仍生成(0.2,2) 区间的均匀分布随机数 z=exp(-x.2-y.4).*sin(x.*y.2+x.2.*y)./(3*x.3+y); plot(x,y,x) figure, plot3(x,y,z,x) 利用下面的语句可以得出三维插值结果,同时可以绘制出插值的绝对误差曲面可见插值结果
5、还是很好的但由于边界样本点信息不能保证所以不能像网格数据那样对(0.2,2) 区域而只能选择(0.3,1.9) 区域进行插值。 x1,y1=meshgrid(0.3:0.02:1.9); z1=griddata(x,y,z,x1,y1,v4); surf(x1,y1,z1) z0=exp(-x1.2-y1.4).*sin(x1.*y1.2+x1.2.*y1)./(3*x1.3+y1); surf(x1,y1,abs(z1-z0) 、 假设已知一组数据,试用插值方法绘制出区间内的光滑函数曲线,比较各种插值算法3x,(2,4.9)的优劣。 -2 -1.7 -1.4 -1.1 -0.8 -0.5 -
6、0.2 0.1 0.4 0.7 1 1.3 x i.10289 .11741 .13158 .14483 .15656 .16622 .17332 .1775 .17853 .17635 .17109 .16302 y i1.6 1.9 2.2 2.5 2.8 3.1 3.4 3.7 4 4.3 4.6 4.9 x i.15255 .1402 .12655 .11219 .09768 .08353 .07015 .05786 .04687 .03729 .02914 .02236 y i【求解】用下面的语句可以立即得出给定样本点数据的三次插值与样条插值得出的结果如可见用两种插值方法对此例得出的
7、结果几乎一致效果均很理想。 x=-2,-1.7,-1.4,-1.1,-0.8,-0.5,-0.2,0.1,0.4,0.7,1,1.3,. 1.6,1.9,2.2,2.5,2.8,3.1,3.4,3.7,4,4.3,4.6,4.9; y=0.10289,0.11741,0.13158,0.14483,0.15656,0.16622,0.17332,. 0.1775,0.17853,0.17635,0.17109,0.16302,0.15255,0.1402,. 0.12655,0.11219,0.09768,0.08353,0.07019,0.05786,0.04687,. 0.03729,0.
8、02914,0.02236; x0=-2:0.02:4.9; y1=interp1(x,y,x0,cubic); y2=interp1(x,y,x0,spline); plot(x0,y1,:,x0,y2,x,y,o) (,)xy、 假设已知实测数据由下表给出,试对在(0.1,0.1)(1.1,1.1)区域内的点进行插值,并用三维曲面4的方式绘制出插值结果。 yxxxxxxx xxxx i35678910111240 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 1.1 0.1 .83041 .82727 .82406 .82098 .81824 .8161 .8
9、1481 .81463 .81579 .81853 .82304 0.2 .83172 .83249 .83584 .84201 .85125 .86376 .87975 .89935 .92263 .94959 .9801 0.3 .83587 .84345 .85631 .87466 .89867 .9284 .96377 1.0045 1.0502 1.1 1.1529 0.4 .84286 .86013 .88537 .91865 .95985 1.0086 1.0642 1.1253 1.1904 1.257 1.3222 0.5 .85268 .88251 .92286 .9734
10、6 1.0336 1.1019 1.1764 1.254 1.3308 1.4017 1.4605 0.6 .86532 .91049 .96847 1.0383 1.118 1.2046 1.2937 1.3793 1.4539 1.5086 1.5335 0.7 .88078 .94396 1.0217 1.1118 1.2102 1.311 1.4063 1.4859 1.5377 1.5484 1.5052 0.8 .89904 .98276 1.082 1.1922 1.3061 1.4138 1.5021 1.5555 1.5573 1.4915 1.346 0.9 .92006
11、1.0266 1.1482 1.2768 1.4005 1.5034 1.5661 1.5678 1.4889 1.3156 1.0454 1 .94381 1.0752 1.2191 1.3624 1.4866 1.5684 1.5821 1.5032 1.315 1.0155 .62477 1.1 .97023 1.1279 1.2929 1.4448 1.5564 1.5964 1.5341 1.3473 1.0321 .61268 .14763 【求解】直接采用插值方法可以解决该问题得出的插值曲面。 x,y=meshgrid(0.1:0.1:1.1); z=0.83041,0.8272
12、7,0.82406,0.82098,0.81824,0.8161,0.81481,0.81463,0.81579,0.81853,0.82304; 0.83172,0.83249,0.83584,0.84201,0.85125,0.86376,0.87975,0.89935,0.92263,0.94959,0.9801; 0.83587,0.84345,0.85631,0.87466,0.89867,0.9284,0.96377,1.0045,1.0502,1.1,1.1529; 0.84286,0.86013,0.88537,0.91865,0.95985,1.0086,1.0642,1.1
13、253,1.1904,1.257,1.3222; 0.85268,0.88251,0.92286,0.97346,1.0336,1.1019,1.1764,1.254,1.3308,1.4017,1.4605; 0.86532,0.91049,0.96847,1.0383,1.118,1.2046,1.2937,1.3793,1.4539,1.5086,1.5335; 0.88078,0.94396,1.0217,1.1118,1.2102,1.311,1.4063,1.4859,1.5377,1.5484,1.5052; 0.89904,0.98276,1.082,1.1922,1.3061
14、,1.4138,1.5021,1.5555,1.5573,1.4915,1.346; 0.92006,1.0266,1.1482,1.2768,1.4005,1.5034,1.5661,1.5678,1.4889,1.3156,1.0454; 0.94381,1.0752,1.2191,1.3624,1.4866,1.5684,1.5821,1.5032,1.315,1.0155,0.62477; 0.97023,1.1279,1.2929,1.4448,1.5564,1.5964,1.5341,1.3473,1.0321,0.61268,0.14763; x1,y1=meshgrid(0.1
15、:0.02:1.1); z1=interp2(x,y,z,x1,y1,spline); surf(x1,y1,z1) axis(0.1,1.1,0.1,1.1,min(z1(:),max(z1(:) 其实若光需要插值曲面而不追求插值数值的话完全可以直接采用MATLAB 下的shading interp 命令来实现。可见这样的插值方法更好得出的插值 曲面更光滑。 surf(x,y,z); shading interp 5、 习题3和4给出的数据分别为一元数据和二元数据,试用分段三次样条函数和B样条函数对其进行拟合。 【求解】先考虑习题4相应的三次样条插值和B-样条插值原函数与导数函数分别为: x
16、=-2,-1.7,-1.4,-1.1,-0.8,-0.5,-0.2,0.1,0.4,0.7,1,1.3,. 1.6,1.9,2.2,2.5,2.8,3.1,3.4,3.7,4,4.3,4.6,4.9; y=0.10289,0.11741,0.13158,0.14483,0.15656,0.16622,0.17332,. 0.1775,0.17853,0.17635,0.17109,0.16302,0.15255,0.1402,. 0.12655,0.11219,0.09768,0.08353,0.07019,0.05786,0.04687,. 0.03729,0.02914,0.02236;
17、S=csapi(x,y); S1=spapi(6,x,y); fnplt(S); hold on; fnplt(S1) Sd=fnder(S); Sd1=fnder(S1); fnplt(Sd), hold on; fnplt(Sd1) 再考虑习题5 中的数据原始数据不能直接用于样条处理因为meshgrid() 函数产生的数据 格式与要求的ndgrid() 函数不一致所以需要对数据进行处理其中需要的x 和y 均应该 是向量而z 是原来z 矩阵的转置所以用下面的语句可以建立起三次样条和B-样条的插值 模型函数的表面图所示可见二者得出的结果很接近。 x,y=meshgrid(0:0.1:1.1);
18、 z=0.83041,0.82727,0.82406,0.82098,0.81824,0.8161,0.81481,0.81463,0.81579,0.81853,0.82304; 0.83172,0.83249,0.83584,0.84201,0.85125,0.86376,0.87975,0.89935,0.92263,0.94959,0.9801; 0.83587,0.84345,0.85631,0.87466,0.89867,0.9284,0.96377,1.0045,1.0502,1.1,1.1529; 0.84286,0.86013,0.88537,0.91865,0.95985,
19、1.0086,1.0642,1.1253,1.1904,1.257,1.3222; 0.85268,0.88251,0.92286,0.97346,1.0336,1.1019,1.1764,1.254,1.3308,1.4017,1.4605; 0.86532,0.91049,0.96847,1.0383,1.118,1.2046,1.2937,1.3793,1.4539,1.5086,1.5335; 0.88078,0.94396,1.0217,1.1118,1.2102,1.311,1.4063,1.4859,1.5377,1.5484,1.5052; 0.89904,0.98276,1.
20、082,1.1922,1.3061,1.4138,1.5021,1.5555,1.5573,1.4915,1.346; 0.92006,1.0266,1.1482,1.2768,1.4005,1.5034,1.5661,1.5678,1.4889,1.3156,1.0454; 0.94381,1.0752,1.2191,1.3624,1.4866,1.5684,1.5821,1.5032,1.315,1.0155,0.62477; 0.97023,1.1279,1.2929,1.4448,1.5564,1.5964,1.5341,1.3473,1.0321,0.61268,0.14763; x
21、0=0.0:0.1:1; y0=x0; z=z; S=csapi(x0,y0,z); fnplt(S) figure; S1=spapi(5,5,x0,y0,z); fnplt(S1) S1x=fnder(S1,0,1); fnplt(S1x) figure; S1y=fnder(S1,0,1); fnplt(S1y) 6、 重新考虑习题3中给出的数据,试考虑用多项式插值的方法对其数据进行逼近,并选择一个能较好拟合原数据的多项式阶次。 【求解】可以选择不同的多项式阶次例如选择3,5,7,9,11则可以对其进行多项式拟合并绘制出曲线。 x=-2,-1.7,-1.4,-1.1,-0.8,-0.5,
22、-0.2,0.1,0.4,0.7,1,1.3,. 1.6,1.9,2.2,2.5,2.8,3.1,3.4,3.7,4,4.3,4.6,4.9; y=0.10289,0.11741,0.13158,0.14483,0.15656,0.16622,0.17332,. 0.1775,0.17853,0.17635,0.17109,0.16302,0.15255,0.1402,. 0.12655,0.11219,0.09768,0.08353,0.07019,0.05786,0.04687,. 0.03729,0.02914,0.02236; x0=-2:0.02:4.9; p3=polyfit(x,
23、y,3); y3=polyval(p3,x0); p5=polyfit(x,y,5); y5=polyval(p5,x0); p7=polyfit(x,y,7); y7=polyval(p7,x0); p9=polyfit(x,y,9); y9=polyval(p9,x0); p11=polyfit(x,y,11); y11=polyval(p11,x0); plot(x0,y3; y5; y7; y9; y11) 从拟合的结果可以发现选择5 次多项式就能较好地拟合原始数据。 221,()/2x,yxe,()7、 假设习题3中给出的数据满足原型,试用最小二乘法求出的值,并,2用得出的函数将函数
24、曲线绘制出来,观察拟合效果。 221,()/2xaa12yxe(),【求解】令则可以将原型函数写成 aa,122,a2这时可以写出原型函数为 f=inline(exp(-(x-a(1).2/2/a(2)2)/(sqrt(2*pi)*a(2),a,x); 由原型函数则可以用下面的语句拟合出待定参数aa,。这样拟合曲线得出的拟合效果是满意的。 12 x=-2,-1.7,-1.4,-1.1,-0.8,-0.5,-0.2,0.1,0.4,0.7,1,1.3,. 1.6,1.9,2.2,2.5,2.8,3.1,3.4,3.7,4,4.3,4.6,4.9; y=0.10289,0.11741,0.1315
25、8,0.14483,0.15656,0.16622,0.17332,. 0.1775,0.17853,0.17635,0.17109,0.16302,0.15255,0.1402,. 0.12655,0.11219,0.09768,0.08353,0.07019,0.05786,0.04687,. 0.03729,0.02914,0.02236; a=lsqcurvefit(f,1,1,x,y) a = 0.34605753554886 2.23400202798747 x0=-2:0.02:5; y0=f(a,x0); plot(x0,y0,x,y,o) 222zxyaxybyxcxdxye
26、(,)sin()cos(),,8、 假设习题4中数据的原型函数为,试用最小二乘方法识别出abcde,的数值。 【求解】用下面的语句可以用最小二乘的得出 x,y=meshgrid(0.1:0.1:1.1); z=0.83041,0.82727,0.82406,0.82098,0.81824,0.8161,0.81481,0.81463,0.81579,0.81853,0.82304; 0.83172,0.83249,0.83584,0.84201,0.85125,0.86376,0.87975,0.89935,0.92263,0.94959,0.9801; 0.83587,0.84345,0.8
27、5631,0.87466,0.89867,0.9284,0.96377,1.0045,1.0502,1.1,1.1529; 0.84286,0.86013,0.88537,0.91865,0.95985,1.0086,1.0642,1.1253,1.1904,1.257,1.3222; 0.85268,0.88251,0.92286,0.97346,1.0336,1.1019,1.1764,1.254,1.3308,1.4017,1.4605; 0.86532,0.91049,0.96847,1.0383,1.118,1.2046,1.2937,1.3793,1.4539,1.5086,1.5
28、335; 0.88078,0.94396,1.0217,1.1118,1.2102,1.311,1.4063,1.4859,1.5377,1.5484,1.5052; 0.89904,0.98276,1.082,1.1922,1.3061,1.4138,1.5021,1.5555,1.5573,1.4915,1.346; 0.92006,1.0266,1.1482,1.2768,1.4005,1.5034,1.5661,1.5678,1.4889,1.3156,1.0454; 0.94381,1.0752,1.2191,1.3624,1.4866,1.5684,1.5821,1.5032,1.
29、315,1.0155,0.62477; 0.97023,1.1279,1.2929,1.4448,1.5564,1.5964,1.5341,1.3473,1.0321,0.61268,0.14763; x1=x(:); y1=y(:); z1=z(:); A=sin(x1.2.*y1) cos(y1.2.*x1) x1.2 x1.*y1 ones(size(x1); theta=Az1 theta = -0.89204693251635 3.09378647090361 -0.12203277931186 2.70828089435211 -2.42507028220675 用下面的语句可以绘
30、制出拟合结果如图所示。 x,y=meshgrid(0.1:0.02:1.1); z=theta(1)*sin(x.2.*y)+theta(2)*cos(y.2.*x)+theta(3)*x.2+. theta(4)*x.*y+theta(5); surf(x,y,z) 9、 假设已知一组实测数据在文件c8pdat.dat中给出,试通过插值的方法绘制出三维曲面。 【求解】由该文件可见给定的数据是非网格型的x; y; z 向量故提取这些向量并按非网格 数据进行插值则将得出如图所示的插值结果。 load c8pdat.dat x=c8pdat(:,1); y=c8pdat(:,2); z=c8pdat(:,3); max(x), min(x) max(y), min(y) % 找出插值区域 ans = 0.9943 0.0129 0.9994 0.0056 x1,y1=meshgrid(0:0.02:1); z1=griddata(x,y,z,x1,y1,v4); surf(x1,y1,z1)
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1