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: \begin{equation} A = \begin{bmatrix} 0 & 0 & 0 & \cdots & 0 & 0 & 0 \cr 0 & 1 & 1 & \cdots & 1 & 1 & 0 \cr 0 & 1 & 1 & \cdots & 1 & 1 & 0 \cr \vdots & \vdots & \vdots & \ddots & \vdots & \vdots & \vdots \cr 0 & 1 & 1 & \cdots & 1 & 1 & 0 \cr 0 & 1 & 1 & \cdots & 1 & 1 & 0 \cr 0 & 0 & 0 & \cdots & 0 & 0 & 0 \end{bmatrix}. \end{equation}
  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=\lambda 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): \begin{align} 3x+4y+5z &=2 \cr 2x-3y+7z &=-1\cr x-6y+z &=3. \end{align} Does Ax is exactly b (look at the difference A*x-b)?
  4. The Hilbert matrix of order \(n\) is defined as \begin{equation} A = \begin{bmatrix} 1 & \frac{1}{2} & \frac{1}{3} & \cdots & \frac{1}{n} \cr \frac{1}{2} & \frac{1}{3} & \frac{1}{4} & \cdots & \frac{1}{n+1} \cr \vdots & \vdots & \vdots & \ddots & \vdots \cr \frac{1}{n} & \frac{1}{n+1} & \frac{1}{n+2} & \cdots & \frac{1}{2n-1} \end{bmatrix}, \end{equation} that is the entry \(a_{ij}=1/(i+j-1)\). 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 \(A^{1000}\) and \(A^{-10}\), where \begin{equation} A = \begin{bmatrix} 3/2 & 1/2 \cr -1/2 & 1/2 \end{bmatrix}. \end{equation} Check whether you get the exact answer \begin{equation} A^{1000} = \begin{bmatrix} 501 & 500 \cr -500 & -499 \end{bmatrix},\qquad A^{-10} = \begin{bmatrix} -4 & -5 \cr 5 & 6 \end{bmatrix}, \end{equation} 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: \begin{equation} \begin{bmatrix} a_{1,1} & a_{1,2} & \cdots & a_{1,n} \cr a_{2,1} & a_{2,2} & \cdots & a_{2,n} \cr \vdots & \vdots & \ddots & \vdots \cr a_{n,1} & a_{n,2} & \cdots & a_{n,n} \end{bmatrix} \qquad \to \qquad \begin{bmatrix} a_{1,1} & a_{1,2} & 0 & \cdots & 0 \cr a_{2,1} & a_{2,2} & a_{2,3} & \cdots & 0 \cr 0 & a_{3,2} & a_{3,3} & \cdots & 0 \cr \vdots & \vdots & \vdots & \ddots & \vdots \cr 0 & 0 & 0 & \cdots & a_{n,n} \end{bmatrix}. \end{equation} Try your command to find the 10-by-10 tridiagonal matrix with ones \begin{equation} \begin{bmatrix} 1 & 1 & 0 & \cdots & 0 & 0 & 0\cr 1 & 1 & 1 & \cdots & 0 & 0 & 0\cr 0 & 1 & 1 & \cdots & 0 & 0 & 0\cr \vdots & \vdots & \vdots & \ddots & \vdots & \vdots & \vdots\cr 0 & 0 & 0 & \cdots & 1 & 1 & 0 \cr 0 & 0 & 0 & \cdots & 1 & 1 & 1 \cr 0 & 0 & 0 & \cdots & 0 & 1 & 1 \end{bmatrix}. \end{equation}
  9. Verify numerically that the determinant of two matrices is the product of these two matrices (that is \(\mbox{det}(AB)=\mbox{det}(A)\mbox{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 \(\mbox{det}(AB)-\mbox{det}(A)\mbox{det}(B)\) should be small, of order \(10^{-14}\) or smaller).
  10. The characteristic polynomial of a n-by-n matrix is defined as \( P(\lambda)=\mbox{det}(\lambda I-A)\). 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 \begin{equation} P(\lambda) = \mbox{det}\begin{pmatrix} \lambda-2 & 0 & 0 \cr 0 & \lambda-2 & 0 \cr 0 & 0 & \lambda-2\end{pmatrix} =\lambda^3-6\lambda^2+12\lambda-8. \end{equation} 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);: