Download Week 8

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

System of linear equations wikipedia , lookup

Covariance and contravariance of vectors wikipedia , lookup

Rotation matrix wikipedia , lookup

Eigenvalues and eigenvectors wikipedia , lookup

Jordan normal form wikipedia , lookup

Determinant wikipedia , lookup

Singular-value decomposition wikipedia , lookup

Matrix (mathematics) wikipedia , lookup

Non-negative matrix factorization wikipedia , lookup

Perron–Frobenius theorem wikipedia , lookup

Gaussian elimination wikipedia , lookup

Orthogonal matrix wikipedia , lookup

Four-vector wikipedia , lookup

Cayley–Hamilton theorem wikipedia , lookup

Matrix calculus wikipedia , lookup

Matrix multiplication wikipedia , lookup

Transcript
APPM 2460: Week 8
Vectors and Matrices
You have seen some of the basics of matrices and vectors in Matlab. In this worksheet, you’ll
begin to use what you have learned. You will get to create your own program to implement
Cramer’s rule to solve a matrix-vector system.
1
Entering matrices
First, let’s review how to enter matrices and vectors into Matlab. Commas or spaces separate
elements in different columns. Semicolons separate elements in different rows. So,
A = [1,2;3,4;5,6]
will enter a 3 by 2 matrix called A,
B = [0,1;-1,2]
will enter a 2 by 2 matrix called B,
x = [5;-2;1]
will enter a 3 by 1 matrix (a column vector), and
y = [5,-2]
will enter a 1 by 2 matrix (a row vector). Try them.
The dimensions are important in Matlab, because it will try to perform matrix operations by
default. For example, A + B will return an error because the matrices A and B are incompatible
sizes. Try matrix multiplications: A ∗ B, B ∗ A, y ∗ B. Scalar multiplication works as well: 3 ∗ A,
−y, etc. Try that too. A simple but useful tool is the transpose operator, 0 . A0 will give the transpose
(actually the complex conjugate transpose) of A. Now you can see the difference between A ∗ x
and A0 ∗ x.
2
Matrix manipulation
Building bigger matrices out of smaller matrices: [A;B] will build a 5 by 2 matrix by putting A
and B together, one on top of the other. If you try [A,B], Matlab will try to put them side by side
which can’t be done because their number of rows dont match; Matlab will give an error message.
An important skill in manipulating matrices is to extract elements or parts of matrices. A
single element is referenced by A(3, 1) where the ordering of the indices is according to standard
mathematical convention: row number then column number. A vector, on the other hand, has only
one index. For example, we access the second element of the column vector x with x(2). Similarly,
we access the second element of the row vector y with y(2).
1
To access whole blocks, use the : operator. Let’s try this. First enter
z=rand(5)
which will create a 5 by 5 matrix of random numbers. Now try
z(2:4,4)
This should extract the 2nd , 3rd , and 4th rows of the 4th column of z.
z(3:5,3:5)
will return the bottom right hand corner of size, 3 by 3. The : operator on its own stands for all
possible values, so z(:,3) will give the entire 3rd column of z, and z(1:2,:) will give the first
two rows of z.
3
Matrix division
Let’s say we want to solve A ∗ x = b for x. If you were in high school algebra you might try to
write
b
x= .
A
But of course this is silly for matrices - what does it mean to divide by a matrix? Instead, you
should write
x = A−1 b.
BUT - it turns out you can use either notation in Matlab! First try the inverse style:
x = inv(A)*b
Now try the division style. It’s a little different from what you would expect:
x2 = A\b
Notice that the A comes first, and that the slash is a back-slash. This basically tells Matlab “Solve
Ax = b for x.” Try comparing the answers x and x2.
One important fact: the backslash operator will sometimes work even if the inverse method
doesn’t work. For example, sometimes Ax = b is solvable even if A is not square; but if A is
not square it doesn’t have an inverse. Also, if the system Ax = b is overdetermined, the backslash
operator will return the least-squares solution; but the inverse won’t work at all. You can read more
about this if you’re curious.
4
Homework #7
Create an M-file that takes an n × n matrix A and an n × 1 vector b as input, and solves Ax = b
and outputs x. Instead of using Matlab’s inverse function or backslash operator, you must use
Cramers’ Rule from page 161 of your textbook. (Hints: This will probably require at least one for
loop! Read example 4, page 161, if you’re confused.)
If the determinant of A is zero, you should display an error message and then exit the function.
This might be implemented via a line such as
if det(A) == 0
disp(’Singular matrix OMG!’);
return
end
2