实验二
- 格式:docx
- 大小:17.29 KB
- 文档页数:9
实验二MATLAB矩阵及运算一、实验目的1、掌握矩阵和数组的表示与赋值方法2、了解字符串、结构和单元等数据类型在MATLAB中的使用3、掌握MATLAB中基本的数值运算,了解基本统计函数的使用4、掌握多项式基本运算以及线性方程组的求解方法二、实验内容1. >> a= 0:6:42a =0 6 12 18 24 30 36 42>> b=reshape(a,4,2)b =0 246 3012 3618 42>> c=reshape(a,2,4)c =0 12 24 366 18 30 422.(1)>> A = [3 6 8 1;-5 7 22 17;6 9 16 -12;15 13 -21 0];>> B = A(:,2:4)B =6 8 17 22 179 16 -1213 -21 0(2)>> C = A(3:4,:)C =6 9 16 -1215 13 -21 0(3)>> D = A(1:2,2:4)D =6 8 17 22 17(4)>> a=A([2 3 4])a =-5 6 15>> b=A(1,2:4)b =6 8 1>> E=[a;b]'E =-5 66 815 1(5)>> A(2,:)=[];A(:,3)=[]A =3 6 16 9 -1215 13 03. >> A=zeros(5);>> A(2:4,2:4)=ones(3);>> A(3,3)=3A =0 0 0 0 00 1 1 1 00 1 3 1 00 1 1 1 00 0 0 0 0 4. >> A=[-4 -1 2;-3 0 3;-2 1 4];>> B=find(A>=0)B =56789>> a=A(B)a =1234>> C=find(A<=0)C =12345>> A(C)=0A =0 0 20 0 30 1 45. >> a=[1 2 3;4 5 6];>> b=[2 4 -1;1 3 5];>> c=[1 0 -2];>> d=[1 4 7;8 5 2;3 6 0];>> result1=a'result1 =1 42 53 6>> result2=a*b??? Error using ==> mtimesInner matrix dimensions must agree. a的列与b的行不相等>> result3=a+bresult3 =3 6 25 8 11>> result4=b*dresult4 =31 22 2240 49 13>> result5=[b:c']*d??? Error using ==> mtimesInner matrix dimensions must agree. [b;c’]的列与d的行不相等>> result6=a.*bresult6 =2 8 -34 15 30>> result7=a./bresult7 =0.5000 0.5000 -3.00004.0000 1.6667 1.2000 >> result8=a.*c??? Error using ==> timesMatrix dimensions must agree.a c矩阵个数不相等>> result9=a.\bresult9 =2.0000 2.0000 -0.33330.2500 0.6000 0.8333>> result10=a.^2result10 =1 4 916 25 36>> result11=a^2??? Error using ==> mpowerMatrix must be square.a必须是方阵>> result12=2.^aresult12 =2 4 816 32 646. >> a+bans =-6 11 817 36 8275 3 30>> a*bans =301 60 1303101 108 6081883 113 413 >> a.*bans =-7 24 1660 288 1672476 -40 81 >> a/bans =0.0966 0.0945 0.0080-3.6125 1.5838 -0.5778-1.9917 0.9414 -0.2682>> a^2ans =44 71 244373 511 1736236 333 1109 >> a.^2ans =1 9 1625 144 193649 64 729(2)>> mean(c)ans =9.1000>> median(c)ans =4.5000>> max(c)ans =46>> min(c)ans =-4>> sort(c)ans =-4 0 1 2 3 6 6 8 23 46 (3)>> d=b(2:3,[1,3])d =12 3868 3取出b的第二、三行和第一、三列7. >> A = [7 2 1 -2;9 15 3 -2;-2 -2 11 5;1 3 2 13];>> rank(A)ans =4>> det(A)ans =12568>> inv(A)ans =0.1744 -0.0303 -0.0125 0.0270-0.1050 0.0789 -0.0121 0.00060.0083 0.0173 0.0911 -0.03110.0095 -0.0185 -0.0103 0.0795>> [C,D]=eig(A)C =Columns 1 through 3-0.7629 0.0919 + 0.0640i 0.0919 - 0.0640i0.6223 0.6087 + 0.0276i 0.6087 - 0.0276i0.0807 -0.7474 -0.7474-0.1554 0.0342 - 0.2374i 0.0342 + 0.2374iColumn 4-0.02990.26370.64340.7180D =Columns 1 through 34.8554 0 00 12.6460 + 1.8333i 00 0 12.6460 - 1.8333i0 0 0 Column 415.85269.(1)>> A=[2 0 -1 3 2];>> B=[3 2];>> conv(A,B)ans =6 4 -37 12 4>> [Q,R]=deconv(A,B)Q =0.6667 -0.4444 -0.0370 1.0247R =0 0 0 -0.0000 -0.0494(2)>> X=roots(A)X =0.8214 + 0.9387i0.8214 - 0.9387i-1.0000-0.6427(3)>> x=1;>> f1=2*x^4-x^2+3*x+2f1 =6>> f2=3*x+2f2 =5>> x=[1 3;2 4];>> f1=2*x^4-x^2+3*x+2f1 =396 866578 1260>> f2=3*x+2f2 =5 118 1410.ticA=[7 14 -9 -2 5;3 -15 -13 -6 -4;-11 -9 -2 5 7;5 7 14 16 -2;-2 5 12 -11 -4];b=[100 200 300 400 500];x=A/btocx =0.0045-0.02000.00360.0209-0.0036Elapsed time is 1.542676 seconds.ticsyms x1 x2 x3 x4 x5[x1 x2 x3 x4 x5]=solve(7*x1+14*x2-9*x3+5*x5-100,3*x1-15*x2-13*x3-6*x4-4*x5-200,-11-9*x2-2*x3+5*x4 +7*x5-300,5*x1+7*x2+14*x3+16*x4-2*x5-400,-2*x1+5*x2+12*x3-11*x4-4*x5-500)tocx1 =11685371/98961x2 =-3282811/98961x3 =1791404/32987x4 =-1294405/32987x5 =1495051/32987Elapsed time is 4.746663 seconds.第一种方法快11. >> A={magic(4),18.66,'matlab',7:2:99}A =[4x4 double] [18.6600] 'matlab' [1x47 double]>> A{1}(4,2)+A{2}+A{3}(2)+A{4}(10)ans =154.660012. >> ='li';>> student.age=20;>> student.score=rand(3,10)student =name: 'li'age: 20score: [3x10 double]>> student(2).name='wang';>> student(2).age=21;>> student(2).score=rand(3,10)student =1x2 struct array with fields:nameagescore>> student(3).name='liu';>> student(3).age=21;>> student(3).score=rand(3,10)student =1x3 struct array with fields:nameagescore(1) >> student(2).scoreans =Columns 1 through 50.7060 0.0462 0.6948 0.0344 0.76550.0318 0.0971 0.3171 0.4387 0.79520.2769 0.8235 0.9502 0.3816 0.1869Columns 6 through 100.4898 0.7094 0.6797 0.1190 0.34040.4456 0.7547 0.6551 0.4984 0.58530.6463 0.2760 0.1626 0.9597 0.2238(2) >> mean(student(2).score)ans =Columns 1 through 50.3383 0.3223 0.6541 0.2849 0.5825Columns 6 through 100.5272 0.5800 0.4991 0.5257 0.3832(3) >> avg=[( mean(student(1).score)+ mean(student(2).score)+ mean(student(3).score))./3] avg =Columns 1 through 50.4861 0.5733 0.5089 0.4778 0.6642Columns 6 through 100.4287 0.6209 0.5869 0.6352 0.4523 (4) >> Name={student(1).name,student(2).name,student(3).name} Name ='li' 'wang' 'liu'13.(1) >> str='decision 20 made on 10/20/10';>> findstr(str,'20')ans =10 24(2)>> s1='matlabexpress';s2='matlabexcellent';if s1(1:8)==s2(1:8)disp('1')else disp('0')end1(3) >> s1='matlabexpress';>> k=findstr(s1,'ex');s1(k)=E;s1(k+1)=X;length(k)s1 =matlabEXpressans =1。