matlab ex ercise 1 matrices &...

Download MATLAB Ex ercise 1 Matrices & Arraysgdjpkc.xmu.edu.cn/filedown.aspx?did=194&downlink=q2008063006191997237.… · Remark : From above example we know, the name of Matlab function

If you can't read please download the document

Upload: others

Post on 29-Jan-2020

9 views

Category:

Documents


0 download

TRANSCRIPT

  • MATLAB Exercise 5  School of Mathematical Sciences Xiamen University  http://gdjpkc.xmu.edu.cn 

    Ex51 

    MATLAB Exercise 5 – Symbol Computation 1.  Compare the following commands, and show the data types and values of a1, a2, a3 and a4. 

    >> a1=1/4+1/6 >> a2=sym(1/4+1/6) >> a3=’1/4+1/6’ >> a4=’1/4+1/6’; eval(a4) 

    2.  Input following commands, try to compare and analysis the results. 1)  a) >> clear; c1= a+2*a; c1 2)  a) >> clear; c3=’ a+2*a’; eval(c3) 

    b) >> clear; c4=’ a+2*a’; a=3; eval(c4) 3)  a) >> clear; syms a; c5=’ a+2*a’; eval(c5) 

    b) >> clear; syms a; c6= a+2*a; eval(c6) c) >> clear; syms a; c7=sym('a+2*a'); subs(c7, a, 2) d) >> clear; syms a; c8=sym('a+2*a'); subs(c8, 2) 

    4)  a) >> clear; c9=sym(‘a+2*a+b’); c9 b) >>  clear; c10=sym('a+2*a+b'); subs(c10, 1) c) >> clear; c11=sym(‘a+2*a+b’); subs(c11, a, 1) d) >> clear; syms a; c12=sym('a+2*a+b'); subs(c12,a,1) e) >> clear; syms a b; c13=sym('a+2*a+b'); subs(c13,a,1, b,2) f) >> clear; syms a b; c14=sym('a+2*a+b'); subs(c14, [a,b], [1, sym('pi')]) 

    3.  Let  3 2 6 11 6 f x x x = − + −  ,  ( 1)( 2)( 3) g x x x = − − −  ,  [ ( 6) 11] 6 h x x x = − + −  .  Please 

    use function factor, horner, expand to prove the following conclusion: 1) f is the expanded form of g and h 2) g is the factor form of f 3) h is the nested form of f 

    4.  Please simply the following functions by using simple, simplify and pretty respectively 

    1)  3  3 2 1 6 12  8 x x x

    + + +  2)  2 cos sin x x + − 

    3) 1 1 1 1 x x

    + + − 

    4) ln c a b e + 

    5) sin 2 sin cos 

    (1 cos 4 )(1 cos )(1 cos ) α α α

    α α α + + − 6)  cos 4 4cos 2 3 α α − + 

    5.  Let  s  express  the  symbolic  expression  sin( )  y a x e +  .  Use  subs  function  to  compute  the 

    following expressions, and simply the results. 

    1)  ln sin( )  t a x e +  2)  sin( ) 1 3 

    a π + 

    6.  Set  2 2 2 3 p a xy xy x y = + + +  . Please use collect function to

  • MATLAB Exercise 5  School of Mathematical Sciences Xiamen University  http://gdjpkc.xmu.edu.cn 

    Ex52 

    1)  collects all the coefficients with the same power of x 2)  views p as a polynomial in its symbolic variable y 

    7.  Use  randn  function  to  generate  a  random  2×2  matrix  A.  Substitute  a  matrix  A  into  the 

    symbolic expression  2 3 2 5 x x − +  . 

    8.  Set symbolic expression  x f e − =  . Please compute the value of 

    1)  (0) f 

    2)  ( ) f eps 

    3)  (1), (2), (3),..., (20) f f f f 

    9.  Generate two symbolic matrices  _ , _ a b u v 

    s a s b c d s t

    = =

     , compute 

    1) s_a+s_b  2) 2*s_a  3) s_a*s_b 4) s_a.*s_b  5) s_a\s_b  6) s_a/s_b 7) s_c=s_a+i*s_b  8) s_c’  9) s_c .’ 10) det(s_a)  11) inv(s_a) 

    10.  Let  a  be  2×2  matrix,  b  be  a  symbolic  matrix 1 

    b x

    =

     .  For  the  following  coefficient 

    matrices a, solve the linear equations ax=b. 

    1) 1 2 3 4 

    a =  

    2) 1 2 3 6 

    a =  

    3) 1 2 

    a =  

    4) 1 2 3 

    a c

    =

     5) 

    c d a 

    e f

    =  

    6) 1 2 3 2 3 4 

    a =  

    11.  For the following matrix a, compute the eigenvalues and eigenvectors of a. 

    1) numeric matrix 1 2 3 4 

    a =  

    2) symbolic matrix 1 2 3 4 

    a =  

    3) symbolic matrix 1 3 4 

    x a =

     4) symbolic matrix 

    s t a 

    u v

    =  

    12.  For the matrices as Ex11, compute their lu, schur, svd decompositions respectively.

    Key to MATLAB Ex 1 - Matrices & Arrays.pdf

  • Key to MATLAB Exercise 1  School of Mathematical Sciences Xiamen University  http://gdjpkc.xmu.edu. 

    Key to Ex11 

    Key to MATLAB Exersise 1  Matrices & Arrays 1.  >> x0=10; v0=15; t=5; a=9.81; x=x0+v0*t+1/2*a*t^2 2.  >> x=3;v=4; 

    1) >> log(x^2+v^2) 2) >> (x3)^(1/2)/(x2*v)^2 3) >> 4/3*pi*v^2 4) >> abs(sin(2*x))*exp(v) 5) >> (x5)^(1/2)  or      >> sqrt(x5) 6) >> x/(v4)    Warning: Divide by zero. ans =Inf 7) >> x/(v4)  Warning: Divide by zero. ans =Inf 8) >> eps 9) >> (x3)/(v4)  Warning: Divide by zero.  ans = NaN 

    3.  ans = 2 ans =0.5000 

    4.  1) >> d=[23:3:2] >> numel(d,5) or >> d=[23,20,17,14,11,8,5,2] >> numel(d,5) 

    2) >> a=[1 2 3 4 2 4 6 8 3 6 9 12]; 

    or >> a = [1:4; 2:2:8; 3:3:12]; or >> a1 =[1:4]; a = [a1; 2*a1; 3*a1]; 

    5.  1) >> size(a) 2) >> a(2,3) 

    >> b=a(:,[1,3]) 3) >> c=a([1,3,2],:) 4) >> x=a(:,end) 5) >> a(1,1)=0 6) omited 7) omited 8) >> a35=a; a35(:,5)=[1;1;1] or >> aa35=[a, [10; 20; 30]] 

    >> a44=a; a44(4,:)=[1 1 1 1] or >> a44=(a; 1 1 1 1) 

    9)14) omited

  • Key to MATLAB Exercise 1  School of Mathematical Sciences Xiamen University  http://gdjpkc.xmu.edu. 

    Key to Ex12 

    6.  >> x=A\B ans x= 0.5000 1.0000 0.5000 >> A/B ??? Error using ==> mrdivide matrix dimensions must agree. A/B is equivalent to the expression  1 AB −  , but here B is a vector, there is no invert of B. 

    7.  1) >> x=ones(6,1); A*x ans = 0 

    0 0 0 0 0 

    From A*x, we get the sum of every row of A, so we know that the determinant of A equal to 0, that is A must be singular. 2)  omited 

    8.  >> A = magic (8); sum(A(:,1:7)) ans =260      260      260      260      260      260      260 

    9.  >> a=[1 2 1]; A=[a;a*2;a*3]; C=[1 11]'; D=[2 2 4]'; 1) >> A*C >> A*D The  results  show  that  though  A≠0,  AC=AD,  but  C  is  not  equal  to  D.  Therefore  the proposition is not true. 2) >> A=round(10*rand(3)); B=round(10*rand(3)); >> inv(A+B) >> inv(A)+inv(B) The proposition is not true. 3) >> (A+B)^2 >> A^2+2*A*B+B^2 The proposition is not true. 4) >> A=round(10*rand(3)); B=A+A’; >> B == B’ ans = 1          1          1 1          1          1 1          1          1 We may try it more times. All results show the proposition is true. 

    10.  omited

    Key to MATLAB Ex 2 - Solving Linear Systems of Equations.pdf

  • Key to MATLAB Exercise 2  School of Mathematical Sciences Xiamen University  http://gdjpkc.xmu.edu. 

    Key to Ex21 

    Key to MATLAB Exercise 2  Solving Linear Systems of Equations 

    1.  >> A=round(10*rand(5)) >> B=round(20*rand(5))10 a)  >> det(A)  ans =5972 

    >> det(A')  ans = 5972  Yes b)  >> det(A+B)  ans = 36495 

    >> det(A)+det(B)  ans =26384  No c)  >> det(A*B)  ans =121900464 

    >> det(A)*det(B)  ans =121900464  Yes d)  >> det(A')*det(B')  ans =121900464 

    >> det(A'*B')  ans =121900464  Yes e)  >> det(inv(A))  ans = 1.6745e004 

    >> inv(det(A))  ans =1.6745e004  Yes f)  >> det(A*inv(B)) ans =0.2926 

    >> det(A)*inv(det(B))  ans =0.2926  Yes 2.  >> A=round(10*rand(6)). 

    a)  >> A=round(10*rand(6)); B=A;B(2,:)=A(1,:);B(1,:)=A(2,:); >> det(A)  ans = 4636 >> det(B) ans = 4636 Interchanging two rows of a matrix changes the sign of the determinant. 

    b)  >> C=A; C(3, :)=4*A(3, :) >> det(C)  ans =18544 >> det(A)*4 ans = 18544 Multiplying a single row of a matrix by a scalar has the effect of multiplying the value of the determinant by that scalar. 

    c)  >> D=A; D(5, :)=A(5, :)+2*A(4, :) >> det(A) ans =4636 >> det(D) ans=4636 Adding a multiple of one row to another does not change the value of the determinant. 

    3. a)  >> A=[1 2; 2 2]; b=[4;2]; x=A\b 

    x = 2 1>> A*x ans = 4 2 >> det(A)  ans =6 Or>> A=[1 2; 2 2]; b=[4;2]; x=inv(A)*b Or

  • Key to MATLAB Exercise 2  School of Mathematical Sciences Xiamen University  http://gdjpkc.xmu.edu. 

    Key to Ex22 

    >> A=[1 2; 2 2]; b=[4;2]; c=[A b]; d=rref(c); x=d(:, end) Same does below. Omited. 

    b)  >> A=[2 3; 5 1]; b=[1;4]; x=A\b x = 1 1 >> A*x ans = 1 4>> det(a) ans = 13 

    c)  >> A=[4 2 1;3 1 2;11 3 0]; b=[2;10;8]; x=A\b Warning: Matrix is close to singular or badly scaled. 

    Results may be inaccurate. RCOND = 5.139921e018. x = 1.0e+016 * 0.4053 1.4862 1.3511 

    >> A*x ans = 

    0 8 8 

    >> det(a) ans =0  (it isn’t the solution, because the det(a)=0) d)  >> A=[1 3 1; 2 1 1; 2 2 1];  b=[1;5;8]; x=A\b 

    x = 21 2 

    >> A*x ans = 

    1 58 

    >> det(A) ans = 

    3 4. 

    >> A=[1 2 3; 2 3 4; 5 4 6]; b=[0;0;1]; >> format rational >> A1=det([b A(:, 2:3)]); A2=det([A(:, 1) b A(:, 3)]); A3=det([A(:, 1:2) b]); ADet=det(A); >> x=[A1/ADet; A2/ADet; A3/ADet]

  • Key to MATLAB Exercise 2  School of Mathematical Sciences Xiamen University  http://gdjpkc.xmu.edu. 

    Key to Ex23 

    x = 1/3 2/3 1/3 

    5. >> AdjOfA=det(A)*inv(A) AdjOfA =

    2                        0  1 8  9                        2 7                        6  1 

    6. a) >> A=[2 1 3; 4 5 1; 2 1 4]; b=[0;8;2]; >> x=A\b x = 

    42 2 

    >> c=null(A,'r') c = 

    Empty matrix: 3by0  so x is the solution >> rref([A b]) 

    42 2 

    b) >> A=[2 1 3; 4 5 1; 2 4 4]; b=[0;8;8]; >> c=null(A,'r') c = 

    8/3 7/3 1 

    >> x0=A\b Warning: Matrix is singular to working precision. x0 = 

    4/3 8/3 0 

    >> syms k >> x=x0+c*k x = 

    4/3+8/3*k1 8/37/3*k1 

    k1 >> d=rref([A b])

  • Key to MATLAB Exercise 2  School of Mathematical Sciences Xiamen University  http://gdjpkc.xmu.edu. 

    Key to Ex24 

    d= 1                          0  8/3  4/3 0                          1  7/3                        8/3 0                          0                          0                          0 

    >> x=d(:, end)+d(: end1)*k >> x=d(:, end)+d(:, end1)*k1 x = 

    4/38/3*k 8/3+7/3*k 

    0 7. 

    >> format short >> A=[2 1 3; 4 5 1; 2 2 10]; d=rref(A) d = 

    1.0000  0  2.6667 0  1.0000        2.3333 0                  0                  0 

    >> i=rank(A) i= 

    2 >> x=[d(:, i+1); 1] x = 

    2.6667 2.3333 1.0000 

    >> A*x ans = 1.0e014 * 0.0444 0.1776 

    0 8.  Omitted 9.  Omitted 10. 

    >> A=[1 2 1; 2 4 2; 2 1 1]; B=[3 1 2; 1 2 2; 3 1 4]; C=A+i*B 1).C = 

    1.0000 + 3.0000i      2.0000 + 1.0000i      1.0000 + 2.0000i 2.0000 + 1.0000i      4.0000 + 2.0000i      2.0000 + 2.0000i 2.0000 + 3.0000i      1.0000 + 1.0000i      1.0000 + 4.0000i 

    >> C' ans = 

    1.0000  3.0000i      2.0000  1.0000i      2.0000  3.0000i 2.0000  1.0000i      4.0000  2.0000i      1.0000  1.0000i 1.0000  2.0000i      2.0000  2.0000i      1.0000  4.0000i

  • Key to MATLAB Exercise 2  School of Mathematical Sciences Xiamen University  http://gdjpkc.xmu.edu. 

    Key to Ex25 

    >> C.' ans = 

    1.0000 + 3.0000i      2.0000 + 1.0000i      2.0000 + 3.0000i 2.0000 + 1.0000i      4.0000 + 2.0000i      1.0000 + 1.0000i 1.0000 + 2.0000i      2.0000 + 2.0000i      1.0000 + 4.0000i 

    The  first  matrix  is  the  complex  conjugate  transpose  of  C  while  the  second  one  is  the transpose of C. Because the elements of C are complex, the results are different. 

    2). >> A A = 

    1          2          1 2          4          2 2          1          1 

    >> A' ans = 

    1          2          2 2          4          1 1          2          1 

    >> A.' ans = 

    1          2          2 2          4          1 1          2          1 

    The first matrix is equal to the second one. 3). >> inv(A) Warning: Matrix is singular to working precision. ans = 

    Inf      Inf      Inf Inf      Inf      Inf Inf      Inf      Inf 

    >> pinv(A) ans = 

    0.0727  0.1455        0.6364 0.1273        0.2545  0.3636 0.0182        0.0364        0.0909 

    The first matrix is not equal to the second one. 4). >> inv(B) ans = 

    0.6000  0.2000  0.2000 0.2000        0.6000  0.4000 0.5000                  0        0.5000 

    >> pinv(B) ans = 

    0.6000  0.2000  0.2000 0.2000        0.6000  0.4000

  • Key to MATLAB Exercise 2  School of Mathematical Sciences Xiamen University  http://gdjpkc.xmu.edu. 

    Key to Ex26 

    0.5000  0.0000        0.5000 Because B is not singular, inv(B) is equal to pinv(B). 

    11.  Omitted

    Key to MATLAB Ex 3 - Linear Space.pdf

  • Key to MATLAB Exercise 3  School of Mathematical Sciences Xiamen University  http://gdjpkc.xmu.edu. 

    Key to Ex31 

    Key to MATLAB Exercise 3 – Linear Space 1. 

    1) >> a1=[1;0;0]; a2=[0;1;0]; a3=[0;0;1]; A=[a1,a2,a3] A = 

    1          0          0 0          1          0 0          0          1 

    >> rank(A) ans = 3 {a1,a2,a3} is spanning set for  3 1 R ×  , because the rank of A equals to 3, therefore. Or>> det(A) ans = 

    1 {a1,a2,a3} is spanning set for  3 1 R ×  , for the determinant of A is not zero. 2) >> a1=[1;0;0]; a2=[0;1;1]; a3=[1;0;1]; a4=[1;2;3]; A=[a1,a2,a3,a4]; rref(A) ans = 

    1          0          0  0 0          1          0          2 0          0          1          1 

    The result shows that {a1 a2 a3} is spanning set for  3 1 R ×  . Or>> A1=A(:, 1:3) A1 = 

    1          0          1 0          1          0 0          1          1 

    >> rank(A1) ans = 

    3 >> rank(A) ans = 

    3 The result shows that {a1 a2 a3} is spanning set for  3 1 R ×  . 3) >> a1=[2;1;2]; a2=[3;2;2]; a3=[2;2;0]; A=[a1,a2,a3] A = 

    2          3          2 1          2          2 2  2          0 

    >> rref(A)

  • Key to MATLAB Exercise 3  School of Mathematical Sciences Xiamen University  http://gdjpkc.xmu.edu. 

    Key to Ex32 

    ans = 1          0  2 0  1          2 0          0          0 

    They are not spanning sets for  3 1 R ×  , because there are less than 3 independent vectors. Or>> det(A) ans = 

    0 They  are  not  spanning  sets  for  3 1 R ×  ,  because  the  matrix  A,  which  is  formed  by  the corresponding vectors, is singular. 4) >> A=[2 1 4; 1 1 2; 2 2 4 ]; >> rref(A) 

    ans = 1  1          2 0          0          0 0          0          0 

    Or>> rank(A) 

    ans = 1 

    They are not spanning sets for  3 1 R ×  . 2. 

    1) >> x1=[1;2;3]; x2=[3;4;2]; x=[2;6;6]; y=[9;2;5]; >> A1=[x1,x2,x]; >> rref(A1) ans = 

    1          0          0 0          1          0 0          0          1 

    1 2 Span( , ) x x x ∉  , for the vectors x1, x2, x are independent. 

    Or>> rank(A1) ans = 

    3 >> rank([x1, x2]) ans = 

    1 2 Span( , ) x x x ∉  , for the rank of { x1, x2} is not equal to that of {x1, x2, x}. 

    2)

  • Key to MATLAB Exercise 3  School of Mathematical Sciences Xiamen University  http://gdjpkc.xmu.edu. 

    Key to Ex33 

    >> rref([x1,x2,y]) ans = 

    1          0          3 0          1  2 0          0          0 

    Result implied that  1 2 Span( , ) y x x ∈  . 

    3. 1) >> x1=[1; 0; 0]; x2=[0; 0; 1]; x3=[1; 0; 1]; A=[x1, x2, x3]; r=rank(A); >> m2=size(A, 2);  % calculate the column size of A >> if r==m2 

    disp (‘They are spanning sets for P3’); elsedisp (‘They are not spanning sets for P3’) end 

    It is not spanning set for P3. 2) >> x1=[1; 0; 0]; x2=[2; 1; 0]; x3=[0; 1; 0]; x4=[1; 0; 1]; rank([x1, x2, x3, x4]) ans= 

    3

    { } 2 2, 2, , 2 1 x x x − +  is spanning set for P3. 3) >> x1=[1; 1; 0]; x2=[2; 1; 0]; x3=[3; 0; 1]; rref([x1, x2, x3]) ans = 

    1          0          0 0          1          0 0          0          1 

    It means that { } 2 1, 2, 3 x x x + − +  is spanning set for P3. 4. 

    1) >> a1=[1 1 1]'; a2=[0 1 1]'; a3=[1 0 1]'; rank([a1, a2, a3]) ans = 

    3 They are linearly independent. 2) >> a1=[2 1 2]'; a2=[2 2 0]'; A=[a1, a2]; >> rank(A)==size(A, 2)  %check whether the rank of A is equal to the column size of A. ans = 

    1  % 1 means “true”, 0 means “false” They are linearly independent. 3)

  • Key to MATLAB Exercise 3  School of Mathematical Sciences Xiamen University  http://gdjpkc.xmu.edu. 

    Key to Ex34 

    >> a1=[2 1 2]'; a2=[2 1 2]'; a3=[4 2 4]'; A=[a1,a2,a3]; rank(A)==size(A, 2) ans = 

    0 They are linearly dependent. 

    5. For example 2) >> x1=[1; 0; 0]; x2=[2; 1; 0]; x3=[0; 1; 0]; x4=[1; 0; 1]; rref([x1, x2, x3, x4]) ans = 

    1          0          2          0 0          1          1          0 0          0          0          1 

    Span{ } 2 2, 2, , 2 1 x x x − +  =Span{ } 2 2, 2, 2 1 x x − +  . 6. 

    >> x1=[1 2 3]'; x2=[3 4 2]'; x3=[0 10 11]'; 1) >> rank([x1,x2,x3]) ans = 

    2 Or>> rank(x1, x2, x3)==size([x1, x2, x3], 2) ans = 

    1 2 3 , , x x x  are linearly dependent. 

    2) >> rank([x1 ,x2]) ans = 

    1 x and  2 x  are linearly independent. 

    3) 

    The rank of vector set {  1 2 3 , , x x x  } shows the dimension is 2. 

    4) They are the 2dimension subspace of  3 1 R ×  . 

    7. >> x1=[1 2 3]'; x2=[3 4 2]'; x3=[0 10 11]'; x4=[2 7 3]'; x5=[1 3 2]'; rref([x1,x2,x3,x4,x5]) ans = 

    1.0000                  0        3.0000                  0        0.4194 0        1.0000        1.0000                  0  0.6452 0                  0                  0        1.0000        0.6774 

    {x1, x2, x4} forms a basis for R 3 . 8.

  • Key to MATLAB Exercise 3  School of Mathematical Sciences Xiamen University  http://gdjpkc.xmu.edu. 

    Key to Ex35 

    >> x1=[3;2]; x2=[1;3]; A=[x1 x2]; x=[10;7]; c=inv(A)*x c = 

    3.2857 0.1429 

    9. >> x1=[2; 4]; x2=[1; 3]; B=[x1, x2]; >> Transition_matrix=B Transition_matrix = 

    2          1 4          3 

    >> x=[10; 7]; >> coordinates =inv(Transition_matrix) *x coordinates = 

    11.5000 13.0000 

    10. >> X=[x1, x2]; Y=[y1, y2]; >> Transition_matrix =X\Y Transition_matrix = 

    2.5000        2.5000 4.0000  2.0000 

    11. 1) >> A=[1 2 3;2 3 6;9 4 6] A = 

    1          2          3 2          3          6 9          4  6 

    >> x1=A(1,:); x2=A(2,:); x3=A(3,:); >> y1=A(:,1); y2=A(:,2); y3=A(:,3); >> rref(A) ans = 

    1          0          0 0          1          0 0          0          1 

    So a basis for the row space is {x1, x2, x3}, a basis for the column space is {y1, y2, y3}. The nullspace is {0}. 2) >> x1=A(1,:); x2=A(2,:); x3=A(3,:); y1=A(:,1); y2=A(:,2); y3=A(:,3); y4=A(:,4); >> rref(A) ans = 

    1          0          1          0 0          1          1          0 0          0          0          1

  • Key to MATLAB Exercise 3  School of Mathematical Sciences Xiamen University  http://gdjpkc.xmu.edu. 

    Key to Ex36 

    So a basis for the row space is {x1, x2, x3}; a basis for the column space is {y1, y2, y4} and a basis for the null space is [1 1 1 0]’. 3) >> A=[1 2 1 0;3 2 5 6;2 0 6 6 ]; >> x1=A(1,:); x2=A(2,:); x3=A(3,:); y1=A(:,1); y2=A(:,2); y3=A(:,3); y4=A(:,4); >> rref(A) ans = 

    1.0000                  0                  0        0.7500 0  1.0000                  0                  0 0                  0        1.0000        0.7500 

    So a basis  for  the  row space is {x1, x2, x3};  a basis  for  the  column space is {y1, y2,  y3}. Notice that [ 0.7500 0 0.7500 1]’ is a solution of Ax=0 and rank(A)=3, then a basis for the null space is [ 0.7500 0 0.7500 1]’. 

    12. ²  >> A10=rand(10); 

    >> b10=rand(10, 1); >> tic; >> x1=inv(A10)*b10; >> toc Elapsed time is 28.611000 seconds. 

    ²  >> tic; >> [l,u]=lu(A10); >> y1=inv(l)*b10; >> x2=inv(u)*y1; >> toc Elapsed time is 50.012000 seconds. 

    ²  >> tic; >> [q,r]=qr(A10); >> y1=q'*b; >> y1=q'*b10; >> x1=inv(r)*y1; >> toc Elapsed time is 105.983000 seconds.

    Key to MATLAB Ex 4 - Eigenvalue.pdf

  • Key to MATLAB Exercise 4  School of Mathematical Sciences Xiamen University  http://gdjpkc.xmu.edu.cn 

    Key to Ex41 

    Key to MATLAB Exercise 4 – Eigenvalue 1. 

    1) >> A=[3 2;3 2]; [v,d]=eig(A) v = 

    0.8944  0.3162 0.4472        0.9487 

    d = 4          0 0  3 

    >> det(v) ans = 

    0.9899 It isn't defective, because the determinant of v is not zero. Key to 2) to 9) are omitted. 

    2. >> A=[2 0 0;0 4 0;1 0 2]; B=[2 0 0;0 4 0;3 6 2]; >> eig(A) ans = 

    2 2 4 

    >> eig(B) ans = 

    2 2 4 

    Notice As 2 is the repeated eigenvalue, we can’t judge whether A is similar to B only by their eigenvalues. That is, we may say A is similar  to B if A and B have the same Jordan normal forms. >> [XA, JordanA] = jordan(A) XA = 

    0          0          1 1          0          0 0          1          0 

    JordanA = 4          0          0 0          2          1 0          0          2 

    >> [XB, JordanB] = jordan(B) XB = 

    0          0          2 1          0          0

  • Key to MATLAB Exercise 4  School of Mathematical Sciences Xiamen University  http://gdjpkc.xmu.edu.cn 

    Key to Ex42 

    3  6  3 JordanB = 

    4          0          0 0          2          1 0          0          2 

    A is similar to B because JordanA is equal to JordanB. Or>> [VA, DA]=eig(A) VA = 

    0        0.0000                  0 0                  0        1.0000 

    1.0000  1.0000                  0 DA = 

    2          0          0 0          2          0 0          0          4 

    >> [VB, DB]=eig(B) VB = 

    0        0.0000                  0 0                  0        0.3162 

    1.0000        1.0000        0.9487 DB = 

    2          0  0 0          2          0 0          0          4 

    >> rank(VA)==rank(VB) ans = 

    1 2, 2, 4 are the eigenvalues of A and B, and the rank of VA equals to that of VB, which means 

    that A has the same Jordan normal form as B. Therefore A is similar to B. 3. 

    >> A=rand(4); >> P=poly(A) P = 1.0000  1.5430        0.0898  0.0310        0.1878 >> poly2sym(P) ans = x^41543/1000*x^3+449/5000*x^231/1000*x+939/5000 >> roots(P) ans = 

    1.4313 0.5970 0.2427 + 0.4011i 0.2427  0.4011i

  • Key to MATLAB Exercise 4  School of Mathematical Sciences Xiamen University  http://gdjpkc.xmu.edu.cn 

    Key to Ex43 

    It means that the characteristic polynomial is  4 3 2 1.543 0.0898 0.031 0.1878 x x x x − + − + 

    and the eigenvalues (or characteristic roots ) of A are 1.4313, 0.5970. 0.2427+0.4011i and 0.2427  0.4011i. 4. 

    1) >> A=[0 1;1 0]; [X1, D]=eig(A) X1= 

    0.7071        0.7071 0.7071        0.7071 

    D = 1          0 0          1 

    >> rank(X1)==size(X1, 1) ans = 

    The answer is 0.7071 0.7071 

    1 0.7071 0.7071 

    X −

    =  

    , 1 0 0 1 

    D −

    =  

    . Here X1 is an invertible matrix. 

    We may check X1 by other functions such as det, rref and so on. Or>> [X2 S]=schur(A) X2 = 

    0.7071        0.7071 0.7071        0.7071 

    S = 1          0 0          1 

    >> X2*S*inv(X2) ans = 

    0        1.0000 1.0000                  0 

    Therefore X2 is the answer, too. The key to 2) to 6) is omitted. 

    5. 1) >> A=[2 1;2 1];[v, d]=eig(A);B= v*sqrt(d)* inv(v) B = 

    1.5119        0.3780 0.7559        1.1339 

    >> B*B ans = 

    2.0000        1.0000 2.0000        1.0000

  • Key to MATLAB Exercise 4  School of Mathematical Sciences Xiamen University  http://gdjpkc.xmu.edu.cn 

    Key to Ex44 

    The key to 2) is omitted. 6. 

    >> format rational; A=[0 1 1;1 2 0;1 0 3]; >> [U, T]=schur(A) U = 755/833          1661/6691  1573/4601 1573/4601          755/833  1661/6691 1661/6691        1573/4601          755/833 T = 655/1006                0                        0 

    0                2665/1172                0 0                        0                2874/851 

    7. >> A=[1 1;2 3;1 0]; >> [u, s, v]=svd(A) u = 

    0.3583        0.2312  0.9045 0.9208  0.2474        0.3015 0.1541        0.9409        0.3015 

    s = 3.9090                  0 

    0        0.8485 0                  0 

    v = 0.6022        0.7983 0.7983  0.6022 

    >> SingularValuesOfA = diag(s) SingularValuesOfA = 

    3.9090 0.8485 >> RootOfAA=roots(poly(AA)) RootOfAA = 

    15.2801 0.7199 

    >> abs(SingularValuesOfA.^2RootOfAA) > A=[4 3 12; 17 11 0; 1 12 3]; PolyA=poly(A); roots(PolyA) ans =

  • Key to MATLAB Exercise 4  School of Mathematical Sciences Xiamen University  http://gdjpkc.xmu.edu.cn 

    Key to Ex45 

    16.8781 2.4391 +10.6951i 2.4391 10.6951i 

    >> eig(A) ans = 16.8781 

    2.4391 +10.6951i 2.4391 10.6951i 

    >> schur(A) ans = 16.8781  2.1204  12.4708 

    0  2.4391  7.7919 0  14.6799        2.4391 

    >> svd(A) ans = 

    21.8740 14.6390 

    6.3427 >> svd(A)^2 ans = 478.4705 214.3003 40.2293 

    >> eig(A'*A) ans = 

    40.2293 214.3003 478.4705 

    The  above  show  that we may  use  functions  eig  and  roots  to  calculate  the  eigenvalues  of  a matrix. The result of function schur in real, that is, it will not show the eigenvalues of A if there are  complex  eigenvalues.  And  svd  provides  the  values  which  are  the  square  roots  of  the eigenvalues of A’A. 9. 

    >> A1=rand(4); B1=A1+A1';  % generate a symmetric matrix B1 >> A2=10*rand(4); B2=A2*A2';  % generate a symmetric matrix B2 We omitted the following for it is similar to that of Ex10. 

    10. >> P=fix(10*ran(3)); [Q r]=qr(P); >> D1=diag([0, 1, 2]); D2=diag(1:3); Jordan1=[1 0 0; 0 2 1; 0 0 2]; >> SingularMatrix=P*D1; >> NotSingularMatrix=P*D2; >> DiagonalMatrix=P’ *P;  % any symmetric matrix is diagonal. >> NotDiagonalMatrix=Q’*Jordan1*Q; 

    11.  Omitted

    Key to MATLAB Ex 5 - Symbol Computation.pdf

  • Key to MATLAB Exercise 5  School of Mathematical Sciences Xiamen University  http://gdjpkc.xmu.edu.cn 

    Key to Ex51 

    Key to MATLAB Exercise 5 – Symbol Computation 1. 

    >> a1=1/4+1/6  % double >> a2=sym(1/4+1/6)  % sym >> a3=’1/4+1/6’  % char >> a4=’1/4+1/6’; eval(a4)  % char 

    2. 1)  a) 

    >> clear; c1=a+2*a; c1 ??? Undefined function or variable 'a'. b) >> clear; c2=‘a+2*a’; c2 c2 = a+2*a Conclusion. It is illegal in the statement of “c1=a+2*a” of a) where ‘a’ is not defined before we assign data to variable c1. But “c2=a+2*a” of b) is right, due to here ‘a’ being a character. 

    2)  a) >> clear; c3='a+2*a'; eval(c3) ??? Error using ==> eval Undefined function or variable 'a'. b) >> clear; c4=’ a+2*a’; a=3; eval(c4) ans = 

    9 3)  a) 

    >> clear; syms a; c5=’ a+2*a’; eval(c5) ans = 3*a b) >> clear; syms a; c6= a+2*a; eval(c6) ans = 3*a c) >> clear; syms a; c7=sym('a+2*a'); subs(c7, a, 2) ans = 

    6 d) >> clear; syms a; c8=sym('a+2*a'); subs(c8, 2) ans = 

    6 4)  a) 

    >> clear; c9=sym(‘a+2*a+b’); c9

  • Key to MATLAB Exercise 5  School of Mathematical Sciences Xiamen University  http://gdjpkc.xmu.edu.cn 

    Key to Ex52 

    c8 = a+2*a+b b) >>  clear; c10=sym('a+2*a+b'); subs(c10, 1) ans = 3*a+1 c) >> clear; c11=sym(‘a+2*a+b’); subs(c11, a, 1) ??? Undefined function or variable 'a'. d) >> clear; syms a; c12=sym('a+2*a+b'); subs(c12,a,1) ans = 3+b e) >> clear; syms a b; c13=sym('a+2*a+b'); subs(c13,a,1, b,2) ??? Error using ==> sym.subs Too many input arguments. f) ans = 3+pi 

    3. 1) 

    >> syms x; f=x^36*x^2+11*x6; g=(x1)*(x2)*(x3); h=x*[x*(x6)+11]6; >> expand(g) ans = x^36*x^2+11*x6 >> expand(h) ans = x^36*x^2+11*x6 

    2) >> factor(f) ans = (x1)*(x2)*(x3) 

    3) >> horner(f) ans = x*(x*(x6)+11)6 

    4. 1) >> syms x; f=[x^(3)+6*x^(2)+12*x^(1)+8]^(1/3) f = (1/x^3+6/x^2+12/x+8)^(1/3) >> simple(f) simplify:

  • Key to MATLAB Exercise 5  School of Mathematical Sciences Xiamen University  http://gdjpkc.xmu.edu.cn 

    Key to Ex53 

    ((2*x+1)^3/x^3)^(1/3) radsimp: (2*x+1)/x combine(trig): ((1+6*x+12*x^2+8*x^3)/x^3)^(1/3) factor: ((2*x+1)^3/x^3)^(1/3) expand: (1/x^3+6/x^2+12/x+8)^(1/3) combine: (1/x^3+6/x^2+12/x+8)^(1/3) convert(exp): (1/x^3+6/x^2+12/x+8)^(1/3) convert(sincos): (1/x^3+6/x^2+12/x+8)^(1/3) convert(tan): (1/x^3+6/x^2+12/x+8)^(1/3) collect(x): (1/x^3+6/x^2+12/x+8)^(1/3) mwcos2sin: (1/x^3+6/x^2+12/x+8)^(1/3) ans = (2*x+1)/x >> simplify(f) ans = ((2*x+1)^3/x^3)^(1/3) >> pretty(f) 

    / 1            6            12  \1/3 | +  +  + 8| |    3            2          x            | \ x            x  / 

    Omitted 2) to 6). 5. 

    >>clear; syms a x y; s=a*sin(x)+exp(y) s = a*sin(x)+exp(y) 

    1) >> syms t >> subs(s, y, log(t)) ans = a*sin(x)+ t 

    2) >> subs(s,{x,y},{sym('pi/3'),0})

  • Key to MATLAB Exercise 5  School of Mathematical Sciences Xiamen University  http://gdjpkc.xmu.edu.cn 

    Key to Ex54 

    ans = 1/2*a*3^(1/2)+1 

    6. 1) >> clear; syms x y; p=a+x*y+2*x*y^2+3*x^2*y p = a+x*y+2*x*y^2+3*x^2*y >> collect(p, x) ans = 3*x^2*y+(y+2*y^2)*x+a 

    2) >> collect(a+x*y+2*x*y^2+3*x^2*y, y) ans = 2*x*y^2+(x+3*x^2)*y+a 

    7. >> A=randn(2); syms x; s=3*x^2+52*x; >> polyvalm(sym2poly(s), A) ans = 

    5.2187        4.6712 8.7431      13.0285 

    Notice: subs(s, A) cann’t be used here, because it equals to 3.*A.*A+5.*[1, 1; 1, 1]2.*A. 8. 

    >> clear; syms x; f=exp(x) f = exp(x) 

    1) >> subs(f, 0) ans = 

    1 2) >> subs(f, eps) ans = 

    1.0000 3) >> a=[1:1:20]; format short; subs(f, a) ans = Columns 1 through 9 0.3679        0.1353        0.0498        0.0183        0.0067        0.0025        0.0009 

    0.0003        0.0001 Columns 10 through 18 0.0000        0.0000        0.0000        0.0000        0.0000        0.0000        0.0000 

    0.0000        0.0000 Columns 19 through 20 0.0000  0.0000

  • Key to MATLAB Exercise 5  School of Mathematical Sciences Xiamen University  http://gdjpkc.xmu.edu.cn 

    Key to Ex55 

    9. >> syms a b c d u v s t; s_a=[a b;c d]; s_b=[u v;s t]; 

    1) >> s_a+s_b ans = [ a+u, b+v] [ c+s, d+t] 

    2) >> 2 * s_a ans = [ 2*a, 2*b] [ 2*c, 2*d] 

    3) >> s_a * s_b ans = [ a*u+b*s, a*v+b*t] [ c*u+d*s, c*v+d*t] 

    4) >> s_a .* s_b ans = [ a*u, b*v] [ c*s, d*t] 

    5) >> s_a\s_b ans = [ (b*s+u*d)/(a*dc*b),  (b*tv*d)/(a*dc*b)] [ 1/(a*dc*b)*(c*ua*s),        (a*tc*v)/(a*dc*b)] 

    6) >> s_a/s_b ans = [ (s*b+a*t)/(u*tv*s), (v*a+u*b)/(u*tv*s)] [ (s*dc*t)/(u*tv*s),    (u*dv*c)/(u*tv*s)] 

    7) >> s_c=s_a+i*s_b s_c = [ a+i*u, b+i*v] [ c+i*s, d+i*t] 

    8) >> s_c' ans = [ conj(a+i*u), conj(c+i*s)] [ conj(b+i*v), conj(d+i*t) 

    9) >> s_c.'

  • Key to MATLAB Exercise 5  School of Mathematical Sciences Xiamen University  http://gdjpkc.xmu.edu.cn 

    Key to Ex56 

    ans = [ a+i*u, c+i*s] [ b+i*v, d+i*t] 

    10) >> det(s_a) ans = a*dc*b 

    11) >> inv(s_a) ans = [ d/(a*dc*b), b/(a*dc*b)] [ c/(a*dc*b),    a/(a*dc*b)] 

    10. 1) >> syms x ; b=[1;x]; >> a=[1 2; 3 4]; syms y; y=inv(a)*b y = 

    2+x 3/21/2*x 

    Omitted 2) to 6). 11. 

    1) >> a=[1 2;3 4]; [v,d]=eig(a) v = 

    0.8246  0.4160 0.5658  0.9094 

    d = 0.3723  0 

    0        5.3723 2) >> a=[sym(1) sym(2); sym(3) sym(4)] a = [ 1, 2] [ 3, 4] >> [v, d]=eig(a) ans = v = [  1,                            1] [ 3/4+1/4*33^(1/2), 3/41/4*33^(1/2)] d = [ 5/2+1/2*33^(1/2),  0] [                            0, 5/21/2*33^(1/2)] 

    3) >> syms x ; a=[sym(1) sym(x);sym(3) sym(4)]

  • Key to MATLAB Exercise 5  School of Mathematical Sciences Xiamen University  http://gdjpkc.xmu.edu.cn 

    Key to Ex57 

    a = [ 1, x] [ 3, 4] >> [v, d]=eig(a) v = [ 1/2+1/6*(9+12*x)^(1/2), 1/21/6*(9+12*x)^(1/2)] [  1,                                      1] d = [ 5/2+1/2*(9+12*x)^(1/2),                                      0] [  0, 5/21/2*(9+12*x)^(1/2)] 

    4) >> syms s t u v; a=[s t;u v]; [v,d]=eig(a) v = [  (1/2*s+1/2*v1/2*(s^22*s*v+v^2+4*t*u)^(1/2))/u, (1/2*s+1/2*v+1/2*(s^22*s*v+v^2+4*t*u)^(1/2))/u] [                                                                                                  1, 1] d = [  1/2*s+1/2*v+1/2*(s^22*s*v+v^2+4*t*u)^(1/2), 0] [                                                                                      0, 1/2*s+1/2*v1/2*(s^22*s*v+v^2+4*t*u)^(1/2)] 

    12. For example 2) >> [u,t]=schur(a) ??? Function 'schur' is not defined for values of class 'sym'. Error in ==> schur at 30 [varargout{1:nargout}] = builtin('schur', varargin{:}); 

    >> [l,u]=lu(a) ??? Function 'lu' is not defined for values of class 'sym'. Error in ==> lu at 54 [varargout{1:nargout}] = builtin('lu', varargin{:}); 

    >> [u,v,t]=svd(a) u = [ .40455358483375693164244872262782, .91451429567730445267917697381021] [ .91451429567730445267917697381021,    .40455358483375693164244872262780] v = [ 5.4649857042190426504511884932842,                                                                  0] [                                                                  0, .36596619062625782042296438426142] t =[ .57604843676632079133109858194273,    .81741556047036327308865238846391] [ .81741556047036327308865238846391, .57604843676632079133109858194273]

    MATLAB Ex 1 - Matrices & Arrays.pdf

  • MATLAB Exercise 1  School of Mathematical Sciences Xiamen University  http://gdjpkc.xmu.edu 

    Ex11 

    MATLAB Exercise 1  Matrices & Arrays 1.  The distance traveled by a ball falling in the air is given by the equation 

    2 0 0 

    1 2 

    x x v t at = + + 

    Use MATLAB to calculate the position of the ball at time t=5s if  0  10 x m =  ,  0  15 / v m s =  , and 

    2 9.81 / sec a m = − 

    2.  Suppose that  3 x =  ,  4 v =  . Use MATLAB to evaluate the following expression: 

    1)  2 2 log( ) x v +  2)  2 3 

    ( 2 ) x 

    x v −

    − 3)  2 

    4 3 v π  4)  | sin 2 |  v x e 

    5)  5 x −  6) 4 x 

    v − 7) 

    4 x 

    v − − 

    8) eps  9) 3 4 

    x v

    − − 

    3.  4/2 4\2 

    4.  1) Try to input a vector d = [23, 20, 17, 14, 11, 8, 5, 2] in different ways. Use numel to count the number of elements in d. 

    2) Please input matrix 

    1 2 3 4 2 4 6 8 3 6 9 12 

    a =  

    at least in three different methods. 

    5.  1)  Compute the size of a. 1)  Show  the  value  of  a(2,3)? Obtain  a  subarray b which  is  composed  by  the  1 st  and 3 rd 

    columns of a. 2)  Obtain an new matrix c by exchanging the 2 nd and 3 rd rows of a. 3)  Obtain a vector x which is the last column of a. (end ) 4)  Replace the value of a(1,1) with 0. 5)  Input 

    a ( 10 ) a ( 10 ) = 20 a ( 10 ) 

    and observe what happen. 6)  Input 

    a ( 2, : ) a ( :, 3 ) a (:, :) a (:, 2 : 3) a (:) a (2 : 3) 

    7)  Add a new column to a to form a 3×5 matrix a35, add a new row to a as its 1 st  row to form a 4×4 matrix a44. 

    8)  Input  a ( 20 ) = 100; a  and observe what happen.

  • MATLAB Exercise 1  School of Mathematical Sciences Xiamen University  http://gdjpkc.xmu.edu 

    Ex12 

    9)  Input  d = a’;    d  and compare the value of a with d. 10)  Input  e = a ([1 3]; : ) 11)  *Input  help format  to study “format” 

    Display the value of pi in  short / long  format. Display 0.5 in  short e / rat  format 

    12)  *In help windows seach  fprintf  to study  “fprintf” Display the value of pi as an integer / exponential format / at the second new line 

    13)  Input a 3×5 matrix newmatrix, compare the results after the following commands a .* newmatrix a * newmatirx a .+ newmatrix a + newmatrix a ^ newmatrix a .^newmatrix 

    6.  Try to solve for x in the equation Ax = B where 

    1 2 1 1 2 3 2 , 1 1 0 1 0 

    A B = = −  

    . Compare the 

    command A\B and A/B, try to explain the result. 7.  Set  A  =  round  (10*rand(6)).  Let  us  change  the  6 th  column  A  so  as  to  make  the  matrix 

    singular (奇异). B = A’; A (:, 6) = sum (B (1 : 5, : ) )’ 1)  Set x = ones (6,1) and compute A*x. Explain why do we know A must be singular. 2)  Set C = round ( 10*rand (6 ) ). Check whether AC=CA, why? 

    8.  Set  A  =  magic  (8).  Compute  its  sum  of  elements  on  each  row,  column  and  diagonal respectively. 

    9.  Examine the following statements by MATLAB, and show whether they are true. 1)  If A≠0 and AC = AD, then C = D; 

    2)  If A and B are nonsingular (可逆), then  1 1 1 ( ) A B A B − − − + = + 

    3)  For any 3×3 matrices A and B,  2 2 2 ( ) 2 A B A AB B + = + + 

    4)  A+A’ is a symmetric matrix. (对称) 10.  * Some test 

    test1 = linspace (1, 11, 6) test2 = linspace (1, 11, 5) num_test1 = numel (test1) [ i ] = find (test1 > 4) test1 ( find (test1 ==3)) = 0 test = [test1; 1:2:11] test (:, 2) = [ ] Magic = magic (5) Magic_diag = diag (Magic, 0) Magic_diag = diag (Magic, 1) Magic_diag = diag (Magic, 1)

  • MATLAB Exercise 1  School of Mathematical Sciences Xiamen University  http://gdjpkc.xmu.edu 

    Ex13 

    help clc clc help clear clear test test1 clc

    MATLAB Ex 2 - Solving Linear Systems of Equations.pdf

  • MATLAB Exercise 2  School of Mathematical Sciences Xiamen University  http://gdjpkc.xmu.edu. 

    Ex21 

    MATLAB Exercise 2  Solving Linear Systems of Equations 1.  Generate random 5×5 matrices with integer entries(元素) by setting 

    A=round(10*rand(5))  and B=round(20*rand(5))10 Use MATLAB to compute each of the following pairs of numbers. In each case check whether the first is equal to the second. a) |A|    |A’|  b) |A+B|    |A|+|B|  c) |AB|    |A||B|  d) |A’B’|    |A’||B’| 

    e)  1 | | A −  1| | A 

    f)  1 | | AB −  | | | | A B 

    2.  Set A=round(10*rand(6)). In each of the following, use MATLAB to compute a second matrix as indicated. State how the second matrix is related to A and compute the determinants of both matrices. How are the determinants related? a)  B=A; B(2, :) = A(1, :); B(1, :)=A(2, :) b)  C=A; C(3, :)=4*A(3, :) c)  D=A; D(5, :)=A(5, :)+2*A(4, :) 

    3.  Use  Cramer’s  rule  (det),  backslash  (\)  operation,  inv  function,  rref  function  respectively  to solve  the  following  systems.  And  check whether  the  results  are  the  exact  solutions  of  the corresponding equations. If not, try to show the reason. 

    a)  1 2 

    1 2 

    2 4 2 2 2 x x x x

    + = − =  

    b)  1 2 

    1 2 

    2 3 1 5 4 x x x x + = −

    + =  

    c) 1 2 3 

    1 2 3 

    1 2 

    4 2 2 3 2 10 11 3 8 

    x x x x x x x x

    + − = − + = + =  

    d) 1 2 3 

    1 2 3 

    1 2 3 

    3 1 2 5 2 2 8 

    x x x x x x x x x

    + + = + + = − + − = −  

    4.  Let 

    1 2 3 2 3 4 5 4 6 

    A =  ,  compute  the  third  of  column  of  1 A −  by  using  Cramer’s  rule  to  solve 

    Ax=e3. The result should be displayed in rational format. 5.  Let A  be  the matrix  in Ex4. Compute  the  adj A  (A的伴随矩阵)  by  using  the  inv and  det 

    function. 6.  Compute  the  general  solution of  the  following  linear  systems by using backslash(\),  null 

    and rref function. 

    a) 1 2 3 

    1 2 3 

    1 2 3 

    2 3 0 4 5 8 2 4 2 

    x x x x x x x x x

    + − = + + = − − + =  

    b) 1 2 3 

    1 2 3 

    1 2 3 

    2 3 0 4 5 8 2 4 4 8 

    x x x x x x x x x

    + − = + + = + + =  

    7.  Compute the general solutions of 1 2 3 

    1 2 3 

    1 2 3 

    2 3 0 4 5 0 2 2 10 0 

    x x x x x x x x x

    + − = + + = − − =  

    by using rref function. 

    8.  *Use rand function to generate several 4×3, 3×1; 3×4, 4×1 matrices, compare the results

  • MATLAB Exercise 2  School of Mathematical Sciences Xiamen University  http://gdjpkc.xmu.edu. 

    Ex22 

    of the corresponding linear systems computed by \, inv and pinv. 9.  *Use  rand  to  generate a  linear  system Ax=b.  Try  the  display  the  result  if  r(A b)=r(A); else 

    display “There is no solution.” (if) 10.  Set A=[1 2 1; 2 4 2; 2 1 1]; B=[3 1 2; 1 2 2; 3 1 4]; C=A+i*B. Use MATLAB to compute each 

    of the following pairs of matrices. In each case check whether the first is equal to the second. If they are equal then display ‘The first matrix is equal to the second one.’ Otherwise display ‘The first matrix is not equal to the second one.’ 1) C’  C.’  2) A’  A.’  3) inv(A) pinv(A)  4) inv(B) pinv(B) 

    11.  Use help to study functions: trace, rank, conj, rref, rrefmovie, format

    MATLAB Ex 3 - Linear Space.pdf

  • MATLAB Exercise 3  School of Mathematical Sciences Xiamen University  http://gdjpkc.xmu.edu. 

    Ex31 

    MATLAB Exercise 3 – Linear Space 

    1.  Which  of  the  following  are  spanning  sets  for  3 1 R ×  ?  Justify  your  answers  by  at  least  two different methods. (related function may be used: rank, rref, det) 

    1) 

    1 0 1 0 , 1 , 0 0 1 1

     

    2) 

    1 0 1 1 0 , 1 , 0 , 2 0 1 1 3

     

    3) 

    2 3 2 1 , 2 , 2 2 2 0

    − −  

    4) 

    2 2 4 1 , 1 , 2 2 2 4

    − − − −  

    2.  Given 

    1 2 

    1 3 2 9 2 , 4 , 6 , 2 3 2 6 5 

    x x x y − −

    = = = = −  

    1) Is  1 2 Span( , ) x x x ∈  ? 

    2) Is  1 2 Span( , ) y x x ∈  ? 

    (related function: rref, rank) 3.  Which of the following are spanning sets for P3? Justify your answer. 

    1) { } 2 2 1, , 1 x x + 

    2) { } 2 2, 2, , 2 1 x x x − + 

    3) { } 2 1, 2, 3 x x x + − + 4.  Which of the following collections of vectors are linearly independent? 

    1) 

    1 0 1 1 , 1 , 0 1 1 1

     

    2) 

    2 2 1 , 2 2 0

    −  

    3) 

    2 2 4 1 , 1 , 2 2 2 4

    − − − −  

    5.  For  each  of  the  sets  of  vectors  in Exercise  4,  describe  geometrically  the  span  of  the  given vectors. (i.e. point out the maximal linearly independent subset for each of the sets of vectors.) 

    6.  Given 

    1 2 3 

    1 3 0 2 , 4 , 10 3 2 11 

    x x x −

    = = =  

    1) Show that  1 2 3 , , x x x  are linearly dependent. 

    2) Show that  1 x and  2 x  are linearly independent.

  • MATLAB Exercise 3  School of Mathematical Sciences Xiamen University  http://gdjpkc.xmu.edu. 

    Ex32 

    3) What is the dimension of Span(  1 2 3 , , x x x  )? 

    4) Give a geometric description of Span(  1 2 3 , , x x x  ). 

    7.  The vectors 

    1 2 3 4 5 

    1 3 0 2 1 2 , 4 , 10 , 7 , 3 3 2 11 3 2 

    x x x x x − −

    = = = = =  

    Span R 3 . Pare down the set  1 2 3 4 5 , , , , x x x x x  to form a basis for R 3 . 

    8.  Let  1 2 3 1 10 , , 

    2 3 7 x x x = = =

     .  Find  the  coordinates  (坐标)  of  x  with  respect  to  1 2 , x x  . 

    (i.e. A=(x1,x2), the coordinates of x with respect to  1 2 , x x  is c=A 1 x). 

    9.  Let  1 2 2 1 , 

    4 3 x x = =

     be a basis of R 2 . Find the transition matrix from the standard basis {e1, 

    e2} to the basis {x1, x2} and determine the coordinates of 10 7 

    x =  

    under the basis {x1, x2}. 

    10.  Find  the  transition  matrix  corresponding  to  the  change  of  basis  from  1 2 [ , ] x x  to  1 2 [ , ] y y  , 

    where  1 2 2 1 , 

    4 3 x x = =

     ,  1 2 

    1 3 , 

    2 4 y y

    − = =

     .  (X 1 Y) 

    11.  For each of the following matrices, find a basis for the row space(行空间), a basis for the 

    column space(列空间), and a basis for the null space (零空间={ | 0} x Ax =  ). 

    (If A is an m×n matrix, the subspace of R 1×n by the row vectors of A is called the row space of A. The subspace of R m×1 by the column vectors of A is called the column space of A.) 

    1) 

    1 2 3 2 3 6 9 4 6 

    A = −  

    2) 

    1 2 1 0 3 2 5 6 2 2 0 4 

    A −

    = −  

    3) 

    1 2 1 0 3 2 5 6 2 0 6 6 

    A −

    =  

    12.  *Generate  10×10,  100×100,  1000×1000  random  matrices,  and  corresponding  column vectors.  Compare  the  time  used  in  solving  linear  system  Ax  =  b  by  different  functions  or operators such as inv, \, lu and qr. (Refer to Lecture 3)

    MATLAB Ex 4 - Eigenvalue.pdf

  • MATLAB Exercise 4  School of Mathematical Sciences Xiamen University  http://gdjpkc.xmu.edu.cn 

    Ex41 

    MATLAB Exercise 4 – Eigenvalue 1.  Find  the  eigenvalues and  the corresponding eigenspaces  for  each of  the  following matrices. 

    And judge whether it is a defective matrix. 

    1) 3 2 3 2

    −  

    2) 6 4 3 1

    − −  

    3) 3 1 1 1

    −  

    4) 3 8 2 3

    −  

    5) 1 1 2 3

    −  

    6) 

    0 1 0 0 0 1 0 0 1

     

    7) 

    1 1 1 0 2 1 0 0 1

     

    8) 

    1 1 1 0 3 1 0 5 1

    −  

    9) 

    4 5 1 1 0 1 0 1 1

    − − −  

    2.  Judge whether the following two matrix are similar? 

    2 0 0 2 0 0 0 4 0 , 1 4 0 1 0 2 3 6 2 

    A B = = − −  

    3.  Use poly and roots function to compute the characteristic polynomial and characteristic roots of a random 4×4 matrix. According to the result show the the characteristic polynomial and characteristic roots of A in mathematics formula. 

    4.  In each of the following, factor the matrix A into a product XDX 1 , where D is diagonal. (use two different methods) 

    1) 0 1 1 0 

    A =  

    2) 5 6 2 2 

    A = − −  3) 

    2 8 1 4 

    A −

    = −  

    4) 

    1 0 0 2 1 3 1 1 1 

    A = − −  

    5) 

    2 2 1 0 1 2 0 0 1 

    A = −  

    6) 

    1 2 1 2 4 2 3 6 3 

    A −

    = − −  

    5.  For each of the following, find a matrix B such that B 2 = A. 

    1) 2 1 2 1 

    A = − −  2) 

    9 5 3 0 4 3 0 0 1 

    A −

    =  

    6.  Given 

    0 1 1 1 2 0 1 0 3 

    A −

    = −  

    ,find an orthogonal matrix U that diagonalizes A. Please display the 

    results in rational format. (schur)

  • MATLAB Exercise 4  School of Mathematical Sciences Xiamen University  http://gdjpkc.xmu.edu.cn 

    Ex42 

    7.  Let 

    1 1 2 3 1 0 

    A =  

    . Compute the singular values and the singular value decomposition of A. 

    Compare square of the singular values of A with the eigenvalues of A’A. Are they the same? 

    8.  Let 

    4 3 12 17 11 0 1 12 3 

    A −

    = − −  

    .  Compute  the  eigenvalues  of  A  by  roots  and  eig  functions 

    respectivelyl. Compute its eigenvalue decomposition, Schur decomposition and singular value decomposition respectively. Compare the results and show the differences. 

    9.  Please generate 4 symmetric matrices and check the following proposition “If the eigenvalues 

    of  a  symmetric  matrix  A  are  1 2 , ,...,  n λ λ λ  ,then  the  singular  values  of  A  are 

    1 2 | |,| |,..., | | n λ λ λ  ”. 

    10.  Please generate 10 matrices (some of them are singular 奇异, others are nonsingular 非奇异 即可逆,  others  are  diagonalized  matrices  and  others  are  not  diagonalized  matrices)  and calculate  their  eigenvalues  and  singular  values.  Then  count  the  number  of  nonzero eigenvalues and singular values. Show the conclusion you guess. 

    11.  *Compute the singular value decomposition of matrix 

    2 5 4 6 3 0 6 3 0 2 5 4 

    A

    =  

    1)  Use the singular value decomposition to find orthonormal bases for  ( ') R A  and  ( ) N A 

    2)  Use the singular value decomposition to find orthonormal bases for  ( ) R A  and  ( ) N A′

    MATLAB Ex 6 - Polynomial.pdf

  • MATLAB Exercise 6  School of Mathematical Sciences Xiamen University  http://gdjpkc.xmu.edu.cn 

    Ex61 

    MATLAB Exercise 6 – Polynomial 1.  Generate the following polynomials as row vectors 

    1)  5  3 4 f x x = + −  2)  3 f x = 

    3)  ( 1)( 3) f x x x = − +  4) 1 2 

    3 4 x 

    x −

    2.  Represent polynomial  ( 1)( 2)( 3) f x x x = − − −  in the following forms 

    1) row vector  2) sym  3) pretty  4) horner 

    3.  Evaluate the polynomial  ( 1)( 2)( 3) f x x x = − − −  at 

    1)  point 4  2) point 1 to 10 4.  Execute following codes compare the results and the difference between subs and polyvalm. 

    Why? Here eye(2) means [1 0; 0 1] 1)  >> syms x; mat = eye (2); sym_pol_a = x^2+1; subs(sym_pol_a, mat) 2)  >> clear; mat = eye (2); pol_a = [1 0 1]; polyvalm(pol_a, mat) 3)  replace the code pol_a = [1 0 1] with syms x; pol_a = x^2+1 in 2). How about the 

    result? Why? How to correct it if pol_a = x^2+1 should be used? 

    5.  Let  2 ( ) 4 f x x x = + −  ,  2 ( ) 2 1 g x x = +  . Compute 

    1)  ( ) ( ) f x g x +  2)  ( ) ( ) f x g x  3)  ( ) f g  4)  ( ) ( ) f x g x 

    6.  Let  2 ( ) f x x =  ,  5 ( ) 3 1 g x x = +  . Compute following formula by two different methods (i.e. 

    operations of matrix & operations of sym objects). 

    1)  ( ) ( ) f x g x +  2)  ( ) ( ) f x g x 

    7.  Please display the result of Ex6 1)  sym form and the terms ordered by descending powers 2)  as row vector 

    8.  Generate matrices which are 1)  a symmetric positive definite matrix 2)  a symmetric positive semidefinite matrix 

    and check the matrices by roots function and ‘ operation. 

    9.  Compute the quotient and remainder of ( ) ( ) f x g x 

    and display the results in sym form, where 

    5 4 3 2 ( ) 2 3 2 3 f x x x x x x = + − − − +  ,  4 3 2 ( ) 5 6 g x x x x = + − −  . 

    10.  *Try to compute the greatest common factor (最大公因式) of  ( ) f x  and  ( ) g x  in Ex9.

    MATLAB Ex 7 - Calculus.pdf

  • MATLAB Exercise 7  School of Mathematical Sciences Xiamen University  http://gdjpkc.xmu.edu.cn 

    Ex71 

    MATLAB Exercise 7 – Calculus 1.  Try to express y to be a compose function of x. 

    1)  2 1 1 ,  x y u u e − = + =  ; 

    2)  2 2 1 , ln ,  x y u u v v e − = + = =  ; 

    3)  2 1 , ln , sin ,  x y u u v v w w e − = + = = =  . 

    2.  Try to generate the converse function of y. 

    1)  2 1 ln sin y x = + 

    2)  ln sin y x u = +  , where u is as the independent variable 

    3)  ln sin y x u = +  , where x is as the independent variable 

    3.  Let 4 

    x π =  .  Try  to  evaluate  the  value  of  sin x (express  the  result  as  1/2*2^(1/2)  )  and 

    sin(sin ) arc x  (express the result as pi/4 ). 

    4.  Compute the following limits, and simplify them. 

    1) 0 

    tan sin lim x 

    nx mx x → − 

    2)  lim x y 

    x y 

    e e x y →

    − − 

    3) 3 

    lim 2 100 x x 

    x →+∞ + 

    4) 3 

    lim sin x x x →−∞ 

    5)  tan 2 

    lim (tan )  x x 

    x π +

    → 

    6)  lim tan 2 x x

    π − → 

    5.  1) Compute the limit 0 

    ( ) lim n n 

    x h x h →

    + − , and simplify it. 

    2) Compute  ( ) n x ′ by diff. 

    6.  Compute the following derivatives 

    1) 0 x 

    dgdx = 

    for 3 

    5 ( ) 2 7 x g x x

    − =

    + 2) 

    1 x 

    d g dxdy

    for 3 

    5 ( , ) 2 7 x y y g x y x

    − =

    3) 1, 2 x y 

    dgdy = = 

    for 3 

    5 ( , ) 2 7 x y y g x y x

    − =

    + 4)  (5) f  for  sin sin 2 sin 3 f x x x = 

    7.  Let  1 2 [ , ,..., ] n A a a a =  is  a  vector with  n  elements,  say  [1,5,8, 2,6,3] A = −  ,  how  can we 

    generate a new vector 

    1)  1 2 2 3 1 [ , ,..., ] n n B a a a a a a − = − − −  ?

  • MATLAB Exercise 7  School of Mathematical Sciences Xiamen University  http://gdjpkc.xmu.edu.cn 

    Ex72 

    2)  1 2 3 2 3 4 2 1 [ 2 , 2 ,..., 2 ] n n n C a a a a a a a a a − − = − + − + − +  . 

    8.  Calculate the following calculus 

    1) 1 1 dx 

    x + ∫  2) 1 

    1 1 dx 

    x + ∫  3)  0 1 1 

    t dx 

    x + ∫ 

    4)  2 sin 

    ( 1) y  dx 

    x y +∞

    −∞ + ∫  5)  2 sin 

    ( 1) y  dxdy 

    x y +∞ +∞

    −∞ −∞ + ∫ ∫  . 

    9.  Let  2  1 f x = +  , compare it with the results of int(diff(f)) and diff(int(f)), respectively. 

    10.  Compute the following summations 

    1)  3 1 

    k k

    = ∑  2)  2 1 1 1 k  k

    = − ∑  3)  2 2 1 1 k  k

    = − ∑  4) 2 

    1 k 

    k k x ∞

    = ∑ 11.  EvaluateTaylor series expansions of 

    1)  2 ( )  x f x e =  at point 0 to the first 15 items; 

    2)  2 ( )  x f x e =  at point 1 to the first 9 items; 

    3)  2 ( )  xy f x e =  the first 5 items of Taylor series expansion responding to x. 

    12.  *Compare the result  (cos 2 ) b 

    a x x dx + ∫  with 

    ( ) sin 2 ( ) 2 2 

    a b a b  b a + + + −  when b equals 

    to  10 a π +  ,  5 a π +  ,  a π +  ,  1/2 a π +  ,  1 64 a π +  ,  1 256 a π +  ,  respectively.  What 

    conclusion you may reach? 

    13.  *Examine integral meanvalue theorem, that is for any  ( ) [ , ] f x C a b ∈  , there is a  ( , ) a b ξ ∈  , 

    such  that  ( ) ( )( ) b 

    a f x dx f b a ξ = − ∫  .  For  example,  try  to  find  out  the  (0,1) ξ ∈  ,  such  that 

    2 2 0 

    1 1 ( 1) ( 1) 

    dx x ξ

    = + + ∫  .

    MATLAB Ex 8 - Graphics-Curve.pdf

  • MATLAB Exercise 8  School of Mathematical Sciences Xiamen University  http://gdjpkc.xmu.edu.cn 

    Ex81 

    MATLAB Exercise 8 – Graphics Curve 1.  Plot following 2D curve 

    1)  0.3  cos 2 , [0,6] x y e x x − = ∈  ;  2)  4sin , 4cos , [0, 2 ] x t y t t π = = ∈  ; 

    3)  4 4 2 2 8 10 16 0 x y x y + − − + =  ;  4)  4cos3 ρ θ =  . 

    2.  Let  , [0,10] 1 nx y x x

    = ∈ + 

    1) Set  1,5,10 n =  respectively, display the corresponding curve  1 2 3 , , c c c  at the same axes; 

    2) Use different color to distinguish the curves; 3) Legend the curves; 4) Add grid on the figure above; 5) Label the xaxis with ‘X轴’ and yaxis with ‘函数值’. 

    3.  Add title for 4) in Ex1 with ‘Plot of  4cos3 ρ θ =  ’. 

    4.  Let  4 4 2 2 2( ) x y x y b + − + =  . 

    1) Plot 4 figures  1 2 3 4 , , , c c c c with b=1.0, 0.7, 1.0, 1.05; 

    2) Partition the figure into 4 subplots, plot  1 2 3 4 , , , c c c c  into the subplots in turn. 

    5.  Create 2 figures which plot the following 3D curves, respectively. 

    1)  2 3 0.5 , 0.1 , 9cos2 , [0,6] x t y t z t t = = = ∈  ; 

    2)  0.5sin , 0.1cos , 9cos 2 , [0,9] x t y t z t t = = = ∈  . 

    6.  Plot polylines(折线) with following data on the same axes. Distinguish them by color and line shape and legend them. 

    1)  [0,1, 4,5,7], [1,3,4,5,5.6] x y = =  ;  2)  [1, 2,3, 4,5], [2, 4,1,6,3] x y = =  . 

    7.  Let 1 ( ) 

    5 4cos f x 

    x =

    + ,  1 ( ) ( ) f x f x ′ =  ,  2 ( ) ( ) f x f x ′′ =  , 

    2 2 ( ) ( ) g x f x d x = ∫ ∫  . 

    1) Plot  1 2 ( ), ( ), ( ) f x f x f x  respectively; 

    2) Observe the plots of f (x) and g(x) are the same? Break the Figure window into an 1by2 matrix of small axes, plot f (x) and g(x). 3) Let e be the error between f (x) and g(x). Display the function e and plot it. What do you find out? 

    8.  *Execute following codes several times, observe the results. What they imply? >> syms x y; a=fix(10*randn(1, 6)); f=a(1)*x^2+a(2)*y^2+a(3)*x*y+a(4)*x+a(5)*y+a(6); >> ezplot(f, [10, 10]) 

    9.  *Plot a pentagram(五角星).

    Key to MATLAB Ex 6 - Polynomial.pdf

  • Key to MATLAB Exercise 6  School of Mathematical Sciences Xiamen University  http://gdjpkc.xmu.edu.cn 

    Key to Ex61 

    Key to MATLAB Exercise 6 – Polynomial 1. 

    1) >> a=[1 0 0 0 3 4] a = 

    1          0          0          0          3  4 2) >> a2=[1 0 0 0] a2 = 

    1          0          0          0 3) >> r=[0 1 3]; poly(r) ans = 

    1          2  3          0 4) >> a=[1 2; 3 4]; poly(a) ans = 

    1          3  10 2. 

    1) >> a=[1 2 3]; poly(a) ans = 

    1  6        11  6 2) >> syms x; f=(x1)*(x2)*(x3) f = (x1)*(x2)*(x3) 3) >> pretty(f) 

    (x  1) (x  2) (x  3) 4) >> horner(f) ans = (x1)*(x2)*(x3) 

    3. 1) >> subs(f, 4) ans = 

    6 2) >> subs(f, 1:10) ans = 

    0          0          0          6        24        60      120      210      336      504

  • Key to MATLAB Exercise 6  School of Mathematical Sciences Xiamen University  http://gdjpkc.xmu.edu.cn 

    Key to Ex62 

    4. 1) >> syms x; mat = eye (2); sym_pol_a = x^2+1; subs(sym_pol_a, mat) ans = 

    2          1 1          2 

    It  generate  a  matrix,  the  (i,  j)  element  of  which  is  obtained  by  substituting  the  x  in 

    2  1 x +  with the (i, j) element of mat, that is, 2 2 2 2 11 12 2 2 2 2 21 22 

    1 1 1 1 0 1 . 

    1 1 0 1 1 1 mat mat mat mat

    + + + + =

    + + + +  2) >> clear; mat = eye (2); pol_a = [1 0 1]; polyvalm(pol_a, mat) ans = 

    2          0 0          2 

    The answer is the result of 2 1 0 1 0 

    0 1 0 1

    +  

    3) >> syms x; mat = eye (2); pol_a =x^2+1; polyvalm(pol_a, mat) ??? Inputs to polyvalm must be floats, namely single or double. Error in ==> polyvalm at 27 

    Y = diag(p(1) * ones(m,1,superiorfloat(p,X))); >> clear;syms x; mat = eye (2); pol_a =x^2+1; polyvalm(sym2poly(pol_a), mat) ans = 

    2          0 0          2 

    5. 1) >> clear; syms x; f=x^2+x4; g=2*x^2+1; f_plus_g_sym=f+g  % in sym form f_plus_g_sym = 3*x^2+x3 Or>> clear; f0=[1 1 4]; g0=[2 0 1]; f_plus_g_vector=f0+g0  % in vector form f_plus_g_vector = 

    3          1  3 Or>> clear; f0=[1 1 4]; g0=[2 0 1]; f_plus_g_sym=poly2sym(f0)+poly2sym(g0) f_plus_g_sym = 3*x^2+x3 2) >> clear; syms x; f=x^2+x4; g=2*x^2+1; f_multiply_g_sym=f*g f_multiply_g_sym = (x^2+x4)*(2*x^2+1)

  • Key to MATLAB Exercise 6  School of Mathematical Sciences Xiamen University  http://gdjpkc.xmu.edu.cn 

    Key to Ex63 

    >> poly2sym(sym2poly(f_multiply_g_sym)) ans = 2*x^47*x^2+2*x^3+x4 Or>> clear; f0=[1 1 4]; g0=[2 0 1]; f_multiply_g_vector=conv(f0, g0) f_multiply_g_vector = 

    2          2  7          1  4 >> poly2sym(f_multiply_g_vector) ans = 2*x^47*x^2+2*x^3+x4 3) >> clear; syms x; f=x^2+x4; g=2*x^2+1; subs(f, g) ans = (2*x^2+1)^2+2*x^23 4) >> clear; f0=[1 1 4]; g0=[2 0 1]; [q, r]=deconv(f0, g0) q = 

    0.5000 r = 

    0        1.0000  4.5000 6. 

    1) >> clear; syms x; f=x^2; g=3*x^5+1; f_plus_g_sym=f+g f_plus_g_sym = x^2+3*x^5+1 Or>> clear; f0=[0 0 0 1 0 0]; g0=[3 0 0 0 0 1]; f_plus_g_vector=f0+g0 f_plus_g_vector = 

    3          0          0          1          0          1 >> f_plus_g_sym =poly2sym(f_plus_g_vector) f_plus_g_sym = x^2+3*x^5+1 2) >> clear; syms x; f=x^2; g=3*x^5+1; f_multiply_g_sym=f*g f_multiply_g_sym = x^2*(3*x^5+1) >> poly2sym(sym2poly(f_multiply_g_sym)) ans = 3*x^7+x^2 Or>> clear; f0=[0 0 0 1 0 0]; g0=[3 0 0 0 0 1]; f_multiply_g_vector=conv(f0, g0) f_multiply_g_vector = 

    0          0          0          3          0          0  0          0          1          0          0 >> f_multiply_g_sym =poly2sym(f_multiply_g_vector)

  • Key to MATLAB Exercise 6  School of Mathematical Sciences Xiamen University  http://gdjpkc.xmu.edu.cn 

    Key to Ex64 

    f_multiply_g_sym = 3*x^7+x^2 

    7.  for example 6.2) 1) >> clear; syms x; f=x^2; g=3*x^5+1; f_multiply_g_sym=f*g f_multiply_g_sym = x^2*(3*x^5+1) >> poly2sym(sym2poly(f_multiply_g_sym)) ans = 3*x^7+x^2 Orclear; f0=[0 0 0 1 0 0]; g0=[3 0 0 0 0 1]; f_multiply_g_vector=conv(f0, g0) f_multiply_g_vector = 

    0          0          0          3          0          0          0          0          1          0          0 >> f_multiply_g_sym =poly2sym(f_multiply_g_vector) f_multiply_g_sym = 3*x^7+x^2 2) >> clear; syms x; f=x^2; g=3*x^5+1; f_multiply_g_sym=f*g f_multiply_g_sym = x^2*(3*x^5+1) >> sym2poly(f_multiply_g_sym) ans = 

    3          0          0          0          0          1          0          0 8. 

    1) >> A=diag(1:3); p=fix(10*rand(3)); B=p'*A*p; B = 

    100        71  171 71      135      175 171      175      329 

    >> B==B' ans = 

    1          1          1 1          1          1 1          1          1 

    >> roots(poly(B)) ans = 513.6451 49.4764 0.8785 

    2) >> A0=diag(0:2); p=fix(10*rand(3)); B0=p'*A*p; B =

  • Key to MATLAB Exercise 6  School of Mathematical Sciences Xiamen University  http://gdjpkc.xmu.edu.cn 

    Key to Ex65 

    144      152        44 152      164  50 44        50        17 

    >> B==B' ans = 

    1          1          1 1          1          1 1          1          1 

    >> roots(poly(B)) ans = 320.9116 4.0884 0.0000 

    9. >> f=[1 2 3 1 2 3]; g=[1 1 5 6 0]; >> [q,r]=deconv(f,g) q = 

    1          1 r = 

    0          0          1  10          4          3 >> poly2sym(q) ans = 1+x >> poly2sym(r) ans = x^3+10*x^2+4*x+3 

    10.  Omitted.

    Key to MATLAB Ex 7 - Calculus.pdf

  • Key to MATLAB Exercise 7  School of Mathematical Sciences Xiamen University  http://gdjpkc.xmu.edu.cn 

    Key to Ex71 

    Key to MATLAB Exercise 7 – Calculus 1. 

    1) >> clear; syms y1 u x; y1=(1+u^2)^(1/2); u_x= exp(x); >> y1= compose(y1,u_x,x) Or>> clear; syms x; u=exp(x); y1=(1+u^2)^(1/2)  % no recommend y1 = (1+exp(x)^2)^(1/2) 2) >> clear; syms y2 u v x; y2=(1+u^2)^(1/2); u =log(v); v=exp(x); >> u_x=compose(u,v,x); y2=compose(y2,u_x,x) Or>> clear; syms y2 u v x; y2=(1+u^2)^(1/2); u=log(v); v=exp(x); >> y2=compose(y2, compose(u, v, x)) Or>> clear; syms x; v=exp(x); u=log(v); y2=(1+u^2)^(1/2)  % no recommend y2 = (1+log(exp(x))^2)^(1/2) 3) >> clear; syms y u v w x; y=(1+u^2)^(1/2); u=log(v); v=sin(w); w=exp(x); >> v_x=compose(v,w,x); u_x=compose(u,v_x,x); y=compose(y,u_x,x) Or>> clear; syms y u v w x; y=(1+u^2)^(1/2); u=log(v); v=sin(w); w=exp(x); >> y=compose(y, compose(u, compose(v, w, x))) Or>> clear; syms x; w=exp(x); v=sin(w); u=log(v); y=(1+u^2)^(1/2)  % no recommend y = (1+log(sin(exp(x)))^2)^(1/2) 

    2. 1) >> clear; syms x y; y=(1+(log(sin(x)))^2)^(1/2); >> finverse(y,x) Warning: finverse((1+log(sin(x))^2)^(1/2)) is not unique. > In sym.finverse at 43 ans = asin(exp((1+x^2)^(1/2))) 2) >> clear; syms x u y; y=(x+log(sin(u)))^(1/2); >> finverse(y,u) ans = asin(exp(x+u^2)) 3)

  • Key to MATLAB Exercise 7  School of Mathematical Sciences Xiamen University  http://gdjpkc.xmu.edu.cn 

    Key to Ex72 

    >> clear; syms x u y; y=(x+log(sin(u)))^(1/2); >> finverse(y,x) ans = log(sin(u))+x^2 

    3. >> syms y1=sin(x); y2=asin(sin(x)); >> x=sym(’pi/4’); subs(y1,x) ans = 1/2*2^(1/2) >> subs(y2,x) ans = 1/4*pi Or>> syms y1=sin(x); y2=asin(sin(x)); >> compose(y1,pi/4) ans = 1/2*2^(1/2) >> compose(y2,pi/4) ans = 1/4*pi 

    4. 1) >> clear; syms n m x; y1=(tan(n*x)sin(m*x))/x; >> limit(y1,0) Or>> clear; syms n m x; y1=(tan(n*x)sin(m*x))/x; >> limit(y1) ans = nm 2) >> clear; syms x y; y2=(exp(x)exp(y))/ (xy); >> limit(y2,x,y) ans = exp(y) 3) >> clear; syms x; y3=x^3/(2*x+100); >> limit(y3,x,+inf) ans = Inf 4) >> clear; syms x; y4=x^3/sin(x); >> limit(y4,x,inf) ans = NaN

  • Key to MATLAB Exercise 7  School of Mathematical Sciences Xiamen University  http://gdjpkc.xmu.edu.cn 

    Key to Ex73 

    5) >> clear; syms x; y5=(tan(x)^(tan(x))); >> limit(y5,x,pi/4,’right’) ans = 1 6) >> clear; syms x; y=tan(x/2); >> limit(y,x,pi,'left') ans = Inf 

    5.  1) >> clear; syms x n h; y=((x+h)^nx^n)/h; >> z=limit(y,h,0) z = x^n/x*n >> simplify(z)) ans = x^(1+n)*n 2) >> clear; syms x n ; y= x^n; >> diff(y, x) ans = x^n/x*n >> simplify(diff(y,x)) ans = x^(1+n)*n 

    6. 1) >> clear; syms x g; g=(x^35)/(2*x^2+7); >> subs(diff(g,x),0) Or>> clear; syms x g; g=(x^35)/(2*x^2+7); >> compose(diff(g,x),0) ans = 0 2) >> clear; syms x y g; g=(x^3*y5*y)/(2*x^2+7); >> g_xy=diff(diff(g,x),y) g_xy = 3*x^2/(2*x^2+7)4*(x^35)/(2*x^2+7)^2*x >> subs(g_xy, 1) ans = 

    0.5309 Or

  • Key to MATLAB Exercise 7  School of Mathematical Sciences Xiamen University  http://gdjpkc.xmu.edu.cn 

    Key to Ex74 

    >> clear; syms x y g; g=(x^3*y5*y)/(2*x^2+7); g_xy=diff(diff(g,x),y) >> compose(g_xy,1) ans = 43/81 3) >> clear; syms x y g; g=(x^3*y5*y)/(2*x^2+7); >> g_y=diff(g,y) g_y = (x^35)/(2*x^2+7) >> subs(g_y, [x y], [1 2]) ans = 4/9 4) >> clear; syms x f; f=sin(x)*sin(2*x)*sin(3*x); >> diff(f,5) ans = 1696*cos(x)*sin(2*x)*sin(3*x)+2192*sin(x)*cos(2*x)*sin(3*x)+2208*sin(x)*sin(2*x)*cos(3 *x)1680*cos(x)*cos(2*x)*cos(3*x) 

    7. 1) >> A=[1,5,8,2,6,3]; B=(1)*diff(A) B = 

    4  3        10  8          3 2) >> A=[1,5,8,2,6,3]; C=diff(A,2) C = 

    1  13        18  11 8. 

    1) >> clear; syms x y; y=1/(x+1); >> int(y) ans = log(x+1) 2) >> clear; syms x y; y=1/(x+1); >> int(y,0,1) ans = log(2) 3) >> clear; syms x t y; y=1/(x+1); >> int(y,0,t) ans = log(t+1) 4)

  • Key to MATLAB Exercise 7  School of Mathematical Sciences Xiamen University  http://gdjpkc.xmu.edu.cn 

    Key to Ex75 

    >> clear; syms x y f; f=sin(y)/(x^2*y+1); >> int(f,x,inf,inf) ans = pi*sin(y)/y^(1/2) 5) >> clear; syms x y f; f=sin(y)/(x^2*y+1); >> int(int(f,x,inf,inf),inf,inf) ans = 0 

    9. >> clear; syms x f; f=x^2+1; >> f_int_diff=int(diff(f)) f _int_diff= x^2 >> f_diff_int=diff(int(f)) f_diff_int = x^2+1 diff(int(f)) is not equal to int(diff(f)), the difference between them is the constant item C. 

    10. 1) >> clear; syms k n; >> simplify(symsum(k^3,1,n)) ans = 1/4*n^4+1/2*n^3+1/4*n^2 2) >> clear; syms k; >> symsum(1/(k^21),1,inf) ans = sum(1/(k^21),k = 1 .. Inf) 3) >> clear; syms k; >> symsum(1/(k^21),2,inf) ans = 3/4 4) >> clear; syms k x f; f=k^2*x^k; >> symsum(f,k,1,inf) ans = x*(x+1)/(x1)^3 

    11. 1) >> clear; syms x f; f=exp(2*x); >> taylor(f,9) ans =

  • Key to MATLAB Exercise 7  School of Mathematical Sciences Xiamen University  http://gdjpkc.xmu.edu.cn 

    Key to Ex76 

    1+2*x+2*x^2+4/3*x^3+2/3*x^4+4/15*x^5+4/45*x^6+8/315*x^7+2/315*x^8 2) >> clear; syms x f; f=exp(2*x); >> taylor(f,9,1) ans = exp(2)+2*exp(2)*(x+1)+2*exp(2)*(x+1)^2+4/3*exp(2)*(x+1)^3+2/3*exp(2)*(x+1)^4+4/ 15*exp(2)*(x+1)^5+4/45*exp(2)*(x+1)^6+8/315*exp(2)*(x+1)^7+2/315*exp(2)*(x+1)^8 3) >> clear; syms x f; f=exp(2*x*y); >> taylor(f,x) ans = 1+2*x*y+2*y^2*x^2+4/3*y^3*x^3+2/3*y^4*x^4+4/15*y^5*x^5 

    12. >> clear; format short e; syms a x; f=cos(x)+2*x; f_int=int(f); >> b=[a+10*pi, a+5*pi, a+pi, a+1/2*pi, a+1/16*pi, a+1/1024*pi]; >> for k=1:6 y(k)=int(f,a,b(k)); y_app(k)=(subs(f_int, b(k))subs(f_int, a))*(b(k)a); end >> y, y_app y = [                                                                    20*a*pi+100*pi^2, 2*sin(a)+10*a*pi+25*pi^2,  2*sin(a)+2*a*pi+pi^2, cos(a)+a*pi+1/4*pi^2sin(a),                  sin(a+1/16*pi)+1/8*a*pi+1/256*pi^2sin(a), sin(a+1/1024*pi)+1/512*a*pi+1/1048576*pi^2sin(a)] y_app = [                                                                  10*((a+10*pi)^2a^2)*pi, 5*(2*sin(a)+(a+5*pi)^2a^2)*pi, (2*sin(a)+(a+pi)^2a^2)*pi,                                  1/2*(cos(a)+(a+1/2*pi)^2sin(a)a^2)*pi, 1/16*(sin(a+1/16*pi)+(a+1/16*pi)^2sin(a)a^2)*pi, 1/1024*(sin(a+1/1024*pi)+(a+1/1024*pi)^2sin(a)a^2)*pi] >> y_0=subs(y, 0); y_app_0=subs(y_app, 0); error_y=y_0y_app_0 error_y = 3.0019e+004 3.6290e+003 2.1137e+001 1.9792e+000    1.8777e001    3.0679e003 

    Conclusion:  While  b  closes  to  a, ( ) sin 2 ( ) 

    2 2 a b a b  b a + + + −  

    approaches 

    to  (cos 2 ) b 

    a x x dx + ∫  . 

    13. >> clear; format long e; syms x xi; f=1/(x+1)^2; >> f_int=int(f, 0, 1); xi= subs(finverse(ff_int), 0) xi = 

    4.142135623730949e001  % xi is in (0, 1)

  • Key to MATLAB Exercise 7  School of Mathematical Sciences Xiamen University  http://gdjpkc.xmu.edu.cn 

    Key to Ex77 

    >> abs(subs(f, xi)f_int) ans = 

    0  % 1 

    2 2 0 

    1 1 ( 1) ( 1) 

    dx x ξ

    = + + ∫

    Key to MATLAB Ex 8 - Graphics-Curve.pdf

  • Key to MATLAB Exercise 8  School of Mathematical Sciences Xiamen University  http://gdjpkc.xmu.edu.cn 

    Key to Ex81 

    Key to MATLAB Exercise 8 – Graphics Curve 1. 

    1) >> clear; x=linspace(0,6); y=exp(0.3.*x).*cos(2.*x); >> plot(x,y) 2) >> clear; t=linspace(0,2*pi); x=4.*sin(t); y=4.*cos(t); >> plot(x,y) 

    3) >> clear; syms x y; f=x^4+y^48*x^210*y^2+16; >> ezplot(f) 4) >> clear; theta=2*pi:0.1:2*pi; rho=4*cos(2*theta); >> polar(theta,rho) 

    2. >> clear; syms x y n; x=linspace(0,10); y=n.*x./(1+x); >> y1=subs(y,n,1); y2=subs(y,n,5); y3=subs(y,n,10);

  • Key to MATLAB Exercise 8  School of Mathematical Sciences Xiamen University  http://gdjpkc.xmu.edu.cn 

    Key to Ex82 

    >> plot(x,y1,x,y2,'r',x,y3,'g') >> legend('y1','y2','y3') >> grid on >> xlabel('X轴'); ylabel('函数值') 

    3. >> title('Plot of \rho=4cos3\theta')

  • Key to MATLAB Exercise 8  School of Mathematical Sciences Xiamen University  http://gdjpkc.xmu.edu.cn 

    Key to Ex83 

    4. 1) >> clear; syms x y b f; f=x^4+y^42*(x^2+y^2)b; >> f1=subs(f,b,0.1); f2=subs(f,b,0.7); f3=subs(f,b,1.0); f4=subs(f,b,1.05); >> ezplot(f1); >> ezplot(f2); >> ezplot(f3); >> ezplot(f4); 2) >> subplot(2,2,1); ezplot(f1) >> subplot(2,2,2); ezplot(f2) >> subplot(2,2,3); ezplot(f3) >> subplot(2,2,4); ezplot(f4) 

    5. >> figure(1); syms x t y; t=linspace(0,6); x=0.5*t.^2; y=0.1*t.^3; z=9*cos(2*t); >> plot3(x,y,z,'m*') >> figure(2); subplot; syms x t y; t=linspace(0,9); x=0.5*sin(t); y=0.1*cos(t); z=9*cos(2*t); >> plot3(x,y,z,'m*') 

    6. >> figure; x=[0,1,4,5,7]; y=[1,3,4,5, 5.6]; plot(x,y,'m*');

  • Key to MATLAB Exercise 8  School of Mathematical Sciences Xiamen University  http://gdjpkc.xmu.edu.cn 

    Key to Ex84 

    >> hold on; x1=[1,2,3,4,5];y1=[2,4,3,6,4]; plot(x1,y1,':cs'); legend('Group 1', 'Group 2'); 

    7. >> x = sym('x'); f = 1/(5+4*cos(x)); ezplot(f) 

    >> f1 = diff(f); ezplot(f1)

  • Key to MATLAB Exercise 8  School of Mathematical Sciences Xiamen University  http://gdjpkc.xmu.edu.cn 

    Key to Ex85 

    >> f2 = diff(f,2); ezplot(f2); 

    >> g = int(int(f2)); ezplot(g); 

    At  first  glance,  the plots  for  f  and  g  look  the  same. Look carefully,  however,  at  their formulas and their ranges on the yaxis. >> subplot(1,2,1); ezplot(f) >> subplot(1,2,2); ezplot(g)

  • Key to MATLAB Exercise 8  School of Mathematical Sciences Xiamen University  http://gdjpkc.xmu.edu.cn 

    Key to Ex86 

    e  is  the  difference between  f  and  g.  It has a complicated  formula, but  its  graph  looks like a constant. >> e = f  g >> subplot(1,1,1); ezplot(e) e = 1/(5+4*cos(x))+8/(tan(1/2*x)^2+9) 

    To show  that the difference really is a constant, simplify the equation. This comfirms that the difference between them really is a constant. >> e = simple(e) ezplot(e) e = 1 

    8.  They are curves of second order, such as elipse(椭圆), hyperbola (双曲线), parabola (抛物线) and so on. 

    9. >> phi=[pi/2:4*pi/5:4*pi, pi/2]; B=exp(i*phi); x1=real(B);y1=imag(B); >> plot(x1,y1,'r'); axis square; title('五角星')

    MATLAB Ex 9 - Graphics-Surface.pdf

  • MATLAB Exercise 9            School of Mathematical Sciences Xiamen University  http://gdjpkc.xmu.edu.cn 

    Ex91 

    Key to MATLAB Exercise 9 – Graphics Surface 

    1.  Plot 2 2 2 2 0.15( ) 0.15( ) 0.3 0.03 x y x y z e e − + + = −  on the region of D:  4 4 x − ≤ ≤  ,  5 5 y − ≤ ≤  , 

    by1) plot3  2) mesh  3) surf  4) meshz  5) surfc 

    2.  Plot  2 2 2 2 sin( ) / z x y x y = + +  on  the  region  of D:  9 9, 9 9 x y − ≤ ≤ − ≤ ≤  , with 

    title, x, y, and z label, grid on, displaying multiple plots per figure 1) plot3  2) mesh  3) surf 

    3.  Display  a  graph which  is  cos , sin , 2 8 ;0 , , 4 x u v y u v z u v u v π = = = + ≤ ≤  in  three 

    figures by 1) plot3  2) mesh  3) surf 

    4.  Plot the 3D curve  2 3 0.5 , 0.1 , 9cos2 , [0,6] x t y t z t t = = = ∈  . 

    5.  Plot an upper semisphere. 上半球 6.  Plot a polygon and fill it with blue color. 7.  *Plot a pentagram. 五角星 

    8.  *Displaying nonuniform data on a surface of  cos , sin , 2 8 x u v y u v z u v = = = +  .

    Key to MATLAB Ex 9 - Graphics-Surface.pdf

  • Key to MATLAB Exercise 9            School of Mathematical Sciences Xiamen University  http://gdjpkc.xmu.edu.cn 

    Key to Ex91 

    Key to MATLAB Exercise 9 – Graphics Surface 1.>> clear; clf; x=linspace(4,4,20); y=linspace(5,5,20); >> [X, Y]=meshgrid(x,y); Z=[0.3*exp(0.15*(X.^2+Y.^2)) 0.3*exp(0.15*(X.^2+Y.^2))]; 1)>> plot3(X,Y,Z); grid on; 

    2) >> mesh(X,Y,Z) 

    3) >> surf(X,Y,Z)

  • Key to MATLAB Exercise 9            School of Mathematical Sciences Xiamen University  http://gdjpkc.xmu.edu.cn 

    Key to Ex92 

    4) >> meshz(X,Y,Z) 

    5) >> surfc(X,Y,Z) 

    2. >> clear; clf; >> x=[9:9];y=[9:9]; [X,Y]=meshgrid(x,y); >> XY= (X.^2+Y.^2).^(1/2)+eps; Z=sin(XY)./XY; 1) >> subplot(1,3,1); plot3(X, Y, Z); >> title('Display by plots function'); xlabel('x'); ylabel('y'); zlabel('z'); grid on 2) >> subplot(1,3,2); mesh(X,Y,Z); >> title('Display by mesh function'); xlabel('x'); ylabel('y'); zlabel('z'); grid on 3) >> subplot(1,3,3); surf(X, Y, Z); >> title('Display by surf function'); xlabel('x'); ylabel('y'); zlabel('z'); grid on

  • Key to MATLAB Exercise 9            School of Mathematical Sciences Xiamen University  http://gd