You are on page 1of 10

The University of Manchester

Matlab Tutorial 6: Introduction to matrices

Some mathematics, first


Basic Matrix Algebra
In general the term matrix could refer to an array with any number of dimensions and this is
how it is understood in Matlab. However, in common language, the term matrix tends to
refer to a 2-dimensional array (i.e. in which the elements are arranged into two dimensions:
rows and columns) and this tutorial deals with such arrays. An m n matrix is an array of
scalars arranged in m rows and n columns. E.g.
1 5 4
3 2 2

is called a 2 3 matrix.
A matrix with an equal number of rows and columns is called a square matrix. If the
number of rows (or columns) of a square matrix is n, then the n n matrix is referred to as an
n square matrix.
Two matrices A and B can be added or subtracted if they have the same number of
rows and the same number of columns e.g. if
1 5 4
5 6 4
A=
,
B
=

5 2 2
3 2 2

6 11 8
A+B =
,
8 4 4

4 1 0
AB =

2 0 0

When a matrix is multiplied by a scalar, each element is multiplied by the scalar

1 5 4
3 15 12
e.g. if A =
, then 3 A =

3 2 2
9 6 6
Note that we can write 3A to mean 3 A . Now lets consider how to multiply a matrix
with another matrix.
If A is an m n matrix and B is an n p matrix then we define the matrix product
C = A B as an m p matrix of elements, with the element in its ith row and jth column being
calculated as follows:
1. Take the ith row of A and the jth column of B
2. Multiply the corresponding elements
MACE 10521 Modelling and Simulation 1

P Bonello/ Sep 2010

3. Find the sum of the resulting products


Some examples:

5 6
5 3
13 21
A=
, B=
, AB =

7 8
2 1
19 29

6 9 7
5 10 9
77 159 197
A=
, B = 8 6 9, A B =

54 49
6 7 7
1
3 6 8
4 8
5
7
35

A = 9 7 5, d = 8, A d = 99
8 5 2
4
104
21
14
7
56

d = 8, e = [8 3 2], d e = 64 24 16
4
32
12
8
7
e = [8 3 2], d = 8, e d = 40
4

Work out the above multiplications yourself to master matrix multiplication!


Note that:
1. A vector is a matrix consisting of only one row or one column. We usually use lower
case fonts for symbols representing vectors
2. The term matrix algebra is used to describe algebraic operations performed with
matrices
3. We can write AB to mean A B . Similarly, we can write, say,

4 5 7 44
6 8 5 9

to mean

4 5 7 44
6 8 5 9

MACE 10521 Modelling and Simulation 1

P Bonello/ Sep 2010

4. For two matrices A and B , the multiplication A B can only be performed if the
number of columns of A equals the number of rows of B .
5. For two given matrices A and B that can be multiplied i.e. A B exists, then, in
general A B B A . Indeed, it might not be possible to perform multiplication
B A e.g. consider the case

6 9 7
5 10 9
A=
, B = 8 6 9

6 7 7
3 6 8
A B exists but B A does not. Why?
An n-square matrix with ones (1s) on the main diagonal and zeros elsewhere is referred to
as an n-square identity matrix, e.g.

1 0 0
0 1 0

0 0 1
is a 3-square identity matrix. The symbol I is usually used to represent an identity matrix.
In many respects, an identity matrix is the matrix equivalent of the number 1 in scalar algebra.
For example, any matrix remains unchanged upon multiplication by an identity matrix.
Verify that

1 0 0
5 9 2
5 9 2
4 5 2 0 1 0 = 4 5 2

0 0 1

If a matrix A is n-square, then we define the inverse of A , denoted as A 1 , as the matrix,


which when multiplied by A in any order, yields an identity matrix i.e.

AA 1 = A 1 A = I
If A is a 2-square matrix:

a
A = 11
a 21

a12
a 22

then, its inverse is given by the formula

A 1 =

(a11a22

MACE 10521 Modelling and Simulation 1

a 22
1
a12 a 21 ) a 21

a12
a11

P Bonello/ Sep 2010

