You are on page 1of 4

Depts of Chemical Engineering and Mathematics

An Introduction To Matlab
Phil Ramsden and Caroline Colijn
Second Coursework Task
Deadline: Thursday 12 December, 2013
GENERAL INSTRUCTIONS
You will get more points for following the principles of good programming that we have
covered in the lectures. These include: vectorization where possible instead of loops, logical
indexing where possible instead of loops, clear naming of variables, sensible initialisation of
vectors and arrays, good commenting where relevant, and of course a nal write-up that
makes it clear that you have correctly answered the questions.
1 Random walks
Background
Suppose a molecule starts at a particular location, and is bumped randomly up and down
along a line by other, smaller molecules. At each time, our large molecule takes a randomly
sized step either up or down the path. The path the molecule takes is a random walk. In
some formulations, the steps might be integers, or they might be more general. If his steps
are normally distributed (ie drawn from a Gaussian, or normal, distribution), the path is a
Brownian random walk. Random walks, and the related phenomenon of Brownian motion,
are important in many elds of the physical sciences and engineering.
In particular, they are important in thermodynamics and in many spatial processes. The
motions of molecules in an ideal gas can be thought of as random walks, as they interact with
large numbers of other particles. Similarly, diusive processes chemicals diluting in space,
for example are relavant to many chemical systems, and these are frequently modelled as
random walks. The intuition you build on random walks, and a familiarity with simulating
them, will be helpful as you learn about these topics.
The kinds of questions people ask of random walks include:
What is the molecules expected position as a function of time?
What is the standard deviation of the position over time?
How long do we expect it to take for the molecule to get back to the starting point?
How long will it take to reach a distance L from the starting point?
1
What is the farthest a walker (here, the molecule) gets in a given time?
Mathematically, a random walk consists of a starting point followed by a sequence of
random jumps. This can be represented by the repeated calculation
x
n+1
= x
n
+
where is a small, random step.
The random steps are usually normally distributed (see randn in matlab), but they dont
have to be. Indeed, if the noise is a random integer then the process is an integer random
walk. These are also very interesting and can be studied with relatively basic probability
theory.
Questions
1.1 Plot the position of a single random walker molecule, using normally distributed steps
(randn) for 100 iterations. What is the farthest it gets from its starting point? How
does this change as you increase the number of iterations?
1.2 Now make a vector version with 50 random walkers. What is the farthest any of them
gets at any time (iteration)? (Use max in the right dimension; help max can remind
you of how to do this).
1.3 Now make 1000 random walkers and plot their average position as a function of time.
1.4 Plot the standard deviation of their position as a function of time. The standard
deviation is a measure of the variability in a set of numbers, and can be computed in
matlab with the std function. What do you notice? Can you explain this?
1.5 Now, ensure that your walkers to stay in the same region, say between x = L and
x = +L. Here are two possible ways to have a boundary for them:
(a) Reecting boundary: when their step takes them above L or below L, have the
boundary bounce them back in by the amount that their position would have
exceeded the barrier. That is, when x
n+1
> L, set x
n+1
= L|x
n+1
L| instead.
Draw this out for yourself if youre not sure why this represents reection.
Similarly, when x
n+1
< L, set x
n+1
= L + | L x
n+1
|
(b) Absorbing boundary: when their step takes them above L or below L, they are
absorbed, and the walk ends. You can implement this by keeping their position
at L or L (whichever they have reached).
Implement each of these types of boundaries at a distance L = 10 from the origin.
With the absorbing boundary, plot the number of walkers still remaining in the region
as a function of time. (Youll have to exclude the walkers that reached L and L.)
Compare the walkers average position, and the standard deviation of the position
(as a function of time) to the average and standard deviation without the boundary
2
conditions (your answers to 1.3 and 1.4). Again, for the absorbing boundary, exclude
those who reached L or L.
1.6 Allow your walkers to walk in both the x and y directions. Make a series of 2D plots,
showing how your walkers disperse over time in the x-y plane if they all start at (0,0).
2 Chemical reactions
This problem is on simulating chemical reactions, following on from the non-assessed class
work.
2.1 Consider the reactions:
A + A
k
1
C, A + B
k
2
D

k
3
A,
k
4
B
(1)
We will not worry about the numbers of molecules of C and D, only A and B. There
are now 4 dierent things that can happen: at rate A(A1)k
1
, A decreases by 2. At
rate ABk
2
, A and B both decrease by 1. At rate k
3
, A increases by 1, and at rate
k
4
, B increases by 1.
In this system, the total rate at which events occur is
R(t) = A(A 1)k
1
+ ABk
2
+ k
3
+ k
4
.
So the time to the next event has density
f() = Re
R
.
The approach here is this: we use exprnd to determine the time at which the next
reaction will occur, using this value for f(). Then we must choose which event it is
thats going to happen at t +. Given that a reaction happens, the probability that it
is reaction 1 is k
1
A(A 1)/R, reaction 2 has probability ABk
2
/R, 3 has probability
k
3
/R and 4 has probability k
4
/R.
(a) In intuitive terms, why does reaction 1 proceed at rate k
1
A(A1) and 2 at rate
k
2
AB?
(b) The function randsample can choose randomly from a set of values, with some
values more likely than others. The syntax is
randsample(SetOfThings, NumberOfSelections, true, Probabilities)
Use randsample to draw 20 entries from the set [1 2 3 4], with probabilities 0.2,
0.3, 0.1 and 0.4.
3
(c) Now we are ready to simulate the coupled reactions above. Decide on the number
of steps you will take (eg 100). Initialize an array with 3 columns, one for time,
A, and B and the correct number of rows. Begin with 2 molecules of A and B,
and use rate constants k
1
= 0.001/s, k
2
= 0.01/s, k
3
= 1.2/s and k
4
= 1/s.
In each step of your loop, compute the total rate, draw from the appropriate
exponential distribution, use randsample to determine which reaction occurs at
t + , and update the values of t, A and B accordingly.
(d) Plot your result in two plots, one of A vs time and one of B vs time.
(e) Repeat the simulation, adding lines to the plots as before.
(f) Repeat many times, each time storing the nal numbers of molecules. Create
histograms of the nal numbers of A and B (see hist).
(g) Write a function that takes one of your arrays (from the repeats in 1f) as an
input, and outputs the number of A and B molecules at linearly spaced time
points. This will make the dierent simulations directly comparable: we will be
able to specify a time vector and have the numbers of A and B in the dierent
particular runs at the time points we specify in the time vector.
(h) Use the function you wrote in 1g to process a set of repeated runs. Plot the
mean of A and B and the standard deviation of A and B vs time.
(i) Now change one or more of the rate constants k, and explore how this changes
the numbers of A and B. Discuss.
4

You might also like