1、实验1 Matlab基本操作M文件和流程控制语句实验1 Matlab基本操作、M文件和流程控制语句一、实验目的1、 熟悉Matlab的开发环境、熟悉Matlab中的运算符。2、 掌握建立矩阵的方法,掌握相关命令操作。3、 理解命令M文件和函数M文件的区别。掌握M文件的创建和运行。4、 掌握流程控制语句的使用,掌握程序调试的基本方法。二、实验内容1、计算以下表达式的值,将结果按不同格式输出。掌握format命令的使用方法。(1)(2),其中(3),其中解:(1) y=1.33*sin(pi/3)*sqrt(26); format long yy = 9.701689311661140 forma
2、t short yy = 9.7017 format short e yy = 9.7017e+00 format rat yy =2862/295 (2) x=2 1+2*i;-0.45 5; y=1/2.*log(x+sqrt(1+x.2); format long yy = 0.721817737589405 0.734675872184093 + 0.531720011788876i -0.218024834425870 1.156219170636376 format short yy = 0.7218 0.7347 + 0.5317i -0.2180 1.1562 format s
3、hort e yy = 7.2182e-01 7.3468e-01 + 5.3172e-01i -2.1802e-01 1.1562e+00 format rat yy = 397/550 839/1142 + 813/1529i -404/1853 1162/1005 (3) x=-3.0:0.1:3.0; y=(exp(0.3.*x)+exp(-0.3.*x)/2.*sin(x+0.3)+log(0.3+x)/2); format long yy = Columns 1 through 3 -0.312367695327491 + 3.141592653589793i -0.4608486
4、94418105 + 3.141592653589793i -0.599180365543101 + 3.141592653589793i Columns 4 through 6 -0.727110604491848 + 3.141592653589793i -0.844523552887047 + 3.141592653589793i -0.951436599942239 + 3.141592653589793i Columns 7 through 9 -1.047997384412788 + 3.141592653589793i -1.134481116579716 + 3.1415926
5、53589793i -1.211288603813767 + 3.141592653589793i Columns 10 through 12 -1.278945454639893 + 3.141592653589793i -1.338103070443988 + 3.141592653589793i -1.389542235371071 + 3.141592653589793i Columns 13 through 15 -1.434180423553221 + 3.141592653589793i -1.473084426144430 + 3.141592653589793i -1.507
6、490676418045 + 3.141592653589793i Columns 16 through 18 -1.538836931516015 + 3.141592653589793i -1.568811151666629 + 3.141592653589793i -1.599427279622797 + 3.141592653589793i Columns 19 through 21 -1.633144766821371 + 3.141592653589793i -1.673062621009265 + 3.141592653589793i -1.723247684451257 + 3
7、.141592653589793i Columns 22 through 24 -1.789321830997437 + 3.141592653589793i -1.879593758402831 + 3.141592653589793i -2.007474531643163 + 3.141592653589793i Columns 25 through 27 -2.197440558917193 + 3.141592653589793i -2.503493647586021 + 3.141592653589793i -3.096285353775532 + 3.141592653589793
8、i Columns 28 through 30-37.024482642128881 -2.895719102840682 -2.103826354294834 Columns 31 through 33 -1.601599778224542 -1.219844318728149 -0.906005597625341 Columns 34 through 36 -0.637041984905459 -0.400960501199671 -0.190849241868612 Columns 37 through 39 -0.002456590910143 0.166946527327052 0.
9、319160568569469 Columns 40 through 42 0.455393173963419 0.576461565728255 0.682921238866254 Columns 43 through 45 0.775151698900346 0.853416045321053 0.917904034301621 Columns 46 through 48 0.968764369919729 1.006129768827544 1.030137041556975 Columns 49 through 51 1.040943637429033 1.03874159757128
10、3 1.023769534472631 Columns 52 through 54 0.996323039568938 0.956763772865070 0.905527386509961 Columns 55 through 57 0.843130362634892 0.770175795192880 0.687358109898375 Columns 58 through 60 0.595466691652415 0.495388372369536 0.388108722077005 Column 61 0.274712081272634 format short yy = Column
11、s 1 through 7 -0.3124 + 3.1416i -0.4608 + 3.1416i -0.5992 + 3.1416i -0.7271 + 3.1416i -0.8445 + 3.1416i -0.9514 + 3.1416i -1.0480 + 3.1416i Columns 8 through 14 -1.1345 + 3.1416i -1.2113 + 3.1416i -1.2789 + 3.1416i -1.3381 + 3.1416i -1.3895 + 3.1416i -1.4342 + 3.1416i -1.4731 + 3.1416i Columns 15 th
12、rough 21 -1.5075 + 3.1416i -1.5388 + 3.1416i -1.5688 + 3.1416i -1.5994 + 3.1416i -1.6331 + 3.1416i -1.6731 + 3.1416i -1.7232 + 3.1416i Columns 22 through 28 -1.7893 + 3.1416i -1.8796 + 3.1416i -2.0075 + 3.1416i -2.1974 + 3.1416i -2.5035 + 3.1416i -3.0963 + 3.1416i -37.0245 Columns 29 through 35 -2.8
13、957 -2.1038 -1.6016 -1.2198 -0.9060 -0.6370 -0.4010 Columns 36 through 42 -0.1908 -0.0025 0.1669 0.3192 0.4554 0.5765 0.6829 Columns 43 through 49 0.7752 0.8534 0.9179 0.9688 1.0061 1.0301 1.0409 Columns 50 through 56 1.0387 1.0238 0.9963 0.9568 0.9055 0.8431 0.7702 Columns 57 through 61 0.6874 0.59
14、55 0.4954 0.3881 0.2747 format short e yy = Columns 1 through 5 -3.1237e-01 + 3.1416e+00i -4.6085e-01 + 3.1416e+00i -5.9918e-01 + 3.1416e+00i -7.2711e-01 + 3.1416e+00i -8.4452e-01 + 3.1416e+00i Columns 6 through 10 -9.5144e-01 + 3.1416e+00i -1.0480e+00 + 3.1416e+00i -1.1345e+00 + 3.1416e+00i -1.2113
15、e+00 + 3.1416e+00i -1.2789e+00 + 3.1416e+00i Columns 11 through 15 -1.3381e+00 + 3.1416e+00i -1.3895e+00 + 3.1416e+00i -1.4342e+00 + 3.1416e+00i -1.4731e+00 + 3.1416e+00i -1.5075e+00 + 3.1416e+00i Columns 16 through 20 -1.5388e+00 + 3.1416e+00i -1.5688e+00 + 3.1416e+00i -1.5994e+00 + 3.1416e+00i -1.
16、6331e+00 + 3.1416e+00i -1.6731e+00 + 3.1416e+00i Columns 21 through 25 -1.7232e+00 + 3.1416e+00i -1.7893e+00 + 3.1416e+00i -1.8796e+00 + 3.1416e+00i -2.0075e+00 + 3.1416e+00i -2.1974e+00 + 3.1416e+00i Columns 26 through 30 -2.5035e+00 + 3.1416e+00i -3.0963e+00 + 3.1416e+00i -3.7024e+01 -2.8957e+00 -
17、2.1038e+00 Columns 31 through 35 -1.6016e+00 -1.2198e+00 -9.0601e-01 -6.3704e-01 -4.0096e-01 Columns 36 through 40 -1.9085e-01 -2.4566e-03 1.6695e-01 3.1916e-01 4.5539e-01 Columns 41 through 45 5.7646e-01 6.8292e-01 7.7515e-01 8.5342e-01 9.1790e-01 Columns 46 through 50 9.6876e-01 1.0061e+00 1.0301e
18、+00 1.0409e+00 1.0387e+00 Columns 51 through 55 1.0238e+00 9.9632e-01 9.5676e-01 9.0553e-01 8.4313e-01 Columns 56 through 60 7.7018e-01 6.8736e-01 5.9547e-01 4.9539e-01 3.8811e-01 Column 61 2.7471e-01 format rat yy = Columns 1 through 5 -442/1415 + 355/113i -771/1673 + 355/113i -731/1220 + 355/113i
19、-1223/1682 + 355/113i -239/283 + 355/113i Columns 6 through 10 -1391/1462 + 355/113i -3166/3021 + 355/113i -2438/2149 + 355/113i -837/691 + 355/113i -3590/2807 + 355/113i Columns 11 through 15 -748/559 + 355/113i -1063/765 + 355/113i -621/433 + 355/113i -1423/966 + 355/113i -805/534 + 355/113i Colum
20、ns 16 through 20 -3784/2459 + 355/113i -3853/2456 + 355/113i -1118/699 + 355/113i -1153/706 + 355/113i -2267/1355 + 355/113i Columns 21 through 25 -467/271 + 355/113i -1240/693 + 355/113i -1483/789 + 355/113i -1343/669 + 355/113i -1202/547 + 355/113i Columns 26 through 30 -6091/2433 + 355/113i -2251
21、/727 + 355/113i -9071/245 -1083/374 -385/183 Columns 31 through 35 -1001/625 -627/514 -347/383 -939/1474 -334/833 Columns 36 through 40 -609/3191 -44/17911 696/4169 578/1811 342/751 Columns 41 through 45 769/1334 2983/4368 1279/1650 687/805 1051/1145 Columns 46 through 50 2109/2177 1149/1142 376/365
22、 839/806 429/413 Columns 51 through 55 603/589 271/272 686/717 901/995 2381/2824 Columns 56 through 60 439/570 908/1321 683/1147 376/759 470/1211 Column 61 453/1649 2、已知:求下列表达式的值:(1) A+6*B和A-B+I(2) A*B和A.*B(3) A3和A.3(4) A/B和BA(5) A,B和A(1,3,:);B2解:在对话框输入: A=12 34 -4;34 7 87;3 65 7;B=1 3 -1;2 0 3;3 -2
23、 7;(1)A+6*Bans = 18 52 -10 46 7 10521 53 49 A-B+eye(size(A)ans = 12 31 -3 32 8 84 0 67 1(2) A*Bans = 68 44 62 309 -72 596 154 -5 241 A.*Bans = 12 102 4 68 0 261 9 -130 49(3) A3ans = 37226 233824 48604 247370 149188 600766 78688 454142 118820 A.3ans = 1728 39304 -64 39304 343 658503 27 274625 343(4)
24、A/Bans = 16.4000 -13.6000 7.6000 35.8000 -76.2000 50.2000 67.0000 -134.0000 68.0000 ABans = -0.0313 0.3029 -0.3324 0.0442 -0.0323 0.1063 0.0317 -0.1158 0.1558(5) A,Bans = 12 34 -4 1 3 -1 34 7 87 2 0 3 3 65 7 3 -2 7 A(1,3,:);B2ans = 12 34 -4 3 65 7 4 5 1 11 0 19 20 -5 403、已知,(1)当时,求y的值。解:建立fa.m文件func
25、tion d=fa(n)d=n+10*log(n2+5);End对话框里出入 y=fa(40)/(fa(30)+fa(20)y = 0.6390(2)当时,求y的值。解:建立fb.m文件function d=fb(n); d=0; for i=1:n d=d+i*(i+1); endend在对话框处输入 y=fb(40)/(fb(30)+fb(20)y = 1.76624、请分别用if和switch语句实现。输入一个百分制成绩,要求输出成绩等级A、B、C、D、E。其中90分100分为A,80分89分为B,70分79分为C,60分69分为D,60分以下为E。解:用if语句clear;mark=i
26、nput(请输入分数:);if(mark=100&90=mark) display(成绩等级是:A)elseif(mark90&80=mark) display(成绩等级是:B)elseif(mark80&70=mark) display(成绩等级是:C)elseif(mark790&60=mark) display(成绩等级是:D)elseif(mark60&0=mark) display(成绩等级是:E)else display(输入有误)end用switch语句clear;mark=input(请输入分数:);switch(fix(mark/10) case9,10 display(成绩
27、等级是:A) case 8 display(成绩等级是:B) case 7 display(成绩等级是:C) case 6 display(成绩等级是:D) case 0,1,2,3,4,5 display(成绩等级是:E) otherwise display(输入有误!)end 5、已知求中:(1) 这20个数中的最大值,最小值,这20个数的总和。(2) 统计正数、零、负数的个数。(3) 显示的值。、解:f1=1;f2=0;f3=1;max=1;min=0;n=4;zs=2;fs=0;zero=1;sum0=0;while nmax max=f; elseif f0 zs=zs+1; els
28、eif f0 fs=fs+1; else zero=zero+1; end f1=f2; f2=f3; f3=f; n=n+1;enddisplay(最大值:);maxdisplay(最小值:);mindisplay(总和:);sum0display(正数的个数:);zsdisplay(零的个数:);zerodisplay(负数的个数:);fs运行结果:最大值:max = 4.3776e+11最小值:min = -8.9941e+11总和:sum0 = -7.4275e+11正数的个数:zs = 49零的个数:zero = 2负数的个数:fs = 49前f1-f20的值fu = Columns
29、 1 through 16 1 0 1 2 0 -3 -1 5 4 -7 -10 8 21 -5 -39 -8 Columns 17 through 20 65 42 -96 -1156、编写一个函数文件,输入3个参数,前2个为矩阵,第3个是数字0或1,如果是0,则计算矩阵乘积A*B,否则计算A.*B。解:建立f.m文件function C=f(A,B);k=input(请输入 0 或 1 :);A=input(输入矩阵 A:);B=input(输入矩阵 B:);if k=0 if size(A,2)=size(B,1) C=A*B; else disp(A与B无法相乘) endelse if size(A)=size(B) for i=1:size(A,1) for j=1:size(A,2) C(i,j)=A(i,j)*B(i,j); end end else disp(只有同型矩阵才能点乘) endend