练习
1.3.1 矩阵的输入与生成
>> a=[1,2,3] a = 1 2 3 1.3-3 分行输入一个行矩阵 >> Martrix_b=[1 2 3; 2 3 4;3 4 5] Martrix_b = 1 2 3 2 3 4 3 4 5 1.3-4 matlab提示出现错误 >> [1 2; 3 4] [1 2; 3 4] | Error: The input character is not valid in MATLAB statements or expressions. 1.3-5 用函数zeros生成全零阵 >> Matrix_b=[1 2 3;3 4 5;5 6 7] Matrix_b = 1 2 3 3 4 5 5 6 7 >> B=zeros(2) B = 0 0 0 0 >> C=zeros(2,3) C = 0 0 0 0 0 0 >> B=zeros(size(Matrix_b)) B = 0 0 0 0 0 0 0 0 0 1.3-6 用函数eye生成全零阵 >> Y=eye(2) Y = 1 0 0 1 >> E=eye(size(B)) E = 1 0 0 0 1 0 0 0 1 1.3.2 矩阵运算 1.3-7加减运算 >> A=[1 2;3 4],B=[5 6;7,8],C=A+B,D=A-B A = 1 2 3 4 B = 5 6 7 8 C = 6 8 10 12 D = -4 -4 -4 -4 1.3-8两个矩阵的乘法运算 >> [1,2;-1,0]*[1,2,3;4,5,6] ans = 9 12 15 -1 -2 -3 >> A=[1,2;-1,0];B=[1,2,3;4,5,6];C=A*B C = 9 12 15 -1 -2 -3 1.3-9 数乘运算 >> A=[1,2,3;4,5,6];B=-2*A,C=A*(-2) B = -2 -4 -6 -8 -10 -12 C = -2 -4 -6 -8 -10 -12 1.3-10 向量的点积 >> a=[1,2];b=[3,4];c=[3;4];d_1=dot(a,b),d_2=(a,c),d_3=a*c a=[1,2];b=[3,4];c=[3;4];d_1=dot(a,b),d_2=(a,c),d_3=a*c | Error: Expression or statement is incorrect--possibly unbalanced (, {, or [. >> a=[1,2];b=[3,4];c=[3;4];d_1=dot(a,b),d_2=dot(a,c),d_3=a*c d_1 = 11 d_2 = 11 d_3 = 11 >> d_4=a*b Error using * Inner matrix dimensions must agree. 1.3-11 向量的叉乘 >> a=[1,0,-1];b=[0,1,2];c=cross(a,b),d=cross(b,a) c = 1 -2 1 d = -1 2 -1 1.3-12 向量的混合积 >> a=[1,0,-1];b=[0,1,2];c=[1,2,0]; >> d_1=dot(cross(a,b),c),d_2=dot(a,cross(b,c)),d_3=dot(cross(c,a),b) d_1 = -3 d_2 = -3 d_3 = -3 1.3-13 左除和右除 >> A=[1,2;0,1];B=[3,2,1;1,2,3];C=[-2,1];X_1=A\B,X_2=C/A X_1 = 1 -2 -5 1 2 3 X_2 = -2 5 1.3-14 矩阵的乘方 >> A=[1,2;2,1];B=A^10,C=[1,2;2,1]^(-2) B = 29525 29524 29524 29525 C = 0.5556 -0.4444 -0.4444 0.5556 1.3-15矩阵的转置 >> A=[1,2;3,4;5,6],B=A' A = 1 2 3 4 5 6 B = 1 3 5 2 4 6 1.3-16 对奇异矩阵求求逆时Matlab给出的警告信息 >> A=[1,2,3;4,5,6;7,8,9];B=inv(A) Warning: Matrix is close to singular or badly scaled. Results may be inaccurate. RCOND = 1.541976e-18. B = 1.0e+16 * -0.4504 0.9007 -0.4504 0.9007 -1.8014 0.9007 -0.4504 0.9007 -0.4504 1.3-17 用初等变换的方法来求矩逆阵 >> A=[1,2;3,4]; B=[1,2,1,0;3,4,0,1]; C=rref(B); C,X=C(:,3:4) C = 1.0000 0 -2.0000 1.0000 0 1.0000 1.5000 -0.5000 X = -2.0000 1.0000 1.5000 -0.5000 1.3-18 以有理格式的输出结果 >> A=[2 1 -1;2 1 2; 1 -1 1]; format D=inv(A) D = 0.3333 0 0.3333 0 0.3333 -0.6667 -0.3333 0.3333 0 >> A=[2 1 -1;2 1 2; 1 -1 1]; format rat D=inv(A) D = 1/3 0 1/3 0 1/3 -2/3 -1/3 1/3 0 Demo The HUMPS FunctionA MATLAB function is a file that starts with the keyword function. This is what the function HUMPS looks like: >> type humps function [out1,out2] = humps(x) %HUMPS A function used by QUADDEMO, ZERODEMO and FPLOTDEMO. % Y = HUMPS(X) is a function with strong maxima near x = .3 | % and x = .9. % % [X,Y] = HUMPS(X) also returns X. With no input arguments, % HUMPS uses X = 0:.05:1. % % Example: % plot(humps) % % See QUADDEMO, ZERODEMO and FPLOTDEMO. % Copyright 1984-2002 The MathWorks, Inc. % $Revision: 5.8 $ $Date: 2002/04/15 03:34:07 $ if nargin==0, x = 0:.05:1; end y = 1 ./ ((x-.3).^2 + .01) + 1 ./ ((x-.9).^2 + .04) - 6; if nargout==2, out1 = x; out2 = y; else out1 = y; end
Plot of HUMPS
This figure shows a plot of HUMPS in the domain [0,2] using FPLOT.
fplot(@humps,[0,2]); Zero of HUMPS The FZERO function finds a zeros of a function near an initial estimate. Our guess here for HUMPS is 1. >> z = fzero(@humps,1,optimset('Display','off')); fplot(@humps,[0,2]); hold on; plot(z,0,'r*'); hold off Minimum of HUMPSFMINBND finds the minimum of a function in a given domain. Here, we search for a minimum for HUMPS in the domain (0.25, 1). >> m = fminbnd(@humps,0.25,1,optimset('Display','off')); fplot(@humps,[0 2]); hold on; plot(m,humps(m),'r*'); hold off Integral of HUMPS QUAD finds the definite integral of HUMPS in a given domain. Here it computes the area in the domain [0.5, 1]. >> q = quad(@humps,0.5,1); fplot(@humps,[0,2]); title(['Area = ',num2str(q)]);