You are on page 1of 42

MASSEY UNIVERSITY

Institute of Fundamental Sciences


Mathematics

160.102
Linear Mathematics

Matlab Guide
2
Contents

1 Introduction to Matlab. 1
1.1 Basic Matlab commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

2 Matlab for vectors. 5


2.1 Vector algebra . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.1.1 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.2 Plotting and editing graphs . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.2.1 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.3 Operations on components of vectors . . . . . . . . . . . . . . . . . . . . . 9
2.4 Dot product . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.5 Special commands for vector products . . . . . . . . . . . . . . . . . . . . 10
2.5.1 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
2.5.2 Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12

3 Matlab for solving systems of equations. 15


3.1 The rref command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
3.1.1 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
3.1.2 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
3.1.3 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
3.2 Numerical considerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
3.2.1 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
3.3 Array addresses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
3.3.1 Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21

4 Span and linear independence. 23


4.1 Linear independence - method 1 . . . . . . . . . . . . . . . . . . . . . . . . 23
4.1.1 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
4.1.2 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
4.2 Linear independence - method 2 . . . . . . . . . . . . . . . . . . . . . . . . 25
4.2.1 Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25

5 Matlab for Matrices 27


5.1 Matrix multiplication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
5.2 Systems of linear equations revisited . . . . . . . . . . . . . . . . . . . . . 28

i
ii CONTENTS

5.2.1 Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
5.2.2 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
5.3 Transpose and Inverse Matrix . . . . . . . . . . . . . . . . . . . . . . . . . 30
5.3.1 Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30

6 Complex numbers. 31
6.1 Simple complex number commands . . . . . . . . . . . . . . . . . . . . . . 31
6.2 Plotting complex numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
6.3 Finding all nth roots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
6.3.1 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32

7 Eigenvalues and Eigenvectors 33


7.0.1 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33

8 Diagonalisation and orthogonalisation. 35


8.1 Diagonalisation in Matlab . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
8.2 Gram-Schmidt method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
8.2.1 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35

9 Useful Matlab Commands 37


Chapter 1

Introduction to Matlab.

This workbook has been written with reference to Matlab version R2014a. If you have
another version of Matlab installed on your machine the commands may differ slightly
from what is listed in this workbook.

The Command Window and Workspace: Upon launching Matlab on your machine
you should be greeted with the screen shown in Figure 1.1. For now we will ignore the
buttons and tabs, and will focus on the Command Window and Workspace. The largest
window in the middle of the screen is called the Command Window. Matlab provides a
prompt “>>” at the beginning of the line to indicate it is ready to receive a command.
This is where you type your commands for Matlab to interpret, for example, “1 + 1”,
followed by [Enter] will evaluate and display the solution, 2.

Figure 1.1: The Command Window appears in the center of the screen once Matlab is
opened.

The Workspace on the right of the screen is where Matlab stores any variables you will

1
2 CHAPTER 1. INTRODUCTION TO MATLAB.

define. We will discuss this in further detail later in this manual.

When completing Matlab questions for your assignment you may type your com-
mands into the Command Window followed by [Enter], then highlight the commands you
typed and the result Matlab displays, then simply copy and paste to a text document.

1.1 Basic Matlab commands


Basic Matlab commands include the following:

+ add
− subtract
∗ matrix multiplication
/ divide
∧ raise to a power

As a simple example of using the Command Window interface, type the commands which
are in bold type below. The Matlab response is in light type. Try typing the commands
and press Enter to check that you get the same response.

>> 2 ∧ 4 - 6 * 4 / 8
ans =
13

If we don’t specify a name for the calculation Matlab will call it ans (short for answer).
We can then use this value stored in ans, in this case 13, for further calculations. To
square the answer type

>> ans * ans (or use ans ∧ 2)


ans =
169

We now have a new value for ans. It is better to give a name to results we want to keep.
For example,
>> x = 5 + (3/8)
gives the response x = 5.3750. If you look in the Workspace you will now see a new
symbol x with its value. We can use this result in further calculations.

> x∧2
ans =
28.8906
1.1. BASIC MATLAB COMMANDS 3

Note that if you type any command followed by a semicolon (;) Matlab will compute
the result and store it in the Workspace, but will not display the value on the screen. Try
typing the following on the command line

> y = x ∧ 2;

The answer, y = 28.8906 will not be shown in the command window, but the value of
y will be stored as can be seen in the Workspace.

Getting Help: For help on the meaning of a command type help followed by the
command. For example,

>> help ezplot

will tell you about the command which gives an “easy plot” of a graph. If you don’t know
the name of the command then type

>> helpwin

or select Help Window from the Help menu. Then double click on the appropriate section
to get a list of commands in that category.
A table of common commands you may be interested in is given at the end of this booklet.
4 CHAPTER 1. INTRODUCTION TO MATLAB.
Chapter 2

Matlab for vectors.

In this workbook we write vectors as columns. There are two ways of obtaining column
vectors in Matlab:

Method 1. Enter a list of numbers in square brackets with a semicolon between each
entry.
Method 2. Enter a list of numbers in square brackets followed by an apostrophe. (Re-
member to put a space between the entries.)
Try
>> u= [1; 2; 3]; v= [5 6 7]0

