



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




As suggested by its name, MATLAB (MATrix LABoratory) was originally developed as a set of numerical algorithms for manipulating matrices.
First, we have already learned how to define 1 × n matrices, which are simply row vectors. For example, we can define the 1 × 5 matrix
~v = [ 0. 2. 4. 6 .8 ]
with
v=[0 .2 .4 .6 .8] v = 0 0.2000 0.4000 0.6000 0. v(1) ans = 0 v(4) ans =
We also see in this example that we can refer to the entries in v with v(1), v(2), etc. In defining a more general matrix, we proceed similarly by defining a series of row vectors, ending each with a semicolon.
Example 1. Define the matrix
A =
in MATLAB. We accomplish this the following MATLAB code, in which we also refer to the entries a 21 and a 12.
A=[1 2 3;4 5 6;7 8 9] A = 1 2 3 4 5 6 7 8 9 A(1,2) ans = 2 A(2,1) ans = 4
In general, we refer to the entry aij with A(i, j). We can refer to an entire column or an entire row by replacing the index for the row or column with a colon. In the following code, we refer to the first column of A and to the third row.
A(:,1) ans = 1 4 7 A(3,:) ans = 7 8 9
△
MATLAB adds and multiplies matrices according to the standard rules.
Example 2. For the matrices
(^) and B =
compute A + B, AB, and BA. Assuming A has already been defined in Example 1, we use
B=[0 1 3;2 4 1;6 1 8] B = 0 1 3 2 4 1 6 1 8 A+B ans =
1 3 6 6 9 7 13 9 17
AB ans = 22 12 29 46 30 65 70 48 101 BA ans = 25 29 33 25 32 39 66 81 96
In addition to returning the eigenvalues of a matrix, the command eig will also return the associated eigenvectors. For a given square matrix A, the MATLAB command
[P, D] = eig(A)
will return a diagonal matrix D with the eigenvalues of A on the diagonal and a matrix P whose columns are made up of the eigenvectors of A. More precisely, the first column of P will be the eigenvector associated with d 11 , the second column of P will be the eigenvector associated with d 22 etc.
Example 4. Compute the eigenvalues and eigenvectors for the matrix B from Example 3. We use
[P,D]=eig(B) P = -0.8480 0.2959 -0. A(2,1) ans = 4 In general, we refer to the entry aij with A(i, j). We can refer to an entire column or an entire row by replacing the index for the row or column with a colon. In the following code, we refer to the first column of A and to the third row. >>A(:,1) ans = 1 4 7 >>A(3,:) ans = 7 8 9 △ ## Matrix Operations and Arithmetic MATLAB adds and multiplies matrices according to the standard rules. Example 2. For the matrices ### A = (^) and B = compute A + B, AB, and BA. Assuming A has already been defined in Example 1, we use >>B=[0 1 3;2 4 1;6 1 8] B = 0 1 3 2 4 1 6 1 8 >>A+B ans = 1 3 6 6 9 7 13 9 17 >>AB ans = 22 12 29 46 30 65 70 48 101 >>BA ans = 25 29 33 25 32 39 66 81 96 In addition to returning the eigenvalues of a matrix, the command eig will also return the associated eigenvectors. For a given square matrix A, the MATLAB command [P, D] = eig(A) will return a diagonal matrix D with the eigenvalues of A on the diagonal and a matrix P whose columns are made up of the eigenvectors of A. More precisely, the first column of P will be the eigenvector associated with d 11 , the second column of P will be the eigenvector associated with d 22 etc. Example 4. Compute the eigenvalues and eigenvectors for the matrix B from Example 3. We use >>[P,D]=eig(B) P = -0.8480 0.2959 -0. 0.2020 0.2448 -0. 0.4900 0.9233 0. D = -1.9716 0 0 0 10.1881 0 0 0 3.
We see that the eigenvalue–eigenvector pairs of B are
Let’s check that these are correct for the first one. We should have B~v 1 = − 1. 9716 ~v 1. We can check this in MATLAB with the calculations
B*P(:,1) ans =
-0. -0.
D(1,1)*P(:,1) ans =
-0. -0.
△
Example 5. Solve the linear system of equations
x 1 + 2x 2 + 7x 3 = 1 2 x 1 − x 2 = 3 9 x 1 + 5x 2 + 4x 3 = 0.
We begin by defining the coefficients on the left hand side as a matrix and the right hand side as a vector. If
(^) , ~x =
x 1 x 2 x 3
(^) and ~b =
then in matrix form this equation is A~x = ~b,
which is solved by ~x = A−^1 ~b.
In order to compute this in MATLAB, we use the following code.
A=[1 2 7;2 -1 0;9 5 4] A = 1 2 7 2 -1 0 9 5 4 b=[1 3 0]’ b = 1 3 0 x=Aˆ(-1)*b x =
-1.
x=A\b x =
-1.
Notice that MATLAB understands multiplication on the left by A−^1 , and interprets division on the left by A the same way. △ If the system of equations we want to solve has no solution or an infinite number of solutions, then A will not be invertible and we won’t be able to proceed as in Example 5. In
In this case, we see immediately that the third equation asserts 0 = 1, which means there is no solution to this system. △
A =
compute the determinant, transpose, 3rd power, inverse, eigenvalues and eigenvectors.
Explain how the result of this calculation is related to the eigenvalues of A.
x 1 + 2x 2 − x 3 + 7x 4 = 2 2 x 1 + x 2 + 9x 3 + 4x 4 = 1 3 x 1 + 11x 2 − x 3 + 7x 4 = 0 4 x 1 − 6 x 2 − x 3 + 1x 4 = − 1.
− 3 x 1 − 2 x 2 + 4x 3 = 0 14 x 1 + 8x 2 − 18 x 3 = 0 4 x 1 + 2x 2 − 5 x 3 = 0.
x 1 + 6x 2 + 4x 3 = 1 2 x 1 + 4x 2 − 1 x 3 = 0 −x 1 + 2x 2 + 5x 3 = 0.