matlab实验报告二

  • 格式:docx
  • 大小:21.13 KB
  • 文档页数:16

Matlab实验报告实验2 数值数组及运算一、实验目的(1)掌握一维数组、二维数组、多项式和高维数组的创建(2)掌握对数组操作的常用函数二、实验内容使用冒号运算符创建下面各表达式对应的向量。

>> x=logspace(1,3,3); x=10.^[1:3]x =10 100 1000>> x=logspace(1,3,5)x =1.0e+003 *0.0100 0.0316 0.1000 0.3162 1.0000利用MATLAB内置函数计算下面的值>>cosh(5)ans =74.2099>>sinh(-2)ans =-3.6269>> (exp(5)+exp(-5))/2ans =74.2099>> help erfERF Error function.Y = ERF(X) is the error function for each element of X. X must be real. The error function is defined as:erf(x) = 2/sqrt(pi) * integral from 0 to x of exp(-t^2) dt.Class support for input X:float: double, singleSee also erfc, erfcx, erfinv.Overloaded functions or methods (ones with the same name in other directories) helpsym/erf.mReference page in Help browserdoc erf>>erf(1.2)ans =0.9103利用linspace函数创建下面表达式的对应向量>> x=0:10; x=linspace(0,10,11)x =0 1 2 3 4 5 6 7 8 9 10>> x=0:0.2:10; x=linspace(0,10,51)x =Columns 1 through 160 0.2000 0.4000 0.6000 0.8000 1.0000 1.20001.4000 1.6000 1.80002.0000 2.2000 2.4000 2.6000 2.80003.0000Columns 17 through 323.2000 3.4000 3.6000 3.80004.0000 4.2000 4.40004.6000 4.80005.0000 5.2000 5.4000 5.6000 5.80006.0000 6.2000Columns 33 through 486.4000 6.6000 6.80007.0000 7.2000 7.4000 7.60007.8000 8.0000 8.2000 8.4000 8.6000 8.8000 9.0000 9.2000 9.4000Columns 49 through 519.6000 9.8000 10.0000>> x=-12:12; x=linspace(-12,12,25)x =-12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10 11 12>> x=10:-1:1; x=linspace(10,1,10)x =10 9 8 7 6 5 4 3 2 1>> x=[10 9 8 7]x =10 9 8 7>> x=[10 9 8 7];y=[1 2 3 4]'; z=x-y';>> zz =9 7 5 3计算>> A=[1 2 3 3;2 3 5 7;1 3 5 7;3 2 3 9;1 8 9 4];>> B=[1+4i 4 3 6 7;2 3 3 5 5;2 6+7i 5 3 4;1 8 9 4 3];>> C=A*B;>> CC =1.0e+002 *0.1400 + 0.0400i 0.5200 + 0.2100i 0.5100 0.3700 0.38000.2500 + 0.0800i 1.0300 + 0.3500i 1.0300 0.7000 0.70000.2400 + 0.0400i 0.9900 + 0.3500i 1.0000 0.6400 0.63000.2200 + 0.1200i 1.0800 + 0.2100i 1.1100 0.7300 0.70000.3900 + 0.0400i 1.1400 + 0.6300i 1.0800 0.8900 0.9500>> D=C(4:5,3:5);>> DD =111 73 70108 89 95>> A=[1 2 3 3;2 3 5 7;1 3 5 7;3 2 3 9;1 8 9 4];>> A=[1 2 3 3;2 3 5 7;1 3 5 7;3 2 3 9;1 8 9 4]A =1 2 3 32 3 5 71 3 5 73 2 3 91 8 9 4>> A=[4 12 20;12 45 78;20 78 136];>> B=[1 2 3;4 5 6;7 8 0];>> I=[1 0 0;0 1 0;0 0 1];>> A+5*Bans =9 22 3532 70 10855 118 136>> A-B+Ians =4 10 178 41 7213 70 137>> A.*Bans =4 24 6048 225 468140 624 0>> A*Bans =192 228 84738 873 3061284 1518 528>> A.^Bans =1.0e+015 *0.0000 0.0000 0.00000.0000 0.0000 0.00020.0000 1.3701 0.0000>> A\BWarning: Matrix is close to singular or badly scaled.Results may be inaccurate. RCOND = 1.328379e-018. ans =1.0e+015 *0.0000 -0.0000 3.6192-0.0000 0.0000 -7.23840.0000 -0.0000 3.6192>> A/Bans =9.3333 -1.3333 0.000040.0000 -7.0000 0.000070.6667 -12.6667 0.0000>> A(find((A>50)&(A<100)))ans =7878>> B(find(B>5))ans =786>> A=[1 2 NaNInf -Inf 5 NaN]A =1 2 NaNInf -Inf 5 NaN >>isnan(A)ans =0 0 1 0 0 0 1 >>isfinite(A)ans =1 1 0 0 0 1 0 >>isinf(A)ans =0 0 0 1 1 0 0 >> any(A)ans =1>> all(A)ans =1求出以下矩阵的逆矩阵>> A=[1 -5 -2;3 4 -9;-7 2 6]A =1 -5 -23 4 -9-7 2 6>> B=[sin(1) sin(-5) sin(-2);sin(3) sin(4) sin(9); sin(-7) sin(2) sin(6)]B =0.8415 0.9589 -0.90930.1411 -0.7568 0.4121-0.6570 0.9093 -0.2794>>inv(A)ans =-0.1673 -0.1036 -0.2112-0.1793 0.0319 -0.0120-0.1355 -0.1315 -0.0757>>inv(B)ans =6.8645 23.4969 12.31719.7255 35.0011 19.974615.5090 58.6555 32.4631>> A=[1 2 3;4 5 6;7 8 9];>> B=flipud(A)B =7 8 94 5 61 2 3>> u=[1 2 3];>> v=[4 5 6];>> A=[u;v]A =1 2 34 5 6用两行语句实现从C中引用列向量>> C=[11 5;5 1;18 7];>> s=C(:,1)s =11518>> t=C(:,2)t =517使用diag函数和冒号运算符创建下面矩阵>>diag(1:1:4)ans =1 0 0 00 2 0 00 0 3 00 0 0 4>>diag(4:-1:1)ans =4 0 0 00 3 0 00 0 2 00 0 0 1>>diag(1:6:19)ans =1 0 0 00 7 0 00 0 13 00 0 0 19使用diag函数创建nxn对称三对角阵>> a=[2 2 2 2];>> b=[-1 -1 -1];>> D=diag(a)+diag(b,-1)+diag(b,1)D =2 -1 0 0-1 2 -1 00 -1 2 -10 0 -1 2创建矩阵>> e=eye(3);>> E=fliplr(e)E =0 0 10 1 01 0 0>> a=2*eye(5)-ones(5)a =1 -1 -1 -1 -1-1 1 -1 -1 -1-1 -1 1 -1 -1-1 -1 -1 1 -1-1 -1 -1 -1 1>> b=tril(a)b =1 0 0 0 0-1 1 0 0 0-1 -1 1 0 0-1 -1 -1 1 0-1 -1 -1 -1 1>> b(:,5)=1b =1 0 0 0 1-1 1 0 0 1-1 -1 1 0 1-1 -1 -1 1 1-1 -1 -1 -1 1使用rehape函数和冒号运算符创建矩阵>> reshape(2:2:24,3,4)ans =2 8 14 204 10 16 226 12 18 24>> reshape(-5:1:6,2,6)ans =-5 -3 -1 1 3 5-4 -2 0 2 4 6 (a)找出每个矩阵各列中绝对值最大的元素(b)找出每个矩阵各行中绝对值最大的元素>> c=C(:,1)c =21>> max(real(c)) ans =11>> d=C(:,2)d =-4211>> max(real(d)) ans =11>> e=C(1,:)e =11 -4 >> max(real(e)) ans =11>> f=C(2,:)f =2 2 >> max(real(f))2>> c=D(:,1)c =13213>> max(real(c)) ans =13>> d=D(:,2)d =-6223>>>> max(real(d)) ans =23>> e=D(1,:)e =13 -6 >> max(real(e)) ans =13>> f=D(2,:)f =2 2>> max(real(f))ans =2>> c=E(:,1)c =4.5000-3.0000-0.7500>> max(real(c))ans =4.5000>> d=E(:,2)d =1.00001.00001.7500>> max(real(d))ans =1.7500>> e=E(1,:)e =4.5000 1.0000>> max(real(e))ans =4.5000>> f=E(2,:)f =-3 1>> max(real(f))ans =1思考下面的程序>> A=ones(3,2);>> B=2*ones(2,3);>> A*B;>> A(2,3)=2;>> A*B;??? Error using ==>mtimesInner matrix dimensions must agree.>> A*B??? Error using ==>mtimesInner matrix dimensions must agree.>> u=0:3;>> v=(3:-1:0)';>> w=u.*v;??? Error using ==> timesMatrix dimensions must agree.>> clear all>> A=ones(3,3);>> A(k,k)??? Undefined function or variable 'k'.>> A(i,i)??? Subscript indices must either be real positive integers or logicals.定义多项式>> a=[1 -12 25 6];>> p=poly2sym(a)p =x^3-12*x^2+25*x+6>> x(2)求下面矩阵的特征多项式>> D=[2 -1 0 0;-1 2 -1 0;0 -1 2 -1;0 0 -1 2];>> PA=poly(D)PA =1.0000 -8.0000 21.0000 -20.0000 5.0000>> PPA=poly2str(PA,'S')PPA =S^4 - 8 S^3 + 21 S^2 - 20 S + 5计算>> R=[-0.5 -0.3+0.4i -0.3-0.4i];>> P=poly(R)P =1.0000 1.1000 0.5500 0.1250>> PR=real(P)PR =1.0000 1.1000 0.5500 0.1250>> PDR=poly2str(PR,'x')PDR =x^3 + 1.1 x^2 + 0.55 x + 0.125。