2.1 Vector algebra


Provided the vectors are the right size Matlab will do vector addition and subtraction
and multiply a vector by a number using the obvious commands.
u+v for vector addition
u−v for subtraction of vectors
k∗u to multiply the vector u by the scalar (number) k.

2.1.1 Example
Try out the following commands.

>> u= [1 2 3]0 ;
>> v= [4 5 6]0 ;
>> w=u+v
>> z=u−v
>> −1/3∗z

5
6 CHAPTER 2. MATLAB FOR VECTORS.

Notice that Matlab will also do arithmetic on vectors entered as rows, and if we want the
answer presented as a column we can add the apostrophe at the end.

Try the following commands:

>> u= [4 − 2 5];
>> v= [2 3 − 7];
>> w= u + 3∗v
w=
10 7 − 16
>> w’
ans =
10
7
−16

If we want to pick out a particular component of a vector, v, the command v(n) will do
this where n specifies the component.
Try: >> v(2), >>u(3) and >>w(3).

2.2 Plotting and editing graphs


To plot the points (1, 2), (3, 4), and (5, −1) and join them by lines, we first make a
vector, X, consisting of the x-coordinates and a vector, Y, consisting of the y-coordinates.
We then use the command plot(X,Y) to plot the set of points whose x-coordinates are
in X and y-coordinates are in Y.

Try the following:

>> X= [1 3 5];
>> Y= [2 4 − 1];
>> plot(X,Y)

Also try

>> plot(X,Y,’- -’)


>> plot(X,Y,’r*’)

How did the plot change in each of the above examples? Use the command

>> help plot


2.2. PLOTTING AND EDITING GRAPHS 7

for more information on other line types and colours you can use in plotting.

Did you notice that as soon as a plot command is executed any previous plot disappears?
If you want to plot several lines or curves on the same axes you will need to type hold
on after the first plot command has been executed. Then any subsequent plot command
will add curves to the existing plot/ figure. The hold on command will remain active
until it is turned off. In order to start a new graph, type hold off .
Matlab will also plot position vectors of points in the plane. Use the command com-
pass(X,Y) with X and Y as above.

Editing graphs: Matlab has editing features on the Figure window. It is possible to
change the width or style of a line, add text and to draw arrows or lines by hand on the
graph. All these features can be obtained from the tool bar above the figure plot.

To change the style or colour of a line

Click on Edit plot which is the left pointing arrow on the tool bar.

Double-click on the line to bring up the property editor.

Adjust the parameters using the menu on the screen.

To insert text or give a title to the graph

Click on Edit plot which is the left pointing arrow on the tool bar.

Double-click on the graph area to bring up the property editor.

Type the title into the appropriate text box. On this menu you may also change
the x or y axis label and font.

To insert a text box, under the “Insert” menu choose “TextBox”.

Click on the graph in the position where you would like the text box to appear then
type your text.

You may drag and drop the text box to change its position, or double click on the
box to adjust text font or line style.

To add an arrow or line

Under the “Insert” menu choose “Arrow”.

Click on the graph at the position you would like the arrow to appear.

Drag the arrow (or line) into the correct position (point to the middle of it to move
whole line).
8 CHAPTER 2. MATLAB FOR VECTORS.

Click outside the plot to complete editing.

Saving graphs: There are several ways of saving a graph, but the easiest method is to go
to the Edit menu on the graph screen and click Copy Figure and paste it into MSWord.

Summary of commands

plot(X,Y) plots sequence of points (x,y) and joins them by line segments
x,y are corresponding elements from vectors X and Y
hold on retains present graph
hold off releases the graph(s)
compass(X,Y) plots position vectors of points (x,y) where x,y are corresponding
elements from vectors X and Y.

2.2.1 Example
Work through the following example which illustrates the parallelogram law of vector
addition.

>> u= [−1 2];


>> v= [2 3];
>> w = u + v
w=
1 5

We now draw the position vectors of u, w, and v, in that order. Actually the compass
command doesn’t mind what order the vectors are in, but we need this order when we
come to join up the parallelogram.

>> X = [−1 1 2];


>> Y = [2 5 3];
>> compass(X,Y)
>> hold on
>> plot(X,Y,’- -’)
>> hold off

Label the vectors u, v and u+v using the instructions given in Section 2.2.
2.3. OPERATIONS ON COMPONENTS OF VECTORS 9

2.3 Operations on components of vectors


To perform operations on components of a vector we insert a dot before the operation.

For example, u.∗v will multiply corresponding components of the two vectors u and v,
and u. ∧ 2 will square the components of u. For example, try

>> u= [1 2 3]0 ;
>> v= [4 5 6]0 ;
>> u.∗v >>u. ∧ 2 >>u./3 >>1/3.∗u

The command sum(u) adds the components of the vector. Using the same vector u, the
sum of the components is 1 + 2 + 3 = 6. Try

>> sum(u)

2.4 Dot product


Our first method for calculating the dot product is based on the definition,

u·v = u1 v1 + u2 v2 + . . . + un vn

and reinforces our understanding of the dot product. In the next section we introduce a
simple Matlab command for the dot product.

The dot product, u · v is found by multiplying corresponding components of the vectors


