Sei sulla pagina 1di 4

PHY331: Introduction to Biological Physics

Practical #3:

The Random Walk in Two Dimensions


3.1 Background
This brief exercise is designed to help you synthesize some of what you have
learned about Python as a final step towards the application of the language in
the analysis of your upcoming fluorescent bead experiment. It consists of
programming a simple random walk (which you have already done), but in two
dimensions instead of one. You will also be introducing a variable degree of
directional bias to your random walk, meaning that, instead of hopping in all
directions with equal probability, your particles will tend to prefer one or more
direction over others. Finally, you will plot your results using the now-familiar
pyplot package.
3.2 Part I: the unbiased random walk
Start by opening a new Python script window, and saving it as
2Drandomwalk.py, or something similar. Begin, as usual, by importing the
modules you think you will need to run your planned random walk function. The
header of your function should look like
def RandomWalk2D(timesteps, particles, delta):
...
The three dots ... represent the function itself; remember that the entire
function must be indented by one <tab> with respect to the margin of the
document.
The variable timesteps represents, of course, the number of time steps over
which you would like to carry out the simulation; particles is the number of
independent particles whose motion you would like to simulate; and delta is the
constant size of the steps taken by your particles. Notice that this part of the
exercise is substantially similar to 2.6.2-2.6.3 from the previous practical.
Indeed, you may reuse some of your code from that exercise if you wish.
There is a key difference from the previous Practicals exercise, though: in the
present case, the fact that our random walk takes place in two dimensions
complicates matters by introducing directionality. More explicitly, in your
previous exercise, the particle in question had to choose only whether to hop to
the left or to hop to the right. Since the current problem is two-dimensional,
however, the particle must choose between an infinite number of possible
directions in which to hop. It could hop upwards or downwards, left or right, or
perhaps diagonally upwards and to the left, or any direction at all in between.
This means that your particle now has to choose the angle of its next hop.

Python understands angles expressed in radians, which range between 0 and


2. Supposing that the angle at which your particle hops is denoted by the
variable theta, you need to find a way of choosing for theta a random value
between 0 and 2. How can you do this using np.random()?
Once you have worked out how to find theta, you can use it to update the xand y-coordinates of your particle at each time step using
new_x = old_x + delta*np.cos(theta)
new_y = old_y + delta*np.sin(theta)
where cos() and sin() are, naturally, the usual cosine and sine functions of
trigonometry as implemented by the numpy software package.
To summarize the exercise:
Question 3.2.1
Write a Python script defining the function
def RandomWalk2D(timesteps, particles, delta):
...
that will simulate the random walk in two dimensions, and allows the user to input
the number of time steps, number of particles, and step size to be taken. The
particles should move in all directions with equal probability. (You will need to call
upon your knowledge of loops, arrays, and random functions to complete this
exercise successfully.)
By now you will, hopefully, be familiar with the notion that diffusion can be
modeled as the outcome of a random walk undertaken by multiple particles.
Recall that, in the case of diffusion, the function describing the probability that a
particle has moved a distance x from its starting point in time t is
x2

1
4 Dt
P ( x,t ) =
e
4 Dt
Hence, at each time instant t , the distances of the particles from their starting
points should lie on a distribution like the one above. This function can be
identified with a Gaussian curve which, in its canonical form, is normally written
as

( )

f x =

( x )2
2 2

2
where, as usual, represents the mean and the standard deviation. Now,
suppose that you were given sets of distance data (that is, distances from the
origin) collected at different times, such that the data distribution at each time is
approximately Gaussian, with some standard deviation that in general depends
on t . From inspection of the two equations above, what relation between and
t would you expect to see if the process is diffusive?
Question 3.2.2

Use the function you have defined in the previous section to simulate the motion
of 20 particles over 1000 time steps, using a step size delta of your choosing.
Plot the motion of all particles on the same axes using pyplot. (That is, plot the
motion of all particles on the x-y plane, with no explicit time axis.) From what you
know about diffusion (that is, the expected relation between and t in the
equations above), does what you observe appear to suggest a diffusive process?
3.3 Part II: The biased random walk
In this exercise you will modify the program that you have written above to
introduce a bias in the particles motion. The function header should look like
def BiasRandomWalk2D(timesteps,particles,delta,leftangle,rightangle,bias):
...
Notice that three new inputs have been introduced: leftangle, rightangle
and bias. The first two of these inputs should be floating-point numbers
between 0.0 and 2, and the last should be a floating-point number greater than
0.0. The first two inputs, leftangle and rightangle, define an angular slice
around the position of your particle. The last of these, bias, represents the
relative preference of a particle to hop in a direction defined by that slice, as
opposed to any other direction. This arrangement is best illustrated in a picture,
as in the figure below.

As discussed, the leftangle and rightangle variables define the biased


region, shown above in red. The bias variable describes how much more likely
it is for a particle to hop into the red region than in the blue region. For example,
if bias is equal to 2, then the particle is twice as likely to jump in a direction
between leftangle and rightangle (that is, in the red region) than it is to
jump in the blue region, and the general motion of the particle over many time

steps will betray a substantial upward bias. If, on the other hand, bias is equal
to 0.01, then if leftangle and rightangle are consistent with the figure
above, the particle will have almost no chance of jumping into the red region, and
will spend almost all of its time jumping downwards, left, or right. Over many
time steps, the effect of the left and right jumps are likely to cancel out (since the
geometry of the above figure suggests that the particle will be jumping left and
right about equally often), so the general motion of the particle will be directed
roughly downwards.
Question 3.3.1
Modify your RandomWalk2D script to take into account directional bias in the
manner described above. Your function header should be changed to
def BiasRandomWalk2D(timesteps,particles,delta,leftangle,rightangle,bias):
...
At each time step, your particles should move in a manner consistent with the
input bias selected by the user.
Question 3.3.2
Test out your BiasRandomWalk2D script with 20 particles over 1000 time steps,
with a delta of your choosing and one or two different angular slices with three
or four different choices of bias for each. Plot the results in each case. How do
these results differ from those you obtained in the unbiased case? Are these
differences roughly what you expect based on the inputs?

Potrebbero piacerti anche