Professional Documents
Culture Documents
http://ebooks.cambridge.org/
Chapter
2 - Matrices and Complex Numbers pp. 18-30
Chapter DOI: http://dx.doi.org/10.1017/CBO9780511624117.003
Cambridge University Press
2
Matrices and Complex Numbers
and b = (1.5,2,-1).
a = [-1 2 4]
b = [1.5 2 -1]
a = [-1,2,4]
b = [1.5,2,-1]
or
c=a.*b
where there is a dot before the multiplication sign *, multiplies the vectors 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)
sqrt(sum(a.*a))
18
Cambridge
Books Online Cambridge University Press, 2010
Downloaded from Cambridge Books Online by IP 81.180.218.225 on Fri Apr 22 09:35:57 BST 2016.
http://dx.doi.org/10.1017/CBO9780511624117.003
Cambridge Books Online Cambridge University Press, 2016
19
A = [-1 1 2
3 - 1 1
-1 3 4]
with different rows separated by <Enter>, therefore appearing on different lines. The MATLAB prompt will not reappear until you have
finished defining the matrix by closing the bracket ] . If you accidentally
make one row have more entries in it than another row then you will
get an error message. Note that, if you wish, you can insert commas
between the entries which lie in the same row:
If you find the line spacing in MATLAB output is over-generous for
your taste you can suppress extra spacing with the command
format compact
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
3xi
-xx
+
+
x2
x2
3x2
+
+
+
2x3 =
x3 =
4x3 =
10
-20
40
Cambridge
Books Online Cambridge University Press, 2010
Downloaded from Cambridge Books Online by IP 81.180.218.225 on Fri Apr 22 09:35:57 BST 2016.
http://dx.doi.org/10.1017/CBO9780511624117.003
Cambridge Books Online Cambridge University Press, 2016
(2.1)
20
can be written as
Ax = b
(2.2)
T
b=[10
-20
40]'
det(A)
which gives the answer 10. The solution for x is then obtained by
x = A'1 Ax = A~xh,
which in MATLAB is
x=inv(A)*b
C=A*A
det(C)
D=A~3
det(D)
Cambridge
Books Online Cambridge University Press, 2010
Downloaded from Cambridge Books Online by IP 81.180.218.225 on Fri Apr 22 09:35:57 BST 2016.
http://dx.doi.org/10.1017/CBO9780511624117.003
Cambridge Books Online Cambridge University Press, 2016
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
diag([l 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)
F = [A b]
Cambridge
Books Online Cambridge University Press, 2010
Downloaded from Cambridge Books Online by IP 81.180.218.225 on Fri Apr 22 09:35:57 BST 2016.
http://dx.doi.org/10.1017/CBO9780511624117.003
Cambridge Books Online Cambridge University Press, 2016
22
Typing
G=rref(F)
1
1 0 0
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
r r e f ( [ l 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)
A=diag([l 2 3])
eig(A)
P=[l 2 3;4 5 6; 5 7 8]
det(P)
f Geometrically, this means that the linear map associated with A sends x to another
vector along the same line through the origin as x.
Cambridge
Books Online Cambridge University Press, 2010
Downloaded from Cambridge Books Online by IP 81.180.218.225 on Fri Apr 22 09:35:57 BST 2016.
http://dx.doi.org/10.1017/CBO9780511624117.003
Cambridge Books Online Cambridge University Press, 2016
B=inv(P)*A*P
eig(B)
[Y,D]=eig(B)
23
clear
i
J
clear
i,j
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=l+i; b=2-3i;
c=a*b
Cambridge
Books Online Cambridge University Press, 2010
Downloaded from Cambridge Books Online by IP 81.180.218.225 on Fri Apr 22 09:35:57 BST 2016.
http://dx.doi.org/10.1017/CBO9780511624117.003
Cambridge Books Online Cambridge University Press, 2016
24
d=sqrt(a)
(-iru/2)
(-2+21)^(1/3)
abs(a)
angle(a)
real(a)
imag(a)
produces the imaginary part without the i attached. For example with
a = l + i the answer is 1.
conj(a)
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 KT 15 ).
Try also
exp(i*pi)
Cambridge
Books Online Cambridge University Press, 2010
Downloaded from Cambridge Books Online by IP 81.180.218.225 on Fri Apr 22 09:35:57 BST 2016.
http://dx.doi.org/10.1017/CBO9780511624117.003
Cambridge Books Online Cambridge University Press, 2016
25
for the age structure of the population of a country or other large community. 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 05, 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 GU% per annum
and in the 0-5 band at d/% 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) = (n 1 (2),n 2 (2),n 3 (2),n 4 (2)) T
of populations in the four age-bands in year 2 to the column vector
Cambridge
Books Online Cambridge University Press, 2010
Downloaded from Cambridge Books Online by IP 81.180.218.225 on Fri Apr 22 09:35:58 BST 2016.
http://dx.doi.org/10.1017/CBO9780511624117.003
Cambridge Books Online Cambridge University Press, 2016
26
Age-
No. in
Year
band
band
grps.
0-5
6-19
n2
14
20-59
n3
40
60-69
n4
10
Survive
Die
Leave
Enter
band
band
100 ) ~6~
"14
n3
1^0n3
[} ~ 100 J ~6~
40
40
year within an age-band is the same, so the 'leavers' are just the 'survivors' divided by the number of years covered by the age-band. In practice 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(l) for year
1 by a matrix equation
N(2) =
where L is the so-called Leslie matrix
/I(i-4) o 4
i(!-4) n o
x_
0
U
i
14
39
40
Similarly,
N(3) = LN(2) = L 2 N(1),
and so on. In general, we have:
Given data for t = 0, say, and some estimates of 6, di and GU, 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 l e s l i e . m is provided to allow you to explore these ideas.
Type
Cambridge
Books Online Cambridge University Press, 2010
Downloaded from Cambridge Books Online by IP 81.180.218.225 on Fri Apr 22 09:35:58 BST 2016.
http://dx.doi.org/10.1017/CBO9780511624117.003
Cambridge Books Online Cambridge University Press, 2016
Exercises
27
leslie
L~4
N50=L~50*N
Type
[Y D] = eig(L)
to obtain the eigenvectors and eigenvalues of the Leslie matrix L. Find
the largest eigenvalue and its corresponding eigenvector E. You can compare the population vector after say 50 years, L50N, with this eigenvector
E by
N50=L~50*N
N50./E
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?
2.1
Exercises
As a simple, example of manipulation of complex numbers, consider the following, which will be covered in more general terms
Cambridge
Books Online Cambridge University Press, 2010
Downloaded from Cambridge Books Online by IP 81.180.218.225 on Fri Apr 22 09:35:58 BST 2016.
http://dx.doi.org/10.1017/CBO9780511624117.003
Cambridge Books Online Cambridge University Press, 2016
28
z=3+4i
Now type
z=(z~2-l)/(2*z)
Using the j key, repeat the last command several times. Eventually the answers settle downconvergeto either i or i; for
the starting value 3 + 4z 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 = oo, if it
gives anything at all, should give z = oc again. But there
are other starting values such as z = 2 which produce more
reasonable nonconvergence. Which starting values for z produce 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=[l 1/2 1/3
1/2 1/3 1/4
1/3 1/4 1/5]
b=[l 0 0] '
det(A)
X=inv(A)*b
Cambridge
Books Online Cambridge University Press, 2010
Downloaded from Cambridge Books Online by IP 81.180.218.225 on Fri Apr 22 09:35:58 BST 2016.
http://dx.doi.org/10.1017/CBO9780511624117.003
Cambridge Books Online Cambridge University Press, 2016
Exercises
29
2.3
Cambridge
Books Online Cambridge University Press, 2010
Downloaded from Cambridge Books Online by IP 81.180.218.225 on Fri Apr 22 09:35:58 BST 2016.
http://dx.doi.org/10.1017/CBO9780511624117.003
Cambridge Books Online Cambridge University Press, 2016
30
V=C(:,1)
VI./V2
Cambridge
Books Online Cambridge University Press, 2010
Downloaded from Cambridge Books Online by IP 81.180.218.225 on Fri Apr 22 09:35:58 BST 2016.
http://dx.doi.org/10.1017/CBO9780511624117.003
Cambridge Books Online Cambridge University Press, 2016