and adding the results. It is therefore the sum of the components of the vector given
by the Matlab command u.∗v. Using the vectors u, v from 2.3 above, sum(u.∗v)
= 4 + 10 + 18 = 32. Try

>> sum(u.∗v)
ans =
32

The length of the vector u is the square root of u·u. The Matlab command for square
root is sqrt so the length of a vector can be found by

>> sqrt(sum(u.∗u))
ans =
3.7417

If we want the answer written in terms of the square root we type sym(ans), meaning
the symbolic form of ans.
10 CHAPTER 2. MATLAB FOR VECTORS.

>> sym(ans)
ans =
sqrt(14)

Distinguish carefully between the two very similar commands:

syms x tells Matlab that x is to be treated as a symbol

sym(A) asks for the expression A in symbolic form - fractions, square roots etc.
instead of a decimal number.

Alternatively we could get the length of u from sqrt(sum(u.∧2)). Try it.

2.5 Special commands for vector products


The method given in section 2.4 is designed to reinforce your learning of the definition
of the dot product. There is an easier way of finding dot products - Matlab has special
commands:

dot(u,v) gives the dot product, u·v


cross(u,v) gives the vector cross product, u×v

2.5.1 Example
Let the lines L1 and L2 be given by the vector equations
     
x 1 0
L1 : y  = 0 t + 1
z 0 0
     
x 0 2
L2 :  y = 0 s + 1
   
z 1 3
where t and s are parameters.

1. State the direction vector for the line L1 .

2. Calculate the shortest distance from the point P1 = (1, 0, 1) to the line L1 .

3. Prove that the lines L1 and L2 intersect, and find their intersection point.
2.5. SPECIAL COMMANDS FOR VECTOR PRODUCTS 11

Solution:

 
1
1. Reading directly from the vector equation, the direction vector is given by 0.

0

2. We first define in Matlab the direction vector of line L1 which we name u1, and we
write the point P1 in a vertical array.

>> u1 = [1; 0; 0];


>> P1 = [1; 0; 1];

We must
 now construct a vector, v1 , from the line L1 to the point P1 . Since the
0
point 1 is on the line, we define v1 in Matlab as;

0

>> v1 = P1 − [0; 1; 0]
v1 =
1
−1
1

We now project this vector v1 onto the direction vector u1 and name the resultant
vector d;

>> d= dot(v1,u1)./dot(u1,u1).∗u1
d=
1
0
0

From inspection the magnitude of d= 1 which is the distance required. We could


find this using Matlab by typing

>> sqrt(dot(d,d))
ans =
1
12 CHAPTER 2. MATLAB FOR VECTORS.

3. To solve this system of equations we first define two symbolic variables which rep-
resent the parameters;

>> syms s t;

We then set the two vector equations equal to each other. Before entering this into
Matlab we rearrange
  the equation
  so that
 the right hand side equals the zero vector.
1 0 0 2
That is, 0 t + 1 = 0 s + 1 becomes
0 0 1 3
         
1 0 0 2 0
0 t + 1 − 0 s + 1 = 0;
0 0 1 3 0
In Matlab;

>> ([1; 0; 0].*t+[0; 1 0]) - ([0; 0; 1].*s + [2; 1; 3])


ans =
t−2
0
−s − 3

From inspection we can see that the equations have a single solution when t = 2 and
s = −3. Substituting this into line L1 we find the intersection point (2, 1, 0).

2.5.2 Exercise
    
4 8 −3
u= −2 , v= 3 , w=  2 .
5 7 6

1. Calculate u·v, v·w, u·w.

2. Find the lengths of u, v, and w.

3. (a) Use the special command dot(u,v) to check your answers to 1 above.
(b) Calculate the cross products u × v, v × w, u × w.

4. Calculate the lengths in question 2 using the dot command.

5. Using the dot command, calculate the projections, proj u on v, proj v on u, proj
v on w, proj w on u.
2.5. SPECIAL COMMANDS FOR VECTOR PRODUCTS 13

6. (a) Calculate cos θ, where θ is the angle between vectors v and w.


(b) The command for the inverse cosine (or arccos) is acos( ). Find the angle θ
for part (a) in degrees (type pi for π).

7. Show that the vector (w - proj w on u) is orthogonal to the vector u. Also try (v
- proj v on u).
14 CHAPTER 2. MATLAB FOR VECTORS.
Chapter 3

Matlab for solving systems of


equations.

3.1 The rref command


The process for solving a system of linear equations using Matlab begins with defining an
array which contains the coefficients (numbers in front of the variables) of the system of
equations. If there is a unique solution then the command rref(A) produces an array in
which the coefficients of the variables are either 1 or 0, and the solution can be read off
from the right hand column. For example, consider the following system of equations:

3x + y − z = 7
2x − 3y + 4z = −1
5x + 2y + 3z = 2

The array of coefficients is given by

3 1 -1 7
2 -3 4 -1
5 2 3 2

This is entered into Matlab by listing the numbers in the rows on one line, with a semicolon
at the end of each row and enclosing the list in square brackets. Notice that there must
be a space between each number in the list. Type

>> A = [3 1 -1 7; 2 -3 4 -1; 5 2 3 2]

