Introduction to MATLAB Exercise 1



Setup MATLAB Directory



Exercises in MATLAB

Only a small portion of commands or built-in funcitons will be covered in this short course (there are hundreds of thousands of them!), and you need to learn to to read the help file. For example you do not know how to use eye to generate random numbers, try help eye or doc eye, which usually contain many examples about the usuage.
  1. Create the following 10-by-10 matrix, with 8-by-8 submatrix (all ones) and zeros around it: A=[000000011110011110011110011110000000].
  2. Create a 3-by-3 rand matrix using rand and find its eigenvalues and the associated eigenvectors using eig. Verify directly the definition Ax=λx by extracting the eigenvalue and eigenvector from your output.
  3. Solve the following system using x = A\b (define the matrix A and the colum vector b first): 3x+4y+5z=22x3y+7z=1x6y+z=3. Does Ax is exactly b (look at the difference A*x-b)?
  4. The Hilbert matrix of order n is defined as A=[112131n1213141n+11n1n+11n+212n1], that is the entry aij=1/(i+j1). You can generate this special matrix using hilb(n). Let A be the Hilbert matrix of order 11 and x be a random vector of length 11. Define b=A*x, and xx=A\b. What do you expect the size of x-xx? Try the same with a random matrix A of size 11.
  5. Calculate A1000 and A10, where A=[3/21/21/21/2]. Check whether you get the exact answer A1000=[501500500499],A10=[4556], by taking the difference between the numerical output and the above matrices.
  6. You can get the diagonal element of a matrix A by using diag(A). For a vector a, you can also create a matrix with diagonal element a using diag(a). Experiment the following commands:
        A = magic(3)
      diag(A)
      a = [1 2 3]
      diag(a)
    Does it matter whether a is a column or row matrix? Try to apply diag to the transpose of a, which is simply a'.
  7. You can retrieve the lower and upper triangular part of a matrix using tril and triu. Create a 5-by-5 random matrix A and check the output of tril(A,2) , tril(A,-1) , triu(A,3) and triu(A,-2) .
  8. Based on the experiments from the last question, use tril and triu to retrieve the tri-diagonal part (the diagonal, with two sub-diagonal right above and below the main diagonal) of a matrix: [a1,1a1,2a1,na2,1a2,2a2,nan,1an,2an,n][a1,1a1,200a2,1a2,2a2,300a3,2a3,30000an,n]. Try your command to find the 10-by-10 tridiagonal matrix with ones [110000111000011000000110000111000011].
  9. Verify numerically that the determinant of two matrices is the product of these two matrices (that is det(AB)=det(A)det(B)) for two random 5-by-5 matrices (either uniform random rand or normal random randn), using det for the determinant of a matrix. (Here "verify numerically" means that the numerical value of det(AB)det(A)det(B) should be small, of order 1014 or smaller).
  10. The characteristic polynomial of a n-by-n matrix is defined as P(λ)=det(λIA). You can get the polynomial using poly(A), whose output is the coefficients of the polynomial. For example, if A is the matrix 2*eye(3), then P(λ)=det(λ2000λ2000λ2)=λ36λ2+12λ8. The output from poly(2*eye(3)) is the vector [1 -6 12 -8]. Verify numerically the following statements for a 5-by-5 random matrix, starting with A = rand(5); p = poly(A);: