《MATLAB作业2参考答案(2018)(13页).doc》由会员分享,可在线阅读,更多相关《MATLAB作业2参考答案(2018)(13页).doc(11页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、-MATLAB作业2参考答案(2018)-第 11 页 MATLAB作业二参考答案1、试求下面线性代数方程的解析解与数值解,并检验解的正确性。【求解】求出A, A;B 两个矩阵的秩,可见二者相同,所以方程不是矛盾方程,应该有无穷多解。 A=2,-9,3,-2,-1; 10,-1,10,5,0; 8,-2,-4,-6,3; -5,-6,-6,-8,-4;B=-1,-4,0; -3,-8,-4; 0,3,3; 9,-5,3;rank(A), rank(A B)ans =4 4用下面的语句可以求出方程的解析解,并可以验证该解没有误差。 x0=null(sym(A);x_analytical=sym(
2、A)B; syms a;x=a*x0 x0 x0+x_analyticalx = a+967/1535, a-943/1535, a-159/1535 -1535/1524*a, -1535/1524*a, -1535/1524*a -3659/1524*a-1807/1535,-3659/1524*a-257/1535,-3659/1524*a-141/1535 1321/508*a+759/1535, 1321/508*a-56/1535, 1321/508*a-628/1535 -170/127*a-694/307, -170/127*a+719/307, -170/127*a+103/
3、307 A*x-Bans = 0, 0, 0 0, 0, 0 0, 0, 0 0, 0, 0用数值解方法也可以求出方程的解,但会存在误差,且与任意常数a 的值有关。 x0=null(A); x_numerical=AB; syms a;x=a*x0 x0 x0+x_numerical; vpa(x,10)ans = .2474402553*a+.1396556436, .2474402553*a-.6840666849, .2474402553*a-.1418420333-.2492262414*a+.4938507789,-.2492262414*a+.7023776988e-1,-.249
4、2262414*a+.3853511888e-1 -.5940839201*a, -.5940839201*a, -.5940839201*a .6434420813*a-.7805411315, .6434420813*a-.2178190763,.6434420813*a-.5086089095-.3312192394*a-1.604263460, -.3312192394*a+2.435364854, -.3312192394*a+.3867176824 A*x-Bans = 1/18014398509481984*a, 1/18014398509481984*a, 1/18014398
5、509481984*a -5/4503599627370496*a, -5/4503599627370496*a, -5/4503599627370496*a -25/18014398509481984*a, -25/18014398509481984*a, -25/18014398509481984*a 13/18014398509481984*a, 13/18014398509481984*a, 13/18014398509481984*a2、求解下面的联立方程,并检验得出的高精度数值解(准解析解)的精度。【求解】给出的方程可以由下面的语句直接求解,经检验可见,精度是相当高的。 x1,x2
6、=solve(x12-x2-1=0,(x1-2)2+(x2-0.5)2-1=0,x1,x2)x1 =-1.3068444845633173592407431426632-1.2136904451605911320167045558746*sqrt(-1)-1.3068444845633173592407431426632+1.2136904451605911320167045558746*sqrt(-1)1.06734608580668971340859731280701.5463428833199450050728889725194x2 =-.765201989840551246338855
7、65606586+3.1722093284506318231178646481143*sqrt(-1)-.76520198984055124633885565606586-3.1722093284506318231178646481143*sqrt(-1).139227666886861440483624988051411.3911763127942410521940863240803 norm(double(x1.2-x2-1 (x1-2).2+(x2-0.5).2-1)ans =6.058152713871457e-0313、用Jacobi、Gauss-Seidel迭代法求解方程组 ,给定
8、初值为。【求解】:编写Jacobi、Gauss-Seidel函数计算,function y=jacobi(a,b,x0)D=diag(diag(a); U=-triu(a,1); L=-tril(a,-1);B=D(L+U); f=Db;y=B*x0+f;n=1;while norm(y-x0)=1.0e-6 x0=y; y=B*x0+f; n=n+1;endn a=10,-1,-2;-1,10,-2;-1,-1,5;b=72;83;42; jacobi(a,b,0;0;0)n = 17ans = 11.0000 12.0000 13.0000function y=seidel(a,b,x0)
9、D=diag(diag(a);U=-triu(a,1);L=-tril(a,-1);G=(D-L)U ;f=(D-L)b;y=G*x0+f; n=1;while norm(y-x0)=1.0e-6 x0=y; y=G*x0+f; n=n+1;endn seidel(a,b,0;0;0)n = 10ans = 11.0000 12.0000 13.00004555、假设已知一组数据,试用插值方法绘制出区间内的光滑函数曲线,比较各种插值算法的优劣。-2-1.7-1.4-1.1-0.8-0.5-0.20.10.40.711.3.10289.11741.13158.14483.15656.16622.
10、17332.1775.17853.17635.17109.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
11、.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;y1=interp1(x,y,x0,cubic);y2=interp1(x,y,x0,spline);plot(x0,y1,:,x0,y2,x,y,o)5、
12、假设已知实测数据由下表给出,试对在(0.1,0.1)(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.8
13、4286.86013.88537.91865.959851.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
14、.41381.50211.55551.55731.49151.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.830
15、41,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,1.0086,1.06
16、42,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.082,1.1922,1.30
17、61,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:0
18、.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 interp6、习题4和5给出的数据分别为一元数据和二元数据,试用分段三次样条函数和B样条函数对其进行拟合。【求解】先考虑习题4,相应的三次样条插值和B-样条插值原函数与导数函数分别为: x=-2,-
19、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;S=csapi(x,y
20、); 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);z=0.83041,
21、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,1.0086,1.0642,
22、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.082,1.1922,1.3061,
23、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; x0=0.0:0.1:1; y0=x0; z
24、=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)7、 重新考虑习题4中给出的数据,试考虑用多项式插值的方法对其数据进行逼近,并选择一个能较好拟合原数据的多项式阶次。 【求解】可以选择不同的多项式阶次,例如选择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
25、,.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,y,3); y3=polyval(p3,x0);p5=po
26、lyfit(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 次多项式就能较好地拟合原始数据。8、 假设习题4中给出的数据满足原型,试用最小二乘法求出的值,并用得出的函数将函数曲线绘制出来,观察拟合效果。 【求解】令则可以将原型函数写成这时可以写出原型函数为 f=inline(ex
27、p(-(x-a(1).2/2/a(2)2)/(sqrt(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,.
28、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)9、 假设习题5中数据的原型函数为,试用最小二乘方法识别出的数值。 【求解】用下面的语句可以用最小二乘的得出 x,y=meshgrid(0.1:0.1:1.1);z=0.83041,0.82727,0.82406,0.82098,0
29、.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.322
30、2;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.55
31、73,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=x(:); y1=y(:); z1=z(:);A=sin(x1.2.*y1) cos(y1
32、.2.*x1) x1.2 x1.*y1 ones(size(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)10、假设已知一组实测数据在文件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)