Press Enter.

A=

15
16 CHAPTER 3. MATLAB FOR SOLVING SYSTEMS OF EQUATIONS.

3 1 -1 7
2 -3 4 -1
5 2 3 2

This allows us to check that we put the right numbers in. Now type

>> rref(A)

ans =
1 0 0 2
0 1 0 -1
0 0 1 -2
Interpreting the rows as equations, we have x = 2, y = −1, z = −2. Notice that the
right hand column gives the solution.
The name rref for this command is an acronym for reduced row echelon form. This
is the mathematical name for an array of the type given in the answer above. In a row
echelon form each successive row begins with more zeros than the row above it. An array
of pivot rows from elimination is a row echelon form. In a reduced row echelon form the
first non-zero entry in each row is a 1 and the entries above these 1s are zero.
What happens if the system of equations does not have a unique solution? The rref
command just does elimination (a bit more than we do by hand) so the rows it produces
are coefficients for a system of equations that has the same solutions as the original
equations. We treat these as pivot equations and either identify inconsistency or carry
out back substitution introducing a parameter as necessary. Often it is simpler to do the
back substitution on paper than to try and do it on Matlab.

3.1.1 Example
Solve the system of equations:
7x + 14y = 21
5x + 10y − 2z = 7
3x + 6y = 9
We represent these equations in Matlab by the array of coefficients, we’ll call it M. Type

>> M = [7 14 0 21; 5 10 -2 7; 3 6 0 9] ;
>> rref(M)

ans =
1 2 0 3
0 0 1 4
0 0 0 0
We interpret the non-zero rows as pivot equations:
3.1. THE RREF COMMAND 17

x + 2y = 3
z = 4
There are infinitely many solutions to these equations. Taking y as parameter we solve
for x in terms of y:
x = 3 − 2y and z = 4.
The set of all solutions is {(3 − 2y, y, 4): y a real number}.
In more complicated examples we may want to use Matlab in the back substitution. Once
Matlab has assigned a value to a variable it remembers it until such time as we assign a
different value or close Matlab. This means we can assign a value to z, and then make
y the subject of the second pivot equation and Matlab immediately gives the value of y.
We don’t have to substitute the value for z, Matlab does it automatically. When we want
to solve in terms of a parameter, t, we have to tell Matlab that this letter is to be treated
as a symbol by typing the command syms t. If we fail to do this Matlab will tell us we
have an undefined variable t.

3.1.2 Example
Find all solutions for the system of equations:
4x − 2y − 11z = 12
3x − 4y − 7z = 19
2x − 10y − z = 42

>> A= [4 −2 − 11 12; 3 −4 − 7 19; 2 − 10 − 1 42]


A=
4 -2 -11 12
3 -4 -7 19
2 -10 -1 42
>> rref(A)
ans =
1.0000 0 -3.0000 1.0000
0 1.0000 -0.5000 -4.0000
0 0 0 0
>> syms t
>> z = t; y = −4 + 0.5 ∗ z; x = 1 + 3 ∗ z;

Matlab will list the solutions if we type x y z in square brackets.

>> [x y z]
ans =
[1 + 3 ∗ t, −4 + 1/2 ∗ t, t]
18 CHAPTER 3. MATLAB FOR SOLVING SYSTEMS OF EQUATIONS.

The solutions are x = 1 + 3t, y = −4 + t/2, z = t, and t may be any real number.

Summary of new commands

syms t establishes t as a symbol


A=[ ; ; ] gives an array with 3 rows
rref(A) gives the reduced row echelon form of A
- solutions to the system of equations can be read off from this.

3.1.3 Exercises
1. Solve the systems of equations using the rref command.

2x + y + 3z − w = 4
-2x + y − 3z + 2w = −1
4x + 4y + 9z + 2w = 11
2x − y + 12z + 9w = −1

2. Use the rref command on the following system of equations and interpret the result.

3x + 5y = 10
x + 3y = 4
2x − 4y = 7
4x + 2y = 9

3. Find all solutions for the system of equations:

x + y + 2z + 3w = 13
x − 2y + z + w = 8
3x + y + z − w = 1

3.2 Numerical considerations


Matlab does its computations in binary floating point arithmetic. This is accurate to
approximately 16 decimal places. However rounding errors can build up during the elimi-
nation process, and solutions may be less accurate than that. The command rref includes
special procedures such as Gaussian elimination with pivoting to minimise propagation
of error. Notice that even when we use 2 decimal place format Matlab will be working to
full precision.
3.2. NUMERICAL CONSIDERATIONS 19

3.2.1 Example
1. (a) Solve the system of equations using format bank (2 decimal places).

2. (b) Substitute the 2dp solution into the left hand sides of the equations.
0.27x + 0.41y + 0.1z = 0.63
0.55x + 0.25y − 0.12z = 0.86
0.47x − 0.68y + 0.38z = −0.02

Solution:

1. (a)
>> format bank
>> A = [0.27 0.41 0.10 0.63; 0.55 0.25 -0.12 0.86; 0.47 -0.68 0.38 -0.02]
A=
0.27 0.41 0.10 0.63
0.55 0.25 -0.12 0.86
0.47 -0.68 0.38 -0.02

