Matlab For Chemists
Matlab For Chemists
P.E.S. Wormer
Theoretical Chemistry
University of Nijmegen
The Netherlands
April 2003
ii
PREFACE
matlab is an interactive program package for numerical computation and
visualization. It originated in the middle of the 1980s as a user interface
to matrix manipulation packages written in fortran. Hence its name:
Matrix Laboratory. Over the years the system has been extended, it now
includes a programming language, extensive visualization tools, and many
numerical methods.
These are notes accompanying a course in matlab for chemistry and
natural science students at the University of Nijmegen. The course has the
following objectives:
Offering of practical exercises supporting an obligatory course in linear
algebra.
Offering of practical exercises supporting an obligatory course in quantum mechanics.
A first introduction to programming (loops, if then else constructs,
functions).
A tool for fitting and plotting data obtained in the chemical laboratory.
Although no new mathematical concepts are introduced in the present lectures, the mathematical knowledge necessary to do the matlab exercises,
is briefly reviewed.
The author thanks dr. B. J. W. Polman of the Subfaculty of Mathematics
for his careful reading of the notes and his useful comments.
Contents
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
ii
1
1
7
9
2 Matrices
2.1 Matrix multiplication . .
2.2 Non-singular and special
2.3 Matrix factorizations . .
2.4 Exercises . . . . . . . .
. . . . .
matrices
. . . . .
. . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
13
13
15
18
20
flow
. . .
. . .
. . .
control
29
. . . . . . . . . . . . . . . . . . . . . . 29
. . . . . . . . . . . . . . . . . . . . . . 31
. . . . . . . . . . . . . . . . . . . . . . 34
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
39
39
40
44
46
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
49
49
51
53
56
57
.
.
.
.
.
.
.
.
iii
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
iv
CONTENTS
7 More plotting
7.1 3D plots . . . . . . . . . . .
7.2 Handle Graphics . . . . . .
7.3 Handles of graphical objects
7.4 Polar plots . . . . . . . . .
7.5 Exercises . . . . . . . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
61
61
63
64
67
69
8 Cell
8.1
8.2
8.3
8.4
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
73
73
78
80
84
arrays and
Cell arrays .
Characters .
Structures .
Exercises .
structures
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
.
.
.
.
.
.
.
.
All internal operations in matlab are performed with floating point numbers 16 digits long, as e.g., 3.141592653589793 or 0.3141592653589793e-1
(e-1 indicates 101 ). The simplest data structure in matlab is the scalar
(handled by matlab as a 1 1 matrix). Scalars can be given names and
assigned values, e.g.,
>> num_students = 25;
>> Temperature = 272.1;
>> Planck
= 6.6260755e-34;
It is important to understand fully the difference between the mathematical
equality a = b (which can be written equally well as b = a) and the matlab
assignment a=b. In the latter statement it is necessary that the right hand
side has a value at the time that the statement is executed. The right hand
side is first fully evaluated and then the result is assigned to the left hand
side. Example:
>> a = 33.33;
>> a = 3*a - a
a =
66.6600
First the expression 3*a-a is fully evaluated. The value of a, which it has
just before the statement, is substituted everywhere. Only at the end of the
evaluation a is assigned a new value by means of the assignment symbol =.
A variable name must start with a lower- or uppercase letter and only the
first 31 characters of the name are used by matlab. Digits, letters and underscores are the only allowed characters in the name of a variable. matlab
is case sensitive: the variable temperature is another than Temperature.
Scalars can be added: a+b, subtracted: a-b, multiplied: a*b, divided: a/b
and taken to a power: a^b. The usual priority rules hold (multiplication
1
before addition, etc.), but it is better not to rely on this and to force the priority by brackets. Do not write a/b^3, but (a/b)^3, or a/(b^3), whatever
your intention is.
The second simplest data structure in matlab is the vector, which is
simply a sequence of floating point numbers. Vectors come in two flavors:
row vectors and column vectors. Row vectors are entered either space delimited or comma delimited, as
>> a=[1 2 3 -2 -4]
a =
1
2
3
-2
-4
>> b=[1,2,3,-2,-4]
b =
1
2
3
-2
-4
The sequence is delimited by square brackets. Notice that the vectors a and
b are indeed equal. After they have been entered at the matlab prompt
(>>), matlab echoes the input. This echoing of input is suppressed when
the statement is ended by a semicolon (;). The vector is shown by typing
its name, thus,
>> a=[1 2 3 -2 -4]; % No output
>> a
% echo the vector a
a =
1
2
3
-2
-4
Note that comments may be entered preceded by a % sign, they run until
end of line. The rules for naming vectors are the same as for scalars. (Case
sensitive, starting with letters, length 31, only digits, letters and underscores in the name). Column vectors are entered either semicolon delimited
or end of line delimited.
>> a=[1;2;3]
a =
1
2
3
>> a=[
1
2
3]
a =
1
2
3
A row vector can be turned into a column vector and vice versa (transposition) by the transposition operator ()
>> a=[1 2 3];
>> b=a
b =
1
2
3
>> c=b
c =
1
2
Note
Experience shows that the most common error in matlab is forgetting
whether a vector is a row or a column vector. We adhere strictly to the
convention that a vector is a column.
The easiest way to enter a column vector is as a row plus immediate
transposition, thus,
>> a=[5 4 3 2]
a =
5
4
3
2
A single element of a row or column vector can be retrieved as e.g., a(4),
this gives the fourth element of a (the number 2 in the example). A range
can be returned by the use of a colon (:). For instance, b=a(2:3) returns
a column vector b with the digits 4 and 3 as elements. The word end gives
the end of a vector: b=a(2:end) creates a column vector b containing 4, 3,
2.
In mathematics and physics the norm and the length of a vector are often
used as synonyms. In matlab the two concepts are different, length(a)
returns the number of components of a (the dimension of the vector), while
norm(a) returns the norm of a. Remember that the norm |a| of a is by
definition |a| = a a.
The usual mathematical operations can be performed on vectors: addition and subtraction of two vectors (provided the vectors are of the same
length) and multiplication and division by a number.
>> a=[1 2 3 4 5];
>> b=[5 4 3 2 1];
>> 2*a+3*b
ans =
17
16
15
14
13
In mathematics the following operation is not defined: s + a, where s is
a scalar (number) and a is a vector. However, in matlab the following
happens,
>> a
a =
0.5028
0.7095
0.4289
0.3046
>> a=a+1
a =
1.5028
1.7095
1.4289
1.3046
The inner (dot) product of two real vectors consisting of n elements is
mathematically defined as
b1
n
b2
X
ai bi
a b a1 b1 + a2 b2 + + an bn = a1 a2 an . =
..
i=1
bn
In matlab:
>> a =[ 0.4225
>> b =[ 0.4574
>> a*b
ans =
1.5193
0.8560
0.4507
0.4902
0.4122
0.8159
0.9016
0.4608];
0.0056];
for
i = 1, . . . , 5,
successively. Suppose we forgot the dot in the division, then matlab does
not give an error, but the following unexpected result1 (a 5 5 matrix):
>> error=[100*(obs-fit)/obs]
error =
-2.9216
-0.3320
0.1328
0
0
0
0
0
0
0
0
0
0
0
0
0.3652
0
0
0
0
4.8141
0
0
0
0
1.2
The reason is that if matlab meets an expression of the type s/a, where a is a column
vector, it tries to solve the equation xa = s, and obviously one of the possible solutions is
the row vector x = (s/a1 , 0, 0, . . . , 0). We do not get a matrix of five of these row vectors,
because there is a prime on the result, but the transposed matrix.
for i, j = 1, . . . , n, i 6= j.
(1.1)
An arbitrary real vector a of dimension n can be written as a linear combination of the n orthogonal vectors ri
a = a1 r1 + a2 r2 + + an rn =
n
X
ai ri ,
i=1
n
X
i=1
ai rj ri =
n
X
i=1
1.3. EXERCISES
1.3
Exercises
Exercise 1.
Try to reproduce the examples above to get acquainted with matlab.
Exercise 2.
Type in the column vector a containing the six subsequent elements: 1, 2,
3, 4, 5, 6. Normalize this vector. That is, determine its norm and divide
the vector by it. Check your result by computing the norm of the normalized
vector.
Exercise 3.
Type in the column vector b containing the subsequent elements: 1, 2, 3,
4, 5, 6. Determine the angle (in degrees) of this vector with the vector of
the previous exercise. The answer is very simple, explain why.
10
Exercise 4.
A root (or zero) of a polynomial P (x) = a0 + a1 x + + an xn is a solution
of the equation P (x) = 0. The main theorem of algebra states that this
equation has exactly n roots, which may be complex.
Determine the roots of the 4th degree polynomial
x4 11.0x3 + 42.35x2 66.550x + 35.1384.
Hint:
Put the coefficients of powers of x into a column array c (in decreasing
power of x) and issue the command roots(c) (a call to the matlab function
roots, use the matlabhelp function to see the syntax).
Exercise 5.
In the USA the curious temperature scale Fahrenheit2 is in daily use.
To convert to the Celsius scale use the formula C = 5(F - 32)/9. Prepare
a table F starting at 20 C, ending at 100 C with steps of 5 C that
contains Celsius converted to Fahrenheit.
Hint: A row vector containing the Celsius steps can be prepared by the
command: C=[-20:5:100]. To get a nice table on the screen use [C F].
Compute in degrees Celsius the points: 0, 32, 96, and 212 F by interpolation with the matlab function interp1. For instance, interp1(F,C,104)
returns 40.
Exercise 6.
Consider the methane molecule CH4 . The four protons are on the 4 alternating corners of a cube, with one CH bond pointing into the (1, 1, 1)
direction and the other bonds pointing to other corners in agreement with
methane being tetrahedral. All CH bondlengths are 1.086
A. Compute the
6 HCH angles and the 6 HH distances (all should be equal due to the
high symmetry of methane).
Exercise 7.
Compute
[1 2 3 4].*[24 12 8 6]
[1 2 3 4]*[24 12 8 6]
[1 2 3 4]*[24 12 8 6]
2
It is named after Gabriel Daniel Fahrenheit (16861736), who invented it in 1714,
while working in Amsterdam.
1.3. EXERCISES
11
4
X
x5i .
i=1
Hint:
The matlab function sum(y) sums the components of the vector y.
Exercise 9.
2
Consider the vector x = 1. Construct two vectors y and z orthogonal
3
3
3
2
r1 = 2 and r2 = 1
1
4
and e3 = 0
and e2 = 1
e1 = 0
1
0
0
with respect to the basis r1 , r2 , r3 .
Exercise 11.
The OH bond distance in the water molecule H2 O is 0.91
A. The H-O-H
12
2. Matrices
Before introducing matrix multiplication we wish to point out thatas a
modern computer packagematlab has extensive built-in documentation.
The command helpdesk gives access to a very elaborate interactive help
facility. The command help cmd gives help on the specified command cmd.
The command more on causes help to pause between screenfuls if the help
text runs to several screens. In the online help, keywords are capitalized to
make them stand out. Always type commands in lowercase since all command and function names are actually in lowercase. Recall in this context
that matlab is case sensitive. Dont be confused by the capitals in the help,
practically all matlab commands are in lower case only.
2.1
Matrix multiplication
3 -5];
>> b = [2
7];
>> c = [-1 6
1];
>> [a b c]
ans =
1
-1
-5
>> [a b c c b a]
ans =
1
-1
-1
-5
-5
13
14
CHAPTER 2. MATRICES
A matrix may be entered as rows, just like column vectors, which for matlab
are in fact nothing but non-square matrices. Example of entering a 2 4
matrix (2 rows, 4 columns):
>> A=[ 10.1
-11.0
A =
10.1000
-11.0000
67.3000
1.2000
88.0000
14.7000
m
X
Aji ci ,
i=1
where the matrix element Aji (ai )j is the j th component of ai . The result
of the multiplication A c is a column vector of length n.
Back in matlab we note that the matrix-vector multiplication is simply
given by *, provided the dimensions are correct. In order to give an example
we introduce the matlab function rand; rand(n,m) returns a matrix with
n rows and m columns of which the elements are positive random numbers
in the range 0 1.
>> A=rand(4,3)
A =
0.1934
0.1509
0.6822
0.6979
0.3028
0.3784
0.5417
0.8600
0.8537
0.5936
0.4966
0.8998
15
m
X
Aj 0 i (cj )i .
i=1
2.2
Non-singular matrices and determinant. The unit matrix, random matrix and
a matrix with ones.
16
CHAPTER 2. MATRICES
The identity (or unit) matrix I plays an important role in linear algebra. It
is a square matrix with off-diagonal elements zero and the number one on
the diagonal,
(I)ij = ij ,
where the Kronecker delta was defined in Eq. (1.1). matlab has the phonetic name eye for the function that returns the identity matrix.
>> eye(4)
ans =
1
0
0
0
0
1
0
0
0
0
1
0
0
0
0
1
17
a square matrix
0.4449
0.6946
0.6213
0.7948
0.9568
0.5226
0.8801
0.1730
>> Ai=inv(A)
% Calculate the inverse
Ai =
3.6375
0.3718
0.0517
-3.0577
-3.2104
0.1104
-0.6997
3.7590
-1.9243
3.4861
0.0636
1.0540
0.3974
-2.4293
0.9644
0.0306
>> A*Ai-eye(4)
% Subtract unit
ans =
1.0e-015 *
% Prefactor for
-0.1110
-0.2220
0.0555
-0.0139
-0.2220
-0.0069
0
0
-0.1110
0.1943
-0.2220
0.1110
matrix
the total matrix
-0.0278
-0.0278
-0.0486
0
Here we used that matrices of the same dimensions can be subtracted and
we see that A A1 is equal to the identity matrix within numerical precision
(15 to 16 digits). Here matlab applies the rule that cA gives a matrix in
which each individual matrix element is multiplied by the real number c.
We have seen that the operator turns a row vector into a column
vector and vice versa. If we replace all rows of a matrix by its columns we
transpose the matrix, i.e., if A has the matrix elements Aij , i = 1, . . . , n, j =
1, . . . , m then AT has the matrix elements Aji , i = 1, . . . , n, j = 1, . . . , m.
Transposition of matrices is performed by as well.
>> A=rand(4,2)
18
CHAPTER 2. MATRICES
A =
0.4235
0.5155
0.3340
0.4329
>> B=A
B =
0.2259
0.5798
0.7604
0.5298
0.4235
0.2259
0.5155
0.5798
2.3
0.3340
0.7604
0.4329
0.5298
Matrix factorizations
(2.1)
I = QT = Q1 = QQT = I
X
X
ij = QQT ij =
Qik QTkj =
Qik Qjk .
=
(2.2)
Since the sum is over the column index k, the rightmost expression is nothing
but the inner product between row i and row j of Q.
An important theorem of linear algebra states that any real nm matrix
A can be factorized as
A = QR,
where Q is an n n orthogonal matrix and R is an n m upper triangular
matrix. (Remember that an upper triangular matrix R has only vanishing
elements below the main diagonal, i.e., Rij = 0 for i > j). This theorem
is known as the QR decomposition of A. One way of looking upon this
19
n
X
Qik Rkj =
k=1
aj =
j
X
k=1
j
X
j
X
(qk )i Rkj
k=1
(2.3)
k=1
for i = 1, . . . , n.
(2.5)
and
q q = (q) (q) = 1.
(2.6)
20
CHAPTER 2. MATRICES
2.4
Exercises
Exercise 12.
Consider the following matrices and their dimensions: A (4 5), B (4 10),
C (10 4), and D (5 5). Predict the dimensions of ADAT and BCAD.
Verify your answer by creating the matrices with the matlab function rand
and by explicit matrix multiplication in matlab.
Exercise 13.
1. Enter the statements necessary to create the following matrices:
1 4 6
A = 2 3 5
1 0 4
2 3 5
B = 1 0 6
2 3 1
5 1 9 0
C = 4 0 6 2
3 1 2 4
3
4
D=
0
2
2
1
2
5
5
3
1
6
2. Compute the following using matlab, and write down the results. If
you receive an error message rather than a numerical result, explain
the error.
a.
b.
c.
d.
e.
f.
A+B
A-2.*B
(A-2).*B
A.^2
sqrt(A)
C
g.
h.
i.
j.
k.
l.
C+D
C+D
C.*D
A-2*eye(3)
A-ones(3)
A^2
Exercise 14.
Read the help of eye and ones. Create the following matrices, each with
one statement,
1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
Exercise 15.
and also
1
1
1
1
1
1
1
1
1
1
1
1
.
1
1
2.4. EXERCISES
21
X
m
1
1
2
c=
mi ri =
(r1 r2 rn ) .
M
M
..
i=1
mn
Here mi is the mass of nucleus i (mass of electrons is neglected) and ri is
Pn
the position of nucleus i, M =
i=1 mi is the total nuclear mass of the
molecule.
Consider now the isotope substituted methane CH2 D2 with mC 12
u, mH 1 u, mD 2 u, where u is the unified atomic mass unit. Take a
frame (system of axes) with C in the origin and use the geometry of methane
described in exercise 6 of Sec. 1.3.
Compute the position coordinates of the protons and the deuterons
and put these together with the position vector of carbon into a 3 5
matrix R.
Put the five nuclear masses into a corresponding column vector and
compute the total mass (the matlab function sum returns the sum of
the elements of a vector).
Compute the center of mass c by matrix vector multiplication. Is the
center of mass closer to the deuterons than to the protons?
Next we translate the frame so that its origin coincides with the center of
mass. Mathematically, this is accomplished as follows
R0 R (c c c) with R (r1 r2 rn )
| {z }
n times
since
m1
m1
m1
m1
m2
m2
m2
m2
R0 . (r10 r20 rn0 ) . = R . (c c) .
.
.
.
.
.
.
..
mn
mn
mn
mn
n
X
= Mc c
mi = 0
i=1
or
n
1 X
c
mi ri0 = 0.
M
0
i=1
22
CHAPTER 2. MATRICES
Compute the coordinates of the nuclei of CH2 D2 (including the position of carbon) with respect to the translated frame.
Hint: The translation matrix can be constructed as [c c c c c],
more elegant is by repmat(c,1,5), see help repmat.
Exercise 16.
The function reference qr(A) gives the QR decomposition of A. The function
qr(A) is an example of a matlab function that can optionally return more
than one parameter: [Q R] = qr(A) returns both the orthogonal matrix Q
and the upper triangular matrix R. Decompose A=rand(5,i) for i=1,3,5,7.
Check in all four cases whether Q is orthogonal and inspect R to see if it is
upper triangular.
Exercise 17.
In this exercise we construct a symmetric matrix with a known spectrum
(the set {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}) and we will diagonalize this matrix to verify
that the spectrum is indeed generated.
Create a 10 10 diagonal matrix D with 0,1, . . . , 9 on the diagonal.
(See help of diag).
Create a 10 10 random orthogonal matrix Q from A=rand(10) and
qr(A).
Create a 10 10 symmetric matrix H=Q*D*Q. Check whether H-H is
the zero matrix, i.e., that H is indeed symmetric. The matrix H has by
construction the spectrum 0,1,...,9.
Get the eigenvectors V and eigenvalue matrix D1 of H. (See the help
of eig). Inspect the diagonal matrix D1, are the diagonal elements
indeed what you expect? Verify that V is orthogonal.
Get the eigenvalues (diagonal elements of D1) of H into a column vector
D2 (see again the help of diag) and sort this vector, with the result
in Ds (see the help of sort). In addition to Ds, the function sort can
also return a permutation that may be applied to the columns of V.
Apply this permutation to get a sorted 10 10 matrix Vs. (Note in
this connection that a statement of the type V(:,[2,3,1] returns an
array with the second, third, and first column of V, respectively).
Make out of the vector Ds a 10 10 diagonal matrix D3 and verify that
within numerical precision
2.4. EXERCISES
23
H*Vs = Vs*D3
Do this by performing explicitly the matrix multiplication on the left
and right hand side of this equation.
Compare the columns of Vs with those of Q. Are they the same?
24
CHAPTER 2. MATRICES
Scripts
26
more on/more off. Press the q key to exit out of displaying the current
item.
It is important to notice that a script simply continues your matlab
session, all variables that you introduced by hand are known to the script.
After the script has finished, all variables changed or assigned by the script
stay valid in your matlab session. Technically this is expressed by the
statement: script variables are global.
Sometimes statements in a script become unwieldily long. They can be
continued over more than one line by the use of three periods ..., as for
example
s = 1 - 1/2 + 1/3 - 1/4 + 1/5 - 1/6 + 1/7 ...
- 1/8 + 1/9 - 1/10 + 1/11 - 1/12 ...
+ 1/13 - 1/14;
Blanks around the =, +, and signs are optional, but they improve readability.
3.2
Plotting
Grids, 2D plotting.
Scripts are particularly useful for creating plots. matlab has extensive
facilities for plotting. Very often one plots a function on a discrete grid.
The matlab command for creating a grid is simple,
grid = beg:inc:end
Here beg gives the first grid value, inc the increment (default 1) and end
the end value. Example:
>> g=0:pi/10:pi
g =
Columns 1 through 5
0 0.3142 0.6283 0.9425 1.2566
Columns 6 through 10
1.8850 2.1991 2.5133 2.8274
Column 11
3.1416
1.5708
If we now want to plot the sine function on this grid we simply issue the
command plot(g, sin(g)), since the command plot(x,y) plots vector y
versus vector x. After this command a new window opens with the plot.
3.2. PLOTTING
27
Suppose we now also want to plot the cosine in the same figure. If we
would enter plot(g, cos(g)), then the previous plot would be overwritten.
We can toggle hold on/off to hold the plot. Alternatively, we can create
two plots in one statement: plot(g, sin(g), g, cos(g) ), i.e,
>> plot(x1, y1, x2, y2)
plots vector y1 versus vector x1 and y2 versus x2. Different colors and line
types can be chosen, see help plot for information on this. For example,
plot(g,cos(g), r:) plots the cosine as a red dotted line. Even briefer:
plot(g,[sin(g) cos(g)]) (columns of a matrix plotted against a vector, whta happens if you leave the s off).
The xlabel and ylabel functions add labels to the x and y axis of the
current figure. Their parameter is a string (is contained in quotes), thus,
e.g., ylabel(sin(\phi)) and xlabel(\phi). The title function adds
a title on top of the plot. Example:
phi=[0:pi/100:2*pi];
plot(phi*180/pi,sin(phi))
ylabel(sin(\phi))
xlabel(\phi (degrees))
title(Graph of the sine function)
For people who know the text editing system LATEX this part of matlab is
easy, since it uses quite a few of the LATEX commands. \phi is LATEX to get
the Greek letter . (The present lecture notes are prepared by the use of
LATEX).
In case we want to plot more than one set of y values for the same grid
of x values, matlab offers a solution. In the command plot(x,y) x is
a vector, let us say it is a column vector of length n. Then y can be an
n k matrix (by definition consisting of k columns of length n). The plot
command gives k curves as function of the grid x.
Example:
phi=[0:pi/100:2*pi];
y(:,1) = cos(phi);
y(:,2) = sin(phi);
plot(phi, y)
title(Graph of the sine and cosine functions)
28
3.3
Exercises
Exercise 18.
Return to exercise 4 where you were asked to find the roots (zeros) of the
polynomial
x4 11.0x3 + 42.35x2 66.550x + 35.1384.
Write a matlab script to plot this function. Define first a grid (set of
x points) with 0.6 x 4.9 and plot the function on this grid (do not
forget dots in the dot operations!). Try to find the roots of this polynomial
graphically. When you have located the roots approximately refine your
grids and try to get the roots with a two digit precision. The command
grid on is helpful, try it!
Exercise 19.
Write a matlab script that plots the functions exp(R) and R exp(R)
in one figure. Do not forget the dot (pointwise operation) at the appropriate
places! Let the first curve be solid green and the second dashdotted blue.
Assume that the grid R and the parameter are first set by hand in the
matlab session that calls the script. Once the script is finished, experiment
with and the grid to get a nice figure. Put to the x axis: distance R (a0 ),
and use as a title Radial part of s and p functions. Hint: the subscript 0
is obtained by the underscore: a_0.
Exercise 20.
Write a matlab script that plots the function
y=
1
1
+
6,
(x 0.3)2 + 0.01 (x 0.9)2 + 0.04
which has two humps. Compute in the script first the grid 0:0.002:1 and
then the array (= vector) y and then call plot(x,y). Start the script with
the command clear all, which removes all variables from your matlab
workspace, to avoid possible side effects.
Exercise 21.
Draw a circle with radius 1 and midpoint at (0, 0).
Two possibilities (try both):
Matrix sections
Thus far we have met twice the colon (:) operator: once to get a section
out of a matrix and once to generate a grid. In fact, these are the very same
uses of this operator. To explain this, we first observe that elements from
an array (= matrix) may be extracted by the use of an index array with
integer elements.
Example:
>> A = rand(5);
>> I = [1 3 5];
>> B = A(I,:);
>> A, B
A =
0.9501
0.7621
0.2311
0.4565
0.6068
0.0185
0.4860
0.8214
0.8913
0.4447
0.6154
0.7919
0.9218
0.7382
0.1763
0.4057
0.9355
0.9169
0.4103
0.8936
0.0579
0.3529
0.8132
0.0099
0.1389
0.6154
0.9218
0.1763
0.4057
0.9169
0.8936
0.0579
0.8132
0.1389
B =
0.9501
0.6068
0.8913
0.7621
0.0185
0.4447
Explanation:
For illustration purpose we created a 5 5 matrix A suppressing its printing.
Then we created the integer array I and used it to create an array B that
has the same columns as A but only row 1, 3, and 5 of A. Parenthetically,
note the use of the comma, we can put more than one command on a single
line: the commands must be separated by a semicolon (no printing) or a
comma (do print).
We get the very same matrix B by the command B=A(1:2:5,:), because
on the fly an array [1,3,5] is prepared and used to index A. Recall that
29
30
1:2:5 generates the grid [1,3,5], so that indeed the two uses of the colon
are the same.
A similar mechanism can be used to remove rows and/or columns from
a matrix. Using the same A and I as in the previous example,
>> C=A;
>> C(I,:)=[]
C =
0.2311
0.4860
0.4565
0.8214
0.7919
0.7382
0.9355
0.4103
0.3529
0.0099
31
% show x
Alternatively, we may use the matlab command repmat, which in fact uses
the same mechanism. The following command constructs the very same
matrix X: repmat(x,1,3).
4.2
Flow control
It often happens that one wants to repeat a calculation for different values
of a parameter. To this end matlab uses the for loop. The general form of
a for statement is:
for var = expr
statement
...
statement
end
Here expr can be a rectangular array, although in practice it is usually a row
vector of the form x:y, in which case its columns are simply scalars. The
columns of expr are assigned one at a time to var and then the following
statements, up to end, are executed. For loops can be nested:
A=zeros(10,5);
for i = 1:10
for j = 1:5
32
% pi=3.14.. is non-zero
% output of disp
% no output
33
equal
less than
greater than
less or equal
greater or equal
not equal
34
Explanation:
The command while repeats statements up to end an indefinite number of
times. The general form of a while statement is:
while expr
statements
....
statements
end
The statements in the body of the while loop are executed as long as the
expr has only non-zero elements. Usually expr is a logical expression that
results in 0 or 1. As soon as expr becomes 0 the loop is quitted.
4.3
Exercises
Exercise 22.
What is the value of i printed as last statement of the following script?
V=[rand(2,1); -rand(2,1); rand(2,1); -rand(2,1)]
i = 1;
v = V(i);
while v >= 0
i = i+1;
v = V(i);
end
i
Exercise 23.
4.3. EXERCISES
35
1 2 1
A = 2 4 2 .
3 3 5
Exercise 25.
x1
A Vandermonde1 matrix is generated from the column vector x = ...
xn
as follows:
1 x1 x21 x31
1 x2 x2 x3
2
2
.. ..
..
..
. .
.
.
1 xn x2n x3n
xn1
1
xn1
2
..
.
xn1
n
Write a script that computes the Vandermonde matrix from a column vector
x of arbitrary length.
1
ory.
36
Hint:
This script does not need more than three statements if we use cumprod;
see its help.
Exercise 26.
You may have noticed that the description above of the if elseif else
end construction was very brief. In particular it was not explained what
happens if conditions in different elseifs are simultaneously true. Look at
the following three programs and predict what they put on the screen.
a=-3;
if a < -5
disp( < -5 )
elseif a < -2
disp( < -2 )
elseif a < -1
disp( < -1 )
else
disp(rest)
end
|
|
|
|
|
|
|
|
|
|
a=-3;
if a < -5
disp( < -5 )
elseif a < -1
disp( < -1 )
elseif a < -2
disp( < -2 )
else
disp(rest)
end
|
|
|
|
|
|
|
|
|
|
a=-3;
if a < -3
disp( < -3 )
elseif a < -4
disp( < -4 )
elseif a < -5
disp( < -5 )
else
disp(rest)
end
Make a script out of the first program and verify your prediction. Then edit
this script to get the second program and verify again, and do this also for
the third program.
Exercise 27.
In quantum mechanics the angular momentum operators lx , ly , lz play an
important role. Also l+ lx +ily and l lx ily are often considered. The
latter operators are represented by (2l + 1) (2l + 1) matrices with indices
m = l, . . . , l and m0 = l, . . . , l. They are defined by
(L )mm0 = m,m0 1
l(l + 1) m0 (m0 1)
That is, L+ is almost completely zero with only non-zero elements on the
diagonal above the main diagonal. Likewise, L has only elements on the
diagonal below the main diagonal.
p
1. Compute the two vectors s = l(l + 1) m(m 1) for l = 5 and
m = 5, . . . 5. Both vectors are of length 2l + 1.
2. Read the help of diag and construct from the two vectors s the
matrices L+ and L by means of diag.
4.3. EXERCISES
37
38
Linear equations
or more compactly
Ax = y
where A is n n,
x and y are n 1.
(5.1)
Here A and y are known and x must be solved. We already met the matrix
function inv. This function computes the inverse of a non-singular matrix.
One solution would be therefore to write simply x = inv(A)*y. Since inverting a matrix is in fact equivalent to solving n times a system of n linear
equations with the n columns of the identity matrix playing the role of the
right hand sides y, it is clear that inversion of A is usually too expensive
in terms of computer time. matlab can solve the system in one statement:
x=A\y. Here we meet for the first time the backslash operator (\). This
operator has much similarity with the slash operator (/). A quantity hiding
under the slash is inverted, thus 1/5=0.200 and
5 0
1 1 /
0 2
1 1
5 0
0 2
1
= 1/5 1/2 .
40
way a quantity hiding under the backslash is inverted, thus 5\1=0.200. The
equation
1
5 0
1
1/5
=
0 2
1
1/2
reads in matlab
>> [5 0; 0 2]\ [1; 1]
ans =
0.2000
0.5000
So, as long as the matrix A in Eq. (5.1) is non-singular, the solution of linear
equations is straightforward: if a column vector x must be solved we use the
backslash and if a row vector x must be solved we use the ordinary slash,
Ax = y = x = A\y
xT A = y T
5.2
= xT = y T /A
Least squares
41
m
X
#1/2
f (xi ) yi
2
is a minimum.
(5.2)
i=1
n
X
cj j (x),
j=1
n
X
j (xi )cj ,
i = 1, . . . , m.
j=1
n
X
j=1
Aij cj = f = A c.
42
The functions i (x) are known, we assume that we know the points xi at
which the measurements are performed and hence we can compute the mn
matrix A. Usually we have many more measurements than fit parameters:
m n. We do not know f , but we want it to be as close as possible to the
known (measured) vector y.
Minimization of |f y| leads to the n equations
AT A c = AT y.
(5.3)
These are known as the normal equations of the linear least square problem.
Before proceeding we prove Eqs. (5.3). We minimize S |f y|2 (which
obviously has the same minimum as |f y|) with respect to ci , i = 1, . . . , n,
i.e., we put the first derivatives equal to zero. By invoking the chain rule we
get:
S
ci
Now,
m
m
X
fj
X
(fj yj )2 = 2
(fj yj ).
ci
ci
j=1
n
n
X
fj
X
=
Ajk ck =
Ajk ki = Aji ATij ,
ci
ci
k=1
(5.4)
j=1
(5.5)
k=1
where we used ck /ci = ki . Note that ki , with k running and i fixed, gives
zero always, except for k = i, in that case we get Aji 1. The Kronecker
delta filters from the sum over k only the k = i term. Substitute Eq. (5.5)
into Eq. (5.4) and put the derivatives equal to zero
m
X
S
0=
=2
ATij (fj yj ),
ci
j=1
or, using f = A c,
AT f = AT y = AT A c = AT y.
Strictly speaking, we must consider next the second derivatives to check
whether we have a minimum, saddle point or maximum, but we skip this
part of the proof1 .
The matrix AT is n m and A is m n, so their product is square:
n n. The vector AT y is of dimension n. If the expansion functions i (x)
are linearly independent and the points xi are well chosen (not too close to
each other), then the matrix A is of rank n, i.e., its n columns are linearly
independent. The n n matrix AT A is non-singular if and only if A is of
1
The second derivatives lead to consideration of AT A. This matrix is positive definite
(has only positive eigenvalues) and therefore we have a minimum.
43
0.1050
-0.0055
% To check for the identity matrix
0
1.0000
% No identity?
0.0663
0.9945
-0.0331
0.3978
-0.0331
0.8011
44
>> y = rand(3,1)
y =
0.4057
0.9355
0.9169
>> B*y
ans =
-0.0270
0.1545
>> A\y
ans =
-0.0270
0.1545
An example of fitting a straight line through data that are made somewhat noisy:
% Synthesize data:
% Straight line, slope 3/2, 10% noise.
>> x=[1:25];
% simple x values
>> r=rand(25,1)-0.5;
% -0.5 < random < 0.5
>> y=1+3/2*(x+r.*x/10);
% The actual fit:
>> A=[x.^0 x.^1];
% f(x) = c(1) + c(2)*x
>> c=A\y
% c(1) is intercept, c(2) is slope
% Plot fit and original:
>> plot(x,y, x, A*c);
% (x,y) original, (x, A*c) fitted
5.3
(5.6)
45
From this we see clearly that the column vector y must depend linearly
on the columns of A in order that the approximation f y makes sense.
Usually this is not the case, of course, and then these equations do not have a
solution; simplification of the equations by elementary row transformations
will lead to contradictory equations.
In order to show this, we try to solve the equations (5.6) by the usual
techniques for solving linear equations. That is, we consider the augmented
matrix [A, y] and by elementary row transformations we sweep this matrix to its simplest form. matlab has the command rref to do this. Using
A and y of the previous example, we get
>> rref([A, y])
ans =
1
0
0
0
1
0
0
0
1
0
0
0
...
0
0
0
(the last 22 rows contain only zeros). Hence the first two equations of
A c = y are equivalent to
1 0
c1
0
=
0 1
c2
0
which have the solution c1 = c2 = 0. The third equation is equivalent to
0 = 1. From this contradiction we conclude that, although the linear least
square solution c is uniquely determined, the equations A c = y have no
solution in the ordinary sense. Or in other words, the three vectors a1 , a2
and y are linearly independent. (This is due to the addition of noise, if we
had simply put y = 1 + 3x/2, y would have been the first column plus a
multiple of the second column of A, and the three vectors would have been
linearly dependent).
Suppose now that y depends linearly on the columns of A. This means
that there is a vector d = (d1 , d2 , . . . , dn ) of length n such that
y = d1 a1 + d2 a2 + + dn an = A d,
where ai is the ith column of A. These columns are of length m. It is
immediately obvious that the least square solution c gives the correct answer,
for
c = (AT A)1 AT y = (AT A)1 AT A d = d.
46
5.4
Exercises
Exercise 28.
Visit for this exercise
http:/www.theochem.kun.nl/~pwormer,
where you find a link to course material. Follow this link and click the
link heatH2O.txt and an ascii file containing comments and two columns
is opened. The columns are temperatures ( C) and heat capacities Cp of
steam at 1 atm [units: Cal/( C g)]. Save this file in your current directory
(most browsers have this option under their File button).
matlab can read ascii files that contain columns and skips comments
while reading. Issue the command load heatH2O.txt in your matlab session. You now have an array heatH2O containing the two columns. (The
command: load arr.any creates an array with the name of the string before the dotin this case arrand discards the arbitrary string after the
dot). Create a column vector T containing the temperatures and the column
vector Cp containing the heat capacities.
Perform a quadratic fit Cp c1 + c2 T + c3 T 2 and compute the error
(norm of the vector containing the differences between the original and
the fitted points). Plot the fitted and the original values as a function
of temperature.
Perform a cubic fit Cp d1 + d2 T + d3 T 2 + d4 T 3 and compute the
error (norm of the difference vector). Which of the two fits has the
smaller error? Plot also this fit.
Use both fits to extrapolate the heat capacity to 750 and 1000 C. Plot
the fits in one figure including the extrapolated points. Which of the
two fits give extrapolated values closest to the real value, you think?
Exercise 29.
The linear parameters obtained in a least squares fit can be given statistical
significance. Write m n for the number of degrees of freedom (number
5.4. EXERCISES
47
..
.. .
X0 = ...
.
.
1 xm0 x2m0
xn1
m0
48
Exercise 30.
Visit for this exercise
http:/www.theochem.kun.nl/~pwormer.
Follow the link to the course material. When you click O2.txt an ascii
file containing comments and two columns is opened. The columns are
interatomic distances (bohr) and energies (hartree) for the oxygen molecule
O2 . Save this file in your current directory. See previous exercise for more
details on how to do this.
Write a script that loads the oxygen file into your matlab session
and fits the energies as function of the distance r with the following
function: V D0 + D1 exp(r) + D2 exp(2r). The linear fit
parameters D0 , D1 and D2 can be obtained by the backslash operator.
Use = 1. Plot the fit and the original data.
To determine the nonlinear fit parameter modify the script. Put
a loop with 0.8 1.8 with steps of 0.1 around the linear fit.
Compute in the body of the loop the norm of the difference vector
V Vfit (original and fitted values) and determine the value of
which has the smallest norm. Plot for this again the original and
fitted values.
Functions
Functions resemble scripts: both reside in an ascii file with extension .m.
As a matter of fact, many of the matlab functions that we used so far are
simply .m files. One can inspect them by the command type, e.g., type
fliplr will type the content of fliplr.m. The difference between scripts
and functions is that all variables in scripts are global, whereas all variables
in functions are local. This means that variables inside functions lead their
own life. Say, we have assigned the value 1.5 to the variable D in our
matlab session and we call a function that also assigns a value to a variable
called D. Upon return from the function the variable D still has the value
1.5. Conversely, D must be assigned a value inside the function before it
can be used. The function does not know that D already has a value in our
matlab session.
If both are stored in an .m file, how does matlab tell the difference
between a script and a function? This is a good question with a simple
answer: the very first word of a function is function. So, when matlab
reads an .m file from disk, it looks at the first string and decides from this
whether it is a function or a script.
If all variables in a function are local, what is the use of a function? One
would like to get something useful out of a function, as for instance an array
with its columns flipped. In other words, how does a function communicate
with its environment? To explain this we give the syntax of the first line of
a function:
function [out1, out2, ...] = name(in1, in2, ...)
The first observation is that a function accepts input arguments between
round brackets. Any number (including zero) of variables can be written
here. These variables can be of any type: scalars, matrices (and matlab
objects that we have not yet met). The second observation is that a function
returns a number of output parameters in square brackets. The values of
out1, out2, etc. must be assigned within the function. These variables can
also be of any type. The square brackets here have nothing to do with the
49
50
concatenation operators that made larger matrices from smaller ones. The
third observation is that the function has a name. It is common to use here
the same name as of the .m file, but this is not compulsory. The name of an
.m file begins with an alphabetic character, and has a filename extension of
.m. The .m filename, less its extension, is what matlab searches for when
you try to use the script or function.
For example, assume the existence of a file on disk called stat.m and
containing:
function
n
mean
stdev
[mean,stdev] = stat(x)
= length(x);
= sum(x)/n;
= sqrt(sum((x-mean).^2)/n);
This defines a function called stat that calculates the mean and standard
deviation of the components of a vector. We emphasize again that the
variables within the body of the function are all local variables. We can call
this function in our matlab session in three different ways:
>> stat(x)
ans =
0.0159
% no explicit assignment
% default assignment to ans
>> m = stat(x)
m =
0.0159
51
[mean,stdev] = stat(x)
= length(x);
= avg(x,n);
= sqrt(sum((x-avg(x,n)).^2)/n);
[mean] = avg(x,n)
= sum(x)/n;
Subfunctions are not visible outside the file where they are defined. Also,
subfunctions are not allowed in scripts, only inside other functions. Functions normally return when the end of the function is reached. We may
use a return statement to force an early return from the function. When
matlab does not recognize a function by name, it searches for a .m file of
the same name on disk. If the function is found, matlab stores it in parsed
form into memory for subsequent use. In general, if you input the name of
something to matlab, the matlab interpreter does the following: It checks
to see if the name is a variable. It checks to see if the name is a built-in
function (as, for instance, sqrt or sin). It checks to see if the name is a
local subfunction. When you call an .m file function from the command line
or from within another .m file, matlab parses the function and stores it
in memory. The parsed function remains in memory until cleared with the
clear command or you quit matlab.
6.2
Function functions
[y] = hump(x)
dot operations so that hump may be called
vector, e.g., for plotting.
((x-0.3).^2 + 0.01) + 1 ./ ((x-0.9).^2 + 0.04) - 6;
52
53
6.3
matlab has several ordinary differential equation solvers (ODE solvers, see
help ode45). An ordinary differential equation has only one parameter,
which often is the time t. We will restrict ourselves in this section to first
order equations, i.e., equations containing only first derivatives with respect
to t. Usually differential equations are coupled and have the following general form,
dy1 /dt
f1 t, y(t)
dy /dt f t, y(t)
2
(6.1)
=
,
...
...
dyn /dt
fn t, y(t)
here y(t) = y1 (t), y2 (t), . . . , yn (t) is to be computed by matlab from a
given initial vector y(t0 ). The functions f1 , f2 , . . . , fn are known and must
be supplied by the user. The ODE solver most often used is ode45. It is
called as
54
A
B 3 C
(6.2)
k2
= k1 [A] + k2 [B]
= k1 [A] [B](k2 + k3 )
(6.3)
= k3 [B].
The concentrations [A], [B] and [C] as functions of time are obtained by
solving these equations. We call ode45 as follows:
[T Y]=ode45(@kinetic, Tspan, Init)
The array Y will contain the concentrations at the points in time t1 ti tK
at which matlab computed them. We make the following identifications:
Y (i, 1) = [A],
Y (i, 2) = [B],
Y (i, 3) = [C],
i = 1, . . . , K,
which means that the ith row Y(i,:) contains the concentrations at t = ti .
The vector T contains the time points: T (i) = ti , i = 1, . . . , K. Furthermore
the name of the .m-file supplied by the user is kinetic. It must return the
right hand sides of the differential equations as a column vector of the same
dimension as Init. This is the number (n) of coupled equations. In the
present example n = 3.
First ode45 assigns the values of Init (the initial concentrations) to the
first row [Y(1,1), Y(1,2), Y(1,3)] of Y. During integration ode45 will
repeatedly call kinetic.m with the respective rows of Y as input parameters.
The function kinetic returns f1 , f2 and f3 for given time t. Besides Y, also
the time t (a scalar) is inputted. Although in reaction kinetics t is not used
55
explicitly, time must be present in the parameter list of the function, since
ode45 expects a parameter in this position.
The function kinetic may look as follows
function [f] = kinetic(t, Conc)
% Conc is a vector with 3 concentrations and
% k1, k2 and k3 are rate constants.
k1 = 0.8;
k2 = 0.2;
k3 = 0.1;
f
f(1)
f(2)
f(3)
= zeros(length(Conc),1);
= -k1*Conc(1) + k2*Conc(2);
= k1*Conc(1) - Conc(2)*(k2+k3);
= k3*Conc(2);
56
The parameters function, Tspan and Init are as before. We skip explanation of options, we take it simply to be empty []. After it we find an undetermined number of parameters P1, P2.. that are passed unchanged to
function. Supposing that the function referred to ode45 is called steady.m,
then its first line may look like this
function [f] = steady(t, Conc, k1, k2, k3)
where k1, k2, k3 are the rate constants that must be assigned before calling ode45. Further t and Conc are the times and concentrations passed
to steady by ode45. The function steady is called through ode45 from a
matlab session as
[Ts Ys] = ode45(@steady, [0 30], [1 0], [], k1, k2, k3);
The fourth parameter (the third array) can contain options that govern the
accuracy of the solution. Because we are satisfied with the default options
we leave it empty: []. We reiterate that k1, k2 and k3 must have a value
before we invoke ode45.
6.4
(6.5)
6.5. EXERCISES
57
(6.6)
= yn2
dn y
= f (t, y, y 0 , y 00 , . . . , y (n1) )
dtn
= f (t, y1 , y2 , y3 , . . . , yn ).
Example
d2 y
dy
= t y2
2
dt
dt
(6.7)
becomes
dy1
dt
dy2
f2
dt
f1
= y2
(6.8)
= ty2 y12 .
(6.9)
In matlab the corresponding function called by the ode solvers would look
like:
function [f] = difeq(t, y)
f = zeros(2,1); % Tell matlab we return a column vector
f(1) = y(2);
f(2) = t*y(2) - y(1)^2;
6.5
Exercises
Exercise 31.
A classic test example for multidimensional minimization is the Rosenbrock
banana function f (x, y) = 100(y x2 )2 + (1 x)2 .
1. Write the matlab function banana that evaluates f for the vector
r = (x, y).
2. Call by hand fminsearch from your matlab session to minimize the
banana function. Use as a start r = (1.2, 1).
3. Modify function banana to f (x, y) = 100(y x2 )2 + (a x)2 and let a
be an input parameter. Read the help of fminsearch to discover how
to pass a to banana via fminsearch.
58
Exercise 32.
Compute the integral
Z 1
0
1
1
+
6 dx
(x 0.3)2 + 0.01 (x 0.9)2 + 0.04
x1
r sin cos
x2 = r sin sin
x3
r cos
Hints:
Check first if r = |r| < 10. If this is the case then set all output parameters equal to zero and return (with matlab command return).
The constant eps (floating point relative accuracy) is built into matlab; it is 252 1016 .
Then test if x21 + x22 10. If this is the case the vector is along the
third axis and = 0 ; it can be pointing up ( = 0 ) or pointing down
( = 180 ).
Finally compute from x3 and from x1 and notice that = arccos y
gives results in the interval 0 . If x2 < 0 then = 2 .
Exercise 34.
In this exercise we will investigate the steady state approximation by looking
at the increase of concentration of reactant C, see Eq. (6.2). In a reasonable
approximation [C] increases as
[C] = 1 exp(keff t),
(6.10)
6.5. EXERCISES
59
1. Write the .m-files that can be used for the integration of the exact
(6.3) and the steady state equations (6.4).
2. Write a function based on Eq. (6.10) that returns keff .
3. Write a script that first solves the exact equations [by calling the function you wrote under (1)], then determines keff , and finally solves the
steady state equations and again returns keff . Repeat this for k1 = 0.5,
k2 = 0.5 and k3 = 100k1 , 10k1 , k1 , k1 /10, k1 /100. Take as initial conditions [A] = 1, [B] = 0 and [C] = 0.
Hint: Do not integrate too long (take tf not too large), a reasonable
upper limit is
tmax= 3/min([k1 k2 k3])
You will see that the steady state approximation is good when B
vanishes quickly, that is if k3 k1 k2 .
In the case that k3 k1 k2 the steady state approximation yields a
keff which is twice too large.
Exercise 35.
Solve Eq. (6.7) by ode45 with initial conditions y1 (0) = 1 and y2 (0) = 0.
Integrate from t = 0 to t = 0.2. Plot the resulting function y(t) y1 (t).
Exercise 36.
For this exercise we immerse a one-dimensional spring, with a point mass
m attached to it, into a vessel containing a viscous liquid, e.g., syrup. We
pull the spring away from equilibrium over a distance y = 1. At the point
t = 0 we let the spring go, so at this point in time the velocity of m is zero:
dy(0)/dt = 0. The spring will start to vibrate following Newtons equation
of motion: md2 y/dt2 = F . The forces acting on the mass are Hookes law:
ky and the friction force: f dy/dt (proportional to the velocity). In total,
this so-called damped harmonic oscillator satisfies the equation of motion1
m
dy
d2 y
= ky f
dt2
dt
or
dy
d2 y
+ 2 y + 2b
= 0,
2
dt
dt
p
where = k/m and b = f /2m.
1
(6.11)
The reader may wonder why somebody in his right mind would put a spring into
a bowl of syrup. However, the damped harmonic oscillator is a useful model in many
branches of science: LCR electric circuits, dispersion of light, etc.
60
Write a script that (i) numerically integrates Eq. (6.11), (ii) implements the analytic solution, Eq. (6.12), and (iii) shows both solutions
in one plot.
7. More plotting
7.1
3D plots
62
7.2
63
Handle Graphics
Graphical hierarchy
matlab has a system, called Handle Graphics, by which you can directly
manipulate graphics elements. This system offers unlimited possibilities
to create and modify all types of graphs. The organization of a graph is
hierarchical and object oriented. At the top is the Root, which simply is
your matlab screen. This object is created when you start up matlab.
Figure objects are the individual windows on the Root screen, they are
referred to as the children of the Root. There is no limit on the number of
Figures. A Figure object is created automatically by the commands that
we introduced earlier, namely plot, mesh, contour and surf. As for all
graphical objects, there is also a separate low level command that creates
the object: figure creates a new Figure as a child of Root. If there are
multiple Figures within the Root, one Figure is always designated as the
current figure; all subsequent graphics commands, such as xlabel will
give output to this figure.
A Figure object has as children the objects Axes, which define regions
in a Figure window. All commands such as plot automatically create an
Axes object if it does not exist. Only one Axes object can be current. The
children of Axes are Image, Line, Patch, Surface, and Text. Image consists
of pixels; see the matlab on-line documentation for more details. The
Line object is the basic graphic primitive used for most 2D and 3D plots.
High level functions such as plot and contour create these objects. The
coordinate system of the parent Axes positions the Line object in the plot.
64
Patch objects are filled polygons and are created by high level commands
such as bar, which creates a bar graph. Surface objects represent 3D data
and are created by mesh and surf.
The final objects are Text, which are also children of Axes. Text objects
are character strings and are created by high level commands such as xlabel,
ylabel and title, which we met earlier.
7.3
Every individual graphics object has a unique identifier, called a handle, that
matlab assigns to it when the object is created. By using these handles as
variables we can manipulate very easily all objects in a plot. As an example
we return to contour. Thus far we plotted contours without numbers, which
is not particularly useful. However, the matlab command contour can
return values associated with each contour line. Moreover the command
returns the handle of every contour line. Example:
>> x
>> y
>> [X Y]
>> Z
>> [c h]
>> whos
Name
c
h
=
=
=
=
=
c
65
66
In the rgb color scheme used here, this implies that the color chosen is
pure red, as is expected because we changed the line color to red. (The array
[1 0 1] would give an equal mixture of red and blue, which is magenta).
We see the possible line styles (see help plot) and the actual choice: solid
(the default).
Suppose now we want change the line style of the second curve to dotted.
We see that linestyle (not case sensitive!) is a property of object with
handle h(2). We can change it with set(h(2), linest, :). This
turns it into a dotted line. Note that unique abbreviations (linest instead
of linestyle) of the property names of the objects are allowed.
As we said, all objects have a handle, also the Axes objects in a Figure
object; by gca (get current axes) we get the handle of the current Axes
in the current Figure and by get(gca) we get the actual values of the
properties of this Axes object. We see that one of the Axes properties is
FontSize. If we want to increase to 20 points the size of the digits on the
axes, we issue set(gca, Fontsize, 20).
Fortunately, it is often not necessary to use these low level commands,
matlab has several high level commands that make life easier. For instance
the aspect ratio (ratio between x-axis and y-axis scale) can be set by a
command of the type set(gca,DataAspectRatio,[1 1 1]). However,
much easier is the use of the high level function axis; see its help.
By left clicking with the mouse on an object we turn it into the current
object. Its handle is returned by the command gco (get current object).
So, if we want to change the color of a line on the screen to blue and we
forgot to save its handle, then we can click on it and issue the command
set(gco, col, b).
Remove an object by using the function delete, passing the objects
handle as the argument. For example, delete the current Axes (and all of
its children!) by delete(gca).
We can draw lines in the coordinate system of the current Axes object
by the low level command
line([x1 x2 .. xn], [y1 y2 .. yn])
This draws a line from point (x1 , y1 ) to point (x2 , y2 ), etc., to point (xn , yn ).
But first we want to give the plot a definite size by
axis([xmin xmax ymin ymax])
which defines an x-axis from xmin to xmax and a y-axis from ymin to ymax.
For example, the following code draws a rectangular box:
67
7.4
Polar plots
x
r sin cos
y = r sin sin , 0 r < , 0 , 0 < 2.
(7.1)
z
r cos
In polar plots one draws only the angular part g(, ), taking a fixed radial
part f (r0 ).
matlab is able to make 2D polar plots, but not 3D polar plots, so we
must do this ourselves. Let us first explain how to make a 2D polar plot
by hand. Say, we want to plot the angular part of the function f (r)g().
Figure 7.2 shows polar graph paper that helps us do this. We choose a
fixed value r0 and mark points for = 0 , 20 , , 340 with the value
of h(i ) |f (r0 )g(i )| as the distance from the origin. The equidistant
circles help us in measuring this distance. In other words, we mark points
68
80
60
120
140
40
160
20
180
200
340
220
320
240
300
260
280
7.5. EXERCISES
69
H
= abs(cos(Theta));
X
= H.*sin(Theta).*cos(Phi);
Y
= H.*sin(Theta).*sin(Phi);
Z
= H.*cos(Theta);
surf(X,Y,Z)
axis equal off
The same can be achieved in a slightly more efficient manner
theta = [0:5:180]*pi/180;
phi
= [0:5:360]*pi/180;
h
= abs(cos(theta));
X
= cos(phi)*sin(theta)*diag(h);
Y
= sin(phi)*sin(theta)*diag(h);
Z
= repmat(cos(theta), length(phi),1)*diag(h);
surf(X,Y,Z)
axis equal off
Here X and Y are obtained from column vector times row vector multiplication (dyadic product).
7.5
Exercises
Exercise 37.
Write a script (or function) that, by using meshgrid, draws the Mexican
hat
z = (1 x2 y 2 ) exp(0.5(x2 + y 2 ))
on the x and y interval [3, 3]. Do not use any for loops. Apply one after
the other: mesh, surf, and contour. Use matrices for all parameters.
Hint:
Between the plot commands you may enter in your script the matlab command pause. This allows you to have a look at the plot until you hit the
enter key.
Exercise 38.
Return to the previous exercise, change your script (or function) so that
the first two parameters of contour, mesh, and surf are the vectors that
were used in the meshgrid command. Verify that the figures are exactly
the same as when the first two parameters were the two matrices created by
meshgrid.
Exercise 39.
70
sin(r)
if r 6= 0
sinc(r) =
r
1
if r = 0.
The matlab function sinc can be called with any matrix as input. Use
this function to plot sinc(x2 + y 2 ) on the grid [3, 3] for both x and y. First
look at the result of surf and use the rotate button to admire the function
from all sides. Then use the contour and clabel command to put manually
values on the contours.
Exercise 40.
Draw the naphthalene molecule as in Fig. 7.3.
Figure 7.3: Standard numbering of the naphthalene molecule
10
7.5. EXERCISES
71
Exercise 41.
A normalized hydrogen 3dz 2 orbital has the form
2 r
Nr e
(3z r ) with r =
x2
y2
z2
1
and N =
3
1 7/2
.
2
Write a script that plots the intersection of this orbital with the yz-plane,
that is, for x = 0. Choose = 3, and let y and z range from 4 to +4.
Hint:
Use for contouring something like
[c h]=contour(Y, Z, dorb, [-1:0.05:1]);
clabel(c,h, fontsize,6.5)
where dorb must contain the values of the 3dz 2 orbital on the y-z grid, the
array [-1:0.05:1] gives the contour levels and the second statement puts
labels on the contours.
Exercise 42.
Plot the d-orbital of the previous exercise in polar form. Here you
may forget about the normalization constant, because only the shape
matters, not the values.
Plot 2p2x = r2 exp(2r) sin2 cos2 (unnormalized) in polar form. Do
not forget the aspect ratio axis equal! Do you recognize this plot?
72
8.1
Cell arrays
8
14
15
16
73
74
These two commands produce (i) the magic square matrix A (all rows and
columns sum to 65, elements are 1, 2, . . . , 25) and (ii) the 1 3 cell array C.
The function prod takes the products of elements in the columns of A and
returns a row vector. The second prod takes the product of the elements
in this row vector. The three cells of cell array C contain now the following
arrays, which are of different dimension:
the square matrix A in C(1,1),
the row vector of column sums in C(1,2),
the product of all its elements in C(1,3).
The contents of the cells in cell array C are not fully displayed because the
first two cells are too large to print in this limited space, but the third cell
contains only a single number, 25!, so there is room to print it.
Here are two important points to remember. First, to retrieve the cell
itself (container plus content) use round brackets. Since a cell is nothing but
a 1 1 cell array, matlab command whos tells us that e.g. C(1,2) retrieves
a cell array. Second to retrieve the content of the cell use subscripts in curly
braces. For example, C{3}, or equivalently C{1,3}, retrieves 25!, whereas
C(3) retrieves the third cell. Notice the difference (C is the cell array of
previous example):
>> c=C(1)
c =
[5x5 double]
>> whos c
Name
c
>> d=C{1}
d =
17
24
23
5
Size
1x1
Bytes
260
Class
cell array
8
14
15
16
6
12
18
75
13
19
25
Size
5x5
20
21
2
22
3
9
Bytes
200
Class
double array
% 9!
76
>> b
= cell(2,2);
>> b(1,2) = a(1)
% cell a(1) assigned to cell b(1,2)
% Content of cell a(1) to content of cell b(1,2):
>> b{1,2} = a{1}
When you use a range of cells in curly brackets, as in A{i:j}, the contents
of the cells are listed one after the other separated by commas. In a situation
where matlab expects such a comma separated list this is OK. For instance,
>>
>>
>>
>>
>>
>>
>>
A{1} = [1 2];
A{2} = [3 4 5];
A{3} = [6 7 8 9 10];
% Comma separated list between square brackets
[A{1:3}]
% identical to:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
To assign more than one element of a cell array to another, do not use
curly brackets on the left hand side, because in general a comma separated
list is not allowed on the left hand side of an assignment. Example:
77
Param
Param(1)
Param(2)
Param{3}
=
=
=
=
cell(1,4);
{linest};
{:};
color;
78
Here Param{:} expands to a comma separated list, i.e., the four parameters
are passed (separated by commas) to plot.
In summary, a cell array is a matlab array for which the elements are
cells, containers that hold other matlab arrays. For example, one cell of a
cell array might contain a real matrix, another an array of text strings, and
another a vector of complex values. You can build cell arrays of any valid
size or shape, including multidimensional structure arrays.
8.2
Characters
We can enter text into matlab by using single quotes. For example,
s = Hello
The result is not the same kind of numeric array as we have been dealing
with up to now. It is a 1 5 character array, briefly referred to as a string.
Internally matlab stores a letter as a two byte numeric value, not as a
floating point number (8 bytes).
There exists a well-known convention called ascii (American Standard
Code for Information Interchange) to store letters as numbers. In ascii the
uppercase letters A,. . . , Z have code 65, . . . ,90 and the lowercase letters
a,. . . , z have code 97,. . . ,122. The function char converts an ascii code
to an internal matlab code. Conversely, the statement a = double(s)
converts the character array to a numeric matrix a containing the ascii
codes for each character. Example:
>> s = hello;
>> a = double(s)
a =
104
101
108
>> b = char(a)
b =
hello
108
111
The result a = 104 101 108 108 111 is the ascii representation of hello.
Concatenation with square brackets joins text variables together into
larger strings. The second statement joins the strings horizontally:
8.2. CHARACTERS
>> s = Hello;
>> h = [s, world]
h =
Hello world
79
The statement v = [s; world] joins the strings vertically. Note that
both words in v must have the same length. The resulting array is a 2 5
character array.
To manipulate a body of text containing lines of different lengths, you
can construct a character array padded with blanks by the use of char.
We have just seen that char converts an array containing ascii codes into
a matlab character array. There are more uses of char. If we write s
= char(t1,t2,t3,..) then a character array s is formed containing the
text strings t1,t2,t3,... as rows. Automatically each string is padded
with blanks at the end in order to form a valid matrix. The function char
accepts any number of lines, adds blanks to each line to make them all the
length of the longest line, and forms a character array with each line in a
separate row. The reference to char in the following example produces a
6 9 character array
>> S = char(Raindrops, keep, falling, on, ...
my, head.)
S =
Raindrops
keep
falling
on
my
head.
>> whos S
Name
S
Size
6x9
Bytes
108
Class
char array
The function char adds enough blanks in each of the last five rows of S to
make all the rows the same length, i.e., the length of Raindrops.
Alternatively, you can store text in a cell array. As an example we
construct a column cell array,
>> C ={Raindrops; keep; falling; on; my; head.};
>> whos C
80
Size
6x1
Bytes
610
Class
cell array
The contents of the cells are arrays (in this example character arrays), in
agreement with our definition of a cell array. You can convert a character
array S to a cell array of strings with C = cellstr(S) and reverse the
process with S = char(C{:}) (comma separated list passed to char).
Generally speaking cell arrays are more flexible than character arrays.
For instance, transposition of the 6 1 cell array C gives
>> C
% cell array from the previous example
ans =
Raindrops
keep
falling
on
my
head.
which is a 1 6 cell array with the same strings as contents of the cells.
Transposition of the 6 9 character array S on the other hand gives
>> S
% character array
ans =
Rkfomh
aeanye
iel a
npl d
d i .
r n
o g
p
s
which is generally less useful (unless you are a composer of crosswords).
8.3
Structures
8.3. STRUCTURES
81
S =
name: Hans Jansen
number: 005143
grade: 7.5000
Like everything else in matlab, structures are arrays, so you can insert
additional elements. In this case, each element of the array is a structure
with several fields. The fields can be added one at a time,
>> S(2).name
= Sandy de Vries;
>> S(2).number = 995103;
>> S(2).grade = 8;
The scalar structure S has now become a 1 2 array, with S(1) referring to
student Hans Jansen and S(2) to Sandy de Vries.
An entire element can be added with a single statement.
>> S(3) = struct(name,Jerry Zwartwater,...
number,985099,grade,7)
The structure is large enough that only a summary is printed,
S =
1x3 struct array with fields:
name
number
grade
There are several ways to reassemble the various fields into other matlab
arrays. They are all based on the notion of a comma separated list. We have
already met this notion in the discussion of the cell arrays. Typing
>> S.number
is the same as typing S(1).number, S(2).number, S(3).number. This is
a comma separated list. It assigns the three student numbers, one at a time,
to the default variable ans and displays these numbers. When you enclose
the expression in square brackets, [S.grade] it is the same as [S(1).grade,
S(2).grade, S(3).grade] which produces a row vector containing all of
the grades. The statement
>> mean([S.grade])
ans =
7.5000
82
gives the average grade of the three students now present in our data base.
Just as in the case of comma separated lists obtained from cell arrays, we
must be careful that we generate the lists only in contexts where matlab
expects such lists. This is in six situations:
Combination of statements on one line plus output, e.g. >> a,b,c,d
inside [ ] for horizontal concatenation, e.g. [a,b,c,d]
inside { } to create a cell array, e.g. {a,b,c,d}
inside ( ) for function input arguments, e.g. test(a,b)
inside ( ) for array indexing, e.g. A(k,l)
inside [ ] for multiple function output arguments, e.g. [v,d] = eig(a).
Because of this expansion into a comma separated list, structures can be
easily converted to cell arrays. Enclosing an expression in curly braces, as
for instance {S.name}, creates a 1 3 cell array containing the three names.
>> {S.name}
ans =
Hans Jansen
Sandy de Vries
Jerry Zwartwater
Since S.name expands to a list containing the contents of the name fields,
this statement is completely equivalent to
ans = {Hans Jansen, Sandy de Vries, Jerry Zwartwater}
which, as we saw above, creates a cell array with three cells having the three
student names as their contents.
An expansion to a comma separated list is useful as a parameter list in
a function call. Above we gave an example where we passed a parameter
list to plot by expanding the cell array Param. As an example of expanding
a structure, we call char, which, as we saw in subsection 8.2, creates a
character array with the entries padded with blanks, so that all rows are of
equal length:
>> N=char(S.name) % expansion to comma separated list
N =
Hans Jansen
Sandy de Vries
Jerry Zwartwater
>> whos N
Name
Size
N
3x16
Bytes
96
Class
char array
8.3. STRUCTURES
83
In Table 8.1 we list a few matlab functions that can handle text, cell
arrays, and structures.
Table 8.1: Some functions useful for cell array, structure and character handling; see help files for details.
cell
cell2struct
celldisp
cellfun
cellplot
char
deal
deblank
fieldnames
findstr
int2str
iscell
isfield
isstruct
num2cell
num2str
rmfield
strcat
strcmp
strmatch
struct
struct2cell
Cell arrays are useful for organizing data that consist of different sizes or
kinds of data. Cell arrays are better than structures for applications when
you dont have a fixed set of field names. Furthermore, retrieving data from
a cell array can be done in one statement, whereas retrieving from different
fields of a structure would take more than one statement. As an example of
the latter assertion, assume that your data consist of:
A 3 4 array with measured values (real numbers).
A 15-character string containing the name of the student who performed the measurements.
The date of the experiment, a 10-character string as 23-09-2003.
A 3 4 5 array containing a record of measurements taken for the
past 5 experiments.
A good data construct for these data could be a structure. But if you usually
access only the first three fields, then a cell array might be more convenient
for indexing purposes. To access the first three elements of the cell array
TEST use the command deal. The statement
84
retrieves the contents of the first three cells and assigns them to the array
newdata and the strings name and date, respectively. The function deal is
new. It is a general function that deals inputs to outputs:
[a,b,c,...] = deal(x,y,z,...)
simply matches up the input and output lists. It is the same as a=x, b=y,
c=z,. . . . The only way to assign multiple values to a left hand side is by
means of a function call. This is the reason of the existence of the function
deal. On the other hand, to access different fields of the structure test, we
need three statements:
newdata = test.measure
name
= test.name
date
= test.date
8.4
Exercises
Exercise 43.
1. Type in
t = oranges are grown in the tropics
Read the help of findstr. Parse this string into words by the aid of
the output of findstr. That is, get six strings (character arrays) that
contain the respective words. Store these strings in a cell array. Make
sure that you do not have beginning or trailing blanks in the words.
2. Write a function parse that parses general strings.
Hints:
Check the input of the function by ischar. Make sure that the input
string does not have trailing blanks by the use of deblank.
Exercise 44.
Write a script that writes names and sizes of files in the current directory to
the screen. Each line must show the filename followed by its size (in bytes).
Show the files sorted with respect to size.
Hints:
1. The command dir returns a structure with the current filenames and
sizes.
8.4. EXERCISES
85
2. The command sort can return the sorted array together with the
permutation that achieves the actual sorting.
3. matlab does not echo properly an array of the kind [char num].
Apply int2str to num to get readable output.
Exercise 45.
Predictwithout executing the following scriptwhat it will put on your
screen:
clear all;
a = magic(3);
b = {a b c
d e f};
c(1,1).income = 22000;
c(2,4).age = 24;
d = rand(4,7);
e = {a b ...
c d };
u = size(e);
k = 0;
for i=1:u(1)
for j=1:u(2)
k = k+1;
siz(k,:) = [size(e{i,j})];
end
end
siz = [siz; u]
clear u i j k siz
A = whos;
for i=1:length(A)
siz(i,:) = A(i).size;
end
siz
Visit
http:/www.theochem.kun.nl/~pwormer/matlab/ml.html.
86
where you will find this script under the name size_struct.m. Download
it to your directory and execute it. Was your prediction correct? If not,
experiment with the appropriate matlab statements until you feel that you
understand what is going on in this script (that serves no other purpose
than comparing matlab data structures and their dimensions).
Exercise 46.
Design a matlab data structure for the periodic system of elements. Include
the following information:
The full English name of the element and its standard chemical abbreviation.
The masses and abundances of the naturally occurring isotopes.
Electron configuration, i.e., number of electrons in n = 1, 2, . . .. shells.
Prepare the periodic system for the first 10 elements. A .txt file of
isotopic masses can be found at the url
http:/www.theochem.kun.nl/~pwormer/matlab/ml.html.
The same site contains a periodic system as a .pdf file. This information originates from the usa government:
http://physics.nist.gov/PhysRefData/Compositions/index.html