%% Question 1
(also serve as a line of comment), to avoid excessive output from commands in the file. eye to generate random numbers,
try help eye or doc eye, which usually contain many examples
about the usuage.
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.
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)?
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\).
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'.
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) .
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}
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).
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);:
p(2), is the trace -trace(A). p(6), is the determinant -det(A). polyvalm
(that is evaluate the polynomial with matrix argument. To evaluate a polynomial at a point or a vector, use polyval).