>> B = rref(A)
B=
1.00 0 0 1.18
0 1.00 0 0.78
0 0 1.00 -0.11

The solution is x = 1.18, y = 0.78, z = −0.11.

2. (b)
We substitute these numbers into the left hand side of the equations, using format
short.

>> format short


>> x = 1.18; y = 0.78; z = −0.11;
>> 0.27 ∗ x + 0.41 ∗ y + 0.1 ∗ z
ans =
0.6274

>> 0.55 ∗ x + 0.25 ∗ y − 0.12 ∗ z


20 CHAPTER 3. MATLAB FOR SOLVING SYSTEMS OF EQUATIONS.

ans =
0.8572

>> 0.47 ∗ x − 0.68 ∗ y + 0.38 ∗ z


ans =
−0.0176

None of these answers is exactly equal to the right hand side of the corresponding equation,
so our solution is not completely accurate. Nevertheless, rounding these answers to 2dp
would give the correct right hand sides for all three equations. We can therefore say that
our solution is correct to 2 decimal places. Matlab is carrying a lot more decimal places in
the values for x, y and z, so it does have a more accurate version of the solution. We can
see more decimal places by typing format long and re-entering the rref(A) command.
(This will certainly fill the screen with numbers!) Try it.
How accurate is the solution Matlab displays? We can substitute the full values for x,y,z
into the left hand sides of the equations by using the array address of the number instead
of copying a rounded version of it.

3.3 Array addresses


The array address for an element of an array named A is given as A(m,n) where m is
the number of the row and n the number of the column. For example an array, A, with
3 rows and 3 columns has addresses:
A(1,1) A(1,2) A(1,3)
A(2,1) A(2,2) A(2,3)
A(3,1) A(3,2) A(3,3)
This means that the second number in row 3 of A has address A(3,2) as it is in row 3
and column 2. For the array A in Example 3.2.1 above,
A(1,3) = 0.1, the entry in row 1 and column 3
A(2,4) = 0.86, the entry in row 2 and column 4.
To test out Matlab’s solution to our previous example we use the array addresses from
the matrix B (= rref(A)) for the values of the variables. They are all in column 4.

>> format long


>> x =B(1, 4); y =B(2, 4); z =B(3, 4);
>> 0.27 ∗ x + 0.41 ∗ y + 0.1 ∗ z
ans =
0.63000024303027
3.3. ARRAY ADDRESSES 21

>> 0.55 ∗ x + 0.25 ∗ y − 0.12 ∗ z


ans =
0.86000031631148
>> 0.47 ∗ x − 0.68 ∗ y + 0.38 ∗ z
ans =
−0.02000001488525

This shows that Matlab’s full solution (with all its decimal places) is fairly good. It gives
the right hand sides correct to 6 decimal places. The digits in the final decimal places of
the answers are the result of rounding errors in the calculations.

3.3.1 Exercise
1. Solve the system of equations using array addresses in the back substitution. Check
your answer by substituting it in the original equations.

2.2x − 3.5y + 1.2z = −18.3


4.4x + 4.6y − 3.8z = −25.6
−6.3x + 3.8y + 2.4z = 57.1

2. The following system of equations is from the study notes, Example 1/16.
Use the rref command to solve the system of equations. Compare your answer with
the ones in the book. This system of equations is very sensitive to rounding errors.

0.067x + 0.071y + 0.077z = 0.22


0.071x + 0.077y + 0.083z = 0.23
0.077x + 0.083y + 0.091z = 0.24.
22 CHAPTER 3. MATLAB FOR SOLVING SYSTEMS OF EQUATIONS.
Chapter 4

Span and linear independence.

4.1 Linear independence - method 1


This method uses the definition of linear independence.
If au + bv + cw = 0 has only one solution, a = 0, b = 0, c = 0, then u, v, w are
linearly independent.
We use the rref command to solve the system of equations.

4.1.1 Example
    
1 −1 2
u= 2 ,
 v= 1  ,
 w= −1 .
3 0 1

1. Solve a system of equations to get all solutions of the vector equation


au + bv + cw = 0.

2. Are u, v, w linearly independent?

3. Use the solution to the equations to write u in terms of v and w.

Solution:

1. The array of coefficients for the system of equations has the three vectors for the
first three columns and a column of zeros. Enter the three rows of the array.
>> A=[ 1 -1 2 0; 2 1 -1 0; 3 0 1 0 ]
A=

23
24 CHAPTER 4. SPAN AND LINEAR INDEPENDENCE.

1 -1 2 0
2 1 -1 0
3 0 1 0
>> format rat This gives fractions (rational format) instead of decimals.
>> rref(A)
ans =
1 0 1/3 0
0 1 -5/3 0
0 0 0 0
Back substitution.
>> syms t; c = t; b = 5/3 ∗ c; a = −c/3;
>> [a b c]
ans =
−t/3 5/3t t
The set of all solutions is {(−t/3, 5t/3, t) ; t any real number }.

2. No the vectors are not linearly independent as a = 0, b = 0, c = 0 is not the only


solution of the vector equation au + bv + cw = 0. Can you see this from the form
of rref(A) without actually finding the solution?