Verify this by multiplying A with A 1 in either order. The inverse of n-square matrices is
difficult to perform manually for n 3 and so we shall leave matrix inversion to Matlab.

Note also that the matrix inverse is only defined for square matrices i.e. the inverse of a
non-square matrix does not exist!
Solution of simultaneous linear algebraic equations
Consider the following pair of simultaneous equations

2 x1 + 5 = 3 x2
6 x1 + 3x 2 = 1
How can we use matrices to find the values of x1 and x2 ?
We first put the unknown terms on one side and the known terms on the other:
2 x1 3 x 2 = 5

(1a,b)

6 x1 + 3 x 2 = 1
You can show that the above system can be written as a matrix equation:

2 3 x1 5
6 3 x = 1

(2)

Prove this by multiplying out the matrix-vector product on the left hand side of the equation.
Equation (2) can be written as

Ax = c

(3)

2 3
x1
5
where A =
x
=
,
, c=

6 3
1
x2
If we pre-multiply both sides of this equation (3) by A 1 we get:
A 1 Ax = A 1c Ix = A 1c x = A 1c
Hence the solution is given by x = A 1c .
This means that, going back to eq. (2), to find x1 and x2 we simply invert the coefficient
matrix and multiply by the vector of known terms:
1

x1 2 3 5
x = 6 3 1

2
Using the expression for matrix inverse of a 2 2 matrix on page 3 show that

MACE 10521 Modelling and Simulation 1

P Bonello/ Sep 2010

x1 1 2
x = 4 3

2
i.e. x1 = 1 2 , x 2 = 4 3 .
Show that these values of x1 and x2 satisfy the given equations (1a,b) i.e. substitute the
values of x1 and x2 into the equations and see that the resulting terms on either side of the
equality sign balance each other for each equation.

Now, back to Matlab


1 2 3
Matrices are the variables that Matlab actually works with. Enter a matrix A = 4 5 6 as
1 2 3
follows:
>>A=[1 2 3; 4 5 6; 1 2 3]
A =
1
4
1

2
5
2

3
6
3

Note that we could use a comma instead of the space to separate elements i.e.
A=[1,2,3; 4,5,6; 1,2,3]. Also note that the rows are separated by semicolons.

Scalar-Matrix Operations
Observe what happens here
>>A+2
>>2-A
>>3*A
>>A/2

Matrix-Matrix Operations
Type
>>A=[1 2; 3 4]
A =
1
3

2
4

>>B=[3 5; 2 1]
B =
3
2

5
1

MACE 10521 Modelling and Simulation 1

P Bonello/ Sep 2010

Type the following and understand what happens:


>>A+B
>>A-B
>>A*B

So * is interpreted as matrix multiplication in Matlab. Verify that the Matlab result for
A*B is the same as that obtained by hand calculation.

1 2
Now what if we actually wanted an element-by-element multiplication i.e. from
and
3 4
3 5
3 10
2 1 we want to get 6 4 . In that case we write:

>>A.*B

Type the following and explain what happens:


>>A./B
>>A.^B

Note that the dot is always used when element-by-element operations are performed between
matrices. Also note that such operations can only be performed between matrices of the same
dimensions.
Observe and explain the difference between the operations:
>>A.^2
>>A^2

The inverse of a matrix is computed using the command inv


>>inv(A)

Verify that the result obtained agrees with that obtained using the formula on page 3.
The transpose of a matrix A is the matrix obtained by interchanging the rows and columns of
A i.e. first row becomes first column, second row becomes second column, .etc. The
transpose of A is obtained by typing:
>>A.

Addressing Elements or Sub-matrices of a Matrix


Create the following matrix:
>>A=[1 2 3 6; 4 5 6 8; 1 2 3 5; 4 5 7 8]

Find out and explain what the following lines produce


>>A(2,3)
>>A(3,1)
MACE 10521 Modelling and Simulation 1

P Bonello/ Sep 2010

>>A(1,1:3)
>>A(2:4,1)
>>A(1:3,2:4)
>>A([2 3 1],[4 2 3]);

