Simulations involving a collections of particles moving around in a random fashion are fundamental in physics, biology, chemistry as well as other sciences and are used to describe many different phenomena. The purpose of this mini-lab is to become familiar with a random walk. A random walk is the term used to describe the movement of a particle based on a random direction at each step. There are many different ways to simulate this. In this mini-lab, we will explore random walks in one space dimension, where a particle randomly moves left or right, and random walks in two space dimension, where a particle randomly moves left or right, or up or down.
np
particles moving
randomly along the x-axis for ns
steps. We will think of
these particles as starting at x = 0 and moving left or right along the
axis. At each step, we flip a coin for each particle. If the
coin comes up heads, the particle moves 1 unit to the right; if the
coin comes up tails, the particle moves 1 unit to the left.
Stop and
Think: What is the maximum distance the
particle can move to the right or the left after
ns
steps? What are the other possible
positions a particle could finish in? Where is the particle
more likely to finish?
Editor
in Spyder, create a new file for the functions you will
write in
this mini-lab. Then save the new file with a name
representative of this mini-lab.walk1D
that takes
a number of particles and a number of steps as
parameters.positions =
numpy.zeros(np)
np
as a variable name, so if you use
np
as a shortcut for
numpy
you should make sure to use a different
variable name for the number of particles.
numpy
and
random
modules. Add statements to do this at the
beginning of your file. You will eventually need the
matplotlib.pyplot
module, so go ahead and add
that import statement now as well.plt.xticks(range(-ns-1, ns+2,2)) plt.hist(positions, 21, range=[-ns-.5, ns+.5], facecolor='blue', align='mid')This code generally worked well with 10 as the number of steps. If you vary the number of steps, you may want to vary the range in the xticks function and/or the number of bins (the parameter 21 in the
hist
function).np
particles
moving randomly in the x-y coordinate plane for
ns
steps. We will think of these paricles as
starting at the origin, (0,0), and moving up, down, left, or right. At
each step, we draw a random number among 1, 2, 3, or 4, which will
determine which way the particle will move.
walk2D
that takes the number of
particles and the number of steps as parameters.
random_walk_2D
function in Section 8.7.1 in your text, but there will be some
modifications. The following instructions explain what to copy,
change, or omit from the code in the book:
xpositions
and
ypositions
arrays.
xymax
.
NORTH, SOUTH, WEST, EAST
constants.
xpositions
and
ypositions
.
zip
function to
help with this?)
plt.figure() plt.axis([-ns, ns, -ns, ns]) plt.scatter(xpositions, ypositions)Note that
ns
is the variable representing the number of
steps.
What do you see? If it looks like there is only one particle, that's
correct - they are all starting at (0,0)!