3. We just need to pick out a solution for a, b, c and make u the subject of the formula.
Putting t = −3 in the parametric solution we get a = 1, b = −5 and c = −3.
So u - 5v - 3w = 0 and u = 5v + 3w (u is in the span of {v,w}).

4.1.2 Example
Are the vectors
     
1 0 1
u= 2 , v= 2 , w= 0 ,
2 2 1

linearly independent?

Enter the vectors into an array named B.

>> B= [1 0 1 0; 2 2 0 0; 2 2 1 0];

Solve using rref(B).

>> rref(B)
4.2. LINEAR INDEPENDENCE - METHOD 2 25

ans=
1 0 0 0
0 1 0 0
0 0 1 0

From the Matlab output we see that the only solution is when a = 0, b = 0, c = 0,
hence the set of vectors are linearly independent.

4.2 Linear independence - method 2


We have a second method of testing for linear independence, namely to write the vectors
as rows of an array and do elimination. If there are no rows of zeros the vectors are
linearly independent. In Matlab the rref command does the elimination and we check
for zero rows. We use the vectors from the Example in method 1 and write them as rows
to give an array, B.
>> B=[ 1 2 3; -1 1 0; 2 -1 1 ]
B=
1 2 3
-1 1 0
2 -1 1
>> rref(B)
ans =
1 0 1
0 1 1
0 0 0
The row of zeros indicates a redundant vector so the vectors are not linearly independent.

4.2.1 Exercise
       
1 3 1 4
3 4 −2 −13
Let p= 
4 ,
 q= 
7 ,
 r= 
−1 ,
 s= 
 −9  .

1 0 −2 −11

1. Use two different methods to decide whether the vectors are linearly independent.

2. Solve equations (ap + bq + cr = s ) to show that there are many ways of writing
s in terms of the spanning vectors p, q, r.
26 CHAPTER 4. SPAN AND LINEAR INDEPENDENCE.
Chapter 5

Matlab for Matrices

5.1 Matrix multiplication


Matlab is made for matrices, so the standard commands for addition, subtraction, and
multiplication apply to matrices. In particular, if matrices A and B are the right sizes for
matrix multiplication, the command A∗B will produce the matrix product. If we want
to multiply every entry in a matrix by a scalar number k, we use the command k.∗M.
Note the period in front of the ∗ is used to denote “term by term” multiplication. This
also works if we want to multiply every entry in a matrix M by the corresponding entry
in another matrix N. For example, if
   
1 2 −1 − 2
M= , N= ,
3 4 −5 − 6

then the command M.∗N returns


   
(1 ∗ −1) (2 ∗ −2) −1 − 4
= ,
(3 ∗ −5) (4 ∗ −6) −15 − 24

but the command M∗N returns the matrix product


 
−11 − 14
.
−23 − 30

Vectors are also matrices. The Matlab command for the product Mv of a matrix times a
vector is simply M∗v. However, we have to be careful that the vector v is presented as a
column vector of the right size to match up with the elements in the rows of M. There
are two ways of obtaining column vectors in Matlab:

Method 1. Enter a list of numbers in square brackets with a semicolon between each
entry.
Method 2. Enter a list of numbers in square brackets followed by an apostrophe. (Re-
member to put a space between the entries.)

27
28 CHAPTER 5. MATLAB FOR MATRICES

Try the following commands:

>> u = [1; 2; 3], w = [5 6 7]0


>> M = [1 2 3; 4 5 6]
>> v = [1 1 1]0
>> M∗v
>> A = [1 2; 3 4]; B = [3 − 5 2; 1 2 7];
>> C = A∗B

To see Matlab’s response to the wrong kind of vector try

>> u = [1 1 1];
>> M∗u
>> B∗A

Notice that the order is important, AB is not generally equal to BA for matrices.

If we need to pick out a particular column from a matrix the command C( : , i ) will
pick out column i of C. Try

>> C( :, 3 )

5.2 Systems of linear equations revisited


A system of linear equations can be written in matrix form as Mx = b, where M is the
matrix of coefficients of the variables and b is the column vector containing the right hand
sides of the equations. If the system has a unique solution it can be obtained from the
Matlab command x = M \b. (Read it as M back-slash b) Note that b must be entered
as a column vector. (This command can be associated with the inverse matrix method
where the solution is found as x = M−1 b.)

For example, to solve the system of equations:

3x + y - z = 7
2x - 3y + 4z = -1
5x + 2y + 3z = 2

>> M = [3 1 − 1; 2 − 3 4; 5 2 3 ]; b = [7 − 1 2]0 ;
>> x = M \b
x=
2.0000
-1.0000
-2.0000
5.2. SYSTEMS OF LINEAR EQUATIONS REVISITED 29

This is a very simple method for solving systems of equations, but unfortunately it doesn’t
work for every system of equations!

The conditions required are:

(i) the number of equations is equal to the number of variables,


(ii) the matrix, M, of coefficients is invertible (this means det M non-zero).

Summary of commands
A+B adds matrices of the same size
A-B subtracts matrices of the same size
k.∗A multiplies each element of A by the number k
A∗B matrix multiplication A an (m x n) matrix, B an (n x p) matrix)
A(:, j) picks out column j of matrix A
A(i, :) picks out row i of matrix A
x = M \b gives the solution of the system of equations Mx = b if it is unique.

