实验一x=1x=1y=[123456789];z1=[1:10],z2=[1:2:5];z1=12345678910w=linspace(1,10,10);t1=ones(3),t2=ones(1,3),t3=ones(3,1)t1=111111111t2=111t3=111t4=zeros(3),t5=eye(4)t4=000000000t5=1000010000100001思考题(1)变量名必须以字母开头,并且只能有字母、数字、下划线等三类符号组成,不能含有空格和标点符号等;变量名区分字母的大小写;变量名不能超过63个字符,超过的部分被忽略;关键字不能作为变量名;最好不要使用教材P22当中表2-1的特殊常量符号作为变量名。(2)分号的作用:分隔不想显示计算结果的各个语句;矩阵行与行之间的分隔符;冒号的作用:生成一维数值数组;表示一维数组的全部元素或者多维数组某一维的全部元素。逗号的作用:分隔想要显示结果的各个语句;变量分隔符;矩阵一行中各个元素的分隔符。(3)linspace是线性等分函数,格式为A=linspace(a1,an,n),其中a1是向量的首元素,an是尾元素,意义是把向量等分为n个元素。若省略n,则默认生成50个元素。(4)ones():全1矩阵;zeros():全0矩阵;eye():单位矩阵。a=ones(1,30)a=Columns1through191111111111111111111Columns20through3011111111111formatcompactaa=Columns1through191111111111111111111Columns20through3011111111111formatlongpians=3.14159265358979formatshortpians=3.1416formatlongpians=3.14159265358979format+pians=+-pians=-pisin(pi)ans=+sin(pi)ans=+exist('pi')ans=+pi=0;exist('pi')ans=+pipi=clearpiexist('pi')ans=+结果分析:EXIST('A')returns:0ifAdoesnotexist1ifAisavariableintheworkspace2ifAisanM-fileonMATLAB'ssearchpath.Italsoreturns2whenAisthefullpathnametoafileorwhenAisthenameofanordinaryfileonMATLAB'ssearchpath3ifAisaMEX-fileonMATLAB'ssearchpath4ifAisaMDL-fileonMATLAB'ssearchpath5ifAisabuilt-inMATLABfunction6ifAisaP-fileonMATLAB'ssearchpath7ifAisadirectory8ifAisaJavaclass实验二A=[2,3,4,5,6]A=23456B=[1;2;3;4;5]B=12345A=1:2:10,B=1:10,C=10:-1:1A=13579B=12345678910C=10987654321A=linspace(1,10),B=linspace(1,30,30)A=Columns1through111.00001.09091.18181.27271.36361.45451.54551.63641.72731.81821.9091Columns12through222.00002.09092.18182.27272.36362.45452.54552.63642.72732.81822.9091Columns23through333.00003.09093.18183.27273.36363.45453.54553.63643.72733.81823.9091Columns34through444.00004.09094.18184.27274.36364.45454.54554.63644.72734.81824.9091Columns45through555.00005.09095.18185.27275.36365.45455.54555.63645.72735.81825.9091Columns56through666.00006.09096.18186.27276.36366.45456.54556.63646.72736.81826.9091Columns67through777.00007.09097.18187.27277.36367.45457.54557.63647.72737.81827.9091Columns78through888.00008.09098.18188.27278.36368.45458.54558.63648.72738.81828.9091Columns89through999.00009.09099.18189.27279.36369.45459.54559.63649.72739.81829.9091Column10010.0000B=Columns1through1912345678910111213141516171819Columns20through302021222324252627282930A=logspace(0,4,5)A=110100100010000练习:logspace(1,4*pi,10)ans=1.0e+012*0.00000.00000.00000.00000.00000.00000.00050.00990.19113.6844A=[12345],B=3:7,AT=A',BT=B',A=12345B=34567AT=12345BT=34567E1=A+B,E2=A-BE1=4681012E2=-2-2-2-2-2F=AT-BT,F=-2-2-2-2-2G1=3*A,G2=B/3,G1=3691215G2=1.00001.33331.66672.00002.3333A=ones(1,10);B=(1:10);BT=B';E1=dot(A,B)E1=55E2=A*BTE2=55clearA=1:3,B=3:5,A=123B=345E=cross(A,B)E=-24-2A=[123;456]A=123456B=[147258369]B=147258369A(1)ans=1A(4:end)ans=536B(:,1)ans=1B(:)ans=147258369B(5)ans=5clearA=[1234;5678;9101112;13141516]A=12345678910111213141516B=A(1:3,2:3)B=23671011C=A([13],[24])C=241012D=A([13;24])D=19513clearA=ones(3,4)A=111111111111B=zeros(3)B=000000000C=eye(3,2)C=100100D=magic(3)D=816357492clearA=ones(3,4)A=111111111111B=zeros(3)B=000000000D=[AB]D=111100011110001111000C=eye(4)C=1000010000100001F=[A;C]F=1111111111111000010000100001clearA=[01;11]A=0111B=2*ones(2)B=2222cat(1,A,B,A)ans=011122220111cat(2,A,B,A)ans=012201112211repmat(A,2,2)ans=0101111101011111repmat(A,2)ans=0101111101011111A=[12;3-1],B=[-10;12]A=123-1B=-1012A+Bans=02412*Aans=246-22*A-3*Bans=543-8A*Bans=14-4-2formatrat;A=[101;212;046]A=101212046A1=inv(A)A1=-1/32/3-1/6-2104/3-2/31/6A*A1ans=100*10**1a=[121;314;221],b=[112],d=b'a=121314221b=112d=112c1=b*inv(a),c2=b/ac1=6/73/7-4/7c2=6/73/7-4/7c3=inv(a)*d,c4=a\dc3=12/7-4/7c4=12/7-4/7注意:左除和右除斜杠向左边倾斜式左除,斜杠向右边倾斜式右除;左除就是左边的数或者矩阵作为分母,右除就是右边的数或者矩阵作为分母。练习:(1)A=[634;-257;8-1-3];B=[3;-4;-7];X=A\BX=51/50-14243/25(2)求矩阵的特征值与特征向量使用函数[x,numdar]=eig(A);(3)矩阵的乘幂与开方表达式为A^n,n^A和sqrtm(A);(3)矩阵的指数与对数表达式为expm(A)和logm(A);(4)矩阵的提取与翻转参照教材P46的表2-10。A=[634;-257;8-1-3];B=[3;-4;-7];X=A\BX=51/50-14243/25A1=[123;456;789];A2=reshape([10:18],3,3)A2=101316111417121518T1(:,:,1)=ones(3);T1(:,:,2)=zeros(3)T1(:,:,1)=111111111T1(:,:,2)=000000000T2=ones(3,3,2)T2(:,:,1)=111111111T2(:,:,2)=111111111T3=cat(3,A1,A2),T4=repmat(A1,[1,1,2])T3(:,:,1)=123456789T3(:,:,2)=101316111417121518T4(:,:,1)=123456789T4(:,:,2)=123456789A=[1:6];B=ones(1,6);C1=A+B,C2=A-BC1=234567C2=012345C3=A.*B,C4=B./A,C5=A.\BC3=123456C4=11/21/31/41/51/6C5=11/21/31/41/51/6I=A3,C6=A(I)I=000111C6=456A1=A-3,I2=A1&AA1=-2-10123I2=110111I3=~II3=111000练习:A1=[13;42],A2=[12;21],A3=[35;71]A1=1342A2=1221A3=3571A(:,:,1)=A1,A(:,:,2)=A2,A(:,:,3)=A3A(:,:,1)=1342A(:,:,2)=1221A(:,:,1)=1342A(:,:,2)=1221A(:,:,1)=1342A(:,:,2)=1221A(:,:,3)=3571B=reshape(A,3,2,2)B(:,:,1)=124132B(:,:,2)=271531S1='IlikeMATLAB'S1=IlikeMATLABS2='I''mastudent.'S2=I'mastudent.S3=[S2,'and',S1]S3=I'mastudent.andIlikeMATLABlength(S1)ans=13size(S1)ans=113CS1=abs(S1)CS1=733210810510710132776584766566CS2=double(S1)CS2=733210810510710132776584766566