共计 6354 个字符,预计需要花费 16 分钟才能阅读完成。
Extending Justin’s Guide to MATLAB in MATH 240 – Part 4
- Method
We assume you are comfortable with (and remember or can review) commands used in the earlier
projects. New Commands
(a) Eigenvalues can be found easily. If A is a matrix then:eig(A)
will return the eigenvalues. Note that it will return complex eigenvalues too. So keep an i open
for those.
(b) If we have an eigenvalue λ for A, we can use rref on an augmented matrix [A λI | 0] to lead
us to the eigenvectors. For example if A is 4 × 4 and λ = 3 is an eigenvalue, then we can obtain
the coefficient matrix of this system by entering A – 3*eye(4).
(c) Even better: MATLAB can do everything in one go. If you recall from class, diagonalizing a
matrix A means finding a diagonal matrix D and an invertible matrix P with A = P DP ?1
.
The diagonal matrix D contains the eigenvalues along the diagonal and the matrix P contains
eigenvectors as columns, with column j of P corresponding to the eigenvalue in column j of D.
To do this we use the eig command again but demand different output. The format is:
[P,D]=eig(A)
which assigns P and D for A, if possible. If it’s not possible MATLAB returns very strange-looking
output.
(d) We can compute the dot product of two vectors using the command dot. For example:
dot([1;2;4],[-2;1;5])
(e) We can find the length of a vector from the basic definition. If v is a vector then:
sqrt(dot(v,v))
(f) Or we can just use the norm command:
norm(v)
(g) To get the transpose of a matrix A we do:
transpose(A)
or
A’
(h) To find the rank of a matrix A we do
rank(A)
(i) When A is a matrix with linearly independent columns, the command
[Q,R]=qr(A,0)
will create and exhibit the matrices Q, R which give the QR factorization of A as defined in the
text of Lay.
(j) MATLAB lets you define vectors and submatrices from matrices. For example, suppose A is an
m × n matrix and we enter the commands
F=A(1:3,2:4), G = A(1:3, :), H = A(:,2)
Then F is the 3 × 3 matrix built from entries of A in rows 1-3 and columns 2-4; G is the 3 × n
matrix built out of the first 3 rows; and H is 1 × n matrix (column vector) which equals columnof A.
(k) If you already have a coefficient matrix A and a vector b stored in MATLAB, then you can form
the augmented matrix M of the system Ax = b with the commandM = [A b]
This is useful when finding least-squares solutions. We can form the augmented matrix of the
system of normal equations AT Ax = AT b with the command
N = [A’A A’b]- Finding Commands
If you don’t know commands to achieve a MATLAB goal, you can go back to earlier projects, or use
the help function in MATLAB. You can also guess commands and experiment to see what happens.
Just remember that the output you turn in to your T.A. should be clean and devoid of any explorations
or mistakes along the way. MATLAB Help: an example
Suppose A is a 4 × 3 matrix with rank(A) = 3. As it turns out, the command[Q,R]=qr(A)
produces something different from the QR factorization defined as in Lay. What’s up?
In MATLAB, I click on help; there it seems sensible to click on the function browser; then to click
mathematics; then linear algebra; then factorization. Scrolling down, I click on qr and get the news
that I need qr(A,0). (And I see a variety of qr command variants, if I want to know more.)
MATH 240 Spring 2019 MATLAB Project 4 – due in class Tuesday, 5/7
Directions:
Previous guidelines on format and collaboration hold. Please review them if you forget them. For this
project, do all problems in format short.
As before, a question part marked with a star ? indicates the answer should be typed into your output
as a comment – the question isn’t asking for MATLAB output.- Review all directions and rules from the previous project. Then enter the command clock
(a) Execute the command [P,D] = eig(A) to diagonalize A.
(b) Use MATLAB to verify that A = P DP ?1
.
(c) Use the previous results to give the eigenvalues of A, and give an eigenvector for each eigenvalue.
(a) Use MATLAB to compute An for n = 2, 3, 4, 5, 6, 7, 8. Do you notice a pattern?
(b) Have MATLAB produce an invertible P and a diagonal D such that A = P DP ?1
. Notice that
complex numbers get involved.
(c) To understand An, it suffices to understand Dn because An = P DnP
. Describe the pattern
that emerges when we consider powers of D: D, D2
, D3
, D4
,etc.
(d) Without doing a computation in MATLAB, determine A20000001
(a) Execute the command [P,D]=eig(A). Something strange should occur in the output.
(b) Use MATLAB to try see if A = P DP ?1
.
(c) Find a basis for the eigenspace of A corresponding to the eigenvalue λ = 3.
(d) Is there a basis for R
consisting of eigenvectors for A? Does this explain why something went
wrong in part (b) (There is a relevant theorem in §5.3.)
(a) Enter A into MATLAB and use MATLAB to compute the dot product of the first column of A
with its second column. Also compute the dot product of the third column with itself. (See guide
for how to extract a column from a matrix.)
(b) Compute the matrix product AT A.
(c) What is the relationship between the entries of AT A and the dot products of the columns of A?
Make sure your answer is consistent with your computations.
(d) What in general is the relationship between the entries of AAT and dot products of vectors
associated to A?
(e) Compute AAT and do at least two dot product computations to support your previous answer.. Do a single matrix computation which shows that the
columns of form an orthonormal set.
(g) Explain carefully why your computation above shows that the columns form an orthonormal
set.
(h Explain why the rows of Q must also form an orthonormal set. - Let W be the subspace of R
(a) Enter the four vectors into MATLAB as v1, v2, v3 and v4 respectively.
(b) Let A = [v1 v2 v3 v4]. Compute the rank of this matrix. Explain briefly in a comment why
this shows that this set of four vectors is a basis for W.
(c) We shall apply the Gram-Schmidt Process to produce an orthogonal basis {w1, w2, w3, w4} for
W. To begin, let w1 = v1 and w2 = v2 – (dot(w1,v2)/dot(w1,w1))*w1.
(d) Continue the Gram-Schmidt Process and compute w3 and w4.
(e) Rescale each vector of the orthogonal basis {w1, w2, w3, w4} to produce an orthonormal basis
{u1, u2, u3, u4} for W. (Recall there is a command in MATLAB to compute the norm of a
vector.)
(f) Enter the vectors u1, u2, u3, u4 into the columns of a matrix Q. Verify that the columns of Q are
orthonormal with a single matrix multiplication.
(g) Compute R = QT A. Verify that R is upper triangular with positive diagonal entries and that
A = QR.
(h) MATLAB can compute a QR factorization in a single command. Enter [Q1, R1] = qr(A,0).
Notice that there is a small discrepancy between your Q, R and the Q1, R1 produced by MATLAB.
This is because QR factorizations are not unique. They are only unique if we insist that the
diagonal entries of R are all postive. Nonetheless, the columns of Q1 still form an orthonormal
basis for W. - Let W be the subspace of R
- given by
W = Span
(a) Enter those five vectors as the columns of a matrix A and compute its rank.
(b) The previous computation shows that the five vectors are linearly dependent, hence they do not
form a basis for W. Find a basis for W. (Hint: Treat W as Col A)
(c) Enter your basis for W as the columns of a matrix B. Compute the factorization B = QR with
a single command. Give an orthonormal basis for W.
(d) Let E = QQT
. As a linear transformation on R
6
, E is the orthogonal projection onto the subspace
W. Use E to compute the orthogonal projection of the vector v = (1, 1, 1, 1, 1, 1)T onto W.
(e) Find a basis for W⊥ as follows. Note that W = Col A = Col B. From a theorem in class, we have
W⊥ = (Col B)
so it suffices to find a basis for Nul(BT).
(f) As you did for W, find an orthonormal basis for W⊥.
(g) Compute the matrix F for the orthogonal projection onto W⊥. What is the sum E + F?