5.2.1 Exercise
Use x = M \b on the following systems of equations.

x + 2y + 3z = 5
1. 4x + 5y + 5z = 5
7x + 8y + z = 27

3x + y - z = 7
2. 2x - 3y + 4z = -1
5x + 2y + 3z = 2.

5.2.2 Example
Try the following commands:

>> A = [1 2; 3 4]; B = [0 1; −1 0]; C = [−1 2 1; 2 3 0];


>> A+B
>> A-B
>> 3.*A
>> A.*3
>> format rat
>> 1/5.*B
30 CHAPTER 5. MATLAB FOR MATRICES

>> B./5
>> 3.*A +2.*B
>> A*B
>> B*A
>> A*B -B*A
>> A*C
>> B*C
>> C*B

5.3 Transpose and Inverse Matrix


The transpose of A interchanges rows and columns. This means the rows of A become the
columns of A transpose. We were transposing when we changed row vectors into column
vectors. The command is the same.
A’ transpose of A (rows become columns)
inv(A) inverse of A ( A∧ − 1 may also be used).

5.3.1 Exercise
Use the matrices A = [1 2; 3 4]; B = [0 1; −1 0]; C = [−1 2 1; 2 3 0];
to find

1. A−1 . Show that AA−1 =I and A−1 A=I, where I is the 2 × 2 identity matrix.

2. A transpose, B transpose, (AB)t , Bt At , At Bt , (BA)t .

3. The inverse of A, the inverse of B, the inverse of AB, A−1 B−1 , B−1 A−1 (you should
find that (AB)t = Bt At and (AB)−1 = B−1 A−1 ).
Chapter 6

Complex numbers.

6.1 Simple complex number commands


Matlab is able to perform
√ complex arithmetic. The variables i and j are reserved to be
the complex number −1. Try

>> sqrt(−1)
ans =
0 + 1.0000i

By defining complex numbers (for example, p = 1 + 2 ∗ i) we may perform addition,


subtraction, multiplication, and division of complex numbers in the same way we would
for real valued numbers.
If we define a complex number z = x + iy, then we may make use of the following
commands:

real(z) returns x, the real part of z


imag(z) returns y, the imaginary part of z
z’ or conj(z) returns z̄=x-iy,pthe complex conjugate of z
abs(z) returns |z| = x2 + y2 , the modulus of z
angle(z) returns argument (angle) of z
nthroot(z,n) returns the nth root of z
exp(i*x) returns cos(x) + i sin(x).

6.2 Plotting complex numbers


If we wish to plot a sample of complex numbers on the complex plane we may use the
command compass(A), where A is an array containing the complex numbers to be
plotted.

31
32 CHAPTER 6. COMPLEX NUMBERS.

6.3 Finding all nth roots


To find the nth roots of a complex number w, we solve zn = w. The command w∧(1/n)
will only give you one solution.

>> w=3-4*i
>> w∧(1/12) (this gives only one solution)
>> z=solve(‘z∧12=3-4*i’) (gives all 12 roots of w)

6.3.1 Example
Let z = 2 − i, w = −1 + 3i, q = −2i, and r = 2.

1. Calculate the following complex numbers;

(a) z z̄
(b) zw
(c) q + r ∗ q
(d) w45

2. Plot the complex numbers from part (a) on the complex plane.

3. Find all of the 5th roots of z.

Solution:

First we enter the complex numbers into Matlab.

>> z=2-i; w=-1+3*i; q=-2*i; r=2;

1. (a) >> z ∗ z 0
(b) >> z ∗ w
(c) >> q + r ∗ q
(d) >> w ∧ 45

2. >> A= [z ∗ z 0 z ∗ w q + r ∗ q w ∧ 45]
>> compass(A).

3. >> solve(0 s ∧ 5 = 2 − i0 ).
Chapter 7

Eigenvalues and Eigenvectors

The following commands are useful for checking work done by hand

det(A) determinant of matrix A


eig(A) eigenvalues of matrix A
[V,D]=eig(A) This gives an orthogonal matrix V of eigenvectors of A and the
corresponding diagonal matrix D of eigenvalues.
eye(n) gives the n × n identity matrix I.

A note on finding eigenvectors using Matlab:


When using the command [V,D]=eig(A), Matlab will return a diagonal matrix D (the
diagonal elements are the eigenvalues of A), and a matrix V which contains the eigen-
vectors of matrix A. Note that matrix V is an orthogonal matrix (the eigenvectors are
normalised and orthogonal to one another, if they form a basis). Although completely
correct, viewing eigenvectors in this format is not particularly aesthetically pleasing.
To obtain “nicer” looking eigenvectors try the following.

>> A=[0 0 0; 0 0 1; 0 1 0];


>> Asym=sym(A);
>> [V1,D1]=eig(A);
>> [V2,D2]=eig(Asym);

Using the technique above we get eigenvectors which are somewhat easier to interpret.

7.0.1 Example
   
111 101
List the eigenvalues and eigenvectors of the matrices A= 1 1 1, B= 1 1 1
111 111

