Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Lecture 8 Matrices and Matrix Operations in Matlab, Study notes of Printing

Here both A and B are 2 × 2 matrices. Matrices can be multiplied together in this way provided that the number of columns of A match the number of rows of B. ...

Typology: Study notes

2021/2022

Uploaded on 09/12/2022

ammla
ammla 🇺🇸

4.5

(37)

275 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Lecture 8
Matrices and Matrix Operations in Matlab
You should review the vector operations in Lecture 1.
Matrix operations
Recall how to multiply a matrix Atimes a vector v:
Av=1 2
3 4 1
2=1·(1) + 2 ·2
3·(1) + 4 ·2=3
5.
This is a special case of matrix multiplication. To multiply two matrices, Aand Byou proceed as follows:
AB =1 2
3 4 12
2 1 =1+4 2+2
3+8 6+4 =3 0
52.
Here both Aand Bare 2 ×2 matrices. Matrices can be multiplied together in this way provided that the
number of columns of Amatch the number of rows of B. We always list the size of a matrix by rows, then
columns, so a 3 ×5 matrix would have 3 rows and 5 columns. So, if Ais m×nand Bis p×q, then we can
multiply AB if and only if n=p. A column vector can be thought of as a p×1 matrix and a row vector as
a 1 ×qmatrix. Unless otherwise specified we will assume a vector vto be a column vector and so Avmakes
sense as long as the number of columns of Amatches the number of entries in v.
Printing matrices on the screen takes up a lot of space, so you may want to use
format compact
Enter a matrix into Matlab either as
A = [ 1 3 -2 5 ; -1 - 1 5 4 ; 0 1 -9 0]
or
A = [1 , 3 , - 2 ,5 ; - 1 , -1 , 5 ,4 ; 0 ,1 , - 9 ,0 ]
Also enter a vector u:
u = [ 1 2 3 4]
To multiply a matrix times a vector Auuse *:
A*u
34
pf3
pf4
pf5

Partial preview of the text

Download Lecture 8 Matrices and Matrix Operations in Matlab and more Study notes Printing in PDF only on Docsity!

Lecture 8

Matrices and Matrix Operations in Matlab

You should review the vector operations in Lecture 1.

Matrix operations

Recall how to multiply a matrix A times a vector v:

Av =

This is a special case of matrix multiplication. To multiply two matrices, A and B you proceed as follows:

AB =

Here both A and B are 2 × 2 matrices. Matrices can be multiplied together in this way provided that the number of columns of A match the number of rows of B. We always list the size of a matrix by rows, then columns, so a 3 × 5 matrix would have 3 rows and 5 columns. So, if A is m × n and B is p × q, then we can multiply AB if and only if n = p. A column vector can be thought of as a p × 1 matrix and a row vector as a 1 × q matrix. Unless otherwise specified we will assume a vector v to be a column vector and so Av makes sense as long as the number of columns of A matches the number of entries in v.

Printing matrices on the screen takes up a lot of space, so you may want to use

 format compact

Enter a matrix into Matlab either as

 A = [ 1 3 -2 5 ; -1 -1 5 4 ; 0 1 -9 0]

or

 A = [1 ,3 , -2 ,5; -1 , -1 ,5 ,4; 0 ,1 , -9 ,0]

Also enter a vector u:

 u = [ 1 2 3 4] ’

To multiply a matrix times a vector Au use *:

 A * u

Introduction to Numerical Methods... by Young and Mohlenkamp © 2021 35

Since A is 3 by 4 and u is 4 by 1 this multiplication is valid and the result is a 3 by 1 vector.

Now enter another matrix B using

 B = [3 2 1; 7 6 5; 4 3 2]

You can multiply B times A with

 B * A

but A times B is not defined and

 A * B

will result in an error message.

You can multiply a matrix by a scalar:

 2* A

Adding matrices A + A will give the same result:

 A + A

You can even add a number to a matrix:

 A + 3 % add 3 to every entry of A

Component-wise operations

Just as for vectors, adding a ‘.’ before ‘*’, ‘/’, or ‘^’ produces entry-wise multiplication, division and exponentiation. If you enter

 B * B

the result will be BB, i.e. matrix multiplication of B times itself. But, if you enter

 B .* B

the entries of the resulting matrix will contain the squares of the same entries of B. Similarly if you want B multiplied by itself 3 times then enter

 B ^

but, if you want to cube all the entries of B then enter

 B .^

Note that BB and B^3 only make sense if B is square, but B.B and B.^3 make sense for any size matrix.

Introduction to Numerical Methods... by Young and Mohlenkamp © 2021 37

 norm ( A )

For instance the norm of an identity matrix is 1:

 norm ( eye (100))

and the norm of a zero matrix is 0:

 norm ( zeros (50 ,50))

For a matrix the norm defined above and calculated by Matlab is not the square root of the sum of the square of its entries. That quantity is called the Froebenius norm, which is also sometimes useful, but we will not need it.

Some other useful commands

C = rand(5,5)........................................ random matrix with uniform distribution in [0, 1]. size(C).............................................................. gives the dimensions (m × n) of C. det(C).................................................................... the determinant of the matrix. max(C).................................................................... the maximum of each column. min(C)..................................................................... the minimum in each column. sum(C)................................................................................ sums each column. mean(C)..................................................................... .the average of each column. diag(C)...................................................................... just the diagonal elements. C’................................................................................... tranpose the matrix.

In addition to ones, eye, zeros, rand and randn, Matlab has several other commands that automatically produce special matrices: hilb(6) pascal(5)

38 LECTURE 8. MATRICES AND MATRIX OPERATIONS IN MATLAB

Exercises

8.1 Enter the matrix A by  A = [3 2 1; 6 5 4; 9 8 7]

and also the matrix

B =

Find (a) AA, (b) A^2, (c) A.^2, (d) A.B, (e) A*B. Turn in the output. 8.2 By hand, calculate Av, AB, and BA for:

A =

 , B =

 (^) , v =

Check the results using Matlab. Think about how fast computers are. Turn in your hand work. 8.3 Write a well-commented Matlab function program myinvcheck that

ˆ makes a n × n random matrix (normally distributed, A = randn(n,n)), ˆ calculates its inverse (B = inv(A)), ˆ multiplies the two back together, ˆ calculates the residual (difference between AB and eye(n)), and ˆ returns the scalar residual (norm of the difference).

Turn in your program. 8.4 Write a well-commented Matlab script program myinvcheckplot that calls myinvcheck for n = 10 , 20 , 40 ,... , 2 i10 for some moderate i, records the results of each trial, and plots the scalar residual versus n using a log plot. (See help loglog.) What happens to the scalar residual as n gets big? Turn in the program, the plot, and a very brief report on the results of your experiments. (Do not print any large random matrices.)