Mathematical Exploration With MATLAB
Mathematical Exploration With MATLAB
AMBRIDGE
UNIVERSITY PRESS
CAMBRIDGE UNIVERSITY PRESS
Cambridge, New York, Melbourne, Madrid, Cape Town, Singapore, Sao Paulo
Published in the United States of America by Cambridge University Press, New York
www.cambridge.org
Information on this title: www.cambridge.org/9780521630788
A catalogue record for this publication is available from the British Library
Preface page xi
1 Introduction 3
1.1 First steps with MATLAB 3
1.2 Vectors and plots 6
1.3 Creating and editing script files 9
1.4 Getting hardcopy of things 15
Exercises 16
3 Whole Numbers 31
3.1 A loop to calculate Fibonacci numbers 31
3.2 A loop with conditionals: the 3n + 1 or hailstone problem 33
3.3 The euclidean algorithm for greatest common divisors 34
3.4 Fermat's theorem and the power algorithm 36
Exercises 39
3.5 Appendix 41
v
vi Contents
5 Representation of Data 60
5.1 Data analysis 60
5.2 Least squares fitting 63
Exercises 70
5.3 Appendix 71
14 Permutations 171
A Cycle decompositions 171
B Card shuffling 177
14.1 Introduction 177
14.2 Ins and outs 179
14.3 Cycles 185
14.4 Rough riffles (ruffles) 185
14.5 Appendix 188
21 Epidemics 265
21.1 Preliminary look at some data 266
21.2 The SIR model for the dynamics of an epidemic 267
21.3 Studying the behaviour analytically 268
21.4 Analysing the data 269
Exercises 271
23 Tides 281
23.1 Preliminary look at the tidal data 282
23.2 Fourier series and methods 282
23.3 Analysis of an electrical signal 284
23.4 Fourier analysis of the tidal data 284
Exercises 285
Mathematics and its practitioners have come a long way since the days of
drawing polygons in the sand with a stick. Although this cannot be said
of all our degree courses, there is nevertheless an increasing realisation
in higher mathematics education that current computing technology can
open new doors for students and tutors alike. This book arose out of
a largely successful attempt to complement traditional mathematical
courses with one which took this opportunity seriously.
First year students at a UK university are expected to acquire a wide
range of mathematical skills-the ability to argue logically, absorb new
concepts, calculate accurately, translate everyday problems into appro-
priate mathematical language, construct mathematical models and to
assess the approximations made. We chose to use the popular and pow-
erful computer package MATLAB to help promote some of these skills.
It provided a convenient way to help students understand things graph-
ically, to see the wood rather than the trees in complex problems and to
give access to more realistic modelling situations.
We chose MATLAB rather than one of the increasingly sophisticated
and algebraically based packages because of the very gently sloped learn-
ing curve involved. MATLAB allows the student to graduate smoothly
from the functionality of a hand calculator, through increasing use of
powerful numerical and graphical facilities towards a high level pro-
gramming capability. The latter point was considered a bonus in that
it provided a possible access route to programming for students with no
prior computer background. At the very least, students with no key-
board skills at all can acquire a degree of familiarity with an essential
modern tool, the computer.
The course, and this book, were designed for students coming to grips
with a typical first year honours mathematics course at a UK university.
xi
xii Preface
In our case, students had already completed the first semester of core
units and so already had a basic knowledge of calculus, complex num-
bers, vectors and matrices. In the book, we assume that the reader has a
reasonable level of skill in calculus but only limited familiarity with the
other topics. The typical student will be in the process of extending this
base to include some selection of topics such as elementary statistics,
mechanics, linear algebra, number theory, differential equations, Fourier
series and so on. The book is thus intended to help motivate new topics
and to build on old ones.
Like Gaul, the book is divided into three parts. Part one comprises
a very elementary `hands-on' introduction to the features of MATLAB
followed by a series of methods chapters. In these the reader is taken
through a range of mathematical ideas and given `on the job' training in
those MATLAB techniques which are expected to be of particular value
in the ensuing project chapters. Thus all the standard programming
structures and MATLAB commands are introduced through work on:
matrices; whole numbers and elementary number theory; graphing plane
curves; data fitting and approximations to functions using least squares
techniques; simulation of random distributions; and ordinary differential
equations. In this way, the student learning how to use MATLAB is
taken through mathematics which is (or should be!) interesting for its
own sake.
Part two contains a variety of projects, termed `Investigations', which
build on the earlier ideas. Matrices are applied in the context of magic
squares, permutations and the solution of linear systems; manipulation
of whole numbers is applied to greatest common divisors of random sets
of numbers, primality testing and card shuffling; approximation tech-
niques are applied to solution of nonlinear equations and interpolations;
and so on. In each case, an exploratory attitude is encouraged, backed
up with plenty of explicit exercises, both purely computational and more
mathematical in nature.
Finally, Part three contains a number of `Modelling Projects' in which
the reader is invited to employ some of the skills developed in Part one.
By its nature, mathematical modelling is a rather open-ended process
and requires a certain degree of mathematical maturity that a first year
student may not yet have attained. Nevertheless, we feel that the avail-
ability of techniques to which MATLAB gives access, and the very great
importance of modelling as an applied mathematical skill, mean that
this is an opportunity not to be missed. In practice, we have found that
students cope well with these challenges.
Preface xiii
Acknowledgments
Introduction
results in
t We will make some further remarks, where appropriate, about editing and file
handling in a non-Windows environment.
3
4 Introduction
ans =
169
If you typed the above line and nothing happened, perhaps you omitted
to press the <Enter> key at the end of the line. The laws of precedence
are built in but, if in doubt, you should put in the brackets. For example
>> 8*(1/(5-3)-1/(5+3))
ans =
>> sgrt(5"2+12"2)
and
>> exp(log(1.7))
pi
format long
pi
format short
>> x=3
produces
1.1 First steps with MATLAB 5
X =
vol =
75.3982
Note that the first line had two `commands' on it, neither of which
seemed to produce a result! When MATLAB encounters an instruction
followed by a semi-colon ; it suppresses any visual confirmation. It
really does obey the instruction but keeps quiet, as you can check with
>> rad=4;
>> rad
rad =
This is useful if you want to avoid cluttering up the screen with inter-
mediate results. Watch out for semi-colons in what follows.
Remember that each variable must somehow be assigned a value be-
fore you can make use of it in further calculations. For example if you
have followed the above examples and now type
>> f = x"2 + 2*x*y + y"2
This is self-explanatory. If you now type y=4; and then repeat the
calculation of f you should have more success.
Incidentally, a quick way of repeating a previous MATLAB instruction
is to press the `up-arrow' key (1) until you recover the command you
want. Try it now. If the original instruction was not quite correct, or
if you want to develop a new instruction from a complex but similar
previous one, you can use the same trick. Recover the command which
6 Introduction
you want then use the sideways arrows (- and ->) together with the
delete key to edit the old command suitably. As an exercise, try using
your previous work to calculate the volume of a right-circular cylinder
with radius 2 and height 1/4. Did you get it?
If you have difficulty remembering the names of variables which you
have assigned, you can try typing who or whos. Try them both now. Do
you recognise the variables listed?
1.2.1 Vectors
Type in the following examples which all result in vector-valued vari-
ables. Pause to think about the result in each case.
u=[2,2,3]
u= [2 2 3]
v=[1,0,-1]
w=u-2*v
range=1:13
odd=1:2:13
down=20:-0.5:0
even=odd+1
xgrid=0:.05:1; x=xgrid*pi
y=sin(x)
The first two lines demonstrate that elements of a vector can be sep-
arated by spaces or commas. If you are worried about inserting blank
spaces by accident you can stick to the comma notation. Thus [1+1 2
3] is the same as [2, 2, 31, whereas [1 +1 2 3] is the same as [1, 1, 2, 31 !
1.2 Vectors and plots 7
Note that vectors can be of any length. They can be row vectors as
here, or column vectors like
>> w'
ans =
0
2
5
Can you make sense of the results? Why did the last one not work?
Now suppose you want a set of values z given by z = y2, where y is
the vector of values already assigned. From the above experiments you
will realise that
>> z=y*y
where the . inserted before the * symbol is the key feature forcing
-
element-by-element operation. Similarly, u. /v and y. 2 are understood
as element-by-element operations with vectors of the same size.
and sit back. A nice simple graph of y = sin x vs x will magically appear.
The axes are chosen automatically to suit the range of variables used.
This is the simplest possible case. In later work you will want to do
more elaborate things. For now, try the following:
You can probably figure out the significance of each of these commands.
For example, yl=2*x defined new function values y = 2x, hold on told
MATLAB to keep the same graph and plot (x, y 1, ' r') plotted the next
graph on top. Note that the axes were adjustedt and the second curve
was plotted in red.
In the above examples of plot, MATLAB joined up the 21 points
with straight-line segments. Should you not want this, you can specify
plotting points using a choice of symbols as follows. Try this
t Assuming you are using MATLAB version 4. There are a number of small differ-
ences between this and earlier versions, particularly with graphics commands.
1.3 Creating and editing script files 9
Did MATLAB do what you expected? Did you remember to use the t
key to reuse previous commands? If you need some help with how to
use any MATLAB instruction you can type for example
>> help plot
>> help hold
>> help sin
and so on.
to solve that particular problem. This is fine, but suppose you have a
set of five points and want to check which pair is the closest together?
You would obviously want to store as many as possible of these steps in
a `script' (file) to reuse as required.
where fname is the name of a text file which either exists or will exist,
by the time you have finished! If that doesn't work, consult someone
knowledgeable about your system setup or ask a patient friend.
Windows users
Windows comes with its own basic text file editor called Notepad, whose
icon is usually found within the Accessories Group. A typical MATLAB
setup within Windows makes direct use of this accessory so we will con-
centrate on this method. To create and edit a new file called myf ile. m,
from within MATLAB, proceed as follows:
(i) In the MATLAB Command Window menu, click on File.
(ii) Click New then M-file.
(iii) Within Notepad which you have now started, you can type some
lines, for example
% myfile.m
It doesn't do very much, just identifies itself.
% These 3 lines are comment lines which MATLAB ignores.
disp(' I am an M-file')
You have now created a file which MATLAB can find and use. You
hope!
Back in the MATLAB Command Window you can now ask MATLAB
whether it can find the file. Type
>> type myf ile
and you should see a list of the lines which you typed in. If not, go
back to step (i) and start Notepad again by clicking File in the MATLAB
Command Window followed this time by Open M-file. You should see an
1.3 Creating and editing script files 11
including any more comment lines (those starting with %) which you
may need to remind yourself how it works. Then the business lines
d=b-a;
dd=d*d';
dist=sqrt(dd)
Remember to save the file by clicking File then Save As as you did
when creating mf ile. m. Review that example if necessary. When you
have finished editing the file, you might as well close Notepad by clicking
File then Exit. You can leave Notepad open if you want, but it may
get confusing if you leave too many Notepad windows open at once.
When exiting, Notepad always reminds you if there are recent changes
to be saved.
Now pick values for the vectors a and b, if you haven't already done
so
>> a=[1,0,-21;
>> b=[2,3,1];
Then find the distance by simply typing
12 Introduction
>> distab
The command help displays the initial comment lines at the top of an
M-file. This is why it is always good practice to include comment lines
(starting with %) at the top of an M-file. It is also a good idea to include
the name or title in the first comment line.
Note that myfile is the name of the MATLAB instruction (what you
type to use it), whereas myfile . m is the name of the file containing its
definition.
or
assigns the correct distance to the variable ab without any further ado.
Here is how to modify the script M-file distab.m to become the func-
tion M-file distfn.m. We will assume that you didn't leave Notepad
open and so will have to start editing from scratch.
(i) In the MATLAB Command Window menu, click on File.
(ii) Click pen M-file to start Notepad and look for the list of avail-
able M-files, that is, ones ending with . in. (If necessary, change
the *. txt ending showing in the File Name box to *. m).
(iii) Select distab.m.
(iv) Now you can make changes. First change the comment lines
to reflect the new name and purpose and then the modify the
MATLAB instructions so that your file looks like this:
distfn.m
Calculates the distance between two vectors a and b
Usage:-
%
input: a,b (position vectors)
% output: distfn is the distance between them
function dist=distfn(a,b)
d=b-a;
dd=d*d';
dist=sqrt(dd);
>> dist=distfn(a,b)
to assign the required distance to the variable dist. If you got more than
one number flashing up, perhaps you forgot some of the semi-colons with
the function. If so have another look at it using Notepad.
Function M-files (or `M-functions'), that is, M-files whose first non-
comment line starts function . . ., have a very important feature. Aside
14 Introduction
from the name itself (distfn in this case), all the other variables (a, b,
dd etc.) are purely internal to the function. This can help reduce con-
fusion with other calculations and variables which you may have used.
Check this by typing
>> who
>> clear
>> who
>> dist=distfn([1,1,1],[2,2,2])
>> who
The command clear clears out all previously defined variables. After
executing the function with dist=distfn(. .), no trace of the internal
variables remains in your MATLAB session.
diary sectl.txt
% Beginning of section 1
now some commands
myfile
dist=distfn([1,1,1],[2,2,2])
diary sect2.txt
>> % I want this stored somewhere else
>> x=0:.1:1;
>> y=x.*x
>> plot(x,y)
>> diary off
The session will proceed fairly normally. The command diary faame
tells MATLAB to put a copy of the text output (numbers and letters)
in the file f name. Typing diary off switches it off. The above example
puts some output into one file and some into another. You can now use
Notepad to have a look at what was generated and, if needed, edit it.
Invoke Notepad as usual by clicking File and try to pen files with the
ending * . txt. You should see sect 1. txt and sect2. txt in the list.
1.4 Getting hardcopy of things 15
that is, you start with a clean sheet. Should you actually wish to save
what you are doing for another time you can type, for example,
>> save monday
or
where the second version saves only the explicitly mentioned variables
in the specified file monday. You can then reload things the next day or
whenever with
>> load monday
Remember that only the variables themselves that is, their current val-
ues, are saved using these techniques. Any formulae which you have
typed in will be lost unless you have entered and saved them in an
M-file.
a (' - transpose)
For plots, you go about getting a hardcopy in the same way but you
use the File and Print buttons on the Window containing the figure
(usually labelled Figure No. 1).
Exercises
Before proceeding, check your level of skill by completing all of the
following short exercises. If you can't remember some of the commands
have a look at the list in Table 1.1. If necessary, go back and reread the
relevant section.
1.1 Find the sum of the first four terms in the sequence
1 2 3
2x3' 3x4' 4x5'
Exercises 17
>> a = [-1,2,4]
>> b = [1.5,2,-1]
where there is a dot before the multiplication sign *, multiplies the vec-
tors a and b element-by-element: in this case, c = (-1.5,4, -4). The
dot product is then obtained by summing the elements of c:
>> sum(c)
18
2.1 Vectors and matrices 19
2.1.2 Matrices
The matrix
7-1 1 2
A= 3 -1 1
-1 3 4
is entered in MATLAB as
>> A=[-1 1 2;3 -1 1;-1 3 4]
At some stage, have a look at help format to see what else you can do
to control the look of MATLAB output.
The equations
-XI + X2 + 2x3 10
3x1 - x2 + X3 -20 (2.1)
-xi + 3x2 + 4x3 40
20 Matrices and Complex Numbers
can be written as
Ax = b (2.2)
say, where x is the column vector (xl, x2i x3)T and b is the column
vector (10, -20, 40)T of right-hand sides of the equations. In MATLAB,
we can write b as the transpose of a row vector by using
>> b=[10 -20 401'
In order to solve linear equations Ax = b, where the determinant of
A is nonzero, we can use the inverse A-l of A, that is, the matrix such
that AA-' = A-'A = I, the 3 x 3 identity matrix. To discover whether
the determinant is zero, type
>> det(A)
which gives the answer 10. The solution for x is then obtained by
x = A-'Ax = A-'b,
which in MATLAB is
>> x=inv(A)*b
displaying the result as a column vector x (in this case (1, 19, -4)T).
Thus matrix products are obtained by using the multiplication symbol
* but the matrices must be of compatible sizes or MATLAB produces
an error. For example b*A is meaningless since the number of columns
of b, namely 1, does not equal the number of rows of A, namely 3. You
can test matrix multiplication by
C=A*A
det(C)
D=A-3
det (D)
Thus C = A2, D = A3 and, by the well-known rule for square matrices
of the same size det(P)det(Q) = det(PQ), the determinants are 100,
1000 respectively.
Actually, one does not usually solve linear equations such as equa-
tions (2.1) or (2.2) by first finding the full inverse of the corresponding
matrix. One applies linear operations to the augmented matrix formed
from A and b. MATLAB can also perform the calculation in this way.t
You simply type
t See Chapter 16 for a more detailed discussion of the solution of linear systems.
2.1 Vectors and matrices 21
>> x = A\b
Again, the matrices must have compatible dimensions. Try it and check
your answer by forming the product Ax or by finding the `residual'
r = b - Ax.
You can also add two matrices of the same size. MATLAB doesn't
even complain if you add a scalar to a matrix. In fact
>> E=A"2+2*A+1
So don't assume that 1 means the identity matrix.t The identity matrix
and the matrix all of whose elements are 1 can be called up by typing
>> diag([1 1 1] )
>> ones(3,3)
so that diag makes a diagonal matrix with the given diagonal entries
and ones makes a matrix of is of the given size. What does
>> diag(ones(1,3))
give? Note the brackets when using the function diag. The identity
matrix is actually built into MATLAB as (e.g. for 3 x 3)
>> eye (3)
As already noted, one may wish to solve the equations Ax = b, by
forming the corresponding augmented matrix
>> F = [A b]
In fact, this method is applicable even when the determinant is zero or
when the matrix A of the equations is not square. The procedure is to
reduce the augmented matrix to `row reduced echelon form', that is, to
use row operations to produce a matrix in which there is a leading 1 in
each row, the entries before, above and below which are all zeros. We
shall not use this in any important way here, so if you have not met
the idea just skip over the remainder of this section. The row reduced
echelon form is used in the project of Chapter 8 on magic squares.
t Unfortunately, typing A*1 produces A again, suggesting that in multiplication
MATLAB thinks 1 means the identity matrix!
22 Matrices and Complex Numbers
Typing
>> G=rref(F)
G= 0 1 0 19
0 0 1 -4
This means that the original equations are equivalent to the equations
whose augmented matrix is the matrix G, and these equations are simply
x = 1, y = 19, z = -4 so the solutions can be read off.
Don't forget the round brackets when using a MATLAB function such
as rref. Thus if you type in a matrix directly you must still include
them, as in
>> rref ([1 2 3; 4 5 6; 7 8 9] )
Note that this produces a single row of zeros (the last row), indicating
that the rows of the original matrix are linearly dependent. You can
check this by finding the determinant, which is zero.
You may also care to try typing
>> rrefmovie(F)
B=inv(P)*A*P
eig(B)
[Y,D]=eig(B)
before starting work on complex numbers, and avoid using i for anything
else!
You will already have seen some complex numbers if you found the
eigenvalues and eigenvectors in 2.1. MATLAB can handle complex
numbers easily. For example,
>> a=1+i; b=2-3i;
>> c=a*b
24 Matrices and Complex Numbers
>> d=sqrt(a)
produces the answers c = 5 - i, d = 1.0987 + 0.4551i. Note that just one
square root is shown. Likewise
(-1)"(1/2)
(-2+21)"(1/3)
produces the imaginary part without the i attached. For example with
a = 1 + i the answer is 1.
>> conj (a)
produces the complex conjugate a of a. (If a = x + iy where x and y are
real, then a = x - iy.) Thus
>> a*conj(a)-abs(a)"2
should always give answer 0 (but might give a very small answer such
as 1.7764e-015, that is, 1.7764 x 10-15)
Try also
>> exp(i*pi)
which produces eivr = -1.
for the age structure of the population of a country or other large commu-
nity. The basic idea is to take a vector representing the age distribution
in one year, construct a matrix of transition probabilities from one year
to the next and then, by matrix multiplication, predict the probable
age distribution for the next year. Predictions for subsequent years are
obtained by further matrix multiplication. The hard bit is to make a
model for the probability matrix! Here is an example.
In some given year, we count the number of people in age-bands 0-
5, 6-19, 20-59 and 60-69. Further subdivisions are of course possible
with a little more work. We then make the following, fairly drastic,
simplifying assumptions
Within one age-band, the age distribution is constant, that is, there
are the same number of people in each year group.
We do not consider anyone who lives beyond 69!
Deaths only occur in the 60-69 age-band at the rate of dA% per annum
and in the 0-5 band at dI% per annum.
Births are due to people in the 20-59 age-band at the rate of b% per
annum.
With these assumptions, we want to relate the column vector
N(2) = (ni(2), n2(2), n3(2), n4(2))T
of populations in the four age-bands in year 2 to the column vector
N(1) = (ni(1),n2(1),n3(1),n4(1))T
of populations in year 1. Recall that, as above, T stands for transpose
of a vector or matrix.
To see how to obtain this relationship, consider Table 2.1.
The various columns contain: the age range within the band; the
symbol nz for the current number of persons in that band; the number
of year groups (for example, ages 0, 1, 2, 3, 4, 5 make six year groups);
the number that, according to our rules, die in that year; the consequent
number (the rest) who survive the year; the number of these survivors
who leave the band (graduating to the next one or, in the case of the
age-band 60-69, passing out of our calculations); and the number who
enter the band, either through being born into it or through graduating
from the one below.
Of course, each band's leavers become the next band's entrants. Ac-
cording to the simplifying assumptions, the number in each individual
26 Matrices and Complex Numbers
60-69 n4 10 100
n4 (1 - 100) 14 (1 - loo) 10 40
year within an age-band is the same, so the `leavers' are just the 'sur-
vivors' divided by the number of years covered by the age-band. In prac-
tice much narrower age-bands might be taken, to make this assumption
more plausible.
It is now easy to see that the vector N(2) of populations of the various
age-bands in year 2 is related to the corresponding vector N(1) for year
1 by a matrix equation
N(2) = LN(1),
where L is the so-called Leslie matrix
1- 0)
b 0
s 0 100
/
01
13
L s11-100 1 0
39
0
0 14 40
0
0 0
1 9 // _ dA )
40 10 ll 1001 1
Similarly,
N(3) = LN(2) = L2N(1),
and so on. In general, we have:
N(t + 1) = LN(t).
Given data for t = 0, say, and some estimates of b, dI and dA, one can
then predict the likely age structure of the population for a number of
years ahead. This is very useful if you have to plan pension schemes,
university sizes or day-care provision.
An M-file leslie.m is provided to allow you to explore these ideas.
Type
Exercises 27
>> leslie
>> L"4
>> N50=L"50*N
Type
>> [Y D] = eig(L)
The second command divides the three elements of N50 by the three
corresponding elements of E. The result should be three approximately
equal numbers, showing that the population after 50 years is roughly
proportional to the eigenvector corresponding to the largest eigenvalue.
What happens if the initial population distribution is proportional to
this eigenvector?
Exercises
2.1 As a simple example of manipulation of complex numbers, con-
sider the following, which will be covered in more general terms
28 Matrices and Complex Numbers
in the project of Chapter 13. (Note the warning given above, in
2.2; for safety you can type `clear i' before starting.) Enter
any complex number into MATLAB, for example
>> z=3+4i
Now type
>> z=(z"2-1)/(2*z)
Using the T key, repeat the last command several times. Even-
tually the answers settle down-converge--to either i or -i; for
the starting value 3 + 4i they settle down to i.
Can you discover the rule which determines, from the starting
z, whether the numbers will converge to i or -i? (You are not
expected to prove the rule. That is covered in Chapter 13.)
If you start with z = 0, MATLAB produces in turn -oo, 0,
oo, 0, which doesn't make much sense since z = too, if it
gives anything at all, should give z = oo again. But there
are other starting values such as z = 2 which produce more
reasonable nonconvergence. Which starting values for z pro-
duce nonconvergence? (Again only an experimental answer is
expected.)
2.2 This exercise combines matrix work with editing files and using
diaries. Make an M-file which contains the following (remember
to leave spaces between the entries of a single row, or to insert
commas there):
A=[1 1/2 1/3
1/2 1/3 1/4
1/3 1/4 1/5]
b=[1 0 0]'
det (A)
X=inv(A)*b
Note that MATLAB suppresses the >> prompt until the loop is com-
pleted by end. What the for... end loop does is to take in succession
the values 1, 2, ... ,15 for the variable k and to augment the vector f by
a new entry f(k + 2) each time round. Thus k = 1 makes f emerge as
[1 1 2]; k = 2 makes it emerge as [1 1 2 3], and so on. The semi-colon
after the equation for f(k+2) suppresses output during the 15 times the
loop is executed.
31
32 Whole Numbers
Typing
>> plot(f,'*')
gives a plot of the values, placing an asterisk at each point (i, f (i)).
Just plot (f) joins these points up with straight segments, producing
a steeply sloping curve. The entries of the vector f are the Fibonacci
numbers, 1, 1, 2, 3, 5, 8, 13, 21, 34. .... The rule for forming them is, as
above, f(1) = f(2) = 1;f(k + 2) = f(k + 1) + f(k) fork > 1.
The above commands can be put into an M-file. As a variant on the
above we could use a `while loop', as follows:
f=[1 11;
k=1;
while f(k) < 1000
f (k+2)=f (k+1)+f (k) ;
k=k+1;
end
f
plot(f)
This M-file is stored under the name f ibno. m and is executed by
typing f ibno. The indentation used is intended to display the structure
of the M-file and has no effect on the running or output. This time the
two commands between while and end are executed while the condition,
f(k) < 1000, of the while loop is valid. Since f(16) = 987 and f(17) =
1597 the last value of k which allows execution of the while loop is
k = 16. The value of k at the end of the whole M-file is 17 since k
is incremented by 1 during the final execution of the loop. Notice an
important difference between for and while loops:
In a for loop the loop variable (k in the above example) is automat-
ically incremented by 1 for each pass through the loop. If you want
the variable incremented by d each time and starting at a while not
exceeding b you use
for k=a:d:b
end
n=input('Enter n')
f=[n] ;
k=1;
while n>1
k=k+1;
if rem(n,2)==O
n=n/2;
else
n=3*n+1;
end
f (k)=n;
end
bar(f)
To use this we type hail and then input say 25 <Enter>. This
version produces a bar-chart rather than a graph.
The resulting bar-chart for n = 1000 is shown in Figure 3.1.
Typing k after running the above M-file produces an answer which is
the exact number of steps taken to reach 1, counting the original number
as Step 1. (Thus for example n = 8 produces 8, 4, 2, 1 which counts as
4 steps.) For n = 1000 it takes 112 steps, as you can roughly see from
the bar chart of Figure 3.1. Typing max (f) after running the M-file
produces the maximum value attained by n during the iteration: the
highest point on the bar-chart. This is 9232 for a starting value of 1000.
Here are some notes on the way that hail.m works.
(i) Note the incrementing of k in the while loop. This could equally
well be done later in the loop, after the f (k) = n line.
(ii) Note the if ... else ... end construction. All conditionals must
be closed with an end statement. You can have if ... end without
an alternative `else'.
(iii) The == symbol is used for comparison. The quickest way to
34 Whole Numbers
10000
9000
8000
7000
6000
5000
4000
3000
2000
1000
20
A 40
Ih N lll1110llllll111L
60 80 100 120
a=bq+r,where0<r< b.
Here q is the quotient and r is the remainder, which is calculated by
r=rem(a,b)
(If needed, the quotient q can be calculated by q=f loor (a/b).) Then
we always have
function h=gcdiv(a,b)
if a<O a=-a;
end
36 Whole Numbers
if b<0 b=-b;
end
while b > 0
r=rem(a,b);
a=b;
b=r;
end
h=a;
Remember that you can get information on the M-file pow. m by typing
help pow.
Using the power algorithm it is easy to verify many examples of Fer-
mat's theorem such as 161998 - 1 mod 1999, which is so because 1999 is
prime.
Examples
(i) pow(3,118,119) gives 32. Hence, by Fermat's theorem, 119 is
not prime (for, if it were, then the answer would have been 1).
Of course, 119 = 7 x 17.
(ii) pow (2, 1993002, 1993003) gives 1121689, so 1993003 is not prime
(in fact, it is 997 x 1999).
does not give the correct answer 1. This is because ten figure numbers
cannot be reliably manipulated by MATLAB. You can rely on pow for
seven or eight figure numbers.
More powerful packages can handle hundreds of figures. These pack-
ages are routinely used to find very large (at least 100-digit) numbers
which are `probably prime', using Fermat's theorem and a more subtle
variant called Miller's test. These `probable primes' are used in cryptog-
raphy, that is in sending messages by virtually unbreakable codes. You
can read about such codes in [12]. Miller's test for more modestly sized
numbers appears in Chapter 9.
3.4.2 Pseudoprimes
There are numbers which satisfy the conclusion of Fermat's theorem
without satisfying the hypothesis that p is prime. That is, we can some-
times have rem(a'm-1, m) = 1, for some a > 1, without having m prime.
When this happens we call m a pseudoprime to base a.
For example, rem(724, 25) = 1, rem(2340 341) = 1, so that 25 = 52 is
a pseudoprime to base 7, and 341 = 11 x 31 is a pseudoprime to base
2. Nevertheless if, for a fixed in, lots of values of a are found for which
rem(am_l, m) = 1, then this can be regarded as accumulating evidence
for the primality of m. For very large m this is by far the most efficient
way of selecting `probable' primes, though in practice a variant known
as Miller's test is used. This is the subject of one of the Investigations.
See Chapter 9.
The number 561 (= 3 x 11 x 17) has the rather unpleasant property
that rem(a560 561) = 1 for any a not divisible by 3, 11 or 17. It is called
a Carmichael number. Numbers such as these masquerade as primes
under a large variety of tests, but can usually be unmasked with a little
Exercises 39
patience. It was proved only in 1992 that there are infinitely many
Carmichael numbers.
Exercises
3.1 Run the M-file f ibno. m, i.e. type f ibno in MATLAB. The
length of the vector f is obtained by length(f). What is the
length of f in this case? What is the largest Fibonacci number
< 1000 and the smallest one > 1000?
3.2 Use the hail command to find the number of steps which 27
takes to reach 1, and the largest value reached during the iter-
ation.
Modify hail.m so that it prints out the number of iterations
and the largest value reached, but does not plot the values at
the end. Call the resulting M-file hail2.m.
Modify hail2.m a s follows, calling the result hail3.m. Re-
move the line n = input ( ' Enter nI); , insert the lines
for k=50:100
n=k
at the end. What does this M-file do? Check this by running
it, i.e. typing hail3 in the MATLAB window.
Using a diary (1.3.4) for storing the output from hail3.m
read off all the values of n between 50 and 100 which have the
same largest value during the hailstone iteration as 27 has. For
each one, state the number of iterations each takes to reach 1.
3.3 Type format short <Enter>. Let 6,,, and 7n denote the whole
numbers written in ordinary decimal notation as strings of n 6s
and n 7s, respectively. So 64 = 6666, etc. Use the command
gcdiv to find the gcd of 6n and 7n for n = 1, 2, 3. .... When is
the displayed answer wrong? How do you know it is wrong?
Try typing format long and repeating the exercise. What is
the new value of n? Again, how you can be sure that, for this
n, the answer is wrong?
40 Whole Numbers
m=10"'7+1;
while m<10"7+100
m % Note no semicolon!
pow(2,m-1,m) % Note no semicolon!
m=m+2;
end;
3.5 Appendix
3.5.1 Proof of the power algorithm
The key fact is that we can take remainders at any time during a
calculation and always arrive at the same answer. Thus if we want
x = rem(ab, m) we can work out successively
p = rem(a, m), q = rem(b, m), x = rem(pq,m). (3.2)
Thus
750 = 72 x 716 x 732 .
function x=pow(a,n,m)
3.5 Appendix 43
b=a;
x = 1;
while n>O
d = rem(n,2);
if d==1
x = rem(x*b,m);
end
b = rem(b * b,m);
n = (n-d)/2;
end
4
Graphs and Curves
4.1 Polynomials
A polynomial such as p(x) = x4 + 2x3 - 3x2 + 4x + 5 is entered by means
of its coefficients:
>> p= [1 2 -3 4 5]
Note the spaces between the numbers; commas are also allowed for sep-
aration. In other words, a polynomial is just entered as a vector con-
taining the coefficients, starting with the `leading' coefficient in front of
the highest power of the variable x.
The roots of p (i.e., the solutions of p(x) = 0) are obtained by typing
>> roots(p)
Notice that this finds the complex roots as well as the real ones. We can
increase the number of figures displayed by typing
44
4.2 Initial examples of drawing curves 45
The first line creates a vector x whose entries are the numbers from -4
to 2 at 0.05 intervals. Thus
x = [-4.0 - 3.95 - 3.9 ... 1.95 2.0].
As usual, the semi-colon merely suppresses output to the screen.
The second line evaluates the polynomial p at all these x, producing
a vector y of the same length as x containing p(-4),.. ., p(2).
The third line plots values of y against values of x, taking them in
corresponding pairs and joining up with (very short) straight segments.
By adding
>> hold on
>> plot([-4,2],[0,0])
the x-axis is also plotted. The plot command takes the first entry from
each square bracket, making the point (-4, 0), and joins it to the point
(2, 0) obtained from the second entry from each bracket, thus drawing
the x-axis across the screen. From this it is quite clear that p has two
real roots in the interval from -4 to 2, and their positions agree with
the calculation of roots as -3.18... and -0.728.... See Figure 4.1.
The command plot works with two (identical length) vectors of values
specifying pairs of coordinates. In this case, if you type length(x) or
length(y) you should get the response 126. Although axes are scaled
automatically by the maximum values of x and y, they can be manually
changed if necessary. For example, the line
46 Graphs and Curves
50
40
20
10
-10
-20
-4 -3 -2 2
Fig. 4.1. Graph of y = x4 + 2x3 - 3x3 + 4x + 5, with the x-axis also drawn.
makes the screen cover the region 0 < x < 10, -2 < y < 2. Try this to
see the effect. (In versions of MATLAB before Version 4, you may need
to reissue the plot(x,y) command after axis.)
Similarly, to plot the graph of y = x sin x, say, we could use
>> x=0:.05:2*pi;
>> y=x.*sin(x);
>> plot(x,y)
The first attempt to define y = 1/x will not work; the second should
produce the graph y = 1/x, for -1 < x < 1. Recall (1.2.1) that
the symbol . / is used for elementwise division: divide 1 by each of the
elements of the vector x in turn and call the resulting vector y. The
4.3 Taylor polynomials 47
instruction 1/x makes no sense since you can't divide 1 by a vector. (In
versions of MATLAB before Version 4, y=1. /x was not acceptable either
and a space had to be left after the 1 so that the dot was not confused
with a decimal point.)
Typing
>> t=0:.05:2*pi;
>> x=cos(t);
>> y=sin(t);
>> plot(x,y)
and note how the plot changes to being circular. You can also use
>> axis('equal')
which makes the scales on the two axes equal no matter what the shape
of the figure. Thus
>> t=0:.05:2*pi;
>> x=2*cos(t);
>> y=sin(t);
>> plot(x,y)
>> axis('equal')
produces a picture of an ellipse where the major and minor axes have
their true ratio of 2:1. Replacing axis (' equal') with axis (' square' )
does not have the same effect. Try it!
There are parametric curves in the exercises and investigations which
are more exciting than this!
(2k - 1)!
48 Graphs and Curves
Fig. 4.2. Sine curve (solid) and Taylor approximation of degree 9 (dotted).
The M-file tsine . m calls for the value of k (this is the number of terms in
the polynomial), then plots the `true' curve in green and, once <Enter>
is pressed, plots both this and the approximation in red. MATLAB uses
first the scale appropriate to the sine curve alone and then the scale
appropriate to the two curves together. Remember that to run this M-
file you just type tsine followed by <Enter>. You never type the m
when running M-files.
In Figure 4.2 the sine curve is the solid line and the Taylor approxi-
mation with k = 5 (that is, degree 2k - 1 = 9) is the dotted line.
The M-file tsine2.m on the other hand plots the actual sine curve (in
green) and then freezes the scaling while the approximation is plotted
(in red). This is achieved by the command axis (axis) after the first
plot command in the M-file. Notice the difference with say k = 4.
The approximation hugs the true curve more and more closely from
x = 0 onwards, as k increases. By the time k reaches about 10, the
curves are more or less indistinguishable over the range from 0 to 27r.
Here is the text of the M-file tsine. Even if you don't follow all the
details, you should note how easy it is to do this relatively complicated
thing in MATLAB.
Draws the Taylor approximation
to a sine curve of degree 2k-1
k=input('Type the number of terms
4.4 Approximations using the function polyfit 49
Fig. 4.3. Sine curve (solid) and `polyfit' approximation of degree 3 (dotted).
plot(a,b,'g')
hold off
This finds a polynomial approximation of degree 5 to the sine curve over
the range 0 - 6.3, using a for a change as the independent variable. The
polynomial approximation c is simply a list of coefficients;, to evaluate it
at the points of a we use the function polyval. The first `plot' command
draws the approximation in red and the second `plot' command draws
the true sine curve in green. Notice how completely different this ap-
proximation is from the Taylor approximation of the same degree (given
by k = 3 in the M-file tsine.m).
Figure 4.3 shows the result of running polyex. m with degree 3.
See Figure 4.4 and 4.7.1. Note that cos-1 is the inverse cosine, which in
MATLAB is acos. It does not seem very feasible to solve this equation
explicitly! For a graphical solution the left-hand side of (4.1) can easily
be plotted-the M-file goatgr.m does this. It should be clear from the
plot (also perhaps from looking at Figure 4.4) that there is a solution
somewhere around x = 10. One way of getting MATLAB to calculate an
accurate value of the solution is to use the built-in function fzero. For
this we need an M-file containing a definition of the function in question.
In this case it is goatfn.m and is as follows (this function is available to
you; no need to type it in!):
function y=goatfn(x)
y=2*(x.*x-200).*acos(x/20)-x.*sgrt(400-x.*x) + 100*pi;
end;
Note that the MATLAB command goatfn will produce an error, but
say goatfn(10) will give the value of the function when x = 10. What
happens when you type goatfn(25) ? What in fact is the `domain' of
the goat function, that is, the range of values of x for which y is real?
We now type
a=fzero('goatfn',10)
q=2*t;
r=-t-2*t.*t.*t;
Note the use of ones(size(t)) when the function is simply a con-
stant equal to 1. If it had been p(t) = 5 say, then we would use
p=5*ones(size(t)). Note also the use of .* for cubing each value
of t. The form r=-t-2*t. "3 also works.
Other possibilities for p, q, ,r are considered below.
The M-file parnorm.m requires input of
2.5
1.5
0.5
NM
0
-0.5
-1
-2 -1.5 -0.5 0 0.5 1 1.5 2
Fig. 4.5. The normals to the parabola y = x2 and their `envelope', a downward
cusped curve.
the lines are drawn actually emphasises this cusped curve by drawing it
in white. See Figure 4.5.
The M-file parnorm. m is a special case of the M-file linen . m, which
draws a family of lines
p(t)x + q(t)y + r(t) = 0,
where p, q, r are any functions of a parameter t, over some pre-defined
range of values of t. Thus we obtain the M-file parnorm.m from linenv.m
by specifying the functions p, q, r in the correct place (see below). It has
been adapted to draw the parabola in red and (after pressing <Enter>)
the envelope curve itself in white, by adding a few lines to the file. See
Exercise 4.7 for a method of calculating the envelope curve. You can
read more about envelopes in Chapter 10.
The general M-file linenv.m is incomplete: before it will run you
need to specify the functions p, q, r, in the following place:
Exercises
4.1 Try
>> a=1;
>> x=-5:.05:5;
>> y=x.*x.*x - a*x + 1;
>> plot(x,y)
Can you formulate the general results here, for any integer
a>2?
4.6 Show that the perpendicular bisector of the line joining (a, b) to
(c, d) has equation
2x(a - c) + 2y(b - d) -a2 -b2+c2+d2 =0.
(Hint: Use the equal distance property: (x, y) is on the perpen-
dicular bisector if and only if its distances from (a, b) and (c, d)
are equal. This gives
(x - a)2 + (y - b)2 = (x - c)2 + (y -
Deduce that the perpendicular bisector of the line joining
(0, 1) to (t, 0) has equation
2xt-2y+1-t2=0. (4.3)
X = -4t3, y = 2 (1 + 6t2).
This is the downward cusped curve in Figure 4.5. We can find
the equation of the curve in x, y coordinates by eliminating t.
Thus 6t2 = 2y - 1 so 216t6 = (2y - 1)3. Also 4t3 = -x so
16t6 = x2. Hence 216x2 = 16(2y - 1)3, that is,
27x2 = 2(2y - 1)3
4.7 Appendix
4.7.1 Proof of the goat equation
Referring to Figure 4.4, the region the goat, tethered at G, can graze
is enclosed by a circular arc from G along the boundary of the field to
A, then along a circular arc centred at G to B and finally along the
boundary of the field back to G. The area of this region is the sum of:
(i) the area of a circular sector of the circle centre G, radius x,
bounded by the straight lines BG, GA and the arc centred at
G from A to B;
(ii) twice the area of the piece of the field below the chord BG.
The area of the latter piece is the area of a circular sector of the field
from C along the radius to B, along the circumference to G and along
the radius to C, minus the area of the shaded triangle.
Let 0 be the angle CGA (equal to the angle CGB). The area (i) is
2x2(20) and 0 = cos' Zo, so the area is x2 COs-1 20. The area of the
piece in (ii) is
(d_n22)
s2 (dz - d)2 =
nI
I
60
5.1 Data analysis 61
can be calculated with MATLAB. Here note that sorting is only done in
the ascending order; so what result would the following produce?
>> D1 = - sort(-D)
A useful aid for displaying data is the histogram. The MATLAB com-
mand is simply hist; for example, hist(D) will display the histogram
of the data set D. By default, the interval min(D) < x < max(D) is
partitioned into ten equally spaced subintervals and the height of the
bars is the number of data in the subinterval. We can vary the number
of bars; to have three subintervals, type
>> hist(D, 3)
So, here, the intervals are 1 < x < 3, 3 < x < 5, 5 < x < 7 and the bars
are centred on 2, 4, 6 respectively. To find the sizes (y) of the groups,
type
>> y = hist(D, 3)
and to find the sizes (y) of the groups and mid-points (x) of the intervals,
type
>> [y x] = hist(D, 3)
Here the results are y = [3 1 1] and x = [2 4 6].
There is a link with bar charts: type help bar to find details on
using bar and you will see that bar (x, y) produces an identical plot to
hist(D, 3).
62 Representation of Data
5.1.1 Sorting
Often it is necessary to sort a number of data sets so that one of them
is in increasing or decreasing order. For instance, when there are two
related data sets, the second set must be reordered consistently with the
first set.
An example of this is in the M-file tomato.m, which compares the
amount x of fertiliser applied to a tomato plant with the weight y of
tomatoes produced.
Typing tomato produces two vectors x and y but the entries in x
are not in increasing order. (You can see the vector x by typing x, of
course.) Typing plot (x, y, ' g*') produces a scatter plot of the data, x
against y, the symbol used being green `*'.
Typing plot (x, y) produces a line plot, but because the data are not
ordered it appears as a scribble rather than a graph. One way of making
a proper plot is by typing
[sx k] = sort(x);
sy = y(k);
plot(sx, sy)
The first line sorts x into increasing order, noting the positions of their
components in the original order in the vector k. The sorted x are placed
in a vector sx. The second line reorders y so that their components
correspond to those of x correctly. Type [x y sx sy] to see the four
columns of figures at once.
You will find that the line plot now looks more like a graph. Finally,
as we mentioned earlier in this chapter, it is easy to sort data into a
decreasing order with sort by taking the negative of the values.
5.1.2 Querying
Querying or extracting data lies at the heart of databases, now increas-
ingly used in various management information systems. Here we shall
take two simple examples to illustrate. The essential quantity to specify
in using query is a condition often called the criterion. In MATLAB,
this condition can be represented by a vector (or a matrix) with Os and
is, meaning `not satisfied' and `satisfied' respectively.
As a first example, consider those tomato plants which give more fruit.
The task is to show a line plot of fertiliser versus weight for those with
over 5 kg of fruit. To identify such cases out of the entire data set, we
5.2 Least squares fitting 63
use a vector d with is and Os, count the total number t of the Is, and
extract the satisfied cases in new vectors x1 and yl (both sized t):
>> tomato;
>> d = (y>=5); % Query Condition !!!!
>> t = sum(d) % Number of `satisfied'
>> [v k]=sort(-d); % Bring `satisfied entries' to start
>> k = k(1:t) % Shorten the index to `t' (Query)
>> xl=x(k); yl=y(k); % Pick up query results from x,y
Here, A is the matrix appearing above, with is down its first column
and x down its second column. The second line instructs MATLAB to
find the best approximation c to a solution of the equations Ac = y. As
defined before, c(1) = a and c(2) = /3.
t Mathematically the solvability depends on whether vector Y lies in the span of
the two column vectors of A! As rank(A) < 2 and often in practice n > 2, the
system is more likely to be inconsistent, that is, have no solution!
t Further details are given in an appendix to this chapter.
5.2 Least squares fitting 65
>> Y=A*c ;
>> r = y - Y
>> R2 = r'*r
>> C = polyfit(x, y, 1)
to find the best-fitting curve of degree 1 (that is, a line). This gives
C(2) = a and C(1) = 03. Notice that c was a column vector while C is
a row vector! To find the residual of the least squares fit using the row
vector C, type
>> Y = polyval(C,x);
>> r=y - Y;
>> R2 = r'*r
to obtain the coefficients C(1) = ,32i C(2) = /31 and C(3) = a. To find
the residual of the least squares fit using the row vector C, type
>> Y = polyval(C,x);
>> r = y - Y ;
>> R2 = r'*r
Typing help polyf it, you will find out that polyf it can fit a poly-
nomial function of any degree k to data sets x and y. The general
function will be of the form
Y=/3kxk+...+/32x2 + ,31x+a
So with polyf it, higher order polynomials can be considered with ease.
But it cannot deal with the multiple variable case as discussed in 5.2.3.
For those who enjoy the beauty of the \ method, you will be pleased
to know that this can be done. With the following hint on how to set
up A, you should obtain the same solution as with polyf it
5.2 Least squares fitting 67
Remark: The nonlinear relationship between data sets x and y need not
be polynomial; they may be other functions, for example, we can try the
following
Y=132e-x+/3ieX+a
or
y=a+131x(1)+...+0Px(P)
in which x(i) occurs only to degree 1. In the example below, the response
y to treatment of diabetics is assumed to depend on three factors, x(1) =
age, x(2) = weight and x(3) = diet (which is converted into a numerical
value according to some scheme).
We are often dealing with discrete data sets and so we assume p vari-
ables (or `input' factors) and n values ('output' observations) of each
variable which are put into a vector:
Y [Y1 Y2
... yn1T
X(P)X(P)
[ 1 2
... x(P) T
j
(that is, a particular set of values for coefficient vector c) that minimises
n
E[y2 - Y12,
R2 = IIY - YI 2 =
i=1
5.2 Least squares fitting 69
Y1 Yi 1 x11 x12 x1
Y2 Y2 1 x21 x22 x23
Y= Y= . A=
yn Y. 1 xn1) xn2) xn3)
The data sets will be kept as y, xl, x2, x3. You should find that
the coefficients are cT = [36.9601 - 0.1137 - 0.2280 1.9577] and
R2 = R2 = 567.6629.
Then one may infer that the following function (that is, our least
squares solution) describes the response to treatment
Y = Y(x(1), x(2), x(3)) = 36.9601 - 0.1137x(1) - 0.2280x(2) + 1.9577x(3),
pulls out the first and second columns of data and names them
as x (test marks) and y (homework) respectively. Now try the
following:
(a) Compute the least squares line (see 5.2)
Y=a+Ox
and the least squares line
X =ry+Sy.
In this problem it is not possible to state that one variable
is dependent and the other independent, but it is reason-
able to assume some relationship. If the relationship was
exact (that is, if the residual of the least squares fit was
zero), then the two lines would be coincident. Find the
equations of the two lines and describe briefly the method
you used to obtain your answer.
(b) Plot the data as a scatter plot, and superimpose on it
a plot of the two lines. Calculate the value of the angle
between the lines.t Again, describe the method you used
to obtain your answer.
Hint: you may use this formula
tan 01 - tan 02
tan(Bl - 02) =
1 + tan 01 tan 02
5.2 The M-file mannheim.m gives some production figures for car
power steering units together with the cost of production. After
running mannheim, the data will be in a matrix called mdata
of size 22 x 3. We shall name the first column of mdata as x
and second column as y. Now try the following:
t The relationship between data sets is known as correlation. Here the angle is an
indicator of how the data sets are correlated.
5.3 Appendix 71
X,
x(1) = j x < ry x(2) = rll 0, x < ry
tt ', x?'Y, x - 'Y, x ?'Y
with ry = 3000.
Firstly, work out the new data sets xl = x(1), x2 = x(2).
Secondly, use the method of 5.2.3 with the sorted data
and p = 2 to compute a least squares fit of the form
y = a +,1x(1) + 02x(2)
and find the residual of this fit. Finally, plot the non-
linear fit using plot (sx,sy,'*',sx,Y), where Y is, as
usual, the points of the nonlinear fit, as in 5.2.3. Does
the graph appear to fit the data substantially better than
the linear fit? Suggest a possible (real life) reason why
the fit using two straight lines is so much better than the
fit using one.
Hint What might happen once the number of units be-
ing produced exceeds a certain number? Try also help
mannheim. Note that x(1) = min(-y, x) and x = x(1) +x(2)
for all values of x.
EXTRAS Consider a further fitting
y = a + 011x(1) + 021x(2) (X(l))2
+012 + 022(x(2))2
or alternatively
(1) 2)
y=a+/31e D +/.32e
5.3 Appendix
Here we consider the minimisation of R2 by selecting a and 0; see 5.2.1.
To proceed, we first prove the famous Cauchy-Schwarz inequality.
72 Representation of Data
Theorem 5.1 (Cauchy-Schwarz inequality) For any x, y E Rn we
have
n
EIxjyjI <- IIXII2IIYII2.
j=1
R and a R2 = 0.
The least squares equations can be written out as
a
E(yi - a - /3x2)2 = (-2) J:(yi - a - /3x2) = 0,
as i i
C a - ,3x2)2 = (-2) E xi(yi - a - /3xi) = 0,
i i
5.3 Appendix 73
that is,
na +
li
(Xi)=Yi
Zl
xi l
a+ X2) xiyi.
x1
r11 x2
A= x=
\ 1 xn yn
ATAc=ATy.
det(ATA) = n E x2
i i
which is true if at least two xis are distinct! Then we can easily find the
unique solution for c = (a 3)T
75
76 Probability and Random Numbers
bi = rand(25,1) % A random column vector in (0,1)
rand('seed',O) % Reset the `seed' to DEFAULT.
A = rand(3,4) % 3x4 random matrix in (0,1)
rand('seed',13) % Reset the `seed' to 13
b2 = rand(25,1) % Duplicate bi !
and this vector represents time=[Year Month Day Hour Min Sec] so
we could use
>> rand('seed',time(6)) %or rand('seed',time(5)*time(6))
to link to the nearest second, though this doesn't always seem to work as
well as setting the seed yourself, preferably to an odd number. Numbers
in the range 10000-20000 (or actually the default 0) are good choices.
The simple MATLAB function rand generates a column of n random
numbers uniformly distributed in (0, 1) by x=rand (n, 1), or a row by
x=rand (1, n) . Similarly, rand (n) generates a n x n random matrix
while rand(m,n) produces a m x n matrix with entries in (0, 1).
To generate random numbers in any interval (a, b), use a linear trans-
formation (x --* y), for example
>> x = rand(1,30); % 30 random numbers in (0,1)
>> a = 12; b = 99; % set up the interval
>> y = a + (b-a) * x; % 30 random numbers in (a,b)
Notice that the elements of vector y are now within (a, b).
>> round(10*x)
generates n numbers which do not take the values 0, 1, . . . , 10 with equal
probability. (Can you see why? Compare the x-values rounded to 0 with
those rounded to 1, and consider also round( 9.99 x + 0.5 ).)
If we simply want to generate a random permutation of k integers
1, 2, ... , k, use the command randperm; for example, we may permute
the rows of a matrix in a random manner
>> r = randperm(9)
>> A = ceil( rand(9)*5 ) Generate a matrix in (1,5)
>> B = A(r,:) Permute rows via "r"
The second line here instructs the computer to print 0 if x `fails' the test
x > s and 1 if x `passes'. Assuming that x has a uniform distribution
in [0, 1] you should get Os in approximately s of the cases. You can test
this by typing sum(c)/100 and seeing whether the answer is close to 3
However, a more challenging method than c=(x>1/3) is the following
y= 1 2 2 1 3 2
3 3 4 2 1 1
z= 0 1 1 0 2 1
2 2 3 1 0 0
where seed is similar to that used for rand, but is specific to randn!
Using again a linear transformation, to obtain numbers with mean m
and standard deviation s, type
>> y=m+s*x;
For your convenience we have developed an M-file called normrand.m.
Example To simulate heights of people in two different regions, both
with mean m = 3 and standard deviation s = 5, we take a sample of
2000 people from each region and do the following
>> randn('seed',11) % Set 'seed' to 11
>> y = normrand(3,5,2000,2);% N(3,5) with mean=3 & Std=5
>> hist(y) % Plot the simulated data y
or without normrand.m
>> randn('seed',11) % Set 'seed' to 11
>> x = randn(2000,2); % N(0,1) of mean=0 & Std=1
>> y = 3 + 5*x; % Transformation
>> hist(y) % Plot the simulated data y
and you should find that mean(x) = [0.0318 - 0.0346], mean(y) _
[3.1591 2.8270] and std(y) = [4.9989 5.0467].
Again as with rand, to obtain random integers, combine randn with
any of the functions ceil, fix, floor and round. To get, for example,
seven columns of nine whole numbers with normal distribution of mean
m = 3 and standard deviation s = 4, type
>> n1 = 9; n2 = 7; x = randn(nl,n2); m=3; s=4;
>> y = round(m + s*x);
This example is available in the M-file c6exp. m. The results can be seen
in Figure 6.1, where one graph shows an exponential distribution! The
mean and standard deviation of a negative exponential distribution are
both f = m and so you may use MATLAB commands mean and std to
verify that, for z, both quantities are close.
As discussed in 6.6, in practice, inter-arrival times satisfy the negative
exponential distribution. For instance, take the example of five arrival
times (occurrences) of an event as arr = [1.3 2.4 5.1 6 8.3]. Then inter-
arrival times are the time differences between arrival times (occurrences),
that is, int_arr = [1.3 1.1 2.5 0.9 2.3] counting from t = 0.
So we can simulate inter-arrival times by exprand. m but how do we
work out arrival times then? Each arrival time is a cumulative sum of
inter-arrival times; for the above example, given int_arr, we work out
arr by
6.5 Simulating negative exponential distributions 81
200
100
0
0 0.5 1 1.5 2
4000
2000
0
-4 -3
-0
-2 0 1 2 3 4 5 6
6000
4000
2000
0
0 1 2 3 4 5 6 7 8 9 10
For the above example in Figure 6.1, the cumulative sum for z can be
written as
k
tk=Ez2, k=1,2,...,20000.
=1
Here t(1) = 0.2966 and t(20000) = 24036.2043.
Example There is a concern about the long queues at a bank cash
machine and we have been asked to simulate the arrivals during peak
times of the day. It is known from computer records that (on average)
there are 131 customers between 9 am and noon. In order to simulate
the arrival of the first 50 customers, first work out the mean f = isi
since the average inter-arrival time is 13 minutes. Hence type
82 Probability and Random Numbers
m = 180 / 100; rand('seed',O);
z = exprand(m,50,1); hist(z)
Exercises
6.1 An electrical retailer estimates that, in the weeks before Christ-
mas, there are, on average, 140 customers per day for electronic
games, with inter-arrival times satisfying a negative exponential
distribution. (A shop day is 9 am-5 pm.) On average 3 pay cash
and the rest use a credit card, 50% of the sales are for Nintendo
(Super Mario) and 50% are for Sega (Sonic the Hedgehog). You
are asked to simulate (the first) 30 sales from the opening time,
assuming that each customer buys a product.
(a) Simulate the inter-arrival and arrival times for the first
30 customers of the day. Find the mean and standard
deviation for the inter-arrival times.
(b) Print out a table simulating the first 30 sales in the form
of Arrival Time, in minutes with 1 decimal place, Pay-
ment Method (Cash as +1 and Credit as -1) and Product
Sold (Nintendo as +1 and Sega as -1)
Hints:
Your answer to question (b) will be three columns (all
of size 30):
one set of arrival times,
one set to select cash or credit card and
one set to select Nintendo or Sega.
Consult 6.2 to see how to obtain numbers which are
0, 1 with probability 13 , 3, or both with probability 21.
6.6 Appendix
6.6.1 Generation of random numbers
The popular approach of generating random numbers is by the so-called
multiplicative congruential method. It is implemented in the MATLAB
command rand (' seed' , j) where j is an integer as discussed before
and is called the MATLAB 4 generator. Another approach of gener-
ating random numbers is based on scaled Fibonacci numbers with a
shift (see Chapter 3). This is implemented in the MATLAB command
rand('state', j) where j is an integer and is called the MATLAB 5
generator. For example with the default j = 0,
>> rand('state', 0); rand '/,I, gives rand = 0.9501
>> rand('seed', 0); rand %% gives rand = 0.2190
does! For example we have used a=randme (4) with seed=2 and shift=0,
taking K = 32 and M = 5, to produce the following `home-made' four
random numbers: rand = 0.8 , 0.6, 0.2, 0.4.
To find out what the current seed is, type s = rand('seed ' ), while
rand('seed ' ,45) is to change the seed to 45. Therefore, to observe a
sequence of seed changes in MATLAB type
exp (x
f (x) _a 2a2)
or equivalently the cdf
/ ( )2 l
J- exp { (t } dt.
F(x) v 1 2- l
2Q2 JJJ
giving (P = A-')
z = -flog(1 - x).
This is actually what M-file exprand.m does. For the bank example in
6.5, where there are 131 customers (arrivals) in 180 minutes, the average
inter-arrival time is = ho = 13 minutes. Therefore, the probability of
an arrival (event) in any short interval (t, t + ho) is P = 1 so
lim
P{(t,t+h)} N P{(t,t+ho)} _ 1/ho = 131/180-A.
n-0 h ho
Of course, = 1/A as expected.
7
Differential and Difference Equations
0.9
0.8
0.7
0.6 N\\\\\\\\\\
x 0.5
\\\\ \ \ \ \ \ \
0.4
N\\ \\\\\\\\
0.3
0.2
0.1
0L_
Fig. 7.1. Grain plot for the ODE dx/dt = -x. The curve is a particular
numerical solution.
K=r=1 .
Of course you can use fodesol to study any first order equation irre-
spective of the original choice of variable name used for the independent
and dependent variables (here t and x respectively).
Explore solutions x(t) which have xo = x(to) < 1 and also those with
xo > 1.
Another interesting ODE to look at is
dx
=x -x'
dt t
-3<t<4, -3<x<4 .
You should create another version of fnxt . m and look at its solutions.
7.2 Systems of differential equations 91
are first order and coupled since the derivative of x, say, depends on
y as well as on x. They are also a bit special since t is not explicitly
involved on the right-hand side. They are called autonomous differen-
tial equations for this reason. Equations like these are widely used to
describe the interaction of competing species as shown in the example
below equations (7.7). First some theory.
Fig. 7.2. The phase plane for the system of equations 7.7 for some choice of
parameters. The curves are particular numerical solutions.
tions
x(a - bx - cy), y = y(d - ex - fy) (7.7)
for the fixed points (x, y) in terms of a, b, c, . . . , f . You then just need
to substitute values for a, b etc to get the fixed points (at most 4 of
these) for each case. This helps you start your exploration. In each
case, how many different solutions (fixed points) did you find which had
x > 0 and y > 0? You are, of course, only interested in solutions with
non-negative numbers of each species! The differential equations may
well have solutions which are not of relevance to the physical application
at hand.
Use species to explore the neighbourhood of each of the fixed points
noting the behaviour close to them. The stable points are easy to find
but the unstable points are more difficult. Notice that as you get nearer
to a fixed point the motion of the point slows down and when you go
further away it speeds up. As well as a graph, the M-file displays the
coordinates at the end of each time-step.
called the Van der Pol equation (k is some positive constant). It de-
scribes oscillations in some electrical component. It can be converted
into an autonomous system with variables v and w by setting v = dwldt.
Thus you get the pair of first order equations
dw
=
dt v'
94 Differential and Difference Equations
dv
dt = k(1 - w2)v - W. (7.9)
Here, n is a positive integer labelling the time measured in units of, say,
1 second, that is,
x(t) = xn, t= n x 1s.
It is not hard to verify that
xn = xo(l - ))n (7.11)
7.3 Difference equations 95
This is a second order equation since two differences Xn+2 - xn+1 and
xn+1 - xn are involved. (Try rewriting (7.12) in terms of differences.)
We look for a solution similar to (7.11)
xn = zn . (7.13)
Substituting we get
zn+2 - 5Zn+l + 6Zn = 0
or
zn (z2 - 5z + 6) = 0.
Ignoring the trivial solution z = 0, we solve the quadratic factor to find
z=2 or z=3.
So we get two solutions of the form (7.13) which must be added in the
usual way to give
xn = A2n + B3n
96 Differential and Difference Equations
as the general solution. Notice there are two arbitrary constants since
this is a second order difference equation. Just as for differential equa-
tions, analytic solutions like this one are not always easy to obtain.
However, it is usually simple to get the computer (for example using
MATLAB) to perform the iterations implied by such equations directly.
The M-file diffegn.m is set up to solve the above example in this way.
To begin type
>> diffeqn
and follow the instructions. You should look inside this M-file and
dfeqfn.m so you can understand how to use or modify them to solve
other similar homogeneous equations, or indeed general inhomogeneous
equations.
Try various different initial conditions including [xo, xl] = [1.0, 2.0],
[1.0,1.8] etc.
Exercises
In particular:
(a) Find y(2) given the initial condition y(O) = 1.5.
(b) Find y(3) given the initial condition y(O) = 0.055.
(c) Sketch the general behaviour of all the different types
of solutions which you expect, having studied the `slope
field' or `grain plot'.
7.2 From the competing species examples in 7.2.2, identify an ex-
ample of each of
(a) fixed point surrounded by an orbit;
(b) stable fixed point;
(c) saddle point.
In each case make a rough sketch of the behaviour of the trajec-
tories (solutions) in the neighbourhood of the fixed point, that
is, showing the direction of flow of the solutions.
Exercises 97
7.3 Modify the M-files vderpol.m and vdplfn.m to study the (non-
autonomous) second order differential equation
dew 2
dt2 - a(w - t) = 0 ,
101
102 Magic Squares
8.1 Introduction
This project is about magic squares. An n x n magic square is an n x n
matrix of real numbers with the following property:
All rows, all columns and the two `main' diagonals of the matrix add up to the
same number, r say, called the magic constant.
For example,
3 1 2
1 2 3
2 3 1
has this property for r = 6. The two main diagonals in this case are
3 + 2 + 1 (top left to bottom right) and 2 + 2 + 2 (top right to bottom
left).
There are many algorithms for producing magic squares with the ad-
ditional property that the entries are the integers 1, 2, ... , n2 in some
order. For example, with n = 3,
8 1 6
3 5 7
4 9 2
The MATLAB function magic does this (try typing magic (3) ). In this
project we shall not go into these algorithms but instead investigate the
algebra underlying magic squares, using your knowledge of matrices and
solution of linear equations.
Thus there are nine entries all, ... , a33. Explain why, writing down the
conditions for all rows, all columns and the two main diagonals to add
to the same number r, we get the condition My = 0, where M is the
8.2 Magic squares size 3 x 3 103
8 x 10 matrix
-1 1 1 1 0 0 0 0 0 0
-1 0 0 0 1 1 1 0 0 0
-1 0 0 0 0 0 0 1 1 1
-1 1 0 0 1 0 0 1 0 0
-1 0 1 0 0 1 0 0 1 0
-1 0 0 1 0 0 1 0 0 1
-1 1 0 0 0 1 0 0 0 1
-1 0 0 1 0 1 0 1 0 0
0 1 -1 1 -1 0
El = -1 0 1 , E2 = -1 0 1
1 -1 0 0 1 -1
1 1 1
E3 = 1 1 1
1 1 1
are all magic squares. Why does it follow from (ii) that every 3 x 3 magic
square A can be written uniquely in the form of a linear combination
A =,\1E1 + \2E2 +,\3E3
104 Magic Squares
for Al, A2, A3 E R? (Hint: You are being asked to show that El, E2, E3
form a basis for the three-dimensional subspace of magic squares. Since
there are three matrices Ei it is enough to show that they are linearly
independent, considered as vectors in R9.) What are the Ai when A is
given by (8.1) above?
(iv) A 3 x 3 matrix has, in addition to its two `main' diagonals, four
other `broken' diagonals:
all + a23 + a32, a12 + a21 + a33, a13 + a21 + a32, a12 + a23 + a31.
If, for a magic square A, we require in addition all the broken diago-
nals to add up to the same magic constant r, then the square is called
pandiagonal.
In general, an n x n matrix has two main diagonals and 2n - 2 broken
diagonals, and if all these and all the rows and all the columns add up
to the same r, then the matrix is a pandiagonal magic square.
In the 3 x 3 case, expand your matrix M by the addition of four rows
corresponding to the four broken diagonals and use the new matrix (M1
say) to show that the only pandiagonal magic squares are AE3 for A E R.
(Thus you should find from the row reduced echelon form of M1 that
only one entry of A is now arbitrary, and the others are all equal to this
one.)
(v) We now look at products of 3 x 3 magic squares. By (iii) the product
of two such squares has the form
(A1E1 + A2E2 + A3E3)(liiEl + 2E2 + /-i3E3)
Let
0 0 1
P= 0 1 0
1 0 0
end;
end;
end;
end;
end;
end;
end;
After the permutation p is found, you will want to calculate the permu-
tation matrix, then test whether it is magic. The quickest way to do this
is to use the criterion My = 0 as in Question (i) in 8.2. Now, however,
M will be the 10 x 17 matrix you have just found, and v will be a 17 x 1
column vector of the form (-1 all ... a44)T .
(iii) Add more rows to your matrix M to test for pandiagonal 4 x 4 magic
squares (see Question (iv) in 8.2 for the definition). Call the resulting
matrix M1. What is the dimension of the space now? Are any of these
given by permutation matrices?
(iv) By finding the reduced row echelon form of your matrix Ml, show
that every 4 x 4 pandiagonal magic square has the form
a-b-c+d+e -a+b+c+d+e a+b-c-d+e -a+b+3c+d-e
b+c-d+e b+c+d-e -b+c+d+e b-c+d+e
-a+2c+2d a-2c+2e -a+2b+2c a
2b 2c 2d 2e
matrix, that is, the sum of the entries in the leading diagonal-top left
to bottom right.)
108
GCDs, Pseudoprimes and Miller's Test 109
(i) Try running the M-file several times for say n = 500 and find the
average percentage with gcd equal to 1.
(ii) Adapt the M-file gcdran to find the percentage which have gcd 2,
and also the percentage with gcd 1 or 2 or 3. Thus for the latter case,
for example (4, 10), (6, 9), (12, 21) and (6, 35) count towards those with
gcd 1 or 2 or 3, whereas (5, 10), (7, 35), (30, 50) do not.
(iii) By a bar-chart, histogram or other means, display (for a single run
of say 1000 random pairs) the numbers of pairs which have gcds from 1
to 20. Hint: The best thing is to use a vector, say v, of length 20, whose
kth entry records the number of pairs with gcd equal to k. So you start
by initialising
v=zeros(1,20)
and then, if a gcd is calculated to be k < 21 you increment
v(x) = v(x) + 1;
At the end, bar (v) will display the bar chart.
(iv) Consider the following `argument', writing it out for yourself with
any explanations which you feel able to offer.
Let x be the probability that a randomly chosen pair of numbers has
gcd equal to 1, that is, the pair is coprime. Recall that for integers a, b,
the symbol a I b means `a is a factor of b', that is, b/a is a whole number.
Now
h=1
ri (1- 1p2
P
,
taken over all primes p = 2,3,5,7, 11 ..... This is the probability that a
and b are coprime.
An M-file called primes. m is available to you which produces a vector
p containing the primes < 5000. (So p(l) = 2, p(2) = 3, etc.) Run this
as usual by typing primes . How many such primes are there? Find the
above product over the primes which are < 5000. (This will be a good
approximation to the product over all primes.)
Does this agree with the probability x obtained in Question (iv)?
(vii) Take a random sample of triples of numbers a, b, c and find what
percentage turn out to be pairwise coprime. Here, a, b, c are pairwise
coprime if the gcds of a, b, of b, c and of a, c are all 1. Simply cubing the
probability x that two numbers are coprime does not (you should find)
give the probability that these three pairs are all coprime. Why do you
think this is? Warning: To express `x = y = z = 1' in MATLAB do not
write
x==y==z==1
since this statement will be `true', that is, take the value 1, if, for exam-
ple, x = y and z = 1. Instead, write
x==1 & y==1 & z==1.
Cl p)
GCDs, Pseudoprimes and Miller's Test 111
C1-p 12.
P
Hence the probability that at most one of the three is divisible by p is
the sum of these two which after rearrangement becomes
(1_)2(1_(Pl)2).
Now a, b and c are coprime if and only if, for all primes p, at most one
of the numbers is divisible by p. Assuming these are all independent
events the probability z that the three numbers are pairwise coprime
is the product of the expressions (9.1) for all primes p. You should
recognise the squared factor in front from Question (vi) above. Work
out the value of the other factor using the same vector p of primes you
used above. Now deduce the theoretical value of z.
Does this agree with the experimental value you found?
(ix) Consider the set of all pairs of numbers a, b where a and b are > 2
and < m. For various m, what percentage of these are coprime? How
close is this to the probability of random pairs being coprime?
to do this.
Make amendments to psp2. m as necessary to find all numbers < 5000
which are:
116
Graphics: Curves and Envelopes 117
0.5
-0.5
-0.5 0.5
up and start to repeat itself; (ii) the number of times the curve passes
through the origin before it closes up and starts to repeat itself; (iii) the
number of `petals' the rose has.
Then do a mathematical investigation as follows. Put t = (m - 1)0
in the parametrisation of the hypocycloid given in equation (4.2). Then
use the famous formulae
(You will need to remember the trigonometrical formula for sin(a - b).)
118 Graphics: Curves and Envelopes
-2 6
Use linenv.m to draw the envelope of these chords for some values of
m, say 2, 3 and 4. Print one of these out.
(ii) Verify that the point
in cost + cos(mt) _ m sin t + sin(mt)
X = y(t)
m+1 m+1
satisfies both equation (10.1) and the equation obtained by differenti-
ating equation (10.1) with respect to t. This is a special case of the
situation of Exercise 4.7, and shows that the above point Wt), y(t))
gives a parametrisation of the envelope of the chords. That is, the curve
which your eye picks out from the drawing of all the chords, where the
lines appear to cluster, is parametrised as above.
(iii) An epicycloid is similar to a hypocycloid (Exercise 4.5) but the
rolling circle rolls outside the fixed one. The parametrisation is (using
u for a reason which will become clear)
(iv) Amend hypocy. m so that it draws epicycloids. Note: You will need
to make one subtle amendment, namely `upper' should be redefined as
abs(a+b)+abs(d). If you name the parameter as u then you also need
to use u=tl: tstep: to instead of t=tl : tstep: tu. There is no need to
rename tl and tu. The rest of the changes are simply in the x and y
lines. Draw some of these epicycloids for small (integral) values of m
and compare with the envelope of chords.
(v) Prove the formula for the parametrisation of an epicycloid (in Ques-
tion (iii) above).
B Envelopes
Aims of the project
The idea is to look at two situations where a family. of lines creates an
envelope. One is a family of perpendicular bisectors similar to that en-
countered in Chapter 4 (Exercise 4.6), and the other comes from a sliding
ladder. The investigation is both mathematical and experimental.
Mathematical ideas used
These include parametric equations of curves, elimination of a variable
between two equations, tangents to curves and simple trigonometry. The
ideas on envelopes come from Chapter 4.
MATLAB techniques used
The first part of the investigation uses the M-file linenv. m to draw en-
velopes of perpendicular bisectors. You also need to draw an additional
curve on the same figure. The second part involves the solution of poly-
nomial equations, drawing parametric curves and drawing envelopes of
lines.
Perpendicular bisectors
Investigate the envelope of perpendicular bisectors of lines joining the
point (a, 0) (where you can assume a > 0) to the points of the unit circle
x2 + y2 = 1, which is parametrised by (cost, sin t). Thus to begin with,
show that the equation of the perpendicular bisector is
2x(a - cost) - 2y sin t + 1 - a2 = 0 (10.2)
(see Exercise 4.6). Then use linenv.m to draw the envelope for various
values of a. The M-file should have a couple of lines added to it so that
it draws this circle as well, in red. Print out one example for a > 1 and
one for a<1.
120 Graphics: Curves and Envelopes
The rest of this part is a mathematical investigation to explain the
pictures just obtained. For the perpendicular bisectors given by equation
(10.2) use the method of Exercise 4.7, to show that the equation of the
envelope of perpendicular bisectors is, for a 1,
z
4x2 - 4ax + 14ya2 = 1-a2.
So you have to eliminate t between two equations, namely equation
(10.2) and the derivative of this equation with respect to t. One way
to eliminate t is to arrange the two equations as equations for sin t and
cost, solve for these two, and then use sin2 t + cost t = 1.
What is the envelope if a = 1? Is there a simple geometrical explana-
tion for this?
Assume a # 1. Make the substitution X = x - 2 to reduce the
equation of the envelope to the form
2
4X2 + 4y = 1.
1-a2
What kind of curve is this? Explain with examples how it fits with the
pictures.
Can you find any significance in the particular perpendicular bisectors
which arise when the line joining (a, 0) to the circle is tangent to the
circle? Of course this requires a > 1. (Hint: Show that these correspond
with cost = 1/a and have equations X,/a2 - 1 = y.)
Sliding ladders
A ladder of length 1 rests on the ground and on a vertical wall, just also
resting on a rectangular box of sides a and b, as in Figure 10.3, left.
Writing x for the length shown, show that f (x) = 0, where
P X) = x4 - 2ax3 + (a2 +b 2 -12)x2 + 2a12x - a212.
Take 1 = 10 and a = 2. Verify that b = 5 gives a physically possible
solution (that is, the equation has a root x which is physically possible),
but b = 6 does not. Illustrate with the graph of f but also use roots
to find the roots numerically. The M-file paramc. m draws parametrised
curves (x, y) _ (x(t), y(t)). In the special case of a function graph y =
f (x) you can take x = t, y = f (t), so you have to insert these in the
correct place in paramc. m. (Do not confuse the y here with the y in
either of the diagrams! The M-file is a general purpose parametric curve
plotter, so x, y are the most natural variables to use in it.)
Take a = 2 and b = 5 and find all solutions when 1 = 20. Try also
Graphics: Curves and Envelopes 121
Fig. 10.3. Left: a ladder of length l leaning against a vertical wall and resting
on a box of dimensions a and b. Right: finding the equation of the ladder.
Note that x, y have different meanings in the two diagrams!
Fig. 10.4. All widths, measured between parallel tangents, are equal to 2.
that there are exactly two tangents parallel to any given direction. We
shall meet some more `singular' examples later.
See 4.6 and Exercise 4.7 for details of envelopes.
(ii) Let h(t) = 1+1 cos(3t). Amend the M-file linenv.m so that it draws
the envelope E of the lines l(t) for 0 < t < 27r. Print this envelope out,
taking care to adjust the limits of x and y so that the whole envelope E
is clearly visible on the picture.
(iii) The lines l (t) are of course all tangent to the envelope E drawn
in (ii). Check that h(t) + h(t + ir) = 2 for all t with the function h
given there. Why does this imply that the pairs of parallel tangents to
E are all a distance 2 apart, and hence that E is a curve of constant
width 2?
(iv) Recall from Exercise 4.7 that to find equations for the envelope we
take the equation (10.3), together with the equation
-x sin t + y cos t = h'(t) (10.4)
124 Graphics: Curves and Envelopes
obtained by differentiating the equation (10.3) with respect to t. (Here
and below,' stands for differentiation, d/dt.) Solve the equations (10.3)
and (10.4) to obtain
x (t) = h (t) cos t - h'(t) sin t y (t) = h (t) sin t + h'(t) cos t, (10.5)
Fig. 10.6. The line joining the two points of contact of parallel tangents is in
fact normal to the curve at both contact points.
(x) The equation (10.7) is not in quite the same form as equation (10.3),
because of the presence of a in the coefficient of y. However, the method
of solving for the points of the envelope E of the lines m(t) is still the
126 Graphics: Curves and Envelopes
same as before. Writing r(t) for the right-hand side of equation (10.7),
show that the points of the envelope are
x(t) = r(t) cost - r(t) sint, y(t) = 1(r(t) sint + r(t) cost). (10.8)
a
Why does the envelope E, together with the half-ellipse, make a curve
of constant width 2?
(xi) Amend your M-file constwl.m, or write another M-file, to draw the
envelope curve E given by equation (10.8) for it/2 < t < 37r/2, and also
to draw the original ellipse for the same range of values of t. Call the
result constw3.m.
(xii) Draw the following three examples of E:
a = 0.75, xl = -1, xu = 2, yl = -1.5;
a = 1.25, xl = -1.25, xu = 1.25, yl = -1.25;
a = 3, xl = -3, xu = 1, yl = -2.
As usual xl, xu, yl stand for the lower and upper limits of x and the
lower limit of y. In each case make a hand sketch of the half-ellipse and
envelope. You should find that the third curve is singular, that is, has
cusps or sharp points.
(xiii) By taking various values of a (and suitable limits for x and y)
determine as accurately as you can the range of values of a for which
the envelope E above is free from cusps. You need only consider a > 0.
(xiv) The remaining work here uses the theory of envelopes to find
Graphics: Curves and Envelopes 127
the range of values of a > 0 for which there are no cusps on the above
envelope. Writing equation (10.7) as
px+qy=r,
it can be shown that the condition for a singular point (cusp) to exist is
that (using ' for differentiation as before)
p q r
p' q' r' = 0
p"r q// r"
has a solution t between it/2 and 3ir/2. Using p = cos t, q = a sin t
show that adding the top row to the bottom row and evaluating the
determinant by the bottom row reduces this condition to simply r+r" =
0 (remember that a is nonzero). Hence show that the condition is v = 0,
where
128
Zigzags and Fast Curves 129
Fig. 11.1. The basic zigzag, defined by lengths of 100 and 1, and angles 01, 02.
In the right-hand figure, pi and p2 are rotations through 01 and 02 respectively.
obtained by rolling one circle on another and drawing the path traced
out by a point rigidly attached to the rolling circle. (Compare Investi-
gation A of Chapter 10.)
The basic idea is illustrated in Figure 11.1. A straight horizontal line
is drawn to the right from the origin, of length 100. At the end of this
another straight horizontal line is drawn of length 1. If 1 < 0 then the
line is drawn to the left and otherwise to the right; in either case it
terminates at (100 + 1, 0). At this stage we say that one step has been
completed.
Now the zigzagging begins. We have two angles 01, 02 given to us
(usually they will be whole numbers of degrees). We draw a straight line
of length 100 from (100+1, 0), at an angle 01 with the positive x-axis (so
this angle is measured anticlockwise from this axis). The termination of
this line is at (100 + 1 + 100 cos 01 i 100 sin 01). From this point we draw
a line of length 1 at an angle 02 with the horizontal, thereby arriving at
the point
(100+1+ 100 cos 01 +lcos02i100sin01 +lsin02).
At this stage, two steps have been completed.
The lengths of the added lines are always alternately 100 and 1, but
the angles between the added lines and the horizontal go up by 01 and
02 respectively at every step. Thus the third step consists of two lines at
angles of 201i 202 to the horizontal, the fourth step of two lines at angles
of 301i 302 to the horizontal, etc.
130 Zigzags and Fast Curves
We can write pi for the rotation through k01; the matrix for this is
obtained by replacing 01 by k01. For P2, we replace 01 by 02. Using this
notation we can write the position of the point at the end of k steps of
the zigzag as (see the right-hand part of Figure 11.1):
p2_lv2
vl + V2 + plvl + p2v2 + pivl + p2v2 + ... pi-lvl +
(v1 + plvl + plv1 + ... pi-lv1)
p2-l
+ (v2 + p2v2 + P2v2 + ... V2)- (11.2)
Zigzags and Fast Curves 131
Fig. 11.2. Part of a `regular polygon' with central angle B1 and centre cl.
Hence the point of the zigzag reached after k steps is, using equations
(11.2), (11.4) and the corresponding result about v2i
cl - picl + c2 - p2c2. (11.5)
So to find a good expression for the point that the zigzag has reached
we just need to find cl and c2. This is easy from equation (11.3) and
its counterpart for v2, but first we look at the number of steps.
The number of steps to closure
It is not obvious that the zigzag ever closes, of course. But so long as
132 Zigzags and Fast Curves
we stick to simple angles (for example, whole numbers of degrees) it will
close, and we can calculate the number of steps needed.
From now on we assume that 91, 02 are whole numbers of degrees.
From equation (11.5) it follows that the zigzag will certainly close if
both c1 = pic1 and c2 = p2c2. (It is conceivable that the zigzag closes
before both these happen. You might like to investigate that possibility!
But we shall say no more about it here.) Now if a nonzero vector cl is
returned to its original state by k rotations pi through 01 degrees, then
it must be that k01 is a multiple of 3600. Explain why this is the same
as
360
k = multiple of gcd(360, 01)
Explain why this is the same as saying that the zigzag will definitely
close when k = s, where
360
s= (11.6)
gcd(360, 01, 02)'
(Thus you need to show s is a common multiple of the two numbers
given above. In fact it is the least common multiple. .)
Use equation (11.6) to calculate the quantity `steps' that is, the num-
ber of steps to closure-in zz 1. m, using the available M-file gcdiv. m for
calculating gcds. Call the resulting M-file zz2.m. Check several exam-
ples to see that the zigzag does close. Write out the data which you
input to the M-file, and the calculated number of steps to closure.
11
sin 10,
2 , cos 2 Bl
2 sin 101
2 ( T
Hint: You will need to use the 'half-angle formulae'
Of course a similar formula holds for c2i all suffix is being replaced
by suffix 2s.
Now use the matrix for the rotation pi through k01, namely
( cos k01 - sin k01
sin k01 cos kGl J'
a similar matrix for p2, and equation (11.5) to show that the end of the
zigzag after k steps is at the point with coordinates
2
(This time you will need the well-known formulae for sin(a + b) and
cos(a + b).) Of course it is possible to incorporate the formulae obtained
above for c = cl + c2 but for most purposes it is better to use equation
(11.7) which gives the position relative to the `centre' c.
700
600
500
400
300
200
100
0
-300 -200 -100 0 100 200 300 400
Fig. 11.3. A zigzag (with l1 = 100, 12 = 40, 01 = 45, 02 = 9), and the
associated spirograph which passes through the ends of all the segments of
length 12.
The spirograph
The object here is to draw a `spirograph' curve which goes through
all the positions which the zigzag has reached after k steps, for k =
1,2,3 ..... In fact the curve is given precisely by equation (11.7), except
that instead of k taking only integer values 1, 2, ... , s, where s is the
number of steps (given by equation (11.6)), it takes on all real values
from 0 to s. Thus dividing the interval [0, s] into say 1000 parts we will
set up an array of values of k:
n=1:1000;
k=s*n/1000;
Thus k goes in 1000 steps from s/1000 to s. Plot the curve given by
equation (11.7) in white after the zigzag has been drawn, calling the
resulting M-file =4.m. Give examples. Figure 11.3 shows the case 11 =
100, 12 = 40, 01 = 45, 02 = 9. Can the spirograph close up before the
zigzag does?
2 2
(You need not check these.) A case of special interest is b = d which
means that the point P is on the circumference of the rolling circle. This
always produces cusps on the spirograph. Show that this amounts to
1101 sin 202
2
02 sin 201
B Fast curves
Aims of the project
We shall examine various candidates for a `fast curve': taking a smooth
wire in the shape of such a curve, we want to minimise the time taken
by a bead to slide down the wire. The `fastest curve' is known to have
the shape of a cycloid (see, for example, [17]) but here we shall also use
136 Zigzags and Fast Curves
where g is the acceleration due to gravity and m is the mass of the bead.
Now v = ds/dt, where s is the arc-length along the curve, so the time
t can be found by integration. Of course the value of g will depend on
what units are being used. In order to make the times of descent of a
reasonable size (round about 10), the value of g used in the M-files is
set at 0.1.
There are various forms of the resulting formula for the total time of
descent from (0, c) to (d, 0), depending on whether we integrate with
respect to x or whether both x and y are functions of another variable
0 say, as will be the case for the cycloid. We state these forms together
here; you can assume them during the project. Note that most of the
integrations are performed numerically using a MATLAB routine called
quad8. At the end of this project there is a note about evaluating the
integrals numerically.
The time of travel from (0, c) to (d, 0), where it is assumed that c >
0, d > 0 and that x increases steadily along the curve, is
1
d
1 + y/2 1 f91 x/2 + y/2
dx, (ii) d0 (11.10)
(1) 2g J0 c-y 0
c-y
where, in (i), y is a function of x and ' means dl dx, while, in (ii), x, y
are both functions of 0, ' means d/d0 and 00, 01 are the values of 0 at
the points (0, c) and (d, 0) respectively.
138 Zigzags and Fast Curves
Fig. 11.5. A curve joining the points (0, 2) and (4, 0).
t There are techniques called singularity subtraction techniques which are less naive.
Zigzags and Fast Curves 139
(d,0)
Fig. 11.6. A broken line joining (0, c) and (d, 0), the break point being (a, b).
result=zeros(length(m),1);
for i=1:length(m)
result(i)=time;
end % of i=1:length(m) loop
plot(m,result)
Note that the global statement in slide1.m MUST remain outside
the for loop! There is no need to change slidelfn.m.
For c = 2, d = 4 print out the graph of m against time of descent
and estimate from it the value of m which gives the minimum time of
descent. Draw a hand sketch of the corresponding parabola. (Was your
guess right?!) For c = 2, d = 3 use the same method to find the `fastest
parabola' and sketch it, but do not print out the graph of m against the
time.
Broken lines
Suppose we join (0, c) and (d, 0) by two straight segments, joining at the
`break point' (a, b) say. See Figure 11.6. We shall look for the fastest
broken line. So here c and d are given, and a and b can be changed.
(iv) Show that the equations of the two segments are
ac)x x
y=c+ (x < a), y = b(a (x > a).
(b
- d)
(v) For a straight line descent it is actually possible to calculate the time
exactly, by evaluating the first integral in equation (11.10). Consider a
line of slope m, between (xo, yo) and (xl, yi); note that m will generally
Zigzags and Fast Curves 141
c-
dy_
mVg
2g
c-yo- c-yl). (11.11)
Taking c = 2, d = 4 as before (and 0 < a < d, 0 < b < c), show that
the time of descent along the broken line is
2 ( Va2 2(2 b b)2 + (f -)b (4 - a)2 + b2
(11.12)
You can use the built-in MATLAB function fmins to find the minimum
of the function of a and b defined by equation (11.12). You have to make
an M-file, say slide3fn.m, containing the function to be minimised.
This takes the form
function realtime=slide3fn(p)
g=.1; c=2; d=4;
a=p(1); b=p(2);
realtime = ;% Formula for the real time of descent,
as above
You then call fmins by fmins ('slide3fn' , [*, *] ' ), where the asterisks
are replaced by a sensible guess as to the values of a, b which give the
minimum. (Note the ' after the vector, indicating transpose.) Make a
sensible guess and find the values giving the minimum time of descent.
(Note that if you make a guess of say a = 1, b = 2.1 > c then, as you
might expect, fmins does not give a reasonable answer.) Draw a sketch
of the broken line which achieves the minimum time. Also find the
corresponding minimum time. How does this compare with the fastest
parabola?
(vi) An interesting case of equation (11.12) is when b = 0, which makes
the second term now of the form 0/0. Use L'Hopital's rule on the ex-
pression
rO
Fig. 11.7. One complete arch of the cycloid starting at (0,2), generated by
rolling a circle of radius r = 1.5 underneath the line y = 2. The cycloid is
parametrised by 0.
Show, using calculus, that the value of a between 0 and 4 which makes
this a minimum is 2/v'-3-.
The cycloid
A cycloid curve is obtained by rolling a circle along a straight line and
tracing the path of a point attached to the circumference of the circle.
In our case we roll a circle of radius r `under' the straight line y = c. See
Figure 11.7. The cycloid is parametrised by the angle 8 in the figure;
the coordinates of the moving point P are
x=r(8-sine), y=c-r+rcos0. (11.13)
Figure 11.7 shows a complete `arch' of a cycloid, for 0 < 8 < 21r. Note
that 0 = 0 gives (x, y) = (0, c) = (0, 2) in the figure.
As before we take c and d as given. We want the cycloid to pass
through the point (d, 0); in order for this to happen we need, for some
r>0and0<8<21r,
rsinO-rO+d=O, rcos8-r+c=0. (11.14)
of MATLAB lacks f solve then you could try using one of the custom
written M-files from Chapter 15 called full_new.m and gauss_ja.m.)
(vii) Taking c = 2, d = 4, find the solution of equation (11.14) as follows.
Create a function file say cycfun.m of the following form
function q=cycfun(p)
c=2; d=4;
r=p(1); theta=p(2);
q=zeros(2,1);
q(1)=r*sin(theta)-r*theta+d;
q(2)=r*cos(theta)-r+c;
where p is the coefficient of (kinetic) friction. This means that the bead
is acted on by gravity downwards, the normal reaction of the wire, and
friction, which is p times the normal reaction. The equation for v then
follows from Newton's second law of motion (you need not check this
unless you want to!).
As a result, the formula (11.10)(i) for the time of travel becomes
1 0d 1 + yrz
dx.
VI-2-g c - y -x
Adapt slidei.m and slidelfn.m to find the fastest parabola when =
0.3. Call the M-files slide6.m and slide6fn.m . Beware that taking
values of p larger than about 0.5 may result in `infinite' times of travel
along some parabolas. Why is this?
12
Sequences of Real Numbers
where a, b, c, d are real numbers. Once x0, also real, is given, the whole
sequence is determined and consists of real numbers. It is called a
Mobius sequence. There are two M-files available. The first is mob ius . m
which calls for the numbers a, b, c, d, x0 and the number of iterations
to be performed, that is, how many terms of the sequence are to be
calculated. It then plots a bar-chart of the values.
The second M-file is mobius i . m which dispays the values of the suc-
cessive xi as a column vector, using `long format' for greater accuracy.
(i) (a, b, c, d) = (1, 2, 1, 1), x0 = 3. It is fairly clear from mobius.m and
mobius1.m (use 20 iterations) that this sequence is convergent. The
limit also looks suspiciously familiar. Running with other values of x0
produces the same result. Note that some values of x0 produce 00 on
the way, as with x0 = -1.5 for example. (Here, x2 = 00.) However,
146
12.1 Mbbius sequences 147
a=input('Type a
b=input('Type b ');
c=input('Type c ');
d=input('Type d ');
xO=input('Type xO ');
n=input('Type the number of times to be iterated ');
X= [x0] ;
for j=1:n
if x(j)==Inf I x(j)==-Inf
x(j+1)=a/c;
else
x(j+1)=(a*x(j)+b)/(c*x(j)+d);
end
end;
bar (x)
Figure 12.1 shows two examples of cobweb diagrams, one in fact for a
Mobius sequence and one for a quadratic sequence (these are discussed
in Project B below). Possibly the second one gives more of an idea of
why the name `cobweb' is used!
Mobius sequence cobwebs
The M-file cobm.m plots cobweb diagrams for Mobius sequences, which
use the function f (x) = (ax+b)/(cx+d). It requires input of a, b, c, d, x0
and the upper and lower limits of x and y on the diagram. Iteration is
done 30 times, or until it seems that convergence is certain. The sequence
of values of x is printed out at the end.
Note that, when there is a vertical asymptote to the graph of the
function f, this will be replaced by a vertical line joining two points of
the graph. Here are some suitable values:
1 In Example (i) above, lower limits of x and y equal to -3 and
upper limits of x and y equal to 3, x0 = 2 or - 2. If you want to
see the action in close-up then you could take say 1.3 < x < 1.5
and the same for y.
2 In Example (ii) above, lower limits of x and y equal to -10 and
upper limits of x and y equal to 10, xo = 2.
3 In Example (iii) above, lower limits of x and y equal to -5 and
upper limits of x and y equal to 5, x0 = 3.
It is fairly clear in each case what is happening.
The points where the graph of f meets the line y = x are fixed points
of the function y, that is, writing y = f (x), they satisfy f (x) = x. It is
these points, if any, which are limits of the sequence.
AI I =al x11 I
1
150 Sequences of Real Numbers
1.6
1.4
1.2
0.8
0.6
0.9
0.85
0.8
0.75
0.7
0.65
0.6
0.55
0.5
0.45
0.4
0.4 0.45 0.5 0.55 0.6 0.65 0.7 0.75 0.8 0.85 0.9
Fig. 12.1. Two cobweb diagrams, the upper one corresponding to a Mobius
sequence and the lower one to a quadratic sequence.
In other words,
Ak (
In other words,
/ x0 ) wl
if A c then xk =
1 W2
Example 1
Let
A= 1 1 1 ), x0=3.
Then with v = (3,1)T, we have Av = (5, 4)T and the a in (i) of Theorem
1 is 4, while xl = 4. Taking k = 4 in (ii), A4v = (75,53)T and x4 =
53 = 1.4151; the value of 0 is 53.
Example 2
If you type
format long
A=[1 2; 1 11;
v=[2 1] ' ;
w=A- 20*v ;
w(1)/w(2)
then you should get the answer 1.41421356237310, which is the same
answer as you get by typing sqrt (2) . By (ii) of the Theorem 1, the
number obtained is x20 when x0 = 2.
The proof of Theorem 1 is straightforward: (i) is proved by multiplying
out the two sides and taking a = cx,,,+d. Then (ii) is proved by induction
on k. The case k = 1 is just (i) with n = 0. Assuming (ii) for k, we
proceed as follows:
Ak( )=AI I,
1 1
so that by (ii) of the theorem xk = x0i i.e. the values of the Mobius
sequence have recycled back to xo. So periodic sequences can be detected
by calculating powers of the corresponding matrix.
We can also work `backwards': for example, let us work out the start-
ing value x0 which, for the sequence
3xn, + 5
xni-1 =
xn+2
1
gives x6 = -2. Note that when x0 is chosen in this way, the next term
x7 will be `infinite', since the denominator x6 + 2 vanishes. We want
x
A6 ( 1 12 /
for some ,Q. This gives
-6 1-22658
\ 1 ) A 12)-( 2649
where you can verify the last equality by typing A" (-6) * [-2 1] ' hav-
ing first input the matrix
A=(13 5
2).
(You may find that -22658 actually appears as something like -22657.999
of course!) Thus x0 = -22658/12649 = -1.7913. If you use mobiusl.m
to find x7 for this sequence (input x0 in the above fractional form) you
will find that it gives a very large value. Nevertheless x8 is given cor-
rectly as 3.
There is a highly significant fact about the direction of the vector
w = Akv for large values of k. Going back to the matrix A of Example
2 above, try typing
[X D]=eig(A)
You will find that one of the eigenvectors of A is parallel to the vector
w obtained in Example 2. For example, you can type
xl=X(:,1)
y=x1./w
12.3 Mobius functions and powers of matrices 153
to pull out the first eigenvector of A and then compare it with w in direc-
tion. The two entries in the vector y should be approximately the same.
Note that the eigenvector used here is in fact the one corresponding to
the larger eigenvalue.
With matrices we can equally well consider 3 x 3, but note that this
does not have an obvious analogue in terms of sequences. Try
A= [1 2 3;-1 2 -3;1 -3]
-2
and (almost) any starting vector v. This matrix has one real eigenvector,
(-0.5433, 0.2849, 0.7897), as you can discover by typing [X, D] = eig (A).
This has the same direction as Anv for large values of n.
Note on two M-files
It can happen that powers of a matrix A have entries which become very
large indeed, and working out Anv for a large n can be hazardous. There
are two M-files available which overcome this problem when we only
want to know the direction of Anv. They work out Av, A2v, A3v,.. .
but after each step rescale the answer to be a unit vector. They are
called matit2.m for 2 x 2 matrices and matit3.m for 3 x 3 matrices.
Both require previous input of the matrix A but not of the vector v.
Look at them if you are curious.
For those who like to know the theory, we have the following.
Theorem 2 If A n v does have a limiting direction, then this limit must
be an eigenvector of the matrix A.
Proof If we suppose Anv/IIAnvIJ -+ w for some w 0 as n -+ oc, then
A(Anv/II AnvII) -p Aw, so
An+1V
IIAn+1vlI
IIAn+1vII
IAnvII -' Aw.
But, as n oo the first fraction here tends to w and the second to
some real number a, so we obtain Aw = aw, which means that w is
an eigenvector of A. Of course this does not prove that Anv does have
a limiting direction equal to an eigenvector, only that eigenvectors are
the only possible limiting directions.
2 1 1 -2
3 2 4 3
3 2 2
-/ /
1
1 3 -2 1
1 1
-1 1
aI = I A 0
),
where I is the 2 x 2 identity matrix. Write down the value of A in each
case. Also verify that, for (a, b, c, d) = (1, 1, -1, 1), the fourth power of
the matrix is a scalar matrix, and write down the value of A.
(iii) What is the connection between A4 = AI in the last part of the
previous exercise and the fact that the corresponding Mobius sequence
is periodic with period 4? (See 12.3.)
(iv) For which of the examples in Exercise (i) does the M-file matit2.m
give a limiting direction for the iterated product Anv? State the value(s)
of the vector v which you used and the limiting direction obtained.
Verify that this is an eigenvector of A by calculating the eigenvectors.
State both eigenvectors in these cases.
(v) For (a, b, c, d) = (1, 2, 1, 1), find the inverse of the corresponding
matrix (e.g. by using inv(A)). Hence find (exactly) a starting value
x0 for which x5 = oo. Is there any possibility of finding xo such that
X50 = oo?
(vi) This part has nothing to do with Mobius functions but involves pow-
ers of 3 x 3 matrices. Compare 12.3. A population of bugs is of three
kinds: black, red and green. At time t = 0 there are 10 black ones but
no red or green ones. At time t = 1, the following three things happen
simultaneously:
(a) each black bug splits into a black bug, 2 red bugs and a green bug;
(b) each red bug splits into 1 black bug and 2 green bugs;
(c) each green bug splits into 3 red bugs.
The same happens at times t = 2, t = 3, etc. The bugs in this problem
are immortal (this is obviously pure mathematics).
How can you use matrix iteration to discover, for large values of t,
what percentages of the population are black, red and green? Does the
initial population (at t = 0) make any difference? Is there a connection
with one of the eigenvectors of a matrix? Hint: Consider the matrix
1 1 0
12 0 3
1 2 0
156 Sequences of Real Numbers
Theory of Mobius sequences
Here you have the opportunity to work out some of the theory of these
sequences. Illustrate the theory with suitable examples, using the M-
files.
xn - a (ca+d) n (xo-/3
(ix) Show that f'(a) = (c/3+d)/(ca+d), and f'(/3) = (ca+d)/(c/3+d).
Hint: It is easy to check that f'(x) = (ad - bc)/(cx+d)2. Remembering
that a and /3 are the roots of cx2 - (a - d)x - b = 0, deduce that
(ca + d) (c/3 + d) = ad - bc.
(x) Deduce that if I f(a) 1< 1, then xn -+ a as n -> oo.
(xi) Show that if (a - d)2 + 4bc < 0, then the fixed points are not real.
Why does this imply that the sequence {xn} cannot have a limit?
B Attracting cycles
Aims of the project
This project is about sequences formed by iteration of a quadratic (de-
gree 2) function. These sequences do not generally have a unique limit,
but have `many limits' in the sense that successive values of the sequence
come close to a collection of different numbers. We shall investigate these
`attracting cycles' both mathematically and experimentally. More de-
tails of the mathematics involved here can be found in [2]. There is
12.3 Mobius functions and powers of matrices 157
(12.2)
Put the coefficients into a vector p (starting with the leading coefficient
1) and find the solutions with roots (p). Ignore negative real roots and
complex roots. One root should be 2; check that for this A, f(i2) = i2
For the other root, use cobq. m to verify that there really is an attracting
3-cycle, and write down the numbers in it (you should get the same
limiting numbers whatever x0 in (0, 1) you start from).
(v) For p = 4 there is a solution near to A = 3.5. Find this solution
to several more decimal places, using quadn.m, and use it in cobq.m to
find the numbers in the resulting attracting 4-cycle.
(vi) Show that the graph y = f (x) crosses the line y = x when x = 0 or
x = 1 - 1. Deduce that, for 0 < A < 1, the graph of y = f (x) is entirely
under the graph of y = x for 0 < x < 1. Explain with a (hand-drawn)
cobweb diagram how this makes it very plausible that, for any x0 with
0 < xo < 1, the resulting quadratic sequence is convergent with limit 0.
For 1 < A < 2 show that the graph y = f (x) crosses the line y = x
just once for 0 < x < 1, namely for x = 1- a . Calculate the slope of the
graph y = f (x) where this happens, showing that it is positive. Again
using a hand-drawn cobweb diagram explain why this makes it plausible
12.3 Mobius functions and powers of matrices 159
Fig. 12.2. The `period doubling' diagram. For each value of A on the horizontal
axis, the points above it are the values of x in the resulting attracting cycle.
So from A around 3.5 there are longer and longer attracting cycles.
that, for 0 < x0 < 1, the resulting sequence is convergent. What is the
limit this time? Confirm your assertion by means of some examples,
using the M-files.
(vii) The M-file perdoub. m (for `period doubling'!) takes 100 values
of A between two chosen limits 11 and 12. For each one the numbers
xl, , x1oo are calculated (with xo = 1) and just the terms from the
75th on are plotted on the vertical line above the value A. So the picture
has axes A, x, where 11 < A < 12 and 0 < x < 1. Figure 12.2 shows the
picture for 1 < l < 4.
Try running it for say 11 = 0, 12 = 2. How does the resulting picture
confirm what is shown in (vi) above?
Try running it for 11 = 2,12 = 3.5. What do you observe?
(viii) (Optional extra) Between A = 3.9 and A = 4 there are several
more attracting cycles. Find some of them, using the M-files to the best
advantage that you can. (It is possible to be reasonably systematic in
this.)
Fixed points
A fixed point of a function f is a value of x such that f (x) = x. Consider
the functions f (x) = (ax+b)/(cx+d), where c 0, and f (x) = Ax(1-x),
where .A > 0. What are their real fixed points (if any)? Let a be a real
fixed point. It is called
13.1 Introduction
You may have met the Newton-Raphson method for approximating the
roots of an equation f (x) = 0. Briefly, this consists in making a guess
x0 at a root and then refining it by x1, x2, x3, ..., where
f(xk)
Xk+1 = Xk - fT(xk)> k = 0 1 2 .... (13.1)
164
13.2 The equation z2 + 1 = 0 165
The M-file chooses a random starting point z and joins up the successive
complex numbers N(z), N2 (z), N3 (Z).... until they come very close to
one of the roots p, q (marked with large crosses, one red and one green).
So the zig-zag line produced shows how the method gradually approx-
imates to a root of the equation. To choose another random starting
point, press <Enter>. The M-file allows ten such starting points.
Try running the M-file and inputting i, -i for the roots. You need to
specify the square region of the Argand diagram shown on the screen,
namely xl < x < xu, yl < y < yu, by giving in succession xl, xu, yl (yu
is then calculated). Observe which iterations tend to i and which to -i;
this should agree with the theory above!
Try running the M-file with, for example, 1 + 2i, 3 + 4i for the roots,
and a suitable region, for example, 0 < x < 5, 0 < y < 5. Do you have
13.3 General quadratic equations 167
where as before p and q are the roots of the quadratic equation (13.2).
Let L be the set of points z in the plane for which JT(z) J = 1, i.e.
Iz -p1 = Iz - qJ. Draw a diagram to illustrate how L is related top and
q.
Fig. 13.1. A close up view of part of the basin of attraction for the root r = 1
of the cubic equation z3 - z = 0.
In this chapter there are two projects. The first one (A) is about ran-
dom permutations of a finite set, cycles and permutation matrices. The
second (B) is an investigation of card shuffling, introducing many of the
standard ideas including perfect and approximate riffle shuffles.
A Cycle decompositions
Aims of the project
We shall use MATLAB to investigate `random' permutations, especially
their disjoint cycle decompositions. There is a theoretical and experi-
mental investigation of the [5 [5 average number of disjoint cycles occur-
ring in a random permutation. The basic material on permutations is
generally covered in a first course on abstract algebra; see, for example,
[11].
171
172 Permutations
common multiple of the lengths. Also you need to count the total num-
ber of disjoint cycles in a permutation and to average this over a large
number of trials.
A permutation of 1, 2, ... , n is a rearrangement of these numbers in
some order. For example, when n = 8, the permutation
'r _ ( 1 2 3 4 5 6 7 8 ) (14.1)
7 3 8 6 5 4 1 2)
takes 1 to 7, 2 to 3, 3 to 8 and so on. The rearrangement is simply
written in the bottom row, which we shall call the vector p. Any per-
mutation can be written in disjoint cycle notation, . For it this gives
(17)(238)(46)(5), which means that it can be effected by leaving 5 alone;
taking 4 to 6 and 6 to 4; taking 2 to 3, 3 to 8 and 8 to 2; and taking 1 to
7 and 7 to 1. The cycle (46) is called a 2-cycle or transposition, (238) is
a 3-cycle, etc. Often the `1-cycle' (5) is omitted from the notation, but
here we shall include it. We shall be interested in (among other things)
the total number of disjoint cycles in the expression of a permutation.
In the case of it, this number is 4. (It is reasonably clear and true!-
that the disjoint cycle decomposition of a permutation 7r is completely
determined by it, except that the cycles may be written in a different
order and each cycle may itself be cyclically permuted. For example,
the above it is also equal to (5)(382)(71)(46).)
We denote by S(n) the set of all permutations of 1, 2, ... , n. There are
n! of these permutations, since there are n choices for what 1 is taken
to, then n - 1 choices for what 2 is taken to, etc., making
n(n - 1)(n - 2)...1 = n!
choices altogether.
(i) The available MATLAB function randperm(n) produces a 'random'
permutation of the numbers 1, 2, ... , n, that is, a random element of
S(n). Thus you type randperm(10) to get a random permutation of
1, 2, ... ,10. Take a look at this M-file. Use your knowledge of the sort
command to write down how randperm.m works. (Remember you can
type help sort if you've forgotten about sort !)
Write a short M-file which creates 1000 random permutations and
writes them all as the rows of a matrix bigp of size 1000 x n.
Hints: You will want to start with
n=input('Type the value of n')
Permutations 173
ord=clength(1);
for i=2: length(clength)
ord=(ord*clength(i))/gcdiv(ord,clength(i));
end
and then of course you will want to display the order, here called ord.
Give a few examples of results from this M-file.
(iv) Amend randperm.m so that it also calculates the corresponding
permutation matrix A of the permutation 7r E S(n). This is defined as
the matrix which starts off all zeros and then adds a 1 in the ith row at
position 7r(i), for i = 1, ... , n. So you first define A = zeros(n,n) and
then have a loop
for i=1:n
A(i,p(i))=1;
end;
(Recall that p is the name of the vector giving the rearrangement, the
bottom row of equation (14.1) above.)
Why is it true that when you multiply A by the column vector u =
(1, 2, ... , n)T to form Au, you get the permutation vector p, written as
a column? Note that in MATLAB, u could be written u= [ 1 : n] ' . It fol-
lows that powers of the permutation can be calculated by A2u, A3u,....
Use your examples from Question (iii) above to illustrate the fact that
the lcm of the lengths of the cycles equals the order of a permutation
(that is the smallest power k > 1 for which Aku = u). Note: You
may find it better to produce a new version of the M-file you wrote for
the earlier part of this question, in which you are able to specify the
permutation instead of having it chosen randomly for you.
Permutations 175
(v) Amend cycles.m to calculate also the total number of cycles (in-
cluding 1-cycles) in the disjoint cycle expression for a permutation. Then
amend it again so that it takes a given number of random permutations
(which might be 1000 in practice) and calculates the average number of
cycles which are in the disjoint cycle representations of these permuta-
tions. Run this several times with different values of n to get experi-
mental estimates for these averages for the different n.
(vi) This is a theoretical argument which predicts the average found
experimentally in Question (v) above. It involves no computing. You
should fill in the details of the arguments and answer the questions.
We calculate the average E of disjoint cycles calculated over all n!
permutations in S(n). Thus let Pk(n) be the total number of k-cycles
occurring among the disjoint cycle representations of all elements of
S(n). We need
P1(n)+P2(n)+...+Pn(n)
E
n!
For example, let n = 3. The disjoint cycle representations of the six
elements of S(3) are
(1) (2) (3), (1) (23), (2) (31), (3) (12), (123), (132).
For 71 = (12)(34), the cycles containing 1,2 do not also contain 3,4. For
r2 = (1)(234), they do.
Amend cycles. m to calculate the proportion of a random selection
of say 1000 permutations which do have the above property, for a given
m (input at the beginning, with n). Hint: This is a great deal easier
than it sounds. You need to input m and the number of permutations to
test, say testnum permutations. You will also need a counting variable,
say count, to count the number of permutations which do have the
property above. The only other essential change that needs to be made
is to change
while i<n
to
while i<m
Once the cycle decomposition has been found, you need to check whether
it has the required property; this is done by
if p == zeros(size(p))
count=count+1;
end
Can you see why this does the trick?
Taking say n = 8 and various in, conjecture a formula for this pro-
portion in terms of n and m.
(viii) The calculation in Question (vii) can be interpreted, as follows.
We have n boxes, each with its unique key which fits no other box, and
each with a small slot in it through which you can push a key but can't
get it back! The boxes are locked, and the keys are dropped at random
into the boxes. Now boxes 1, 2, ... , m are broken open. What is the
probability that every box can now be unlocked? If you can explain the
connection of this with the calculation above, please do so.
14.1 Introduction 177
B Card shuffling
Aims of the project
Card shuffling is a situation in which permutations occur in (some peo-
ple's) everyday life. In this project we shall study both `perfect' shuffles
in which there is an extreme degree of regularity, and also `rough' shuf-
fles where there is a certain amount of chance, modelling an ordinary
person's (as opposed to a card magician's) riffle shuffle of a pack of 52
cards. We shall find, surprisingly, that three (rough or perfect) riffle
shuffles of a pack are by no means enough to randomise its ordering. In
fact if three such shuffles are performed and then a single card is moved
to another place in the pack, it is usually possible to discover the identity
of that card by a simple technique of laying out the cards in columns.
There is a discussion of card shuffling in [6].
Mathematical ideas used
This project involves permutations of a finite set, thought of as a pack of
cards numbered 1, 2, ... , n. So it is necessary to multiply permutations
(that is, do one after the other). We use the `congruence' notation:
a - b mod m, where a, b, m are integers and m 54 0, means that a - b =
Am for some integer A, which can be > 0, < 0 or zero. The order of
a permutation obtained by shuffling a pack of cards is investigated-
this means the number of times a particular shuffle has to be exactly
repeated so as to return the cards to their original ordering. (There is
an unfortunate clash of terminology here between `order' and 'ordering'!
The latter refers to the sequence of card values top to bottom of the
pack.) There is use of the disjoint cycle representation of a permutation.
Note: We always write composition of permutations from right to left:
the notation 7r2ir1 means `do 7r1 first and then do 1r2'
MATLAB techniques used
Some amendments to existing M-files are required, adapting from odd
numbers of cards to even numbers for example. The MATLAB command
sort will come in handy.
14.1 Introduction
14.1.1 Position permutations (pps)
Consider a pile of six cards, with face values 1, 2, 3, 4, 5, 6 top to bottom.
Suppose these are rearranged (shuffled) so that the face values become
178 Permutations
4, 1, 5, 2, 6, 3 top to bottom. The corresponding position permutation
(pp) is
2 3 4 5 6
Ir= C1
2 4 6 1 3 5 '
We could now `cut' the pack by making it into two piles 2, 4, 6 and
1, 3, 5 and reassembling so that the ordering now has face values 1, 3,
5, 2, 4, 6. The whole sequence of three shuffles (two riffles and a cut)
therefore has pp equal to
1 2 3 4 5 6
(14.2)
1 4 2 5 3 6
(card in position 1 -+ position 4, etc.). You can easily verify that the
permutation in equation (14.2) is precisely air2. Thus we can very con-
veniently work out the pp of the combined effect of the three shuffles by
multiplying together the pps of the individual shuffles: it then it then or,
written o,7r2.
Generally:
suppose we do a sequence of shuffles, with pps 7r1i then 7r2, and so on
up to 7rr. Then the pp of the total shuffle is the product permutation
irr7rr_1 ...7f1. In particular, repeating a shuffle with pp it a total of r
times gives a shuffle with pp equal to 7rr.
14.2 Ins and outs 179
Note that to save excessive writing we shall from now on indicate pps
by their bottom line only. The top line is always 1, 2, ... , n. Sometimes
(14.3) we shall be interested in the disjoint cycles representation of a
PP.
Of course, ordinary playing cards have suits. We shall label them with
face values 1, 2, ... , 52 instead. This is the same as ordering the suits
and ordering the cards A,2,..., 10, J,Q,K within each suit.
3 - 4
1
1
4 2
2 5
5 3
Fig. 14.1. In- and out-shuffles for an even and odd number of cards, which
start out top to bottom in the order 1, 2, 3, ....
Thus here the card which starts in position x before any shuffling is
done ends up, after two shuffles, in position 4x (mod 2k + 1). Verify this
directly in the case n = 6.
What is the smallest number of times you need to repeat this shuffle,
for n = 6, to get the cards back in their original ordering?
t The only properties of = mod n we need are: if a - b and c - d, then (i)
a c = b d and (ii) ac bd. In particular if a = b, then a' = br for any integer
r>0.
14.2 Ins and outs 181
Your task in what follows is to imitate the above analysis in three other
cases. Because these are slightly different, some hints will be given.
1,k+1,2,k+2,3,k+3,..., k, 2k
182 Permutations
See Figure 14.1 for the case k = 3. Verify by looking at say n = 6, n = 10
that the position permutation in this case is ir, where
7r(x) =-2x-1 (mod 2k-1) for 1 <x <2k- 1,
while,7r(2k) = 2k. There is one additional complication: for x = k, the
position is 2k - 1, not 0. So in working out 7r(x) we want to take the
remainder of 2x - 1 on division by 2k - 1, but, since there is no position
0, remainder 0 means position 2k - 1.
To make this easier, a special function called remm.m has been created
for you. This equals the ordinary rem unless the remainder is 0. Thus
remm (19 ,11) gives 8,
but
remm(22,11) and remm(11,11)
give 11.
First, use the function remm.m to modify riffles .m to handle the case
of even out-shuffles. You will need to change the i-loop to go only to
2k - 1 and add store (2*k) =2*k; after the end of the i-loop but before
position-perm=store. Call your modified M-file riffle2.m and use it
to verify that for 52 cards you return to the original ordering after only
eight out-shuffles. How does this compare with the number of in-shuffles
for 52 cards which you found using rifflela.m above?
Next, fill in the details of the following sketch to show that the number
of in-shuffles required to return 2k cards to their original positions is the
smallest r for which
2r = 1 (mod 2k - 1).
Here is the sketch of an argument. Note that the card starting in po-
sition 2k stays there after any number of out-shuffles, so we exclude
x = 2k in what follows. As before, combining shuffles just amounts
to multiplying position permutations in the usual way, so two shuffles
produces a position permutation ir2, where
7r2(x)-2(2x-1)-1=4x-3 (mod 2k- 1).
By induction it follows that for r shuffles we get pp equal to 7rr, where
ir''(x) - 2rx - (2r - 1) (mod 2k - 1).
Hence, if 21 - 1, then 7rr returns every card to its original position, since
7rr(x) - x for all x. Conversely, if every card is returned to its original
position, then the card in position 2 is returned, so that 2r+' -2r+1 - 2,
14.2 Ins and outs 183
Adapt riffle 1.m to these two cases (calling the M-files riffle3.m and
riff1e4.m) and find the smallest number of in- and out-shuffles which
return 15 cards to their original ordering. Note that you will want to
use remm.m rather than rem to avoid having a zero remainder.
Show that for both in- and out-shuffles, the smallest number of shuffles
to return the cards to their original ordering is the smallest value of r
such that 2' - 1 (mod 2k + 1). Use rifflela.m to find the smallest
number r for odd packs of cards with n = 2k + 1 from 39 to 51 inclusive.
Here we are just multiplying permutations in the usual way, so the equa-
tion states that
7r(Uc(x)) Q2c(7r(x)) (modn) for all x.
Show that both sides are congruent to 2x - 2c mod n. Explain why this
implies that when riffles are interspersed with cuts, the same effect can
be achieved by doing a sequence of riffles first and then a sequence of
cuts. Furthermore, it is pretty clear that doing any sequence of cuts one
after the other is the same as doing a single cut. Thus a sequence of
riffles and cuts has pp of the form
14.3 Cycles
Like any other permutation, the position permutation corresponding to
a riffle shuffle can be broken into disjoint cycles. An M-file which does
this for even in-shuffles has been written for you: it is called riffle 1 c . m.
It displays the cycles and the cycle lengths. For even numbers from 40 to
52 use riffle1c.m and the known fact that the order of a permutation
is the lcm of the lengths of its disjoint cycles to verify your results in
14.2.1. For each even number, write down the lengths of the cycles and
the lcm. Note that 52 cards have a special property: the even in-shuffle
is a single cycle. What is the next even number with this property?
Po+Pi+p2+p3=1.
The `expected' number of cards to go into a slot is pl + 2P2 + 3p3 and
there are 2k + m + 1 slots, so the `expected' number of cards used up is
(2k - m + 1)(pl + 2P2 + 3p3). Perhaps we should equate this with the
number of cards available to go into the slots, which is in. Thus we shall
try
(2k - m + 1)(pi + 2p2 + 3p3) = M.
186 Permutations
Fig. 14.2. A perfect riffle shuffle of 22 cards (left) and a rough riffle shuffle
(ruffle) of the cards (right).
Thus if P2 and p3 are given (as well as k and m) then po, pl are given by
In
pi = 2k - m + 1 - 2p2 - 3p3, po = l -PI -P2 -P3- (14.5)
Of course, we need to choose p2, p3 so that po, pi are positive and < 1.
Replacing the fraction in equations (14.5) by 1 (which is a reasonable
approximation when k is large) show that we need only choose P2,P3
with 2P2 + 3p3 < 1. For large k this will be a guide to choosing p2, p3.
What do you think are reasonable values for P2, P3? For n = 2k = 52,
an ordinary pack, what does this give for po, pl, taking the four possible
values of m in turn?
An M-file has been written for you, called ruffle 1. m, which imple-
ments the above model of a ruffle. It is rather complicated since it has
to take care of various possibilities such as the cards 1, 2, ... , m running
out before the slots, or vice versa. If a negative probability po or pl is
detected, then the M-file simple ends.
Run the M-file ruffle 1. m several times, using what you consider rea-
sonable probabilities, and taking k = 26 (an ordinary pack). Perhaps
you can take one of these and show how the cards from one (rough) half
have been slotted into the gaps between cards of the other half, using a
suitable diagram.
14.4 Rough riffles (ruffles) 187
The M-file ruffle 1 c . m finds the cycles for a ruffle, as in 14.3 above.
Run this three times and for each one calculate the order of the ruffle,
which is the lcm of the lengths of the cycles. This equals the number of
times this same ruffle would have to be repeated to get the cards back
to their original ordering. The only point here is that you might find
the order very different from that of a perfect riffle of 52 cards.
1 22 39 29 35 45 10 28 17
2 23 40 30 36 46 11 -1 18
3 24 41 31 37 47 12 -1 19
4 25 42 32 38 48 13 -1 20
5 26 43 33 -1 49 14 -1 21
188 Permutations
6 27 44 34 -1 50 15 -1 -1
7 -1 -1 -1 -1 51 16 -1 -1
8 -1 -1 -1 -1 52 -1 -1 -1
9 -1 -1 -1 -1 -1 -1 -1 -1
14.5 Appendix
This is a sketch proof of a result in 14.2.4. All permutations 7ri here
are in- or out-shuffles for n odd. We perform any sequence of in- or
out-shuffles, interspersed with cuts. As in 14.2.4, this is equivalent to
doing all the shuffles first and then a cut o,.
The idea is to prove that if we choose r with 2' - 1 mod n, then
any sequence of r in- or out-shuffles, interspersed with cuts, produces
merely a cut, that is, a cyclic permutation of the n cards. We also prove
conversely that for this to happen after r in- or out-shuffles interspersed
with cuts, we must have 2' - 1 mod n.
Now 7r1 (x) = 2x or 2x - 1, 7r27rl(x) = 4x or 2(2x - 1) or 2(2x) - 1 or
2(2x - 1) - 1, that is, 4x or 4x - 1 or 4x - 2 or 4x - 3. In general we
have
7r = 7rr7rr-1 ... 7r1(x) - 2rx + 1,
189
190 Iterations for Nonlinear Equations
convergence speed, use the M-file f -rate. m and a visualisation M-file
cont4.m is given as an example.
Throughout this chapter, superscripts are used for sequences while sub-
scripts are reserved for coordinates in the case of multiple variables.
given that the root is approximately near x() = 7.4 (note: the exact
solution is x* = 6). As the first derivative F'(x) = 3x2 - 20x + 27, we
can prepare a file (say) f_exln.m which contains the followingt
t For users of MATLAB V3.5, it should be noted that the second line x = P(1)
in the M-file f_exln.m must be modified to x = P, and the same modification is
needed in f_exlg.m later.
15.3 1D: Convergence analysis 191
where tni is the total number of iterations requested (say 12) starting
from x() = 7.4, tol is the stopping tolerance for IF(x)l < tol (say
1.0E-4) and hi specifies if intermediate iterates are shown (hi = 1 for
`yes', hi = 0 for `no'). With Forms A-C, only the final iterate is displayed
(i.e. hi = 0) and variables tni and tol, if not specified, take the default
values of 20 and 1.0E-3 respectively.
to get a graph like Figure 15.1. Then it can be observed that all roots
are in (say) [0, 7] and so [0, 7] is a good search region for the initial guess
x=x(o).
6 0 0
1 0000000000000000 000
1 2 3 4 5 0 7 8
tni = 30 ; X = [] ; R = [] ;
for xO = 0 : 1/8
8 %%%%% f_exln.m for Example 1 %%%%%
:
1 2 3 4 5 6 7 8 9
[0 1.875] for 1 [2.215 4.25] for 3 [4.875 8] for 6
Apart from a few `odd' points, there are essentially three intervals which
make up a convergence region. So Figure 15.2 can be presented better
in the form of Figure 15.3 which is produced by
x = [0 0 1.875 1.8751; y =[O 1 1 01;
plot(x,y); hold on %% plot(x,y,'o'); % Optional
plot([2.215 2.215 4.25 4.25], [0 3 3 0]);
plot([4.875 4.875 8 8], [0 6 6 0]);
axis ([-1 9 -1 81); hold off
that is,
Ix(l) - xl CIx() -xlk
x(2) _X1 CIx(l) -Xlk
Ix(n+l) - x1 = Clx(n) - xlk
for some constants C and k (independent of n), then the method that
produces this sequence of iterates is of convergence order k. Here x is
a solution of F(x) = 0, a means `proportional' and I denotes the
I
where root is the sequence of iterates that are obtained from full-new. m
or gauss_ga.m, and hi = 1 asks for all estimates to be recorded (while
hi = 0 only requires the averaged and final k estimate to be recorded).
For Example 1, we can call f -rate. m as follows:
>> root = full_new('f_exln', 7.4, 20, 0.0001, 1)
>> k = f_rate(root, 1)
>> mean(k)
where mean finds an average value. As a final example, one may explicitly
specify root
>> root = [ 3.46 3.5 3.61 3.735 3.755]'
>> K = f_rate(root, 1)
K = 3.8743 3.5091 %% Order estimates
>> A = mean(K)
A = 3.6917 %% Average
196 Iterations for Nonlinear Equations
15.4 2D: Iterations for nonlinear systems
In the remaining part of this chapter, we study iterative methods for
systems of equations. The two M-files fullslew.m and gauss-ja.m to-
gether with cont4.m and f_rate.m will be used.
Denote a system of m nonlinear equations (in vector notation) as
F(x) = 0, where
F = (F1iF2,...,Fm)T and X= (xl,x2,...,x,m,)T.
Denote the Jacobian matrix of F by J, that is, the matrix of all first
derivatives with J2j = a . The essential tool we use is the Taylor
theorem that is usually covered in a calculus course.
Given a vector function F(x) with m = 2, i.e. x E R2, each compo-
nent of F(x) can be expanded in the Taylor series, that is,
that is,
F(x(1)) = F(x()) + J J. [x(1) - x(0)] + hot,
where hot stands for `higher order terms' and J is the Jacobian matrix
evaluated at x = x()
dF1 dF1
a21 dx2
J=
8F2 9F2
8x1 a22
Further the famous Newton-Raphson method can be derived.
J=(Jj)= ( aF2
OX
ax
aF2
ay
= 1 1
Again given an initial guess for the root(s), say x() = ( 4.7, -1.9 )T,
we can use the Newton-Raphson iterations to find the exact solution
x = (5.075, -2.075)T.
The iteration formula can be written as x(n+l) = x(n) - d(n), where
d(n) is the solution of the linear equations J(x(n))d(n) = F(x(n)). The
rewritten iteration formula is more efficient as it avoids using, or calcu-
lating, the inverse of matrix J (see Chapter 16). Of course for the single
variable case, there is no difference; refer to 15.1. As we know, the
MATLAB command for finding d(n) is simply d = J \ F. This is how we
have implemented the Newton method in the following M-file.
(Note To solve three or more equations, the function M-file can be pre-
pared similarly.) The MATLAB command is simply
>> Root=full_new('f_ex2n', [4.7 -1.9]') % A
198 Iterations for Nonlinear Equations
>> Root=full_new('f_ex2n', [4.7 -1.9]', tni) % B
>> Root=full_new('f_ex2n', [4.7 -1.9]', tni,tol) % C
>> Root=full_new('f_ex2n', [4.7 -1.9]', tni,tol, hi) % D
where tni is the total number of iterations requested (say 12) starting
from x() = [4.7 - 1.9]T, tol is the stopping tolerance for IF1 < tol
(say 1.0E-4) and hi specifies if intermediate iterates are shown (hi = 1
for `yes', hi = 0 for `no'). One can see that the usage is identical to the
1D case.
G(x) = 3-y
(sin x - 8)13
Here the two equations in (15.3) have been swapped; this does not change
the solution.
52
Fig. 15.5. Intersecting zero contours of functions F1(x, V) and F2 (x, y).
_2L
-2 -1.5 -0.5 0 0.5 1 1.5 2
The plot is shown in Figure 15.6, where we assume Root contains these
iterates
Exercises
15.1 Using full new.m and x() = 1.5, find a real root of
F(x) = ex + e-x + 2 cosx - 6.
15.2 Apply gauss-ja.m and re-solve Exercise 15.1 by selecting two
different G(x) functions to find the root in [1, 2]; e.g. x =
ln(6 - ex - 2cosx) may be a choice.
15.3 Show that ifIx(1)-xI = CIx(0)-xlk, Ix(2)-xI = CIx(1)-xIk
and all terms are assumed to be nonzero, then we have formulae
- xI
Ix(2) Ix(1) - xl.
(a) k = In Ix(1) In
- xI WO) - X1,
(b) C = Ix(2) -xl/lx(1) -xlk.
15.4 A small object, dropped from rest, encounters air resistance as
it falls. Using Newton's second law to model this situation we
obtain the following equation for the height H in metres that it
falls in t seconds:
H(t) = 12.25 [t + 1.25(e-0-8t - 1)] .
Linear systems of algebraic equations are one of the most important sub-
jects in mathematics, since most other subjects, methods and problems
involve or reduce to this subject.
207
208 Matrices and Solution of Linear Systems
16.1 Operation counts
Operation counts give an indication of how many floating point oper-
ations (flops) are needed for a given mathematical problem. Any re-
duction in operation counts leads to a faster solution. This project will
highlight three basic ideas to this end. The first idea, discussed below,
is to implement an existing method in a more efficient way in order to
achieve speed-up. The second idea, discussed in 16.2 and 16.3, is to
seek new and alternative methods for solving the same problem. The
third idea, discussed in 16.5, is to reformulate the given problem so
that existing methods can be efficient.
To illustrate the first idea, we consider the simple task of calculating
z = ABCb, where A, B, C are n x n matrices, and b is a vector of size
n. A naive way to do this is to work out a matrix D = ABC first before
doing z = Cb; this costs roughly 4n3 floating point operations (flops).
A better method is to compute three matrix-vector products, that is,
zl = Cb, Z2 = Bzl, z = Az2; this costs only 6n2 flops!
The efficiency of different methods is therefore crucially determined
by operation counts. We may get estimates of operation counts theoret-
ically from the mathematical formulae but such a job can be done easily
by the MATLAB command flops; to demonstrate its usage, consider
the second method in the above example
>> flops(0); n=2000; % Set flops counter to zero
>> A=rand(n); B=rand(n); % Setup matrices
>> C=rand(n);b=rand(n,1);% Setup matrices
>> z=C*b; z=B*z; z=A*z; % Operations done
>> w=flops, ratio=w/n"2 % Work out flops
where you should find w = 24000000 and ratio = 6. Note that rand
does not contribute to f lops.
Apart from flops, three other commands can be used to monitor the
time spent by MATLAB to carry out a particular calculation; these are
shown in Table 16.1, where `Method' refers to operations of a numeri-
cal method and row `E' shows results from doing the simple operation
rand(9) *5, that is, replacing `Method' by rand(9) *5. Note etime checks
the difference in clock and converts it into seconds; type help etime
to be aware of a possible problem.
Remark Broadly speaking many classical mathematical methods, al-
though elegant in theory and useful in proving the existence of solu-
tions, are not suitable for computer use. For they may either take too
16.2 Dense linear systems 209
long (even years) or lead to large errors; refer to 16.2.3. For example,
A-' = adj(A)/det(A) is good for a theory but not suitable for numeri-
cal computations because the number of operations required is too large!
Here adj(A) denotes the adjoint of matrix A - a matrix of cofactors.
Therefore the analysis, selection (validation) and development of prac-
tical and useful numerical methods are the main aims of computational
mathematics.
where lu name is the name of the factorisation function M-file which may
be replaced by lu2 or lu3 or lu4. Here t indicates that all calculations
are performed to the fixed precision of t-digits only.
The function M-file lu2. m is mathematically the same as the built-in
system file lu.m except that now we only perform t-digit arithmetic.
So if you input t = 16 with lu2.m, then lin_solv.m should produce
identical results to x = A\b.
The function M-file 13. m does not allow partial pivoting and is only
to be used as a comparison, not to compute accurate solutions as no
212 Matrices and Solution of Linear Systems
permutation matrix P is involved and multipliers larger than one are
permitted. The M-file lu4. m always produces the same results as lu3. m
but uses fewer flops for sparse matrices; see 16.5.
The two M-files 1u2. m and 1u3. m can be used individually for factori-
sation (similar to 1u)
>> [L,U,P]=1u2(A,t); %Factorise A (t digits & P)
>> [L,U] =1u3(A,t); %Factorise A (t digits & no P)
So if you solve Ax = b by
>> x = lin_soly( '1u2', A, b, t) ;
1-condA
( )
Here you should find that C = 97.561 so the problem is ill conditioned,
error-theory = 0.7618 is the predicted error by theory (larger) and
error-found=0.3381 is the actual error observed (smaller).
You should find that Work_1 = 8434 and Work_2 = 1506. Here the trick
is that we have found a new ordering for matrix Al; to see and use the
permutation matrix, try
>> P = eye(10); % Generate a 10 x 10 identity matrix
>> P = P(al,:) % Reorder its rows
>> A3 = P*Al*P' % Here A3 = A2 !
>> xy = [ 0 0
1.5 1.3
0.35 2.0
-1.0 1.7
-1.9 0.68
-1.9 -0.68
-1.0 -1.7
0.35 -2.0
1.5 -1.3
2.0 0.0 ], figure(2)
>> gplot(A,xy); hold on % Plot all edges
>> gplot(A,xy,'o') % Plot all nodes as 'o'
Exercises 217
Exercises
16.1 Generate at least three random matrices A,,,,,, of random orders
n = 50 to n = 170 and random column vectors b of size n. Then
use flops to show the following results hold approximately and
estimate constant C in each case:
x = A\b takes Cn3 operations;
y = det(A) takes Cn3 operations;
z = inv(A) takes Cn3 operations;
t = b * b' * b takes Cn2 operations;
u = b * (b' * b) takes Cn operations;
v = b * 9 + 6 takes Cn operations;
w = A * b takes Cn2 operations.
16.2 Solve the following system Ax = b using both A\b and two
separate steps of the LU approach (are the two solutions iden-
tical?)
1 1 0 3 xl 4
2 1 -1 1 x2 _ 1
3 -1 -1 2 x3 -3
-1 2 3 -1 x4 4
218 Matrices and Solution of Linear Systems
16.3 Try the approach of 16.2.2 to solve new systems Ay = e and
Ax = f, immediately after solving Ax = b in Exercise 16.2,
with e = (4 2 - 2 1)T and f = (34 10 1 36)T.
16.4 Based on [L U P] =lu(A) for Exercise 16.2, calculate the follow-
ing relative errors
eL= IIL-L211
andeu= IIU-U211
IILII IIUII
y = lin_soly( ... );
count = 0
while ( norm(e) > 1.0E-9 )
r = b - A * y
e = . . . . . .
y = y + e ;
count = count + 1
norm_e_is = norm(e)
end
16.7 Following Exercise 16.6, solve the two modified problems below
by Gauss elimination with partial pivoting using t = 5 digit
arithmetic. Decide on the conditioning of each case.
(a) Keep the original A as above but modify b by values that
are randomly distributed on the interval [-0.001, 0.001].
Hint b - b + 0.002 * rand(4, 1) - 0.001 * ones(4, 1).
(b) Keep the original b but modify A by values that are
randomly distributed on the interval [-0.002,0.002].
16.8 EXTRAS Find out about how to generate a Hilbert matrix by
help hilb. Let A be a Hilbert matrix of order 6 and bT =
2 : 1 : 7. Find the actual relative error E for the numerical
solution y by solving Ax = b by Gauss elimination without
partial pivoting and using t = 8 digit arithmetic.
Modify your M-file so that it runs for t = 1 : 12, and for each
case print out a relative error for y.
0 1 1 0 0 0 0 0 0
1 1 1 0 0 0 0 0 0
1 0 0 1 1 0 0 0 0
A= 0 0 0 1 1 0 0 0 0
0 0 0 0 0 1 0 0 1
0 0 0 0 0 0 1 1 0
1 0 0 0 0 0 1 1 0
1 0 0 0 0 1 0 0 1
0 0.5
0 -0.5
xy= 0 -1
-1 0
1 0
0.5 0
-0.5 0
f Note that this matrix is not really symmetric but symrcm can still be useful.
17
Function Interpolations and Approximation
221
222 Function Interpolations and Approximation
17.1 1D: Introduction
In one space variable, a degree m polynomial may be written as
P,,,, (x) = C(1)xm + C(2)x"c-1 + + C(m)x + C(m + 1),
where C = [C(1) C(2) . . . C(m) C(m + 1)] is the coefficient vector.
In MATLAB, such a polynomial is evaluated by the command polyval:
Example 1
P4(x) = 2x4 - 5x3 + 3x2 + 8x - 1,
that is, m = 4, C = [ 2 -5 3 8 -1 ]. We can evaluate P4 at
points x = -9.0 : 4.5 : 10 and have a dotted as well as a line plot by
>> C = [ 2 -5 3 8 -1 ] ; % Type in the coefficient
>> x = -9.0 : 4.5 : 10.0 ; % A vector in [-9,10]
>> y = polyval(C, x) ; % Evaluate at x
>> plot (x,y,'ow');hold on; % [0] Scattered points
>> plot (x,y,'-w') ; % [1] Plot in solid lines
>> xp = -9.0 0.01 :10.0 : ; % A large vector in [-9, 10]
>> yp = polyval(C, xp) ; % Evaluate at xp for plots
>> plot ( xp, yp, ':g'); % [2] Dotted lines (better)
>> axis([-9 10 -2000 17500]);% Fix axis display
>> CQ = polyfit(x,y,2) % Quadratic fitting at (x,y)
;
Here the solid line plott is not representative of the polynomial because
it is not a curve at all and the polynomial is far from being a straight
line. We have to use more points to show it as a proper curve (the dotted
line). This is the first important point on plots to be noted in doing this
project; see Figure 17.1.
Notation and Convention All approximations are based on a knowl-
edge of given data points, but test points are artificially chosen for anal-
ysis. To avoid possible confusion, we use x, y (size n) for data points, xi,
yi (size T) for test points and xp, yp (size L) for plot points. Obviously
L should be sufficiently large (say L >> n) but T can be any number.
For the Example 1 above, we have used n = 5 and L = 1901.
t Here we assume that the background colour is black. On some systems where this
is not the case, type set(gca,'Color ','k') to reset or replace the `white' (`ow'
and `-w') by `black' ('ok' and '-k').
17.2 The 1D example M-file intdemol.m 223
-2000
-8 -6 -4 -2 2 10
Fig. 17.1. Example 1 - plotting curves (solid lines - polynomial C with few
plot points, dotted - with more plot points, dashed - a quadratic fitting).
E=E(C)= Y)2
i=1
-1 0 1 2 3 4
File intdemol.m - example 1 D (dotted lines - true curve)
-1 0 1 2 3 4
File intdemol.m - example 1 D (dotted lines - true curve)
f The modern finite element method is one of the best application examples of a
piecewise approximation approach.
t Interested readers can consult any numerical analysis textbook for details and
formulae. Test this out using polyfit!
If one insists, the overall approximation can be made differentiable; see 17.3.2.2.
17.4 How accurate is my approximation? 227
Error = JIIfi_yiII 2 = I 1:
T
(fi _ yi)2.
rp
V i=1
1 + ... +
rnO...Qxm
00...0)
3D plots Two commands mesh and surf (and their variantst) can be
used to visualise ZZ; (see Figures 17.4 and 17.5 for actual plots).
>> mesh(ZZ); title('A mesh plot');
>> disp('Pause ...'); pause
>> surf(ZZ); title('A surf plot')
t Type help mesh to see how many other commands are referred to. Some com-
mands such as surf and plot3 are only available from MATLAB V.4.
230 Function Interpolations and Approximation
12
Sliced plots Sliced plots t are line plots that are sliced off 3D plots in
one axis direction. For Example 3, we may slice at mid-intervals in each
direction and then plot each slice separately (see Figure 17.7):
t MATLAB V.5 introduces a new and convenient command called slice for the
same purpose. Type help slice to find out more details.
232 Function Interpolations and Approximation
0.4
0.2
0
X
0
c0
c2 -0.2
.2
-0.4
-0.61
1 1 1 1 1 1
Here the last two lines annotate the graph in an advanced way; type
get (gca) and help set to find out which other attributes of a graph can
be modified. As sliced plots do not represent the full graph, they should
be used with caution but they can be useful when comparing several
approximations. Of course, slicing can be in any suitable directions, e.g.
along x + y = 1.
These selected points, called test points as in 1D, can be those used
to generate various plots but should not be the starting data points x0
and y0.
Suppose that an approximation Z and its corresponding true function
values z are in the form of matrices (say of size N1 x N2). In the example
of the last two sections, such an approximation Z may be Zl, Z2, zL, zC
and z is calculated using the original function z(x, y). Then the following
two methods can be considered.
Geometric comparison Using the hold on option, we may overlap an
approximation Z on the contour plot of the true values z (using different
colours of course). Then large discrepancies would indicate that the
underlying approximation is not very good and vice versa. Note it is not
easy to overlap plots with mesh and surf.
One may also generate a contour plot and decide where the main fea-
tures of a function are, before producing suitable sliced plots for compar-
isons. Conclusions drawn from a geometric comparison can be subjective
but it is effective to identify the poorest approximation method.
Algebraic comparison A very useful but boring method for measur-
ing discrepancies between two matrices is by use of norms, e.g. E _
norm(Z - z) as an error indicatort - a small E indicates good approx-
imation and vice versa.
In engineering applications, as in 1D, it is more common to use the
so-called root mean squares norm defined by
Ni N2
E= NlN2 1
(ZZj - zi.7)2.
i=1 j=1
The smaller such an error is, the better the method is.
Luckily the above formula resembles the Frobenius norm of matrix
(Z - z) that can be computed via the MATLAB command norm(Z-z,
' fro') . Thus the quantity E can be computed as follows
>> [m n] = size(Z); % Get the dimension of matrix
>> mn = m * n ; % Total number of nodes
>> format compact; % Don't leave extra space lines
>> disp('Root Mean Squares error is :');
>> E = sqrt( norm( Z-z, 'fro')-2 / mn )
Exercises
17.1 Given the function f (x) = 1/(1 + 25x2) in [-1, 1] and nine data
points x = -1 : 0.25: 1, plot the function against its polynomial
approximations of degrees 3 and 7.
17.2 Following Exercise 17.1, plot the original function against its
piecewise linear, cubic and spline approximations.
17.3 For the following given functions
(a) f (x) = -(x + a)R + [aR(1 - x) + (1 + a)Rx] with a = 10-2
and R = -1 in [0,1],
(b) f (x) = (1 - x){tan-1 [b(x - xo)] + tan-1 [bxo] } with b= 100
and x0 = 1/4 in [0,1],
(c) f (x) = tanh[20(x - 1/2)] in [0, 1],
(d) EXTRAS f(x) = 10000(f1(x)+f2(x))I(x-1/4)(x-3/4)3
with fi(x) = -(x + a)R + [aR(1 - x) + (1 + a)Rx] and
f2(x) = -(1 - x + a)R + [aRx + (1 + a)R(1 - x)] where
a = 0.3 and R = -1/4, in [0,1],
(i) Compare the performance of four approximations: least
squares method using for m = 1 20, piecewise lin-
:
18.1 Strategy
Your aim, for each equation in the list of exercises, is to provide the
following information as appropriate:
(a) For first order equations, a grain plot with typical solutions su-
perimposed. For second order, or coupled first order equations,
sketch of a typical phase plot.
(b) An analytic general solution if you can find one.
(c) The particular solution for the specified initial conditions.
239
240 Ordinary Differential Equations
(d) Any other comments you wish to make on the nature of the so-
lutions, their stability etc.
Exercises
18.1 Find y at x = 1 for the particular solution of
dy = -2xy
dx
satisfying the initial condition y(O) = 1.
8.2 Find y at x = 1 for the particular solution of
dy = e_2xy
dx
satisfying the initial condition y(0) = 1.
8.3 Find y at x = 5 for the particular solution of
dx2 dx
+x2y=0
satisfying the initial condition y(O) = 1, y'(0) = 0. (Hint: You
may have difficulty finding the numerical solution when x is
near 0 (called a `regular singular point'). By considering a series
(Maclaurin) expansion such as y(x) .: a+bx+cx2 you can work
out how y and its derivative must behave for very small x-values
should your M-file need to know this.)
18.8 Find x at t = 1 for the particular solution of
d
dt = x(2x + y - 3), t = y(x + 2y - 3)
satisfying the initial condition x(0) = 5, y(0) = 1. (Hint: Think
also about fixed points and stability as studied in Chapter 7.)
18.9 Find y at x = 1 for the particular solution of
z
(1-x2)ddx2y -2xdy+12y=0
dx
satisfying the initial condition y(O) = 0, y'(0) _ -2. (Hint: See
the advice given for exercise 18.7, if you experience difficulty in
finding solutions at particular x-values.)
Part three
Modelling
19
Checkout Queues: Long or Short
245
246 Checkout Queues: Long or Short
This project is divided into three parts. 19.1 deals with some tasks
of modelling simple queues and introduces the main M-file queue. m.
19.2 models queues at a busy motorway petrol filling station while
19.3 considers queues at the Leo's cafe in a retail park. Both problems,
each representing a separate project, involve making decisions on staffing
levels, and improving profit margins and promotion issues.
The M-file queue. in, as used above, has been devised to simulate a simple
queue. For more complicated examples it is necessary to write a simple
program that calls queue. m more than once. f
To simplify the generation of random numbers, you may use the fol-
lowing three M-files: exprand.m, normrand.m and unirand.m; these
M-files should be easy to understand as illustrated in Chapter 6 but
type help to see the usage if necessary. To compute a matrix of n x k,
exponentially distributed with mean .t = mu, type
>> matrix = exprand(mu,n,k)
The arrival time AT can be worked out using cumsum based on the
inter-arrival time IT. Further we can work out when a person leaves the
supermarket till, how long each person needs to wait in the queue, the
average waiting time and the percentage of time when the till is in full
use. All this information can be obtained by a single call to the M-file
queue. m as follows
Main pump
Kiosk at shop
Side pump
work out the payment method (vector s). All times, shown in tables
of this section, are converted into seconds. Further in Table 19.4, we
can construct the corresponding event table to display the results (note:
EoS = end-of-service corresponds to the event in column 1). Again we
may use queue. m to verify the correctness of this EoS column as follows:
>> Arr = [ 58 63 201 275 298]; %% Arrival at kiosk
>> Ser = [ 60 60 30 40 301;
>> [mq su wait_t EoS] = queue(Arr, Ser)
where Arr and Ser are collected from the `*.k' rows (i.e. the actual
combined arrivals at the cash kiosk) of Table 19.4. The results (for EoS)
match those corresponding entries in the last column of Table 19.4, as
expected:
[mq su] = 0.8464 0.6377
wait_t = 0 55 0 0 17
EoS = 118 178 231 315 345 % Good %
Note: For the cash point times, if r denotes a vector of uniformly dis-
tributed numbers in [0, 1], let customers pay by cash if 0 < r < 0.2, by
credit card if 0.2 < r < 0.5 and by cheque if 0.5 < r < 1. A simple
method of generating is for those r < 0.2 and Os for those r > 0.2 is
the following k1 = (r < 0.2).
Superstore 40%, dl
50%, d2
DIY store Leo's cafeteria
items and after some time most of customers arrive at the last stop
the cafe (via the front entrance). The cafe has two entrance doors: a
front (main) one and a rear one, but only one cash kiosk. Previous data
show that:
Exercises
19.1 Assume the mean value L for inter-arrivals of fishing boats at a
sea port is 3 minutes. Consider three kinds of distributions for
the inter-arrival time It:
(a) uniform distribution in [0, 6];
(b) normal distribution with standard deviation or = 1;
(c) negative exponential distribution.
Take the first 100 arrivals in each distribution and, using queue. m
with the service time of 2 minutes for each boat, plot all three
arrivals versus respective waiting times on the same graph. Find
the peak waiting time for each distribution and mark it on the
graph.
19.4 For the Leo's cafeteria example of 19.3 using ml = 44, m2 = 55,
m3 = 36, m4 = 100 and dl = 145, d2 = 108, d3 = 69:
(a) Generate 100 arrivals for each of the three main businesses
and 50 arrivals at the rear door of cafe. Then work out
how many customers out of each 100 actually arrive at
the cafe. Show all four kinds of arrivals (in minutes) at
the cafe on the same graph (using different colours and
line patterns).
(b) In (a), what is the total number of customers arriving at
the cafe. Plot arrival time (in minutes) versus service
time (in seconds) at the cafe. Further show arrival time
(in minutes) against waiting time (in seconds) on another
graph after calling queue. m. Find the maximum waiting
time (or the peak of graph) and mark this point by the
symbol `o' on this second graph.
(c) Following (a) and (b), how many customers of each kind
of arrival have arrived at the cafe within the first 40
minutes. What is the total number of customers in the
first 40 minutes? Suppose that the amounts of money
customers spend in the cafe satisfy a normal distribu-
tion with a mean of 1.49 and a standard deviation of
30 pence. Calculate the takings in the first 40 minutes.
What is the profit margin if the running cost of the cafe
is 90?
Hint 1 For the cash point times, as in 19.2, let customers pay by
cash ifs = (0<=r & r>=0.9), and by credit card ifs =
(0.9 < r), where r is the normalised random vector.
Hint 2 The MATLAB command nnz counts the number of nonze-
ros in a vector. Also, the command sort may be used to
pick up all nonzeros in a vector as in
>> arr = [ 2 0 1 5 0 0 4 0 9]
>> arr_1 = -sort(-arr), number = nnz(arr)
>> arrival-2 = sort( arr_1(1:number) )
257
258 Fish Farming
20.1 Preliminary look at the problem
Use the command fishy to load some data on the fish. Typing help
fishy will show you what is there. You could use the usual plot com-
mand to have a look at how the weight of a fish and its fertility depends
on its age. Similarly you can make a plot or a bar graph of the current
numbers of fish versus age. (Type help bar.)
A first attempt at a Leslie matrix (cf. Chapter 2) has been provided.
Type help lmfish to see what it is supposed to do. To see the whole
M-file in more detail use Notepad. You will find that several additions
and corrections are required, for example to take account of the fact
that the egg-production (fertility) rate refers only to female fish and
to model more realistically the removal rate due to fishing. The latter
clearly depends on the mesh size of the fishing nets, the size distribution
of the particular fish species and the frequency of harvesting. Part of
this procedure will require you to establish a mathematical model which
fits the weight vs age data and also predicts how the cross-section of a
fish depends on its age.
We will return to each of these tasks in due course. In the meantime,
it is worth trying the first two exercises at the end of the chapter.
You should be able to estimate a value for p simply from the fact
that the fish swim in water with no apparent difficulty. Specifying the
dimensions h, w and 1 pre-supposes a very simple rectangular box model
20.2 Models of fish growth 259
of a fish. You may be able to do better. The box model implies that the
volume V = hlw. In any case, m = pV. We will return to these points.
where the parameters r and A (both > 0) have to be determined to fit the
circumstances. Clearly r sets the overall rate of growth and A determines
the time-scale over which the period of rapid growth takes place. You can
estimate 1/A for our fish very roughly by looking at your plot of weight
gain to see at what age the maximum rate of growth occurs. Likewise
you can estimate r very roughly by applying the formula (20.1) at a
couple of t-values, where m and its slope can be estimated. Make a
rough guess of each in this way and use fodesol to see how the model
behaves. Note that you have to supply an initial condition. Choose a
t-value for which you know the corresponding m-value (t = 1?). You
can adjust the parameters in the light of what you see.
However, you might well find you can solve the ODE (20.1) by inte-
gration to obtain an analytic solution for m(t). A good plan would be
to do this and then use fodesol with suitably modified fnxt to obtain
a plot and numerical solution as a cross check. In this way you can be
sure of correct answers. If unsure of fodesol, you can have another look
at Chapter 7.
where the parameters a and 0 are positive. The idea behind it is one of
energetic balance. The first positive term represents energetic (nutrient)
input and is proportional to surface area (hence V23), while the negative
260 Fish Farming
second term represents the drain on resources, that is, food for body
tissue, and is proportional to the volume itself. Again, you should try to
solve this both graphically/numerically with fodesol and analytically
where you may find the change of variable L = V 3 helpful. Here, L
obviously has the dimensions of length. To estimate the parameters in
this case think about the maximum size achieved when dV/dt = 0. You
can also look at spot values of the slope of the data if this helps. (Use
two neighbouring points.)
These features are annotated in the M-file. Your next task should be
to improve the survival probability model to incorporate your knowledge
of the size of a fish of a given age and to take account of different mesh
sizes.
means certain capture for fish bigger than the mesh size and a decreasing
likelihood for smaller fish. One might justify using (h/d)2 rather than
some other function by claiming that the cross-sectional area rather
than a linear dimension is significant here. There is a further modelling
feature which you should include and that is the efficiency of trawling
(harvesting) in any one year. Even if a very fine mesh is used, the
total catch depends on how often and how completely this is done. You
could combine these factors into one `efficiency' factor fe f f with which
to multiply Pc before going on to calculating the probability 1 - P of
not getting caught in a given year.
You should adapt lmfish.m to incorporate as many of the above fea-
tures that you can. The appropriate places to insert these additions
are annotated in the M-file. Add your own comments (lines beginning
with %) as you add more features. This helps to remind you what you
have done. Since you will want to use this code repeatedly to obtain
and study the Leslie matrix H, it might be as well to get the M-file to
prompt you for the necessary values of d and feff. You can do this with
a line such as
d=input(' Give me the mesh size d ');
which will do just that and make the input value of d (10 say) available
to subsequent lines in 1mf i sh. m. In this way you can keep reusing the
M-file without having to edit it every time you need a new value!
can check explicitly what happens in the first year by taking the current
population, for example
>> p0=fish(:,3)
Exercises
20.1 Use the data loaded in with fishy to construct a bar graph of
the total mass of fish (the biomass) as a function of age.
20.2 Find the total biomass (in some sensible units) of fish of all ages
currently swimming about in one square kilometre of sea.
264 Fish Farming
20.3 Estimate the fish density p (in g/cm3) and hence obtain a plot
of the height, width and length of the fish as a function of age
according to the rectangular `box' model of a fish.
20.4 Solve equations (20.1) and (20.2) by the method of separation
of variables.
20.5 Use fodesol to obtain numerical solutions of equations (20.1)
and (20.2) with guessed values of the parameters. By comparing
with the data for fish mass (or volume), try to find better values
of the parameters.
20.6 If you managed to obtain (by integration) closed expressions for
V(t) for either of the models, use mparft to obtain `best fit'
parameters and compare these with those which you estimated
with the help of fodesol.
20.7 Give your estimate of the parameters [mo, r, A] or [mo, a,,3] of
the model (20.1) or (20.2) which, in your opinion, best describes
the growth data for fish. Also give an estimate of the maximum
weight and length which one of your model fish could achieve.
20.8 Estimate how the cross-section of a fish varies with age.
20.9 Adapt the Leslie matrix lmf ish . m to incorporate the effects of
net size, fish size and fishing efficiency as discussed above.
20.10 Give an example of a viable fishing strategy (net size and effi-
ciency factor which will yield a good harvest but conserve stocks
after five consecutive years fishing. What annual biomass does
your harvest yield from each square kilometre?
20.11 Comment on alternative strategies in which years of heavy fish-
ing are interspersed with recovery years. Obtain some numbers
or plots to support these comments.
21
Epidemics
265
266 Epidemics
f ludat. m - data for school flu epidemic
plagdat. m - data for Bombay plague
colddat. m - data for island cold epidemic
sirepi. m - SIR epidemic model integrator
sirfn.m - derivative function for the above
mparft. m - multi-parameter least squares fit
mparst3. m - example set up for the above
resid3.m - residuals function for the above
lagsum.m - cumulative sum
dS
-rSI , (21.2)
dt
dI
dt
rSI - aI , (21.3)
dR
aI. (21.4)
dt
To recap:
The second term in equation (21.3) reflects the corollary to this state-
ment since these same individuals are being lost from class I. The first
term in this equation is the statement that the number of new cases
per unit time is proportional both to the number of potential victims
(susceptibles) S and to the number already having the disease I and so
capable of infecting others. The constant of proportionality is
r = infection rate (r > 0). (21.6)
268 Epidemics
Finally, equation (21.2) represents the corollary to this statement since
these same individuals are being lost to class S.
Any particular model is now fully specified by the infection and re-
moval rates a and r and by a set of intial conditions. Usually, one takes
R(0) = 0 since no one has yet had the chance to recover or die. Thus,
a choice of 1(0) = Io is enough to define the system at t = 0 since then
So = N - 10. Typically, one takes t = 0 to be the moment at which
I = Io = 1 if the epidemic is triggered by a single infected individual.
R=ln(So) (21.10)
In general, you still need to solve the system numerically to get the t
dependence. However, in certain circumstances, you can make approx-
imations to progress further analytically. For example, if you put the
exact result (21.10) back into the differential equation for R(t) (21.4),
you get
dR =a(N - R - Soe P ). (21.11)
fodesol.m and the techniques of Chapter 7. However, when R/p < < 1
the exponential can be expanded to, say, second order in R giving a
separable variable equation which is tractable. To do the integration,
one can use `completing the square' to get something of the form
dR
- adt . (21.12)
A2 - (R - R)2
You should try this for yourself (see the exercises). The result is an
explicit function R(t) which is then an approximate solution to equation
(21.11).
If you then calculate dR/dt you get an expression which could, for
example, be used to compare directly with the plague data loaded with
plagdat. m. The result is of the form
dR
= Aa sech2 (at +,3). (21.13)
dt
Equation (21.10) can also be used to study the final outcome of the
epidemic, when I becomes effectively zero as t - oo. In that case,
equation (21.10) can be rewritten
which could be solved numerically, for example using the MATLAB func-
tion f zero. If we happen to know the final number of susceptibles and
the total community involved, we can use this to estimate p. Equation
(21.10) can also be combined with equation (21.7) to get further rela-
tions between the parameters and the height of the maximum epidemic
Ima,x. There are lots of different ways of using such relations depending
on the type of data available.
dR (t t )2
Ke 26 (21.15)
dt
to the plague data. You then type mparft to start the least squares fit
and follow the prompts. The `residuals' are calculated in an M-file called
resid3.m. When you have tried this successfully, you should go on to
modify a copy of resid3.m to implement the model expression (21.13).
A full numerical solution of the SIR model is accomplished using the
M-file sirepi. m. You should try to find parameters a and r which give
a reasonable description of the school flu data. In order to get rough
estimates of the sort of values to try, you can study the small t and
large t data separately. At small t, before R becomes large you can
approximate S by N - I and equation (21.3) by
dI
dt = rNI. (21.16)
By solving this and comparing the result with the small t data, you can
get an estimate of r. Similarly, at large t when the epidemic has run its
course, S and I are both small so that dI/dt is dominated by the last
term. Again, you can integrate this equation and get an estimate of a
this time. You can use these rough values to get started with sirepi.
In the case of the Atlantic island data, colddat.m provides you with
the number of new cases each day which is clearly something to do with
the infection rate. To get the actual number of infectives I (t), you need
to know something about how long the infection lasts. This will vary
but a reasonable model might be to take it to be, say, 7 days. From this
you can deduce, not only I(t), but R(t) and hence S(t) as well. You
can manipulate the data by hand or you can use MATLAB's facilities
to help you. An M-file lagsum.m is provided which may be of some use.
Given a complete breakdown of the population into the categories S, I
and R, you can test the SIR model in some detail. By comparing with
the SIR model predictions you can decide whether your cold infection
period of 7 days was too long or too short and so on.
Exercises 271
Exercises
21.1 Check the nature of any turning value given by equation (21.7)
using I (tmax)
21.2 Derive equation (21.10) from equation (21.9) using the initial
conditions discussed at the end of 21.2.
21.3 Integrate equation (21.12) using the standard integral
1 _ 1
f a2 - x2 a
tanh-1(x)
a
(21.17)
273
274 Dynamics of Snowboating
22.1 Preliminary look at the problem
Use the command topog to show a mesh plot followed by a contour plot
of the snow hills where the action will take place. It is defined by
z = f(x,y), (22.1)
E = Ep + EK = constant, (22.2)
where
1
Ep = mgz, EK = 2 mv2 (22.3)
You can work out what speed the snowboat will be doing when it
drops to its lowest point - a totally unrealistic and dangerous speed. Of
course, in real life, there are resistive forces:
friction between boat and snow, which will be more or less constant;
air resistance which rises with speed.
Part of your task will be to obtain reasonable values for the parameters
describing these forces. If necessary, you can increase the friction by
roughening the underside of the boat and so obtain a slower ride. The
other major task will be to find a safe starting point and initial velocity
to give an exhilarating, but not life-threatening, ride down the mountain.
Before continuing, try the first three exercises at the end of the chap-
ter. These will help you visualise the problem.
22.2 The equations of motion 275
mr=F=F9+F,,,+Fk+N, (22.4)
F9
Notice that friction always acts in the opposite direction to the sliding.
This simple model therefore breaks down whenr = 0 where the force
changes discontinuously! The numerical solution method described in
Chapter 7 could have problems with this. Fortunately, we are not inter-
ested in following the snowboat after it has come to rest (r = 0)! It will
be left to you to determine a realistic value for p. (See the exercises.)
For the air resistance, it is found that cars, people on bicycles, free-fall
parachutists etc. experience a force like
Fk = -krli I . (22.6)
Again, this always opposes motion but has reasonable behaviour as the
velocity goes to zero. You should be able to figure out a sensible value for
k using the experimental information that a free-fall parachutist weigh-
ing the equivalent of 70 kg, in a spreadeagled pose, reaches a terminal
velocity of around 54 m/s. To convert his/her k-value to that for a
snowboat, you will need to estimate the frontal area of each and use
the fact that the force is proportional to the area presented. To get the
terminal velocity equation, just use the free-fall (projectile) equation de-
scribed above and study it whenr = 0 since the parachutist is no longer
accelerating when terminal velocity is reached (by definition!)
The gravitational force is easy to model:
qx=px-,p z
z
px=nx - Ax
The partial derivatives are not hard to work out! The problem becomes
278 Dynamics of Snowboating
one dimensional since everything can be written in terms of, say, y.
To solve the single second order ODE, the best method is to use v
and y rather than y and t as the dependent and independent variables
respectively, using
y = vv . (22.10)
y
In this way, you can check the validity of much of the formalism and see
how well the numerical procedure is performing.
The M-file snowboat. m prompts you for the parameters y and k, the
starting velocity (horizontal components) uo and v0 and a time interval
6t over which to integrate numerically. Note that you control the start-
ing direction with u0 and v0 making them negative if necessary. The
subsequent direction of motion is determined by the topographical fea-
tures of the snow slopes via the equations of motion (22.8). Ideally, you
want the run to start near the top of a hill and grind to a halt near the
lowest point within the area. Excursions into neighbouring valleys are
expensive.
Exercises
22.1 Print out, or make rough sketch copies of the 3D-view plot and
of the contour plot provided by topog.
22.2 Give the locations of all hill tops and bottoms of valleys within
the area which is 2 km x 2 km. Give the altitudes as well.
22.3 Sketch on the plots a couple of paths suitable for snowboating.
22.4 Quote your estimate of a reasonable value for k based on the
free-fall parachutist data and your estimate of the relative frontal
areas involved.
22.5 Modify the surface function routines to describe the sloping
plane (22.9) as discussed above. Use snowboat to describe mo-
tion down the plane from the point (0, 2000) with initial velocity
(0, -2) m/s and compare your results with the exact answers got
by integrating the equation analytically.
22.6 If, instead, the sloping plane had the equation z = y/10 what
value of a, the coefficient of friction between snowboat and snow,
would lead to zero acceleration down the hill?
22.7 Find a reasonable coefficient of friction , and initial velocity
which allows the snowboat to reach the bottom of the valley
reasonably smoothly starting from the very highest point in the
Exercises 279
snow hills (use the original versions of snowsl.m, etc.). Give the
initial parameters, the approximate time taken and the approx-
imate top speed reached during the descent.
22.8 Try to find other similar runs from the remaining hill tops. Com-
ment on how the initial velocity might be achieved in each case.
22.9 Modify snowboat. m to display the change in potential energy
and the change in kinetic energy from the initial and final po-
sitions and give the results for the trip down from the highest
hill.
22.10 Give the energy differences for the same run but when the fric-
tion and air resistance are switched off. Comment on the two
sets of results.
23
Tides
281
282 Tides
tides.m - load tidal data tm(1:nt), hm(l:nt);
f ourdat . m - load sample signal data;
f ouran. m - Fourier analysis of data;
foursig.m - for checking purposes only;
locmax.m - local maxima of data;
times.mat - vector of tide measurement times;
heights.mat - vector of tide heights;
noisyt.mat - vector of sample signal times;
noisys.mat - vector of sample signal data.
Estimate ho, A, w and 0 as best you can and comment on the reliability
of each of these estimates. To do this, start by estimating A by noting the
`amplitude' of the main oscillation which is half the difference between
maximum and minimum. Then establish 0 by noting where each the
oscillation period starts. You can estimate ho from the water level at
mid-tide.
CO
e ti2nnt
T= cos ( t) f i sin ( Tt) . (23.3)
T
Since sin(2irnt/T) and cos(2irnt/T) have period T/n as a function of
t, each mode labelled by n corresponds to frequency n/T. Conversely
you can write the frequency coefficient An as an integral over t
An = T
fT
f (t)edt . (23.4)
Where the signal f (t) is only known at discrete (but regularly spaced)
times, this integral can be replaced by a sum. This is essentially what
the MATLAB routines fft (fast fourier transform) and its inverse ifft
do. There are two useful MATLAB demos to help you understand how
Fourier sums and transforms work.
studying the Fourier analysis plots and the spectrum results them-
selves;
making simple parametrisations of the dominant sinusoidal behaviour
as in equation (23.1);
using spline to interpolate subsets of data.
Exercises
23.1 Check out the time interval between measurements and the total
time covered by the tidal measurements which are supplied.
23.2 Look at the maximum, minimum and mean of the data.
23.3 Plot the height vs time over different time scales - a day, a week
etc.
23.4 Use the function locmax to estimate the heights of the high and
low tides. Are these really the actual high tide values?
23.5 Estimate as best you can, the typical time between consecutive
high tides.
23.6 Find on which days the tidal variation is strongest/weakest.
23.7 Find the dates covered by the given tidal data and the mean
measured tidal height in this period.
23.8 Find the absolute maximum and minimum measured heights
and when they were recorded.
23.9 Estimate the highest tide (whether measured directly or not)
and when it occurred. Do the same for the lowest tide.
23.10 Find the dominant frequency components in descending order
of importance. Comment on the numerical values of the corre-
sponding periods.
23.11 Estimate the average time between consecutive low or high tides.
23.12 Estimate the sea level at 3.10 pm on 13 January and at at 6pm
on 4 March in the year 1992.
23.13 Construct a plot of the rate in metres/second at which the tide
is going out during the afternoon of 22nd January 1992 over
a beach which has a gradient of 1 in 200 (assuming the tidal
variation is independent of the local geography). What is the
maximum speed achieved across the sand and at what time is
it achieved? You should use the parametrisation (23.1) to help
you do this.
Appendix 1
MATLAB Command Summary
Most commands can be found by using help but here is a quick and
compact summary. For further details, type: help command.
Basic operations
Built in values
pi 7r i,j
inf oo ans current answer
flops floating point count clock wall clock time
286
MATLAB Command Summary 287
Matrix functions
det determinant of square matrix
eig eigenvalue and eigenvectors of a square matrix
inv inverse
rref reduced row echelon form
rank rank of a matrix
288 MATLAB Command Summary
Graph operations
Function operations
Statistics operations
Load/save operations
save save variables to file
load load variables from file
diary fname log all MATLAB operations to file fname
chdir change file directory
dir list contents of directory
Appendix 2
Symbolic Calculations within
MATLAB
Here the semicolon is only optional as MATLAB will add one to meet
the Maple syntax. With syms, the familiar MATLAB syntax applies:
.............
>> syms x t y %%%'/'/%%%'/%%%'/ Declare 3 symbolic variables
>> f = x+2*t*y-t-2*t"3
>> g = diff(f,t), h = solve(g,y)
290
Symbolic Calculations within MATLAB 291
Integration - Chapter 11
To verify that f 1 , y dy = 2 ( c --yo - c - yl for c = 2, try
>> maple( 'c := 2; a := int(1/sqrt(c-y), y=y_O..y_l)' )
3 -1 -1 2 x3 -3
-1 2 3 -1 x4 4
Note that the first two lines for defining A should be typed in as one line
- here the `breaking' is due to text processing only. The final result
will be
x := VECTOR([-1, 2, 0, 1])
To get more help on Maple, type
>> mhelp % Also maple('help')
>> mhelp solve % Here for help on "solve"
Appendix 3
List of All M-files Supplied
The following lists all M-files which we provide for the reader via the
internet from
http://www.cup.cam.ac. uk/Scripts/webbook.asp?isbn=0521639204
or
http://www. cup. cam. ac. uk/Scripts/webbook. asp?isbn=0521630789
These are listed under the chapters in which they are first used. Several
are of course reused in subsequent chapters.
To locate the exact page number where an M-file or a MATLAB com-
mand is used in the book, check the index pages under headings `M-files'
and `MATLAB commands'.
Chapter 1 Chapter 4
none tsine.m
tsine2.m
polyex.m
Chapter 2 goatgr.m
leslie.m goatfn.m
parnorm.m
linenv.m
Chapter 3
cubics.m
fibno.m hypocyc.m
hail.m
gcdiv.m
pow.m
292
List of All M-files Supplied 293
Chapter 5 Chapter 7
tomato.m fodesol.m
toms.m fnxt.m
diabetic.m odegr.m
marks.m mode23.m
mannheim.m species.m
specfn.m
ode23k.m
Chapter 6 vderpol.m
c6exp.m vdplfn.m
exprand.m diffeqn.m
normrand.m dfeqfn.m
randme.m
unirand.m
Chapter 8 Chapter 11
none zzl.m
slidel.m
slidelfn.m
slide4.m
slide4fn.m
Chapter 9 Chapter 12
gcdran.m mobius.m
primes.m mobiusl.m
psp2.m cobm.m
miller.m cobq.m
matit2.m
matit3.m
Chapter 10
quadn.m
hypocy.m perdoub.m
linenv.m
paramc.m
294 List of All M-files Supplied
Chapter 13 Chapter 16
cnrl.m linsolv.m
cnr2.m chop.m
lu2.m
lu3.m
Chapter 14 lu4.m
randperm.m solv6.m
cycles.m spar_ex.m
rifflel.m
rifflela.m
Chapter 17
remm.m
rifflelc.m intdemol.m
rufflel.m intdemo2.m
rufflelc.m cont4.m
ruffle2.m cont7.m
polyfit2.m
polyval2.m
Chapter 15
full_new.m
Chapter 18
gauss ja.m
f_rate.m See Chapter 7
cont4.m
Chapter 19 Chapter 20
queue.m fishy.m
exprand.m lmfish.m
normrand.m mparft.m
unirand.m mparst4.m
resid4.m
fishdat.m
List of All M-files Supplied 295
Chapter 21 Chapter 23
fludat.m tides.m
plagdat.m fourdat.m
colddat.m foursig.m
sirepi.m locmax.m
sirfn.m times.mat
mparst3.m heights.mat
resid3.m noisyt.mat
lagsum.m noisys.mat
Chapter 22
topog.m
snowsl.m
snowboat.m
snbtfn.m
fsnowO.m
fsnowl.m
fsnow2.m
snowmn.m
snowmx.m
Appendix 4
How to Get Solution M-files
296
Appendix 5
Selected MATLAB Resources on the
Internet
The name MATLAB stands for matrix laboratory. MATLAB was orig-
inally written to provide easy access to matrix software developed by the
UNPACK and EISPACK projects, which together represent the state
of the art in software for matrix computation. MATLAB converts user
commands (including M-files) into C or C++ codes before calling other
program modules (subroutines). Today MATLAB has been developed
to cover many application areas. You may search the internet with the
key word MATLAB to see some evidence.
To learn more about the product developers, check the web page
http://www.mathworks.com/
If you hope to find out more of, or wish to use directly, the LAPACK
and EISPACK programs, check the following
http://www.netlib.org/lapack/
For users who would like a gentle and easy introduction to MATLAB,
try the following
http://www.ius.cs.cmu.edu/help/Math/vasc-help-matlab.html
http://www.unm.edu/cirt/info/software/apps/matlab.html
For novice users who prefer short guides to MATLAB with concrete
and simple examples, try
http://www.liv.ac.uk/CSD/acuk-html/486.dir/486.html
http://www-math.cc.utexas.edu/math/Matlab/Matlab.html
http://www.indiana.edu/-statmath/math/matlab/index.html
Advanced users of MATLAB may check the following pages for more
comprehensive guides to MATLAB
297
298 Selected MATLAB Resources on the Internet
http://www.rrz.uni-hamburg.de/RRZ/software/Matlab/
http://www.engin.umich.edu/group/ctm/
ftp://ftp.math.ufl.edu/pub/matlab/
Even more sophisticated examples may be available on the internet; e.g.
try the following for more M-files
ftp://ftp.cc.tut.fi/pub/math/piche/numanal/
http://www.ma.man.ac.uk/-higham/testmat.html
http://www.tc.cornell.edu/-anne/projects/MM.html
http://users.comlab.ox.ac.uk/nick.trefethen/multimatlab.html
ftp://ftp.mathworks.com/pub/mathworks/toolbox/matlab/sparfun/
The following sites contain technical information on MATLAB and
FAQ (frequently asked questions) pages
http://www.mathworks.com/
http://www-europe.mathworks.com/
http://www.math.ufl.edu/help/matlab-faq.html
References
299
300 References
[17]V.M.Tikhomirov, Stories About Maxima and Minima, Washington DC:
Mathematical Association of America, 1990.
Index
301
302 Index
eigenvalue, 22, 105, 106, 153, 155, 215 2D, 228
eigenvector, 22, 105, 152, 155 linear and cubic, 226
envelope, 51, 57, 118, 119, 121, 122 spline, 226
epicycloid, 135 iterations, 189, 196
epidemics, 265 iterative refinement, 212
SIR model, 266
equations of motion, 275 kinetic energy, 137
errors
undefined variable, 5 ladder, 120
Euclid's algorithm, 34 least common multiple, 132, 173
event table, 249 least squares fitting, 63, 260, 270
formulae derivations, 71
Fermat's (little) theorem, 36 nonlinear, 67
Fermat's theorem, 112 least squares method, 224
Fibonacci numbers, 31, 39 1D, 224
files 2D, 233
edit, 9 proof of, 72
save, 9 line
fish, 258 broken, 140
catching, 262 LU decomposition, 210
growth, 258
fixed point, 156 M-file, 9
floating point operations (flops), 208 M-files
Fourier analysis, 284 c6exp, 80
Fourier integral, 282 chop, 211
Fourier methods, 281, 282 curl, 166
Fourier sum, 282 cnr2, 167
fractal set, 170 cobra, 149
friction, 144, 276 cobq, 157, 160
function M-file, 12, 13 colddat, 266
functional, 74 cont4, 199, 230
cont7, 233, 234
garage door, 121 cubics, 54
Gauss elimination method, 210 cycles, 173
Gauss-Jacobi, 191 dfeqfn, 96
global variable, 139 diabetic, 69
goat problem, 50, 55, 58 diffeqn, 96
grad, 276 exprand, 80, 248
grain plot, 90 f_rate, 195, 202
graph, 216 fftdemo, 281
annotation, 233 fibno, 32, 39
connectivities, 216 fishdat, 257
edge, 216 fishy, 257
node, 216 fludat, 266
gravity, 137 fnxt, 90
greatest common divisor, 34, 108, 114 fodesol, 89, 257
three numbers, 40, 110 fouran, 282
fourdat, 282
hailstone function, 33, 39 fourier, 281
hole, square, 122 foursig, 282
hypocycloid, 56, 59 fsnow0, 273
fsnowl, 273
integral, 137 fsnow2, 273
improper, 138 full-new, 190, 197
inter-arrival time, 80, 247 gauss ja, 191, 198
interpolation gcdiv, 36
1D, 221 gcdran, 108
Index 303
random tangents
distributions, 80 parallel, 122
integers, 76 Taylor polynomial, 47, 196
numbers, 75 tides, 281
pairs, 108 data, 282
triples, 109 trace, 106
variable, 85 transpose, 7
remainder, 33 transposition, 172
when numbers are large, 37, 40
residual correction, 212 vector
resistance, 276 representing polynomial, 44
root mean square error, 227, 235 vectors, 6, 18, 31
rotation, 130 column, 7
306 Index
row, 7