>> A= [1 1 1; 1 1 1; 1 1 1];

33
34 CHAPTER 7. EIGENVALUES AND EIGENVECTORS

>> B= [1 0 1; 1 1 1; 1 1 1];
>> Asym=sym(A); Bsym=sym(B);
>> [V1,D1]=eig(Asym)
V1 =
−1 −1 1
1 0 1
0 1 1

D1 =
0 0 0
0 0 0
0 0 3

>> [V2,D2]=eig(Bsym)
V2 =
−1 −5 ∧ (1/2)/2 − 1/2 5 ∧ (1/2)/2 − 1/2
0 1 1
1 1 1

D2 =
0 0 0
0 3/2 − 5 ∧ (1/2)/2 0
0 0 5 ∧ (1/2)/2 + 3/2

From matrix D1 we see that the eigenvalues are 0 (with multiplicity 2), and 3. The
corresponding eigenvectors are given in V1 as
     
−1 −1 1
v1 =  1 , v2 =  0 , v3 = 1.
0 1 1
√ √
3 5 3 5
From matrix D2 we see that the eigenvalues are 0, − , and + . The corre-
2 2 2 2
sponding eigenvectors are given in V2 as
 √  √ 
  − 5 1 5 1
−1 −  − 
2 2 , u3 =  2 2 .
 
u1 = 0 , u2 = 

 1   1 
1
1 1
Chapter 8

Diagonalisation and
orthogonalisation.

8.1 Diagonalisation in Matlab


To diagonalise a matrix A we need a matrix V of eigenvectors and a diagonal matrix D of
eigenvalues. In the previous section on eigenvalues we found the command [V,D]=eig(A)
produced a diagonal matrix D which had the eigenvalues of matrix A on its diagonal, and
a matrix V which contains the (normalised and orthogonal) eigenvectors of matrix A. You
may like to check that the command V*D*inv(V) does indeed return the matrix A.

8.2 Gram-Schmidt method


Given a set of linearly independent vectors, we may wish to construct an orthogonal or
orthonormal basis. We may do this using the Gram-Schmidt method learned in class. To
perform this task manually, try the following example.

8.2.1 Example
       
1 1 1 1 0 1
Let the matrix A= 1 1 0, and the vectors u = 2, v = 2, w = 0.
1 0 1 2 2 1

1. Diagonalise matrix A and show that VDV−1 =A.

2. Since u, v, and w are linearly independent we may form an orthogonal basis using
these vectors. Apply the Gram-Schmidt process to form such a basis.

Solution:

We begin by entering the vectors and the matrix into Matlab.

35
36 CHAPTER 8. DIAGONALISATION AND ORTHOGONALISATION.

>> u= [1; 2; 2]; v= [0; 2; 2]; w= [1; 0; 1];


>> A= [1 1 1; 1 1 0; 1 0 1];

1. >> [V,D]=eig(A)

V=
−0.7071 0.0000 0.7071
0.5000 −0.7071 0.5000
0.5000 0.7071 0.5000

D=
−0.4142 0 0
0 1.0000 0
0 0 2.4142

The matrices V and D diagonalise matrix A. To prove this enter

>> V*D*inv(V)
ans =
1.0000 0 0
0 1.0000 0
0 0 1.0000

2. Let u1 =u; We now apply the Gram-Schmidt process in Matlab.

>> u1 =u;
>> u2 =v - dot(v,u1 )/dot(u1 ,u1 ) .*u1 ;
>> u3 =w - dot(w,u1 )/dot(u1 ,u1 ) .*u1 - dot(w,u2 )/dot(u2 ,u2 ) .*u2 ;

>> [u1 u2 u3 ]
ans =
1.0000 −0.8889 0.0000
2.0000 0.2222 −0.5000
2.0000 0.2222 0.5000
Chapter 9

Useful Matlab Commands

Arithmetic
+, - add, subtract numbers, vectors, matrices
.∗, ./ scalar multiplication, division
.∧ raise to a power
sqrt( ) square root of ( )
format short display 4 decimal places
format long display 14 decimal places
format bank display 2 decimal places
syms t establishes t as a symbol/variable
sym( ) gives the symbolic form of ( )
solve( f ) solves the equation f(x) = 0 for x
solve( g,t ) solves the equation g(t) = 0 for t

Systems of equations

A = [r1 ; r2 ; r3] an array with three rows


rref( A ) reduced row echelon form of A
x = A \b unique solution of Mx=b

Vectors
k.*u multiplies vector u by the scalar number k
v(n) picks out the nth component of v
sum( ) gives the sum of entries in ( )
sqrt( dot(u,u) ) length of u
dot(u,v) dot product of vectors u, v
cross(u,v) vector cross product

Matrices

37
38 CHAPTER 9. USEFUL MATLAB COMMANDS

k.*A multiplies matrix A by the scalar number k


A*B matrix multiplication of matrices of appropriate size
A(:,j) picks out the jth column of matrix A
A(i,:) picks out row i of matrix A
A’ transpose of matrix A
rank(A) gives the rank of A
inv(A) inverse of matrix A
det(A) determinant of matrix A
eig(A) eigenvalues of matrix A
[V,D]=eig(A) diagonalisation of matrix A

You might also like