MATLAB作业4参考答案(10页).doc

上传人:1595****071 文档编号:39280173 上传时间:2022-09-06 格式:DOC 页数:10 大小:389.50KB
返回 下载 相关 举报
MATLAB作业4参考答案(10页).doc_第1页
第1页 / 共10页
MATLAB作业4参考答案(10页).doc_第2页
第2页 / 共10页
点击查看更多>>
资源描述

《MATLAB作业4参考答案(10页).doc》由会员分享,可在线阅读,更多相关《MATLAB作业4参考答案(10页).doc(10页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。

1、-MATLAB作业4参考答案-第 10 页MATLAB 作业四参考答案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 onx1=0:0.001:3; y1=interp1(t,y,x1,spline);plot(x1,y1)由于曲线本身变换太大,所以在目前选定的样本点下是不可能得出理想插值效果的,因为样本数据提供的

2、信息量不够。为了得到好的插值效果,必须增大样本数据的信息量,对本例来说,必须在快变化区域减小样本点的步长。 hold offt=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 onx1=0:0.001:3; y1=interp1(t,y,x1,spline);plot(x1,y1)2、 用原型函数生成一组网络数据或随机数据,分别拟合出曲面,并和原曲面进行比较。【求解】由下面的语句可以直接生成一组网格数据,用下面语句还可以还绘制出给定样本点是三维表面图。 x,y=meshgrid(0.2

3、: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.*y1.2+x1.2.*y1)./(3*x1.3+y1);surf(x1,y1,abs(z1-z0)现在假设已知的样

4、本点不是网格形式分布的,而是随机分布的,则可以用下面语句生成样本点,得出分布的二维、三维示意图。 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)利用下面的语句可以得出三维插值结果,同时可以绘制出插值的绝对误差曲面,可见插值结果还是很好的,但由于边界样本点信息不能保证,所以不能像网格数据那样对(0.2,2) 区域,而只能选择(0.3,1.9) 区域进

5、行插值。 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)3、 假设已知一组数据,试用插值方法绘制出区间内的光滑函数曲线,比较各种插值算法的优劣。-2-1.7-1.4-1.1-0.8-0.5-0.20.10.40.711.3.10289.11741.13158.14483.15656.16622.17332.1775.17853.17635.171

6、09.163021.61.92.22.52.83.13.43.744.34.64.9.15255.1402.12655.11219.09768.08353.07015.05786.04687.03729.02914.02236【求解】用下面的语句可以立即得出给定样本点数据的三次插值与样条插值,得出的结果如,可见,用两种插值方法对此例得出的结果几乎一致,效果均很理想。 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.117

7、41,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;y1=interp1(x,y,x0,cubic);y2=interp1(x,y,x0,spline);plot(x0,y1,:,x0,y2,x,y,o)4、 假设已知实测数据由下表给出,试对在(0.1,0.1)

8、(1.1,1.1)区域内的点进行插值,并用三维曲面的方式绘制出插值结果。00.10.20.30.40.50.60.70.80.911.10.1.83041.82727.82406.82098.81824.8161.81481.81463.81579.81853.823040.2.83172.83249.83584.84201.85125.86376.87975.89935.92263.94959.98010.3.83587.84345.85631.87466.89867.9284.963771.00451.05021.11.15290.4.84286.86013.88537.91865.959

9、851.00861.06421.12531.19041.2571.32220.5.85268.88251.92286.973461.03361.10191.17641.2541.33081.40171.46050.6.86532.91049.968471.03831.1181.20461.29371.37931.45391.50861.53350.7.88078.943961.02171.11181.21021.3111.40631.48591.53771.54841.50520.8.89904.982761.0821.19221.30611.41381.50211.55551.55731.4

10、9151.3460.9.920061.02661.14821.27681.40051.50341.56611.56781.48891.31561.04541.943811.07521.21911.36241.48661.56841.58211.50321.3151.0155.624771.1.970231.12791.29291.44481.55641.59641.53411.34731.0321.61268.14763【求解】直接采用插值方法可以解决该问题,得出的插值曲面。 x,y=meshgrid(0.1:0.1:1.1);z=0.83041,0.82727,0.82406,0.82098

11、,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.1253,1.1904,1.257,1.3

12、222;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,1.4138,1.5021,1.5555,1.

13、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:0.02:1.1);z1=interp2(x,y,z,

14、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 interp5、 习题3和4给出的数据分别为一元数据和二元数据,试用分段三次样条函数和B样条函数对其进行拟合。【求解】先考虑习题4,相应的三次样条插值和B-样条插值原函数与导数函数分别为: x=-2,-1.7,-1.4,-1.1,-0.8,-0.5,-

15、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;S=csapi(x,y); S1=spapi(6,x,y);fnplt(

16、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);z=0.83041,0.82727,0.82406,0.82098,

17、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.1253,1.1904,1.257,1.32

18、22;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,1.4138,1.5021,1.5555,1.5

19、573,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; x0=0.0:0.1:1; y0=x0; z=z;S=csapi(x0,y0,z); fnp

20、lt(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,-0.2,0.1,0.4,0.7,1,1.3,.1.6,1.9,2.2,2.5,2.8,3.1

21、,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,y,3); y3=polyval(p3,x0);p5=polyfit(x,y,5); y5=polyval(

22、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 次多项式就能较好地拟合原始数据。7、 假设习题3中给出的数据满足原型,试用最小二乘法求出的值,并用得出的函数将函数曲线绘制出来,观察拟合效果。【求解】令则可以将原型函数写成这时可以写出原型函数为 f=inline(exp(-(x-a(1).2/2/a(2)2)/(sqr

23、t(2*pi)*a(2),a,x);由原型函数则可以用下面的语句拟合出待定参数。这样,拟合曲线得出的拟合效果是满意的。 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.

24、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)8、 假设习题4中数据的原型函数为,试用最小二乘方法识别出的数值。【求解】用下面的语句可以用最小二乘的得出 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.814

25、63,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.1253,1.1904,1.257,1.3222;0.85268,0.88251,0.92286,0

26、.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,1.4138,1.5021,1.5555,1.5573,1.4915,1.346;0.92006,1.0

27、266,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=x(:); y1=y(:); z1=z(:);A=sin(x1.2.*y1) cos(y1.2.*x1) x1.2 x1.*y1 ones(si

28、ze(x1);theta=Az1theta =-0.892046932516353.09378647090361-0.122032779311862.70828089435211-2.42507028220675用下面的语句可以绘制出拟合结果,如图所示。 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.datx=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)

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育专区 > 高考资料

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号© 2020-2023 www.taowenge.com 淘文阁