%% 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 x = A\b
(define the matrix A
and the colum vector b
first):
Ax
is exactly b
(look at the difference A*x-b
)?
hilb(n)
. Let A
be the Hilbert matrix of order x
be a random vector of length 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 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)Does it matter whether
diag(A)
a = [1 2 3]
diag(a)
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:
rand
or normal random randn
), using det
for the determinant of a matrix. (Here
"verify numerically" means that the numerical value of poly(A)
, whose output is the coefficients of the polynomial. For example, if A
is the matrix 2*eye(3)
, then
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
).