In the last command, we have formed a matrix by taking rows 2, 3 and 1 (in that order) and
from these rows we have extracted columns 4, 2 and 3 (in that order) to give the final matrix.
In a similar fashion, understand what this line produces:
>>A([2 2 1],[2 3 3]);

There is a shorthand way of specifying a whole row or a whole column:


>>A(1,:)
>>A(:,3)

These give respectively the complete 1st row of A and the complete 3rd column of A.
Similarly, find out and explain what these lines produce:
>>A(2:3,:)
>>A([4 1],:)
>>A(:,1:3)
>>A(:,[3 2])

We can re-assign matrix elements. Typing


>>A(1,2)=3

changes just one element of the matrix A, leaving the others unchanged. We can also add
extra rows and columns:
>>A(5,:)=[5 6 7 8]
>>A(:,5)=[5 6 7 8].

Note that row vector in last command needs to be transposed to a column vector since we are
adding a column to the matrix. Alternatively, we could have written:
>>A(:,5)=[5; 6; 7; 8]

Exercises
4 2
1

1. Create a matrix B = 8 1 2 . Use the colon operator to extract from this


2 6
2
matrix the second row, the final column, and the top right hand corner sub-matrix
4 2
1 2 .

MACE 10521 Modelling and Simulation 1

P Bonello/ Sep 2010

2. Create a column time vector t of values from 0 to 2 every 0.001. Create a matrix X
comprising two columns: the first being cos(2 t ) , the second being sin (2 t ) . Plot
out the two curves with plot(t,X).
3. Use Matlab to solve the simultaneous equations on page 4. Check your result ( x) by
computing Ax c , and also by comparing with the hand calculation results.

4. Use your favourite manual method to solve the simultaneous equations

x + 2y = 8 z
2 x = 3 + y + z
2x y + 2z = 6
Now use Matlab to solve the equations and check your result.
You solved the previous problem by expressing the equations in the form Ax = c and
using the inv function to invert A and then multiplying by c. The system of
equations can be solved more efficiently by typing A\c. This use of the backslash
character is called backward division. The command A\c executes faster than
inv(A)c and can be shown to be more precise. The commands A\c and
inv(A)c use different methods to solve the given system of equations. The
command A\c solves the system of equations using an efficient solution procedure
known as Gaussian Elimination. Verify that the solution is indeed given by A\c .
The difference in the computation times of the two methods is negligible for this
small system of equations. However, the larger the system of equations (i.e. the more
equations/unknowns) the greater the difference between the computation times, since
the time taken to invert a matrix shoots up with the size of the matrix. On the other
hand, Gaussian Elimination (as used by the backward division method) does not
require the inversion of a matrix.
Backward division can also used between corresponding elements of two vectors c1,
c2 by typing c1.\c2. In this case it is simply an inverted form of the normal (i.e.
forward ) division and is not very useful. You can see this by typing

5.

[1 2 3]./[1 1 1]
ans =
1

[1 2 3].\[1 1 1]
ans =
1.0000

0.5000

0.3333

MACE 10521 Modelling and Simulation 1

P Bonello/ Sep 2010

6. Only attempt this question if you have done eigenvalue analysis in your matrix
theory in the mathematics module (most likely this will be in the second year of
your course).
Find out what the function eig does. Consider the matrix

2 5
A=

5 6

Use Matlab to find its eigenvalues and corresponding eigenvectors. Check your result
with a manual calculation.

(Answers overleaf)

MACE 10521 Modelling and Simulation 1

P Bonello/ Sep 2010

Answers
1.
>>B(2,:)
>>B(:,3)
>>B(1:2,2:3)

2.
>>t=[0:0.001:2].;
>>X=[cos(2*pi*t) sin(2*pi*t)];
>>figure;
>>plot(t,X);

3.
>>A=[2 -3; 6 3];
>>c=[-5; 1];
>>x=inv(A)*c

6.
>> A=[2 -5;-5 6];
>> [V,D]=eig(A)
V =
-0.8281
-0.5606

-0.5606
0.8281

D =
-1.3852
0

0
9.3852

MACE 10521 Modelling and Simulation 1

10

P Bonello/ Sep 2010

You might also like