APM1513 Study Guide
APM1513 Study Guide
DEPARTMENT OF
MATHEMATICAL SCIENCES
STUDY GUIDE
Copyright © Unisa 2009
TEAM:
Editor:
2
APM1513/1
Preface
This module is the first on the use of computers and computational methods in
applied mathematics.
Nigel Bishop
3
Table of Contents
Study Unit 1: Getting started with the computer software (Octave)................7
1.1 Installing and running the software (from TL101)......................................7
1.1.1 MATLAB or Octave....................................................................................7
1.1.2 Installation of Octave...................................................................................7
1.1.3 Testing simple examples in the program.....................................................8
1.1.4 Exit the program.........................................................................................11
4
APM1513/1
2.6 Control commands: Loops and branches...................................................34
2.6.1 “For” loops.................................................................................................34
2.6.2 Matrix or array operations versus “for” loops............................................35
2.6.3 If statements...............................................................................................36
2.6.4 Evaluation of conditions............................................................................37
2.6.5 Conditional loops.......................................................................................38
2.7 Functions....................................................................................................39
2.7.1 Pre-defined functions.................................................................................39
2.7.2 User-defined functions...............................................................................40
2.7.3 Function handles........................................................................................41
2.8 Graphics.....................................................................................................43
2.8.1 Plotting functions with fplot......................................................................43
2.8.2 Plots in 2 dimensions with plot..................................................................44
2.8.3 Plotting lines in 3-dimensions with plot3...................................................48
2.8.4 Other features of graphics..........................................................................54
2.10 In Summary................................................................................................56
5
Study Unit 5: Eigenvalues, eigenvectors and matrix diagonalization.............82
5.1 Summary of mathematical results..............................................................82
5.1.1 Calculation of eigenvalues and eigenvectors.............................................82
5.1.2 Some useful properties of eigenvalues and eigenvectors...........................83
5.2 The Octave command eig...........................................................................83
5.3 Matrix diagonalization...............................................................................85
5.4 The power method......................................................................................86
5.4.1 Algorithm...................................................................................................86
5.4.2 Justification/why........................................................................................87
5.5 Additional Exercises..................................................................................89
5.6 In conclusion..............................................................................................91
6
APM1513/1
LEARNING OUTCOMES
At the end of this Study Unit, you should be able to:
• Have Octave installed on your computer.
• Use Octave for simple calculations.
• Use Octave to produce simple graphs.
• Execute a simple Octave program by reading from a file.
• Produce Octave results as output, including graphs, to files.
• Use the Octave help facilities.
The installation process should show the Octave icon on your computer’s
Desktop. (If the icon is not there, go to the Start button, then click on Programs,
then on GNU Octave, then on Octave. Otherwise, go back and redo the
installation process).
NOTE: There may be minor differences in the layout in some cases because of
version differences between the programs. The program version shown in this
study guide was developed and tested using Octave version 3.0.0, running under
the Windows operating system, and with gnuplot (as the graphics back-end).
7
Activity 1-1 Open the program
Double click on the program icon to open the program.
A new window should open that contains installation details. Details on the
Octave Startup window should look like this:
If you have problems to install the program, at first, try again. If you still have a
problem contact the lecturer as this is a key step in starting.
Your input is indicated in bold boxed, so look for the input fishbone prompt (>),
then type your input.
For example,
Type the following calculation to use the program as a calculator.
>3.6+5.7
The programme should return the following OUTPUT:
ans = 9.3000
8
APM1513/1
Activity 1-2 Test the program
Type in your own examples of calculations in the program. You are now
investigating the ability of the program, Octave, to be used as a calculator.
Make a note of your own examples in your workbook/work file (you may make
Print Screen copies, and paste them into a wordprocessor file, then print it out
and paste it in your workbook as evidence of your work.)
FEEDBACK:
Were you able to do your own testing? If so, go to myUnisa Discussion Forums
and share your problems with other students to see whether you get any reply
from them. Your Tutor or Lecturer will monitor the discussions and support your
study efforts.
If you have problems with the installation processor the testing activity, go to the
Internet, with your Internet Explorer, to the Unisa student
website, [Link]
Log in with your student number and password, then go to this module’s website
and its Discussion Forums, and ask for help there.
If you are unable to go online, phone 012 429-6202 for help from the department.
Test some more calculations of your own where you use the multiplication,
division, power, and square root.
Now, let us look at an Example where we will use the program to do something
interesting.
FEEDBACK:
Let’s do it together. In order to use Octave, we must first write the above
equations in its matrix form, namely:
Ax=b
(We will discuss such problems in more detail in Study Unit 3.)
We will now give you the Octave INPUT together with the OUTPUT.
(Remember that the part that you must type as INPUT is after the fishbone input
prompt (>) and indicated in boxed bold.)
9
So, type the first row values; and separate rows with a colon, with or without a
space after the ; and before the 3.)
> b=[5;6]
b =
5
6
> x=A\b
x =
-4.0000
4.5000
Following this OUTPUT, you need to rewrite the required answer (in
mathematical terms) as:
x 1 = -4, x 2 = 4.5
The program produces a new window containing a graph of sin(x) in the range
0<x<7.5 (as shown below):
10
APM1513/1
1.1.4 Exit the program
Finally, exit your Octave session.
> quit
Now you know the basics about the program. Congratulations for getting this far.
For example,
Suppose the Home folder is located in your My Documents folder at:
C:\Documents and Settings\User\My Documents\apm113,
Then, to get Octave to use this as the Home folder, enter at the input prompt:
> cd "C:/Documents and Settings/User/My Documents/apm113"
There are two important points to note about the above command that is different
to the normal computer commands:
• The path appears inside quotation marks ("), so that the program will
recognise the command;
• The symbol normally used (\) is replaced by forward slash (/).
Obviously, it can become somewhat tiresome to enter the above long address
command every time you start an Octave session, so we will show you a shortcut.
Short cut:
Quit your Octave session to start a new session.
Confirm where the location of the default Home folder, by entering the command
“pwd” (meaning, Print Working Directory).
Start editing by going to the SciTE-editor with the command “edit”.
11
Activity 1-7 Change the Home directory and name files
Find the working directory you are working in and change it to your own.
Ensure you work in the newly named Home folder, APM113.
Choose a file name, xx.m (where xx is Test1, or whatever you like). Enter, at the
input prompt, Test1.
FEEDBACK:
Type the following:
> quit
Restart the program and type
> pwd
ans = C:\Program Files\Octave
> edit
The result will be that a new window will appear (entitled, SciTE). In this
window, enter just the one line to change to your new Home address directory you
want to use, such as:
cd "C:/Documents and Settings/User/My Documents/apm113"
In this SciTE-editing window, in the Top bar, go to: File, then Save As (to save in
the folder), C:\Program Files\Octave, (or \GNU Octave), (depending on the
program version).
Save the file as Test1.m (or whatever name you choose). Now type in Octave:
> Test1
Test to see where you are:
> pwd
ans = C:\Documents and Settings\User\My Documents\apm113
This illustrates a very important point: namely, that files can be created to execute
a sequence of commands; through making “m-files”. Read further to see the usage
of m-files.
Use only Plain Text in the .m-file. So, use a plain text editor such as Notepad, or
SciTE, or Wordpad (where the saved file type is “Text Document” - not as .rtf).
(IMORTANT: Do not use a word processor, such as Word, as the file it produces
keeps the formatting commands even though you cannot see it.)
Now, let us start using the program. Use the problem of two simultaneous
equations in the next section as an example.
12
APM1513/1
Activity 1-8 Save m-files
Open a SciTE-window (by entering “edit” at the Octave input prompt).
Enter the code for the problem to be solved (below).
Save the resulting file in the current Home folder, as your first example, ex1.m.
FEEDBACK:
Do the following INPUT in the program.
>edit
When you enter ex1 at the input prompt, you save the file in,
C:\Documents and Settings\User\My Documents\apm113
The file name is called ex1.m.
> ex1
A =
1 2
3 4
b =
5
6
ans =
-4.0000
4.5000
Congratulations, now you know how to put information into the Octave program,
so let’s look at the next step.
13
into the Clipboard by pressing the Enter key. Paste the copied text wherever you
like.
For graphs, once you have produced a graph on the screen, click Clipboard icon
on the far left in the Menu bar at the top of the graph window, and this will copy
the plot to Clipboard. You can then paste the graph somewhere else, such as into
a Word document.
Text in the Octave window is displayed using the Courier font. If you copy
output into a Word document, and want to maintain the look of the original
Octave session, then you should also use the Courier font in the Word document
(as we have done in this guide).
Activity 1-9
At the Octave input prompt, enter the command “diary on”.
> diary on
This has the effect of creating a file named diary in the Home folder. You can stop
this at any stage by using the command
> diary off
You can then open the file diary with a text editor, such as the SciTE-editor,
Notepad or Wordpad, and copy and paste parts of it to another document; but note
that you must switch diary off, or end your Octave session, before you can edit it.
Note also that, if the file diary already exists, the command “diary on” causes
the new session to be appended to the end of the file, that is, you do not lose what
is already there.
You can get information that is much more detailed by using the command
“doc”. Scroll around in this mode using the arrow keys.
For example,
> help fplot
-- Function File: fplot (FN, LIMITS)
-- Function File: fplot (FN, LIMITS, TOL)
-- Function File: fplot (FN, LIMITS, N)
-- Function File: fplot (..., FMT)
Plot a function FN, within the defined limits. FN an
be either a
14
APM1513/1
string, a function handle or an inline function. The
limits of
the plot are given by LIMITS of the form `[XLO, XHI]'
or `[XLO,
XHI, YLO, YHI]'. TOL is the default tolerance to use
for the
plot, and if TOL is an integer it is assumed that it
defines the
number points to use in the plot. The FMT argument is
passed to
the plot command.
fplot ("cos", [0, 2*pi])
fplot ("[cos(x), sin(x)]", [0, 2*pi])
See also: plot.
C:\Program Files\Octave\share\octave\3.0.0\m\plot\fplot.m
Additional help for built-in functions and operators is
available in the on-line version of the manual. Use the
command
`doc <topic>' to search the manual index.
Help and information about Octave is also available on the
WWW
at [Link] and via the help@[Link]
mailing list.
-- less (100%) (f)orward, (b)ack, (q)uit
There is a complete user manual available in both html- and pdf-formats, located
at:
C:\Program Files\Octave\doc\HTML\liboctave\[Link]
C:\Program Files\Octave\doc\HTML\interpreter\[Link]
C:\Program Files\Octave\doc\PDF\[Link]
The next activity focusses on getting you to use the Help function of the extensive
program manual more readily, and to transfer knowledge into your everyday use
of the program.
15
Activity 1-11 Write .m-file
Write a .m-file that uses Octave to solve the following problems.
2. Evaluate 9.2-0.5
7.3
3. Evaluate
2.4 + 3.1
3
This skill is required throughout this module, so feel free to refer to the Help-
function often for guidance. Keep evidence of your work (copy and paste your
efforts to your workbook or make a printout from your diary file).
1.5 In conclusion
Well done you now have the program, Octave, working. You have used it to
produce graphs and other simple calculations.
Now you are ready to start with the real work of the module. The next time you
use the program you will find it becomes much easier, and easier.
16
APM1513/1
LEARNING OUTCOMES
At the end of this Study Unit, you should be able to use the computer software
(such as Octave) to:
• Construct scalar, vector and matrix variables
• Manipulate variables to calculate new scalar, vector and matrix variables
• Use loops to repeat calculations
• Uses conditional tests, like “if” statements
• Construct user-defined functions
2.1 Introduction
This unit is a summary of the features of the Octave program that you will use
throughout the module. If you would like to see more detail, you will find it in the
extensive Octave manual.
For example,
> a=17
a = 17
This has the effect of creating a new variable “a” with value 17.
Alternatively, you can use the variable a also in another formula to test for “b”,
such as:
> b=a^2
b = 289
17
2.2.2 Variable names
Names of variables must start with a letter, and may include letters, numbers, as
well as the underscore character ( _ ). Names are case sensitive; therefore, A1 and
a1 will represent different variables. It is important to note that assigning a
variable name supersedes a meaning that may already exist. Thus, although it is
permissible to define log=6.1 it would not be useful to do so because then you
could no longer use the logarithmic function.
> log=6.1
log = 6.1000
You can see the “currently defined variable” names by entering the command
“who”. Look at the last part in the program OUTPUT, the “local user variables”,
to see the variables you have defined yourself.
FEEDBACK:
Type in “who”.
> who
*** dynamically linked functions:
__COM__ builtin: find dispatch getpwuid
You can remove all these definitions, either by quitting and starting a new Octave
session, or, by means of the command “clear”.
> clear
It is a good idea to clear the workspace after finishing one problem and before
starting another one, so as to avoid errors due to a variable having an unintended
value.
18
APM1513/1
2.3 Matrices and vectors
In this section, we deal with variables with more structure.
First, we need to construct vectors and matrices so, work through the following
examples.
FEEDBACK:
Row vectors:
> v1=[3.3 1.7 2.1]
v1 =
3.3000 1.7000 2.1000
Column vectors:
> u1=[3.3; 1.7; 2.1]
u1 =
3.3000
1.7000
2.1000
A matrix:
> A1=[1 2 3;4 5 6;7 8 9]
A1 =
1 2 3
4 5 6
7 8 9
A row vector when the values change by equal increments (that are not 1):
19
> v2=1:-0.2:0.4
v2 =
1.00000 0.80000 0.60000 0.40000
Transposition of a matrix:
> A5=A4'
A5 =
1.00000 5.00000 9.50000 1.00000 1.00000
2.00000 6.00000 11.50000 0.00000 0.80000
3.00000 7.00000 13.50000 0.00000 0.60000
4.00000 8.00000 15.50000 0.00000 0.40000
20
APM1513/1
We use the following rules for the construction of vectors and matrices:
Activity 2-3
Find the values of the following elements of a vector or matrix:
1) In a row vector, v3(2)
2) in a row vector, v3(3)
3) In a column vector, u2(4)
4) in a column vector, u2(3)
5) in a matrix, A2(2,3)
6) in a matrix, A2(3,2)
FEEDBACK:
For a row vector:
> v3(2)
ans = 2
> v3(1,3)
ans = 3
Note v3(1,3)=v3(3)
> u2(3,1)
ans = 0.60000
Note that u2(3,1)=u2(3)
For a matrix:
> A2(2,3)
ans = 7
> A2(3,2)
ans = 11.500
21
2.3.4 Accessing rows and columns of a matrix
We can access the rows and columns of a matrix by the notation
• A(row index, :) for a row of a matrix
• A(:,column index) for a column of a matrix
Activity 2-4
Access the rows and columns of a matrix, with A2 defined as above, using the
notation as above.
FEEDBACK:
> A2(2,:)
ans =
5 6 7 8
> A2(:,3)
ans =
3.00000
7.00000
13.50000
0.00000
In the same way, Octave treats a scalar quantity as being a matrix with only the
(1,1) element, and this is equivalent to it being a vector with just one component.
Therefore (in Octave), there is essentially no difference between scalars, vectors
and matrices.
Activity 2-5
Clear your previous work.
Check that a; a(1); a(1,1) all have the same value
FEEDBACK:
> clear
> a=7.342
a = 7.3420
> a
ans = 7.3420
> a(1)
ans = 7.3420
> a(1,1)
ans = 7.3420
22
APM1513/1
2.3.6 Special matrices
A number of special matrices are pre-defined in Octave, and here we give 4 cases
that will be useful to you later.
a) CASE 1
The statement zeros(m,n) defines a matrix with m rows and n columns whose
entries are all 0.
For example,
> zeros(4,6)
ans =
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
b) CASE 2
The statement ones(m,n) defines a matrix with m rows and n columns whose
entries are all 1.
For example,
> ones(3,2)
ans =
1 1
1 1
1 1
c) CASE 3
The statement rand(m,n) defines a matrix with m rows and n columns whose
entries are random numbers in the range 0 to 1.
For example,
> rand(4,5)
ans =
d) CASE 4
The statement eye(n) defines the square identity matrix with n rows and n
columns.
For example,
> eye(5)
23
ans =
1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1
It is also straightforward to get the inverse and delete a row and column from
matrices.
For example,
> A6=rand(5,5)
A6 =
0.228451 0.744584 0.311120 0.412804 0.415285
0.752986 0.990980 0.539382 0.236439 0.684900
0.079324 0.587490 0.386354 0.543607 0.880370
0.052009 0.885506 0.522269 0.963522 0.262930
0.392542 0.796100 0.162770 0.461631 0.082264
> A7=inv(A6)
A7 =
-7.124120 1.774712 1.615726 0.305008 2.922358
7.035698 -1.304923 -1.866377 -1.131128 -1.064458
0.157018 2.020572 -2.009178 2.431010 -3.883385
-6.063413 0.067611 2.244590 1.005264 2.812304
-0.378068 -0.217589 1.731535 -0.960240 0.414733
For example, where you delete the 2nd row of A6 and the 3rd column of A7.
> A6(2,:)=[]
A6 =
0.228451 0.744584 0.311120 0.412804 0.415285
0.079324 0.587490 0.386354 0.543607 0.880370
0.052009 0.885506 0.522269 0.963522 0.262930
0.392542 0.796100 0.162770 0.461631 0.082264
> A7(:,3)=[]
A7 =
-7.124120 1.774712 0.305008 2.922358
7.035698 -1.304923 -1.131128 -1.064458
0.157018 2.020572 2.431010 -3.883385
-6.063413 0.067611 1.005264 2.812304
-0.378068 -0.217589 -0.960240 0.414733
This enables you to construct any form of matrix or vector that you want or need.
Now let’s go further to see what else is important.
24
APM1513/1
a) Case 1
Addition and subtraction, A + B, and A – B, are defined whenever A and B are
matrices of the same size, namely, they each have the same number of rows and
columns.
FEEDBACK:
> A8=rand(2,3)
A8 =
0.35475 0.95425 0.70169
0.18010 0.15871 0.60431
> B1=ones(2,3)
B1 =
1 1 1
1 1 1
> A8+B1
ans =
1.3548 1.9542 1.7017
1.1801 1.1587 1.6043
b) Case 2
Multiplication, A B, is defined whenever A and B are matrices with the number of
columns in A equal to the number of rows in B, or, if either A or B is a scalar.
FEEDBACK:
> A9=rand(2,3)
A9 =
> B2=rand(3,4)
25
B2 =
> A9*B2
ans =
> 2.3*A9
ans =
c) Case 3
Raising to a power, An, is defined whenever A is a square matrix, and it means
A x A x A ... (n times).
> A10=rand(3,3)
A10 =
> A10^2
ans =
> A10^5
ans =
d) Case 4
Division is not straightforward in matrix algebra. Here we will introduce the
Octave operators \ and / within the context of solving a system of simultaneous
equations (In fact, the operators \ and / can be used more generally, but we will
not discuss that).
For example,
Suppose that we have n by n square matrix A, and column vectors x and b both of
length n, and that we want to solve for x in
A x = b.
Then,
x = A-1 b.
26
APM1513/1
Of course, we could also write in Octave x=inv(A)*b, but this is less efficient so
the \ operator is preferred.
For example,
Suppose instead that we want to solve
yA=c
with y and c now row vectors of length n. Then
y = c A-1.
The Octave notation for the above is
y=c/A
Of course, we could also write in Octave y=c*inv(A), but again this is less
efficient.
> A11=rand(3,3)
A11 =
0.088404 0.120302 0.727375
0.940880 0.350956 0.817928
0.373165 0.451789 0.714572
> b=rand(3,1)
b =
0.093734
0.363911
0.892151
> c=rand(1,3)
c =
0.87188 0.98920 0.42356
> x=A11\b
x =
-0.39420
2.73657
-0.27583
> inv(A11)*b
ans =
-0.39420
2.73657
-0.27583
> y=c/A11
27
y =
-2.147898 0.048056 2.724118
> c*inv(A11)
ans =
-2.147898 0.048056 2.724118
For example,
Suppose that we have a 1 x n array v and we want to construct a new array u, each
element of which is the square of the corresponding element in v. In mathematical
notation, what we want is
u k = v k 2, for each value of the index k.
In Octave, if we write u=v^2 we will get an error message, so instead we use the
operator (.^) to indicate that we are doing a calculation element-by-element, by
writing u=v.^2.
> v=rand(1,4)
v =
0.295962 0.194619 0.078277 0.839960
> u=v^2
error: for A^b, A must be square
error: evaluating binary operator `^' near line 53, column 4
error: evaluating assignment expression near line 53, column
2
> u=v.^2
u =
0.0875937 0.0378766 0.0061273 0.7055324
NOTE: If you are doing matrix calculations there will be NO .^, but in element-
by-element arrays calculations you will need the full stop (.).
> A12=rand(4,3)
28
APM1513/1
A12 =
0.95540 0.56061 0.65058
0.54163 0.53082 0.11537
0.14583 0.27776 0.53204
0.22211 0.63209 0.37170
> B3=rand(4,3)
B3 =
0.787186 0.138921 0.080241
0.390426 0.846420 0.913880
0.561953 0.374361 0.883199
0.039100 0.810616 0.554635
> A12.*B3.*1.8
ans =
1.353745 0.140185 0.093965
0.380638 0.808729 0.189779
0.147506 0.187165 0.845815
0.015632 0.922290 0.371081
> A12.^B3
ans =
0.96472 0.92275 0.96609
0.78710 0.58504 0.13895
0.33893 0.61906 0.57274
0.94287 0.68946 0.57758
> A12./B3./3
ans =
0.404565 1.345152 2.702612
0.462424 0.209044 0.042080
0.086500 0.247316 0.200800
1.893515 0.259922 0.223388
From the above, we see that element-by-element calculations obey the following
rules:
• The operators are “.+”, “.-“, “.*”, “./”, “.^”.
• Each array in the expression must have the same size, i.e. all arrays must
have the same number of rows m as well as the same number of columns n
• One of the operands for .* ./ .^ may be a scalar.
FEEDBACK
Were you able to construct all the above vectors?
Here is a solution to c):
> 1./b
29
• Use size(matrix_name)
This gives the number of rows, followed by the number of columns, in the matrix.
For example
> A13=ones(4,3)
A13 =
1 1 1
1 1 1
1 1 1
1 1 1
> size(A13)
ans =
4 3
• Use length(vector_name)
This is used to find the number of elements in a vector. Formally, it returns the
greater of the number of rows or the number of columns in an array, but it is
better practice to apply it only to vectors.
For example
> v=1:5
v =
1 2 3 4 5
> length(v)
ans = 5
• Use sum(matrix_name)
This adds up the elements in each column of a matrix - with the exception of a
row vector in which case it adds up the elements in the row.
For example
> sum(v)
ans = 15
> sum(A13)
ans =
4 4 4
For example
> A14=ones(2,3);
> A14
A14 =
1 1 1
1 1 1
For example
For example,
> c1=2,c2=c1^2;c3=c2^2
c1 = 2
c3 = 16
For example
> c1=2
c1 = 2
Sometimes, you want to suppress the "variable =" part, for example when
producing a Table. You can also use the disp command to place text into the
Octave output.
31
For example
> disp(c1)
2
Activity 2-13
Write a program that asks the user to enter a square matrix A, and then to enter
a column vector b, and then to request a result using Return, to find or solve the
system of equations Ax=b.
FEEDBACK:
File ex2.m
input("Enter a square matrix A ");
input("Enter a column vector b ");
input("Press Enter to find the result of solving Ax=b");
x=A\b
Octave window
> ex2
Enter a square matrix A, A=[1 2;3 4]
Enter a column vector b, b=[5;6]
Find the 2 + 3i
The variable z with real part 2 and imaginary part 3 is created by the statement,
> z=2+3*i.
All the arithmetic operators and most functions work with complex numbers, for
example,
> sqrt(z)
ans = 1.67415 + 0.89598i
32
APM1513/1
There are some functions that are specific to complex numbers, and if applied to a
real number, they simply treat it as a complex number with zero imaginary part.
For example
> real(3+2*i)
ans = 3
> imag(3+2*i)
ans = 2
> abs(3+2*i)
ans = 3.6056
> conj(3+2*i)
ans = 3 - 2i
If you are working with complex numbers, be careful about the following:
• DO NOT USE by i or j as variables anywhere in your program – the
effect would be to change the default meaning from − 1
• For a matrix with complex elements, the transpose operator (') means to
take the complex conjugate transpose, that means, rows and columns are
interchanged and also the signs of the imaginary parts are changed.
Activity 2-14
Will Octave treat this as x=(a*b)+c or as x=a*(b+c)?
FEEDBACK:
The answer is x=(a*b)+c, because the operator * has precedence over the
operator +.
However, having said this, it is much better practice to write code that makes
extensive use of parentheses () so as to avoid errors due to Octave interpreting
your code differently to what you intended. Thus, for example, the entries in the
left and right columns below are equivalent, but it is better to write code with
parentheses as in the right column.
For example,
x=a^b*c x=(a^b)*c
x=a*b+c x=(a*b)+c
x=a/b/c x=a/(b*c)
33
2.5.8 Comments (%)
Comments can be included in Octave code using the % symbol, so everything to
the right of the % is ignored by the Octave interpreter. Comments are usually made
in .m files, and you can comment out a whole line or just part of a line. Below we
show how we might include comments in the file ex1.m (from Study Unit 1).
For example,
%Octave script to solve the matrix equation Ax=b
A=[1 2;3 4] %The matrix A
b=[5;6] %The column vector b
x=A\b %The answer x
Activity 2-15
In the following Octave code, find n! (n-factorial), with n between 1 and 10,
where
n! = 1 x 2 x 3 ... x (n-1) x n.
FEEDBACK:
In this case, you need to write the Octave code as a .m file.
File fac.m
n=10;
fact=1;s
for k=1:n
fact=k*fact;
factorials(k,:)=[k fact];
end
factorials
Ocatave window
> fac
factorials =
1 1
2 2
3 6
4 24
5 120
6 720
7 5040
8 40320
9 362880
10 3628800
Activity 2-16
Evaluate the following series:
12 + 22 + 32 + ... + 1002
FEEDBACK
Did you get the right answer? It is 338350.
Activity 2-17
Let us look again at the problem of constructing a vector, each element of which
is the square of the corresponding element in a given vector.
FEEDBACK:
We saw that the following code solves the problem.
> v=rand(1,4)
v =
0.295962 0.194619 0.078277 0.839960
> u=v.^2
u =
0.0875937 0.0378766 0.0061273 0.7055324
Alternatively, using for loops, we could instead write the last statement as
> for k=1:4,u(k)=v(k)^2;end
> u
u =
0.0875937 0.0378766 0.0061273 0.7055324
Activity 2-18
Which should you rather use, array and matrix operations, or for loops?
2.6.3 If statements
35
if (CONDITION)
statements
elseif (CONDITION)
statements
elseif (CONDITION)
statements
...
else (CONDITION)
statements
endif
> x = 1;
> if (x == 1)
> disp ("one");
> elseif (x == 2)
> disp ("two");
> else
> disp ("not one or two");
> endif
one
Activity 2-19
A bank wants to calculate the interest due to its customers and add this to their
accounts. This is complicated since the interest rate applicable depends on the
initial balance, as follows:
Initial balance Interest rate
Less than R1 000 0%
R1 000 to R5 000 5%
R5 000 to R10 000 8%
R10 000 to R20 000 9%
Above R20 000 10%
The Octave code is placed in the file bank.m, and the code is:
File bank.m
if (oldbalance < 1000)
rate =0;
elseif (oldbalance <5000)
rate=0.05;
elseif (oldbalance <10000)
rate=0.08;
elseif (oldbalance <20000)
rate=0.09;
else
rate=0.1;
endif
newbalance=oldbalance*(1+rate)
Ocatave window
36
APM1513/1
> oldbalance=30000
oldbalance = 30000
> bank
newbalance = 33000
> oldbalance=3000
oldbalance = 3000
> bank
newbalance = 3150
> oldbalance=300
oldbalance = 300
> bank
newbalance = 300
A condition is something that can be either true or false, and is of the form
a operation b
where a, b are scalars and where operation is one of the following:
If you want to test for equality, use == rather than =, for example
> if(a==b)
However, you should test for equality with caution, using it only with integers and
not with real numbers. The reason is because of round-off error. The exact result
of a calculation may be a=1 but it could well be that what is stored in the
computer is a=1+10-15, and a test if(a==1)would produce the result false. So
instead use if(abs(a-1)<10^(-10)) where abs is the absolute magnitude
function.
In mathematical language you may write something like, if (a<x<b), but you
should not write the equivalent in Octave, since there should be only one
operation in each condition expression. In Octave, you achieve the desired effect
by using the additional operators:
37
2.6.5 Conditional loops
In a “for” loop, the loop is executed a predetermined number of times. Often, you
would want to exit the loop when some condition that is a result of the calculation
being done is satisfied.
In Octave you achieve this by using while or do until loops. The general form
of the these constructs is
while(CONDITION)
statements
endwhile
and
do
statements
until(CONDITION)
The difference between the two constructs is that in the “do until” version the
loop is executed at least once.
Activity 2-20
Suppose that you go to the bank with the variable interest rate described above.
You have an amount of money to invest, and want to see how long you will have
to wait until the investment grows to a target amount. The program is in the
file ex3.m. Note, it makes use of the file bank.m .
FEEDBACK:
File ex3.m
startbalance
oldbalance=startbalance;
years=0;
while(oldbalance<targetbalance)
bank;
years=years+1
oldbalance=newbalance;
endwhile
Ocatave window
> startbalance=4100
startbalance = 4100
> targetbalance=6000
targetbalance = 6000
> ex3
startbalance = 4100
newbalance = 4305
years = 1
newbalance = 4520.3
years = 2
newbalance = 4746.3
years = 3
newbalance = 4983.6
years = 4
newbalance = 5232.8
years = 5
newbalance = 5651.4
years = 6
newbalance = 6103.5
years = 7
38
APM1513/1
Activity 2-21
∑
∞
Evaluate the following series n =1
un in which u n is not known explicitly but is
given in terms of a recurrence relation. You should stop the summation when
|u n | < 10-8 .
un +1 = (un ) with u 1 = 0.5
2
FEEDBACK
Did you get the right answer? It is 0.81642. If you have problems with this
activity, go online and discuss it with your fellow students.
2.7 Functions
The general idea of a function is that it is a piece of code that reads in some form
of data, manipulates it in some way, and produces some other form of data.
Octave has many pre-defined functions, but more importantly it allows the user to
create new functions, and proper use of this feature is essential in the development
of well-structured programs in Octave.
There are many, many predefined functions (see the Octave manual, Function
Index, pages 591-602). These functions can be applied to a scalar or to an array,
and when applied to an array the function operates on every element of the array.
Activity 2-22
Find a matrix B whose elements are the exponential function (exp) applied to
each element of a matrix A
FEEDBACK:
> A=rand(2,2)
A =
0.596494 0.308671
0.073614 0.176858
> B=exp(A)
B =
1.8157 1.3616
1.0764 1.1935
39
2.7.2 User-defined functions
You can define your own functions also. Some important points to note about
function definitions include:
• You must choose actual names for the return_name, function_name
and inputs, and the Octave statements in the function must calculate
the return_name from the inputs.
• The code defining the function must be placed in a separate .m file
called function_name.m.
• Once a function has been defined, you can call it from the Octave
command window, from an Octave script (ordinary .m file), or from
another function.
• A function can have several input slots, and the return_name can be a
scalar or an array.
• From a technical point of view, data is passed to an Octave function by
value rather than by reference, and variables used inside a function are
hidden from the calling program. This means that, even if you have
statement input1=1 the value of the variable equivalent to input1 in the
calling statement will not be reassigned to 1.
Activity 2-23
Define your own functions, using the syntax below.
SYNTAX:
function return_name=function_name(input1,input2,
...,inputn)
Octave statements that use the inputs to calculate
return_name
endfunction
Here is an example.
Activity 2-24
Find a function that computes the roots of the quadratic equation
ax 2 + bx + c = 0
using the formula
− b ± b 2 − 4ac
x=
2a .
FEEDBACK:
We create a function file quadratic.m and then call it from the Octave command
window.
File quadratic.m
function x = quadratic(a,b,c)
disc=sqrt(b^2-4*a*c);
x(1)=(-b+disc)/(2*a);
x(2)=(-b-disc)/(2*a);
endfunction
40
APM1513/1
Ocatave window
> z=quadratic(1,-5,6)
z =
3 2
> disc
error: `disc' undefined near line 21 column 1
The last statement demonstrates that, although disc must have been computed to
have value 1 during the execution of quadratic, this value is hidden from the main
Octave window.
For example,
Suppose that we want to evaluate numerically the area under the curve f(x) in the
range a < x <b, like,
b
∫a
f ( x )dx
In Octave, this is achieved using the predefined function “quad”. The “calling”
syntax is
quad(@f,a,b)
So, the “@” symbol indicates the created “handle” of the function. Let’s look at
the following activity where the code illustrates this in another example.
Activity 2-25
π /6
∫ sin( x )dx
Using a program, calculate, 0
FEEDBACK:
> quad(@sin,0,pi/6)
ans = 0.13397
Activity 2-26
Using a program, evaluate
3
dv
∫1 1 + v1.8
FEEDBACK:
The first step is to write a function file, say fv.m, that defines the integrand, and
then use quad from the command window. Note that we use array arithmetic
41
operators (./ and .^) in fv.m because (for certain later applications) it is convenient
that the function be able to operate on an array.
File fv.m
function y=fv(x)
y=1./(1+x.^1.8);
endfunction
Ocatave window
> quad(@fv,1,3)
ans = 0.50214
Finally, we look at how a function handle that is passed to another function can be
used in that function. In other words, we look at how a function like quad would
deal with the function passed to it. Actually, the internal workings of quad are
fairly complicated, so instead let us look at the problem of calculating the average
slope of a function f(x) over an interval 0< x <b.
Activity 2-27
Calculate the average slope of a function f(x) over an interval 0< x <b, defined as
f ( b) − f ( a )
b−a .
FEEDBACK:
We write a function file, “average_slope.m”, and use it to find the average slope
of the function fv defined above in the interval (1,3), as well as the average slope
of the sine function in the interval (0,π).
NOTE: In the function file, average_slope.m, the program does not permit us to
write, say, f(b); instead, we use feval(f,b).
File, average_slope.m
function x=average_slope(f,a,b)
fb=feval(f,b);
fa=feval(f,a);
x=(fb-fa)/(b-a);
endfunction
Ocatave window
> average_slope(@fv,1,3)
ans = -0.18921
> average_slope(@sin,0,pi)
ans = 3.8982e-017
2.8 Graphics
42
APM1513/1
2.8.1 Plotting functions with “fplot”
You have already, in Study Unit 1, been introduced to this command. The usual
syntax for this command is:
fplot(@function_name,[lower_limit,upper_limit])
You can also use fplot with a user-defined function, provided that the
arithmetic operators used in defining the function are array operators, i.e. .* ./ .^
etc. Do the next example, together with the graph, for the function fv, defined
above.
Activity 2-28
Plot the function fv in the range -1 <x < 1.
> fplot(@fv,[-1,1])
Activity 2-29
Plot a function in 2-dimenstions.
> x=0:0.5:3
x =
43
0.00000 0.50000 1.00000 1.50000 2.00000 2.50000
3.00000
> plot(x,sin(x))
(See the plotted graphic, next.)
You can see that the effect of plot is to plot the points
(x(1),sin(x(1))=(0,sin(0))=(0,0) up to (x(7),sin(x(7)) and then join these points by
straight lines. The above graph is not smooth, but if you use a lot of points, the
graph appears to be everywhere smooth.
For example,
> x=0:0.01:3;
> plot(x,sin(x))
44
APM1513/1
There are many options available for controlling the appearance of a graph. Here
are some of them:
• Change the default colour of the graph Give the desired colour of the
graph as a third argument to either plot or fplot. The recognized colours
are
o "b" blue
o "c" cyan
o "g" green
o "k" black
o "m" magenta
o "r" red
o "y" yellow
• Draw graphs of several functions in the same plot You can do this very
easily with both plot and fplot. All that you do is include additional
arguments specifying the extra graphs in the argument list of plot or
fplot.
• Give the graph a title Once the graph has been created, enter in the
Octave window
title("Name of graph");
• Label the axes Once the graph has been created, enter in the Octave
window
xlabel("Name of horizontal axis");
ylabel("Name of vertical axis");
• Give a legend to an curve Once the graph has been created, enter in the
Octave window
legend("Name of curve");
In the case that there are several functions on the same graph, the syntax is
legend("Name of curve1","Name of curve2");
where curve1 and curve2 refer to the order in which the curves were
defined in the plot or fplot command
The following examples use all the above features.
45
Activity 2-30
Plot the graph, give it a title, labels and add a legend.
FEEDBACK:
Type:
> plot(x,cos(x),"g",xx,sin(xx),"m")
> title("cos(x) and sin(x)");
> xlabel("x");
> ylabel("cos,sin");
> legend("cos(x)","sin(x)");
46
APM1513/1
Activity 2-31
Plot a 3-dimensional graph of the helix (x = sin(2πz), y = cos(2πz), z), with title,
labels, colours and a legend.
FEEDBACK:
Type:
> z=0:0.01:5;
> plot3(sin(2*pi*z),cos(2*pi*z),z)
> xlabel("x");
> ylabel("y");
> zlabel("z");
> title("helix")
47
Activity 2-32
Plot a 3-dimensional graph for the helix (x = sin(2πz), y = cos(2πz), z), together
with the straight line (x = z/2 – 1, y = 1 – z/3, z), with title, labels, colours and a
legend.
FEEDBACK:
Type in the following:
> z=0:0.01:5;
> plot3(sin(pi*z),cos(pi*z),z,"r",z./2-1,1-z./3,z,"g")
> legend("helix","straight line");
NOTE: you can always rotate the view of a 3-dimensional graph; just place the
mouse over the graph, hold down the left mouse button, and move the mouse.
48
APM1513/1
Activity 2-33
Use mesh to produce a 3-D graph for ...
Type:
> for a=1:20 for b=1:20
> A(a,b)=sin(0.3*a)*cos(0.3*b);
> end end
> mesh(A)
49
Activity 2-34
Plot the graph, with a mesh,
sin x 2 + y 2
z=
x2 + y2
in the range -7 ≤ x ≤ 7, -11 ≤ y ≤ 11.
FEEDBACK:
Type:
> [xx,yy]=meshgrid(-7:0.3:7,-11:0.3:11);
> zz=sin(sqrt(xx.^2+yy.^2))./sqrt(xx.^2+yy.^2);
> mesh(xx,yy,zz)
You can also obtain a contour map, or combine a surface plot with a contour map,
using the commands contour and meshc respectively. The syntax is similar to
that of mesh. As with other plotting routines, it is simple to give the plot a title and
to label the axes. In the following examples we use xx,yy,zz (as defined above).
50
APM1513/1
Activity 2-35
Plot the graph
sin x 2 + y 2
z=
x2 + y2
in the range -7 ≤ x ≤ 7, -11 ≤ y ≤ 11, with a countour map.
Type:
> contour(xx,yy,zz)
51
Activity 2-36
Plot the graph, with a mesh,
sin x 2 + y 2
z=
x2 + y2
in the range -7 ≤ x ≤ 7, -11 ≤ y ≤ 11
Type:
> meshc(xx,yy,zz)
> xlabel("x")
> ylabel("y")
> title("example of a surface and contour plot")
52
APM1513/1
One feature that will be needed later is the plotting of discrete points that are not
joined by lines. The discrete points plotted can be any of:
^ * + . o x
and the syntax is just to add the point style to the parameter list when calling plot
or fplot.
Activity 2-37
Plot discrete points in a graph that are not joined by lines.
FEEDBACK:
Type:
> fplot(@sin,[0,7.5],"*")
or
> plot(x,sin(x),"o")
53
2.9 Additional Exercises
Activity 2-38
Do the exercises below. Keep evidence of your work (copy and paste your efforts
to your workbook or make a printout from your diary file).
∑
∞
2. Evaluate the following series n =1
un in which u n is not known explicitly
but is given in terms of a recurrence relation. You should stop the
summation when |u n | < 10-8
un +1 = (un −1 ) + (un ) with u 1 = 0.5, u 2 = 0.6
2 2
The function file should have (a, b, c) as input and should return the two
solutions for x. The amendment required is that the function should check
whether a = 0 and if so it should give the solution x = -c / b; if also b = 0, it
should report an error message and exit the function without trying to
divide by zero. Show that your code is correct by testing it on the cases
a. x2 + 5 x + 6 = 0
b. 2 x + 4 = 0
c. x2 + 4 = 0
d. 0 = 0
5. Use the functions deriv1 and deriv2 constructed in question 5 to find the
first and second derivatives of
54
APM1513/1
f (x) = x exp(x2)
at x = 0.5. Evaluate both derivatives for h = 10-2, 10-3, 10-4, ... , 10-12, and
then for each value of h evaluate the error. The exact answers are
f '(x) = (2 x2 + 1) exp(x2), f ''(x) = (4 x3 + 6 x) exp(x2).
Then plot log 10 (error) against log 10 (h) (In Octave, the function log 10 is
log10). At what value of h is the error minimized?
7. A formula for the population of the USA, using the logistic model, is
197273000
P (t ) =
1 + e −0.03134 ( t −1913.25)
where t is the date in years. Some actual data is as follows
Date Population
1800 5308000
1820 9638000
1840 17069000
1870 38558000
1900 75995000
1930 122775000
1950 150697000
Plot the graph of P(t) against t as a continuous line, and the given data as
discrete circles, i.e. do not join them with lines (Discrete circles are
obtained by adding "o" to the plot parameter list).
2.10 In Summary
Now that you have completed this study unit, you can write your own code in
Octave, and use it to for numerical calculations. You now have sufficient
programming knowledge to be able to tackle interesting problems in linear
algebra, and you will start to do so in the next study unit.
55
Study Unit 3: Use of Octave to
solve linear systems of equations
Time period: 15 hours approximately
LEARNING OUTCOMES
At the end of this Study Unit, you should be able to write computer programs that:
• Solve linear systems of equations using the direct method of Gaussian
elimination
• Solve linear systems of equations using the Jacobi iterative method
• Solve linear systems of equations using the Gauss-Seidel iterative method
• Identify systems of linear equations that are problematic for the above
methods
3.1 Introduction
This Study Unit is concerned with systems of linear equations that have a unique
solution.
Activity 3-1
Suppose that we want to solve
1 x1 + 2 x2 = 5
3 x1 + 4 x2 = 6
FEEDBACK:
The Octave code is
> A=[1 2;3 4];
> b=[5;6];
> x=A\b
x =
-4.0000
4.5000
The above constructs can be used for any number of equations and unknowns
(provided, of course, that the number of equations is the same as the number of
unknowns).
Iteration techniques are usually very easy to apply and can best be illustrated by
examples.
Activity 3-2
Solve the following system of equations:
20 x1 + x2 − x3 = 17
x1 − 10 x 2 + x3 = 13
− x1 + x 2 + 10 x3 = 18. (3.2.1)
FEEDBACK:
Before solving the system let us make two obvious observations:
(i) The system (3.2.1) can be solved very easily by Gaussian elimination to
yield the unique solution ( x1 , x 2 , x3 ) = (1,−1,2 ) but the purpose of the
examples is to illustrate a technique.
(ii) You will notice that, from a numerical point of view, the coefficient matrix
of (3.2.1) viz
20 1 − 1
1 − 10 1
− 1 1 10
is dominated by the diagonal entries in that their absolute values are much larger
than the absolute values of the other entries.
Solution
We shall use two different iterative techniques and we begin with the Jacobi
method. The first step is to ‘solve’ the first equation in (3.2.1) for x1 , the second
for x 2 and the third for x3 . In fact, we don't really solve the equations, we merely
re-write them in the form
57
17 x2 x3
x1 = − +
20 20 20
13 x x
x2 = − + 1 + 3 (3.2.2)
10 10 10
18 x1 x2
x3 = + − .
10 10 10
The second step is to assume that an approximate solution to the system is
(x ( ) ,x ( ) , x ( ) ) = (0,0,0).
1
0
2
0
3
0
The next step is to substitute the initial approximation (0, 0, 0) in (3.2.2) and we
obtain the first approximate solution
(1) 17
x1 = = 0.85
20
(1) 13
x 2 = − = −1.3 (3.2.3)
10
(1) 18
x3 = = 1.8.
10
The first approximation (0.85, − 1.3, 1.8) is better than (0, 0, 0) and it is
obvious that to obtain an even better approximation, we simply repeat the
procedure with the solution (3.2.3) i.e. we iterate. If we substitute (3.2.3) in (3.2.2)
we obtain the second approximation
(2 ) 17 1.3 1.8
x1 = + + = 1.005
20 20 20
(2 ) 13 0.85 1.8
x2 = − + + = −1.035
10 10 10
(2 ) 18 0.85 1.3
x3 = + + = 2.015.
10 10 10
A slight variation of the Jacobi method is the so-called Gauss-Seidel method. The
Gauss-Seidel method is usually (but not always) better than the Jacobi method and
the only difference is that as better approximations become available, so we make
immediate use of them in the next equation. (It's actually much easier than it
sounds.)
58
APM1513/1
(
As before, our initial approximation is x1 , x 2 , x3 = (0, 0, 0 ) and if we
(0 ) (0 ) (0 )
)
substitute this in (3.2.2) then the first equation yields
(1) 17 0 0
x1 = − + = 0.85.
20 20 20
For x 2 (1) , however, we do not put
(1) 13 0 0
x2 = − + +
10 10 10
(1)
but instead we use the value x1 = 0.85 which we have just obtained from the
first equation. In other words,
(1) 13 0.85 0
x2 = − + + = −1.215
10 10 10
(0 )
and we note that we have to use the value x3 = 0 in this equation because at this
(1)
stage, this is all that we know about x3 . We now go ahead and compute x3 from
(1) (1)
(3.2.2) using the values x1 and x 2
as given by immediately above, i.e.
(1) 18 0.85 1.215
x3 = + + = 2.0065.
10 10 10
We now do a second iteration in precisely the same way to obtain
17 1.215 2.0065
(2 )
x1 + =
+ = 1.0111.
20 20 20
The second and third equations of (3.2.2) yield respectively
(2 ) 13 1.0111 2.0065
x2 = − + + = −0.99824.
10 10 10
and
(2 ) 18 1.0111 0.99824
x3 = + + = 2.0009.
10 10 10
Octave program
We give here the Octave code for the Gauss-Seidel method. It is easy to modify
this code so that it implements the Jacobi method instead, and that is left as an
exercise for the student. The code is written as a function file. The user gives to
the function the matrix A, the right hand side b, and the current estimate for the
solution xold. The function performs one iteration of the Gauss-Seidel method
returning the new estimate for the solution. Note the use of the temporary matrix
At which has zeros in its diagonal; this can be avoided, but then the main loop
defining xnew(k) would be rather more complicated. In the Octave window, we
define A, b, and the initial estimate of the solution for the problem stated above,
59
and then use a for loop to make a fixed number (in this case, five) of iterations of
the Gauss-Seidel method.
Activity 3-3
Create the m-file (below) and run it in Octave.
FEEDBACK:
File gauss_seidel.m
function xnew=gauss_seidel(A,b,xold)
n=size(A)(1);
At=A;
xnew=xold;
for k=1:n
At(k,k)=0;
end
for k=1:n
xnew(k)=(b(k)-At(k,:)*xnew)/A(k,k);
end
endfunction
Ocatave window
> A=[20 1 -1;1 -10 1;-1 1 10];
> b=[17 13 18]';
> x(:,1)=[0 0 0]';
> for k=1:5 x(:,k+1)=gauss_seidel(A,b,x(:,k)); end;
> x
ans =
x =
0 3 1 3 1 3 1
0 2 -0 2 -0 2 -0
Thus the result of iteration is that we go around in circles. In other words, this
system cannot be solved by an iteration process and the question automatically
arises: when does a system admit a convergent iterative solution? Incidentally,
60
APM1513/1
note that a solution converges if, after sufficiently many iterations, the exact and
approximate solutions differ by as small an amount as we wish.
In this case it is easy to state the answer but the proof is beyond the scope of this
course. Let us write the coefficient matrix of a system of equations in the usual
notation (as referred to in MAT103N), that is:
a11 a12
A=
a n1 a n 2 a nn
Note that since we are assuming unique solutions throughout this Study Unit, the
coefficient matrix must be square and of rank n. The matrix A is said to be strictly
diagonally dominant if the absolute value of each diagonal entry is greater than
the sum of the absolute values of the remaining entries in the same row, for
example:
However, the conditions on these matrices are too technical for this course and for
our purposes we shall regard the condition as necessary as well as sufficient.
There is one trivial exception to this which we discuss in the next example.
Activity 3-4
Consider the system
x1 + 3x 2 − x3 = 6
4 x1 − x 2 + x3 = 5
x1 + x 2 − 7 x3 = −9.
FEEDBACK:
Can one use iterative techniques to solve this system? Clearly, the coefficient
matrix is NOT strictly diagonally dominant since the first two equations do not
satisfy the required condition. However, we know that we can interchange the first
61
two equations without affecting the solution to the system and if we do this, we
obtain a coefficient matrix which IS strictly diagonally dominant, that is:
4 − 1 1
1 3 − 1 .
1 1 − 7
ans =
0.00000 1.25000 1.22321 1.34683 1.33916 1.34108 1.34088
0.00000 1.58333 2.15575 2.14053 2.14825 2.14761 2.14774
0.00000 1.69048 1.76842 1.78391 1.78392 1.78410 1.78409
The question which you are probably asking yourself at this point is: When does
one stop? One way would be to simply limit the number of iterations to 5 or 10 or
whatever. The problem here is that one doesn't know how fast the approximations
are converging and after a considerable number of iterations one might still be
quite a long way from a reasonable solution. A better way would be to place a
restriction on the relative error. The difficulty here is that one doesn't know the
exact solution - if one did, there wouldn't be much point in finding an approximate
solution.
for i = 1, 2, 3. We have
62
APM1513/1
(4 ) (3 )
x1 − x1 1.3392 − 1.3469
(4 ) = = 0.0057497
x1 1.3392
(4 ) (3 )
x2 − x2 2.1482 − 2.1405
(4 ) = = 0.0035843 .
x2 2.1482
(4 ) (3 )
x3 − x3 1.7839 − 1.7839
(4 ) = = 0.
x3 1.7839
If we were required to iterate until all relative errors were less than, say 0.005,
then we see that we would have to do at least one more iteration, and find ∈r (5 ) .
Octave code with a stopping condition that depends upon the relative error uses
the until construct. We write the code as a function file with inputs A, b (as
usual); the initial estimate of the solution xinitial; the maximum permitted
value for the relative error TOL; the maximum number of permitted iterations
max_it; and the method to be used for obtaining an iterative solution method,
which will be either gauss_seidel or jacobi. As we saw earlier, the iterative
method may not converge, so one needs to specify a maximum number of
iterations otherwise the program will never exit the conditional loop; recall that
when evaluating a conditional statement | means or. The function returns the
solution, and reports the number of iterations used, giving an error message if this
number exceeds max_it. In the Octave window, we set A, b, xinitial; and
then call iterative_linear_solve using the function handle construct @ to
specify the method. We run the program for two cases discussed earlier, one in
which convergence occurs and the other in which it does not.
Activity 3-5
Create the m-file (below) and run it in Octave.
File iterative_linear_solve.m
function
xnew=iterative_linear_solve(A,b,xinitial,TOL,max_it,method)
xold=xinitial;
k=0;
do
xnew=feval(method,A,b,xold);
err=max(abs((xnew-xold)./xnew));
xold=xnew;
k=k+1;
until((err<TOL) | (k>max_it));
k
if (k>max_it)
disp("ERROR: METHOD DID NOT CONVERGE");
xnew=[];
endif
endfunction
Ocatave window
63
> A=[20 1 -1;1 -10 1;-1 1 10];
> b=[17 13 18]';
> x0=[0 0 0]';
> y=iterative_linear_solve(A,b,x0,1/10^5,5,@gauss_seidel)
k = 5
y =
1.0000
-1.0000
2.0000
Activity 3-6
Solve the following systems of equations, using the A\b construct, as well as the
Gauss-Seidel method with a tolerance of 10-7
20 x 1 - x 2 + x 3 = 20
2 x 1 + 10 x 2 - x 3 = 11
x 1 + x 2 - 20 x 3 = -18
In Octave, it is very easy to check these conditions using the pre-defined functions
det and rank.
Activity 3-7
Check these conditions in Octave using the pre-defined functions det and rank.
A =
2 3
6 9
> det(A)
ans = 0
> rank(A)
ans = 1
64
APM1513/1
Even though the matrix is singular and a unique solution does not exist, Octave
nevertheless will construct a solution (with a warning message). For example,
with A defined as above,
> b=[2 2]';
> A\b
warning: matrix singular to machine precision, rcond = 0
warning: attempting to find minimum norm solution
ans =
1.9725e+015
-1.3150e+015
If you see such a warning message, you should check the rank or determinant of
A, and note that the solution given by Octave is not unique and may not be a
solution at all.
Activity 3-8
Consider the following examples
1) 1000 x 1 + 2000 x 2 = 5000
2) 1000 x 1 + 2000.001 x 2 = 6000
ACTIVITY
> A1=eye(6)/10^5
A1 =
1.0000e-005 0.0000e+000 0.0000e+000 0.0000e+000 0.0000e+000 0.0000e+000
0.0000e+000 1.0000e-005 0.0000e+000 0.0000e+000 0.0000e+000 0.0000e+000
0.0000e+000 0.0000e+000 1.0000e-005 0.0000e+000 0.0000e+000 0.0000e+000
0.0000e+000 0.0000e+000 0.0000e+000 1.0000e-005 0.0000e+000 0.0000e+000
0.0000e+000 0.0000e+000 0.0000e+000 0.0000e+000 1.0000e-005 0.0000e+000
0.0000e+000 0.0000e+000 0.0000e+000 0.0000e+000 0.0000e+000 1.0000e-005
> cond(A1)
ans = 1
65
> A2=ones(6,6)
A2 =
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
> cond(A2)
ans = Inf
> A3=A2+rand(6,6)/10^5;
> cond(A3)
ans = 5.9902e+006
1000.00000000000 2000.00000000000
1000.00000000000 2000.00100000000
> det(A4)
ans = 0.999999999976464
> cond(A4)
ans = 10000004.0000005
You can use the condition number to estimate the accuracy at which Octave
solves for x in A x = b. First, we define the residual which is A x - b. Normally,
when we are using a direct method with the A\b construct, the residual is of the
order of machine precision (which in Octave is the variable eps, and is usually
about 2×10-16), but for an iterative method it depends on the tolerance chosen and
can be much larger. Then the accuracy at which we determine x will be no better
than the residual multiplied by the condition number.
1. Solve the following systems of equations, using the A\b construct, as well as
the Gauss-Seidel method with a tolerance of 10-7 (in some cases convergence
may not occur)
a. 2 x1 - x2 + 3 x3 = 8
4 x 1 + 2 x 2 - 5 x 3 = -9
6 x 1 + 3 x 2 + x 3 = 12
c. 10 x 1 + x 2 + 2 x 3 = 3
x 1 + 10 x 2 - x 3 = 1.5
2 x 1 + x 2 + 10 x 3 = -9
d. 12 x 1 - 3 x 2 + 4 x 3 - 2 x 4 = 12
2 x 1 + 10 x 2 - x 3 - 20 x 4 = 15
x 1 - x 2 + 20 x 3 + 4 x 4 = -7
66
APM1513/1
x 1 + x 2 - 20 x 3 - 3 x 4 = -5
e. 10 x 1 - x 2 + 7 x 3 - 18 x 4 = 2
2 x 1 + 10 x 2 – 4 x 3 - 20 x 4 = 1
-1.5 x 1 + 6 x 2 - 20 x 3 - 2 x 4 = 9
2 x 1 + 5 x 2 - 2 x 3 + 30 x 4 = -18
3. Write a function file that takes as input a matrix A, and tests whether or not the
matrix is (a) square, and (b) diagonally dominant, reporting the answers on the
screen. Show that your code is correct by testing it for the matrix in question
2, as well as for cases where the matrix is not diagonally dominant, and not
square.
In practice, this alternative stopping method is not often used. Why not?
1
H ij( n ) =
i + j −1
6. Define the 100 × 100 square matrix A and the column vector b by
1 2
Aij = I ij + , bi = 1 + , 1 ≤ i, j ≤ 100
(i − j ) + 1
2
i
where I ij is the 100 × 100 identity matrix (i.e. 1 on the main diagonal and 0
everywhere else). Solve A x = b for x using both the Gauss-Seidel method and the
A\b construct. Do not give the whole vector x in your output, but only x 2 , x 50 and
x 99 .
67
3.7 In conclusion
You have now used Octave to solve linear systems of equations, including large
systems that are impossible to solve by hand. In these systems, the number of
equations was equal to the number of unknowns, but what can be done if that is
not the case? We will look at this question in the next study unit.
68
APM1513/1
LEARNING OUTCOMES
At the end of this Study Unit, you should be able to write Octave programs that
• Determine whether a system of equations has a unique solution, or
whether it is overdetermined or underdetermined
• Use Octave to find the best "least squares" approximation to an
overdetermined system
• Use Octave to find parameters that give the best "least squares" fit of a
given form of curve to given data
• Use Octave to find the general solution of an underdetermined system
Ax=b
the matrix A has m rows and n columns, x is a column vector with n rows, and b is
a column vector with m rows. The following situations can arise.
1 1 2
x =
1 1 2
1 1 2
x =
1 1 1
69
which has no solutions.
1 1 2
2 3 x = 5
3 3 6
which has the unique solution, x 1 = 1, x 2 = 1; or even that there are fewer than n
independent equations so that there are infinitely many solutions, as in
1 1 2
2 2x = 4
3 3 6
1 2 1 4
x =
1 1 0 2
1 2 1 4
x =
2 4 2 2
70
APM1513/1
Activity 4-1
A car is undergoing uniform acceleration. Measurements have been made of the
car's displacement (s) from the origin against time (t) as follows:
t s
1 6.7118
2 10.3049
3 16.1415
4 23.9482
5 33.8839
6 45.9405
7 60.1468
8 76.3638
9 94.6527
10 115.1413
Use the above data to find the best estimate of the constants s 0 , u 0 and a.
We will solve the example later, but for now, we are interested in issues that are
more general. Because of measurement inaccuracies, it makes sense that that there
will not be values of s 0 , u 0 and a, that exactly fit all the data points, and that a
good approximate solution is all that can be expected. However, that still leaves
the question as to what, precisely, is meant by a good approximate solution? We
are no longer trying to solve A x = b but instead we are seeking a column vector x *
such that the error in the approximation (also called the residual) ε is as small as
possible, where
ε = (A x * – b)
In Octave, the norm of x is found by the call norm(x). So, we want to find a
vector x * such that the value of | ε | is minimized. Minimization is a simple
problem in calculus, and also in this case is easy to work out. However, it requires
applying calculus to vectors and matrices, which is a second level topic so here
we just state the result. The condition that | ε | is minimized leads to the normal
equations
A' A x * = A' b
71
where A' is the transpose of A (which was defined both mathematically and in
Octave in Study Unit 2). The matrix (A' A) is a square n × n matrix and (A' b) is a
n-vector, so (provided det(A' A) ≠ 0) there is a unique solution for x * .
Here is a simple example on the use of the normal equations in Octave for you to
work through.
Activity 4-2
Solve the system of equations
x1 + 2 x2 = 0
3 x1 - x2 = 4
2 x1 + x2 = 1
Octave window
> A=[1 2;3 -1;2 1]
A =
1 2
3 -1
2 1
> b=[0;4;1]
b =
0
4
1
> At=A'
At =
1 3 2
2 -1 1
> An=At*A
An =
14 1
1 6
> det(An)
ans = 83.000
> bn=At*b
bn =
14
-3
> xn=An\bn
72
APM1513/1
xn =
1.04819
-0.67470
> xs=A\b
xs =
1.04819
-0.67470
> err=A*xs-b
err =
-0.30120
-0.18072
0.42169
So, here we have also used Octave to work out the residual (as err).
It is important to note that, in the above example, Octave just gave the answer to
A\b as the solution of the normal equations without any error or warning message.
So, if you use Octave to solve a system of equations, do not assume that the
answer you get is the exact solution until you check that the residual (A*xs-b) is
zero, or at least very small.
Also, note that it is important to check, as we did in the above example, whether
or not the determinant of the normal matrix (A' A) is zero. While the procedure of
transforming to normal form ensures that the normal equations are consistent, if
the determinant is zero the solution found by Octave is not unique and there will
be infinitely many solutions, as described in the next .
Activity 4-3
Find the solution to Activity 4-1
FEEDBACK:
We take the data that we are given and substitute it in to the assumed equation.
We get:
t = 1: s 0 + u 0 1 + a 0.5 = 6.7118
t = 2: s 0 + u 0 2 + a 2 = 10.3049
......
t = 10: s 0 + u 0 10 + a 50 = 115.1413
73
1 1 0.5 6.7118
s 0
1 2 2 10.3049
u0 =
a
1 10 50 115.1413
The Octave code to solve the problem is quite straightforward. First, we type in
the given data as a column vector b; and then generate the matrix A. Look
carefully at how A is constructed, because you do something similar in all
problems in which you try to estimate parameters by matching a given formula to
given data. Then we check that the determinant of A'A is non-zero, and solve the
problem using the A\b construct. We find:
xs(1) = s 0 = 5.13361
xs(2) = u 0 = 0.50944
xs(3) = a = 2.09808
We plot the given data points (as *), as well as the function s = s 0 + u 0 t + ½ a t2
with the parameters having the values just determined, as a continuous blue line.
Finally, we work out the residual, as err. We see in the graph that, in this case,
we have been able to construct a curve that fits the given data very well.
> b=[6.7118 10.3049 16.1415 23.9482 33.8839 45.9405 60.1468 ...
76.3638 94.6527 115.1413]';
> t=[1:10]';
> A=[ones(10,1) t t.^2/2]; end;
> det(A'*A)
ans = 1.0890e+005
> xs=A\b
xs =
5.13361
0.50944
2.09808
> tt=1:0.1:10;
> s=xs(1)+t*xs(2)+xs(3)*t.^2/2;
> plot(t,b,"*r",tt,s,"b")
> err=A*xs-b
err =
-0.0197373
0.0437355
-0.0382168
0.0078059
0.0229036
0.0151764
-0.0441759
-0.0161532
0.0380445
-0.0093827
74
APM1513/1
In this case, the given curve fit the data quite well. Following below is another
example of curve fitting, where the curve does not provide a particularly good fit.
Activity 4-4
Fit a curve of the form
y = a sin(x) + c
to the data:
x y
1.23 1.8934
1.98 1.9721
2.47 1.4022
5.64 0.2967
FEEDBACK:
Octave code
> x=[1.23 1.98 2.27 5.64]';
> b=[1.8934 1.9721 1.4022 0.2967]';
> A=[sin(x) ones(4,1)];
> xs=A\b
xs =
1.01307
0.87810
> det(A'*A)
ans = 6.5990
> err=A*xs-b
err =
-0.060496
-0.164574
0.251255
-0.026185
> xx=1.2:0.1:5.7;
> plot(x,b,"*r",xx,xs(1)*sin(xx)+xs(2),"b")
75
We see here that the curve produced does not fit the given data points particularly
well.
Activity 4-5
Find the unknowns in the following underdetermined system.
FEEDBACK:
> A=rand(3,6)
A =
0.890721 0.161542 0.413566 0.822816 0.132409 0.051145
0.596242 0.327903 0.573553 0.478045 0.095813 0.322198
0.566379 0.054946 0.522928 0.824055 0.903582 0.407719
> b=rand(3,1)
b =
0.266035
0.134006
0.067834
> x0=A\b
x0 =
0.1970789
0.0050822
-0.0092682
0.1400378
-0.1219441
-0.1089772
> err=A*x0-b
err =
-2.2204e-016
-1.1102e-016
-2.7756e-016
76
APM1513/1
The last statement evaluates the residual, as err, confirming that we have indeed
found a solution of the problem. But is it the only solution? Theory tells us that if
there are m equations in n unknowns with m < n, then in the general case we
would expect a general solution to involve n – m arbitrary parameters; so in this
example we would expect three arbitrary parameters. We look for the additional
solutions using a technique that is quite common in many areas of applied
mathematics. We write the general solutions as
x = x 0 + ∑ α i pi
i
where x 0 is the particular solution already found, α i are the arbitrary parameters,
and p i are n-vectors that we wish to find. Applying the equation A x = b, we
immediately see that for each i the p i satisfy
A p i = 0.
This means that, in the language of linear algebra, we are looking for the null
space of A. In this module, we will not say any more about the determination of
the null space. In Octave, finding the null space is trivial: we just use the pre-
defined function null, as shown below. The answer is returned as a matrix, the
columns of which are the vectors p i that we are seeking.
> N=null(A)
N =
-0.582073 0.271316 0.200055
0.347654 0.687301 -0.360124
-0.170757 -0.584404 -0.441237
0.676688 -0.181640 0.069560
-0.213805 0.280840 -0.251502
0.086892 0.024746 0.753331
> A*N(:,1)
ans =
1.1102e-016
0.0000e+000
1.1102e-016
> A*N(:,2)
ans =
-5.5511e-017
1.5266e-016
2.7756e-016
> A*N(:,3)
ans =
1.3878e-017
-6.9389e-018
1.5266e-016
In the above code, we applied the matrix A to each column of N, and confirmed
that it is indeed in the null space of A.
Activity 4-6
Solve,
x1 + 2 x2 + 3 x3 = 1
4 x 1 + 5 x 2 + 6 x 3 = 2.
77
Octave code
> A=[ 1 2 3;4 5 6]
> b=[1 2]'
> A\b
ans =
-0.055556
0.111111
0.277778
> null(A)
ans =
0.40825
-0.81650
0.40825
Activity 4-7
Solve:
1 2 1 4
x =
2 4 2 2
Octave code
> A=[1 2 1;2 4 2];
> b=[4;2];
> x=A\b
78
APM1513/1
warning: dgelsd: rank deficient 2x3 matrix, rank = 1
x =
0.26667
0.53333
0.26667
> null(A)
ans =
0.91287 0.00000
-0.36515 -0.44721
-0.18257 0.89443
Octave gives a warning that there may be a problem, and reports the rank of the
matrix A. Here the rank is 1 which is less than the number of rows (2). In general,
if the rank is less than the number of rows, the system may be inconsistent. What
Octave does in such a case is to construct and solve the normal equations; in other
words, the particular solution found is not an exact solution to the problem but is
the best approximate solution. In such cases, we should check the residual, to see
by how much the solution fails to satisfy the given system of equations.
Here we find:
> err=A*x-b
err =
-2.4000
1.2000
2. Find the cubic polynomial that best fits the data points
(x, y) = (-1, 14), (0, -5), (1, -4), (2, 1), (3, 22).
Produce a graph showing the polynomial, together with the given data points
as discrete points.
3. The sales figures for a business are as follows for the first six months of the
year:
R40 000, R44 000, R52 000, R64 000, R80 000, R84 000.
The owner believes that the sales curve can be approximated by a quadratic
function. Find the best quadratic fit to the data, and use it to estimate the
projected sales for the rest of the year.
79
1950 150697000
Find values of P 0 and a that give a best fit of the formula to the data. Produce
a graph showing the function P(t) against time as a continuous line, together
with the given data points as discrete points
4.6 In conclusion
You have now used Octave to solve linear systems in which the numbers of
unknowns and equations are not the same, and you have applied this to the
problem (which is found in many areas of science) of fitting a given curve to
given data.
80
APM1513/1
Learning outcomes
At the end of this Study Unit, you should be able to write programs that:
• Find the eigenvalues and eigenvectors of a square matrix A using the
program function eig.
• Find, when it exists, a matrix P that diagonalizes A.
• Use the power method to find the dominant eigenvalue and corresponding
eigenvector of A.
Some of this knowledge you might have already encountered in MAT103N. More
detail will also follow in MAT211R. In this module the focus is a statement
(without proof), for mathematical background.
Definition
Given an n × n matrix A, λ and v (≠ 0) are said to be an eigenvalue and
associated eigenvector if
Av=λv
81
given in normalized form, which means that the vector v is rescaled so that it is a
unit vector. Explicitly, the construction is
(In Octave, for a vector v, |v| is coded as norm(v)). Note however that even a
normalized eigenvector is not uniquely defined: if it is real, it is not unique up to
multiplication by -1, and if it is complex up to multiplication by any complex
number c with |c|=1.
• In the general case, there are no repeated roots and there are n distinct
eigenvalues. It can then be shown that the corresponding eigenvectors
form a complete set of linearly independent vectors. This means that any
n-vector x can be expressed uniquely as a linear sum of eigenvectors,
n
x= ∑
i =1
αi vi
• Now suppose that the characteristic equation has repeated roots, and that
there are r distinct eigenvalues with r < n. Suppose that there are m
linearly independent eigenvectors, then m will be somewhere in the range
r ≤ m ≤ n. If m = n then the set of eigenvectors is complete, and if m < n
we say that the set of eigenvectors is degenerate.
L1 =
1 0 0
0 3 0
0 0 4
82
APM1513/1
We see that the diagonal entries of L are the eigenvalues, and the columns of P are
the corresponding normalized eigenvectors. Here are some more examples
L2 =
-3 2
-2 1
> rank(P3)
ans = 2
> cond(P3)
ans = 6.2649e+008
λ2 + 2λ + 1 = 0
1 1
v=
2 1
83
small). Thus it shows that the rank of the eigenvector matrix is 2 when the
correct value is 1. However, the large condition number found for the matrix
of eigenvectors indicates that the value found for the rank may not be reliable.
1 -3 3
3 -5 3
6 -6 4
L4 =
4.00000 0.00000 0.00000
0.00000 -2.00000 0.00000
0.00000 0.00000 -2.00000
> cond(P4)
ans = 4.1103
> inv(P4)*A4*P4
ans =
84
APM1513/1
problems is the so-called dominant eigenvalue i.e. the eigenvalue whose absolute
value is larger than the absolute values of all the other eigenvalues. The dominant
eigenvalue is particularly important in circumstances where we are interested in
the behaviour of x p = Ap x 0 for large p.
Now, we describe the algorithm for the power method, and then explain why it
works.
5.4.1 Algorithm
xi x
εi = − i −1 .
xi xi −1
Our estimate of the relative error is the norm of the difference between successive
estimates of x i normalized. We continue the computation until ε i is less than a
given tolerance, at, say, i = p. Then we estimate the dominant eigenvalue as λ =
μ p , and the normalized dominant eigenvector as v = x p / | x p |.
5.4.2 Justification/why
We suppose that A has a complete set of eigenvectors, so that the initial estimate
x 0 can be written as
x 0 = α 1 v 1 + α 2 v 2 + ... + α n v n
85
where v 1 ... v n are the eigenvectors, and suppose that λ 1 ... λ n are the
corresponding eigenvalues.
λ 2 λ3 λ
, ,..., n < 1 .
λ1 λ1 λ1
Ap x 0 = α 1 λ 1 p v 1 + α 2 λ 2 p v 2 + ... + α n λ n p v n
p p
α λ α λ
= α1 λ1 p
(v 1 + 2 2 v 2 + 3 3 v 3 + ... +
α1 λ1 α1 λ1
p
α n λ n
v n ).
α1 λ1
p p p
λ λ λn
Now as p → ∞, 2 , 3 , ... , → 0, so that
λ1 λ1 λ1
Ap x 0 → α 1 λ 1 p v 1 ,
as required.
Usually the power method converges quite rapidly, although there are exceptions
• If x 0 is chosen such that α 1 = 0, then the method cannot converge to v 1 . In
order to avoid this possibility, we usually set x 0 randomly, i.e. using
Octave's random function, and run the power method for two different
initial vectors x 0 .
• If the largest eigenvalue of A is complex, then its complex conjugate is
also an eigenvalue. In this case, although λ 1 ≠ λ 2 , |λ 1 | = |λ 2 | and the power
method will probably fail.
• If the largest eigenvalue of A is a multiple root of the characteristic
equation, with or without degenerate eigenvectors, then the power method
will probably fail.
• Note, however, that although the theorem guarantees convergence only if
there is a complete set of eigenvectors, in practice convergence usually
does occur in the case that there are degenerate eigenvectors provided the
associated eigenvalue has an absolute value strictly less than that of the
largest eigenvalue.
We now give a function file power_method.m that implements the power method,
using a conditional loop that exits once the relative error is below a given
tolerance, or the maximum number of iterations has been exceeded. The inputs to
the function are the matrix A, the tolerance TOL, and the maximum number of
iterations max_it.
File power_method.m
86
APM1513/1
function [e_vec lam]=power_method(A,TOL,max_it)
k=0;
n=size(A)(1);
e_vec_old=rand(n,1);
do
e_vec_new=A*e_vec_old;
lam=(e_vec_new'*e_vec_old)/(e_vec_old'*e_vec_old);
err=norm(e_vec_new/norm(e_vec_new)-
e_vec_old/norm(e_vec_old));
e_vec_old=e_vec_new;
k=k+1;
until((err<TOL) | (k>max_it));
k
e_vec=e_vec_new/norm(e_vec_new);
if (k>max_it)
disp("ERROR: METHOD DID NOT CONVERGE");
e_vec=[];
lam=[];
endif
endfunction
The following extract from the Octave command window shows some examples
of the use, and failure, of the power method. First, we apply the method to the
various matrices A1 to A4 specified in Section 2.
87
Feedback: Octave window output
> [P L]=power_method(A1,1/10^6,100)
k = 38
P =
0.57735
-0.57735
0.57735
L = 4.0000
> [P L]=power_method(A2,1/10^6,100)
k = 8
P =
0.60969
0.61906
0.49502
L = 1.4149
> [P L]=power_method(A3,1/10^6,100)
k = 101
ERROR: METHOD DID NOT CONVERGE
P = [](0x0)
L = [](0x0)
> [P L]=power_method(A4,1/10^6,100)
k = 20
P =
0.40825
0.40825
0.81650
L = 4.0000
> A5=rand(1000,1000);
> [P L]=power_method(A5,1/10^6,100);
k = 5
> L
L = 499.93
> [P L]=eig(A5);
> L(1,1)
ans = 499.93
In the last example we generated a large (1000 × 1000) random matrix, and we
see that both eig and power_method give the same result for the largest
eigenvalue. Also, if you run the program, you will see that eig takes much more
time than power_method. Note that, because we used a random generator, if you
run the program you would get different numerical values.
88
APM1513/1
-0.087144 0.606003 2.977860 -0.140473 -0.750938
0.212440 -2.477599 0.980236 4.233562 -1.207581
-0.136646 -1.168924 0.453692 0.915245 1.712964
3. Modify the power method so that it finds the smallest eigenvalue and
corresponding eigenvector. You do this by evaluating
x n+1 = A-1 x n
rather than x n+1 = A x n . When convergence occurs, it is to the eigenvector
corresponding to the smallest eigenvalue, and to the inverse of the smallest
eigenvalue. Demonstrate the validity of your code by running it on a number
of test cases.
4. Consider a fictional species, and suppose that the population can be divided
into three different age groups: babies, juveniles and adults. Let the population
in year n in each of these groups be
xb ( n )
x (n) = x j ( n )
x
a(n)
The population changes from one year to the next according to x (n+1) = A x (n) ,
where the matrix A is
12 5 3
A = 12 0 0
0 2 0
3
In the long term, what will be the relative distribution of the population
amongst the age groups?
89
5. spread of an infectious disease in a town can be modeled (when the number of
infected people is much less than the total population) as x (n+1) = A x (n) ,
where n refers to the month, and
xs(n)
xm( n )
x (n) =
x
r(n)
x
d (n)
The disease is spread by mosquito bites, and x m(n) refers to the number of
mosquitoes that carry the disease; x s(n) is the number of sick people; x r(n) is the
number of people who recover; and x d(n) is the number of people who die. The
matrix A is
0.7 0.8 0 0
1.6 0 0.1 0
A=
0.2 0 0 0
0 . 1 0 0 0
In the long term, at what rate will the incidence of the disease increase every
month? Consequently as a public health initiative, the mosquito breeding areas
are being sprayed with insecticide, and the matrix A changes to B where
0.7 0.3 0 0
0.6 0 0.1 0
B=
0.2 0 0 0
0.1 0 0 0
Show that the effect will be that the disease will be eradicated
5.6 In conclusion
You have now successfully used Octave to find the eigenvalues and eigenvectors
of a matrix, and applied this to some real-world problems.
90
APM1513/1
LEARNING OUTCOMES
At the end of this Study Unit, you should be able to:
• Understand the basic concepts of linear programming.
• Use the Octave package glpk to solve problems in linear programming
• Express realistic problems in mathematical terms, and then use the Octave
package glpk to solve them
The focus here is the solving of the linear programming problems with some
touching on realistic problems.
Example 6.1.1
Maximize the function
L = 40 x 1 + 60 x 2
91
Fig 6.1.1
However, there are geometric results that will be useful. The following are easy to
show when there are two variables, and in fact hold whatever the number of
variables (but we will not prove this)
• The maximum or minimum of the objective function occurs at one of the
extreme points (i.e. corners or vertices) of the feasible region
• The feasible region is convex (which means that any straight line between
two points in the region lies entirely within the region)
The consequence of convexity is that we do not have to worry about local minima
or maxima: if an extreme point is maximum (or minimum) relative to
neighbouring extreme points, then it is the solution to the problem. For example,
suppose we want to maximize L = x 2 over the shaded region in Fig. 6.1.2. In both
cases (a) and (b), this occurs at the point D; however, in case (a) the region is non-
convex and there is another local maximum at the point B, whereas in case (b) the
region is convex and there are no other local maxima.
92
APM1513/1
Fig. 6.1.2
In addition, we will assume (as in Example 6.1.1) that all variables are non-
negative, i.e.
xi ≥ 0, i = 1 n
The reasons for doing so are that, in practice, the variables usually represent
quantities like production runs which cannot be negative, and also making the
assumption does simplify matters.
You might think that the simplest way to tackle a linear programming problem,
given the power of modern computers, is just to find all the extreme points (which
are just solutions of linear systems of equations), and then evaluate the objective
function at these points. However, for a large system, that problem is far too
complex. For example, if there are 50 variables and 100 constraints, the number of
points that must be examined is
100!
≈ 10 29
50! 50!
Modern computers run at about 109 operations per second, so the above
computation would take much longer than the age of the Universe. So instead, an
iterative approach is used.
93
be infinite, but in practice that would mean that the constraints have not been
completely specified.
After this summary/background, lets see how we can use a program to solve such
problems.
40
60
2 1
1 1
1 3
> b=[70;40;90]
b =
70
40
90
> lb=[]
lb = [](0x0)
> ub=[]
ub = [](0x0)
> ctype="UUU"
ctype = UUU
> vartype="CC"
vartype = CC
> s=-1
s = -1
> [xmax,Lmax]=glpk(C,A,b,lb,ub,ctype,vartype,s)
xmax =
15.000
25.000
Lmax = 2100
We now examine the above code and describe how it relates to the mathematical
specification of the problem.
• C is a column vector such that L=Cx' (with x a column vector, x' a row
vector) is the function to be minimized or maximized
94
APM1513/1
• A is a matrix such that Ax expresses the left hand side of the constraints
• b is a column vector that specifies the right hand side of the constraints
• lb is a column vector that specifies the lower bound of each element in x.
If this lower bound is always 0, then, according to the Octave manual, we
do not need to specify lb and simply define it as the empty matrix.
However, in some cases we have found that Octave is unstable when lb is
not set explicitly to 0
• ub is a column vector that specifies the upper bound of each element in x.
If this upper bound is always infinity, then we do not need to specify ub
and simply define it as the empty matrix
• ctype is a string of characters containing the sense of each constraint in
the constraint matrix. Each element of the string may be one of the
following values
o U An inequality constraint with an upper bound ( ≤b i ).
o S An equality constraint ( = b i ).
o L An inequality constraint with a lower bound ( ≥ b i ).
Here, there are 3 constraints each of which is an upper bound
• vartype is a string of characters with length equal to the size of x. For our
purposes, each entry will always be C
• s is either -1 meaning that L must be maximized, or +1 meaning that L
must be minimized
• xmax is the solution found for x that maximizes (or minimizes) L
• Lmax is the maximum (or minimum) value found for L that satisfies all the
constraints
ACTIVITY 6.2.1
Maximize the function
L = 2 x1 + 5 x2 + 4 x3
1 2 1
1 2 2
> b=[4;6]
b =
4
6
95
> lb=[]
lb = [](0x0)
> ub=[]
ub = [](0x0)
> ctype="UU"
ctype = UU
> vartype="CCC"
vartype = CCC
> s=-1
s = -1
> [xmax,Lmax]=glpk(C,A,b,lb,ub,ctype,vartype,s)
xmax =
1.6934e-316
1.0000e+000
2.0000e+000
Lmax = 13
Example 6.2.2
Minimize the function
L = -2 x 1 + x 2
-2
1
2 1
1 -1
-1 1
> b=[20;4;5]
b =
20
4
5
> lb=[]
lb = [](0x0)
> ub=[]
ub = [](0x0)
> ctype="UUU"
ctype = UUU
> vartype="CC"
vartype = CC
> s=1
s = 1
96
APM1513/1
> [xmin,Lmin]=glpk(C,A,b,lb,ub,ctype,vartype,s)
xmin =
8
4
Lmin = -12
45000
10000
5000
40000
Lmax = 260000
Activity 6.3.1
As an example: The Milko Dairy can receive no
more than 100 000 litres of milk per day. Due to a
long-term contract, at least 10 000 litres of each
day's milk must be used for cheese manufacture.
The balance can be used for bottled milk, butter or
yoghurt. At today's market prices, the contribution
to profit and fixed cost of each litre of milk, when
put to these uses, is as follows
Butter R2.50
Cheese R2.00
Bottled milk R1.50
Yoghurt R3.00
97
butter and cheese packaging equipment can handle a
combined usage of at most 55 000 litres per day.
Solution/Feedback
We start by defining our variables:
x1 + x 2 + x 3 + x 4 ≤ 100 000
x2 ≥ 10 000
x1 ≤ 50 000
x3 ≤ 40 000
x2 + x 4 ≤ 50 000
x1 + x 2 ≤ 55 000
Octave code
> C=[2.5 2 1.5 3]';
> A=[1 1 1 1;0 1 0 1;1 1 0 0];
> b=[100000 50000 55000]';
> lb=[0 10000 0 0]';
> ub=[50000 Inf 40000 Inf]';
> ctype="UUU";
> vartype="CCCC";
> s=-1;
> [xmax,Lmax]=glpk(C,A,b,lb,ub,ctype,vartype,s)
xmax =
45000
10000
5000
40000
Lmax = 260000
Comments
Thus the Milko Dairy will maximize its trading profit for the day at R260 000 by
using 45 000 litres of milk for butter, 10 000 for cheese, 5 000 for bottled milk
and 40 000 for yoghurt. In the Octave code, we have expressed constraints that
involve only a single variable by setting lower bounds or upper bounds on the
variables by specifying the vectors lb and ub. In the vector ub, we then set the
upper bound as Inf, meaning infinity, for those variables (x 2 and x 4 ) that do not
98
APM1513/1
have explicit upper bounds. Then the constraint matrix A and right hand side b
represent only the three constraints comprising two or more variables.
99
Example 6.3.1
The ABC clothing manufacturing company has a number of different product
lines, T-shirts, jeans, dresses, jackets, coats, etc. Let us call these products P 1 , P 2 ,
P 3 and P 4 . The manufacture of the clothing requires raw materials including
different types and quality of raw cloth, dyes, accessories like buttons, buckles,
zips, etc., packaging materials, etc. and we call the raw materials R 1 , R 2 , R 3 , R 4
and R 5 . Each product line requires different quantities of raw materials, and the
following table shows how much of each raw material should be available in
order to produce one unit of each product line
R1 R2 R3 R4 R5
P1 0.1 0.09 0.095 0.03 0.02
P2 0.23 0.17 0.132 0.19 0.03
P3 0.17 0.19 0.201 0.02 0.021
P4 0.11 0.08 0.123 0.04 0.07
D1 D2 D3 D4
P1 0.7 1.1 0.8 0.2
P2 1.1 0.85 0.95 0.3
P3 0.6 0.9 0.6 0.4
P4 0.9 1.2 0.4 0.2
Management needs to set a production schedule for the next month. The buying
department has obtained quotations for the various raw materials, although
some items are in short supply and there are supply limitations
R1 R2 R3 R4 R5
Price (Rands) 3050 3550 4800 3970 3020
Supply limitations 60 70
The sales department advises current market rates for the product lines,
together with any upper limits that should not be exceeded to avoid market
over-supply and price depression.
P1 P2 P3 P4
Price (Rands) 6200 7000 8000 7300
Sales limit 100 200 200 200
100
APM1513/1
Solution
We start by defining our variables:
x1 = P 1 , x2 = P 2 , x3 = P 3, x4 = P 4 , x5 = R 1 , x6 = R 2 , x7 = R 3 , x8 = R 4 , x9 =
R5
The objective function is the income expected from sales of each product line, less
the cost of the raw materials,
The amounts of raw materials required for each product line translate into equality
constraints
x 1 ≥ 10
File ex6_3_2.m
ctype="UUUUSSSSS";
lb=[10 0 0 0 0 0 0 0 0];
ub=[100 200 200 200 60 Inf 70 Inf Inf];
s=-1;
b=[400 400 400 400 0 0 0 0 0]';
vartype="CCCCCCCCC";
A=[0.7 1.1 0.6 0.9 0 0 0 0 0;
1.1 0.85 0.9 1.2 0 0 0 0 0;
0.8 0.95 0.6 0.4 0 0 0 0 0;
0.2 0.3 0.4 0.2 0 0 0 0 0;
-0.1 -0.23 -0.17 -0.11 1 0 0 0 0;
-0.09 -0.17 -0.19 -0.08 0 1 0 0 0;
-0.095 -0.132 -0.201 -0.123 0 0 1 0 0;
-0.03 -0.19 -0.02 -0.04 0 0 0 1 0;
-0.02 -0.03 -0.021 -0.07 0 0 0 0 1];
C=[6200 7000 8000 7300 -3050 -3550 -4800 -3970 -3020]';
[xmax,Lmax]=glpk(C,A,b,lb,ub,ctype,vartype,s)
101
Feedback: sOctave window
> ex6_3_2
xmax =
10.000
38.411
200.000
146.959
60.000
57.187
64.296
17.476
15.839
Lmax = 2.1918e+006
Comments
Thus the company will maximize its trading profit for the month at R2 191 800 by
producing 10 units of product P 1 , 38 units of product P 2 , 200 units of product P 3 ,
and 147 units of product P 4 . The company will need to order 60 units of raw
material R 1 , 57 units of raw material R 2 , 64 units of raw material R 3 , 17 units of
raw material R 4 , and 16 units of raw material R 5 .
2. Find the minimum value as well as the point at which the minimum occurs of
L= -3 x 1 -4 x 2 + x 3
subject to the constraints
-x 1 + x 2 + 2 x 3 ≤ 5
2 x 1 + x 2 + x 3 ≤ 20
x1, x2, x3 ≥ 0
3. Find the minimum value as well as the point at which the minimum occurs of
L= -2 x 1 -5 x 2 + x 3
subject to the constraints
x1 + 2 x2 - x3 ≤ 6
x2 + 2 x3 ≤ 6
2 x2 + x3 ≤ 4
x1, x2, x3 ≥ 0
4. Find the maximum value as well as the point at which the maximum occurs of
L= 2 x 1 + 3 x 2 + 4 x 3 + 3 x 4
subject to the constraints
1.5 x 1 + 2 x 2 + 1.5 x 3 + x 4 ≤ 30
1 x 1 + 2 x 2 + 1 x 3 + 3 x 4 ≤ 45
5 x 1 + 4 x 2 + 7 x 3 + 2 x 4 ≤ 65
102
APM1513/1
6 x 1 + 3 x 2 + 7 x 3 + 4 x 4 ≤ 60
8 x 1 + 4 x 2 + 8 x 3 + 2 x 4 ≤ 70
x1, x2, x3, x4 ≥ 0
Each department can provide 24000 minutes per month, except Department
D 6 , which can only offer 15000 minutes. How much of each product line
should be produced so as to maximize the company's trading profit?
103
[Hint: Choose the variables as x 1 to x 9 where x 1 is the number of truckloads
from P to A, x 2 is the number from P to B, ... , , x 9 is the number from R to C]
6.6 In conclusion
You have now successfully used Octave to solve linear programming problems
that cannot be tackled by the simple graphical method. You have also successfully
solved some real-world linear programming problems.
104