Mini-Lab: Random Walks in One and Two Space Dimensions

 


Introduction

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.



Random Walk in One Space Dimension

In this section, we will simulate 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?
  1. In the 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.
  2. Create a function called walk1D that takes a number of particles and a number of steps as parameters.
  3. Copy the code from Section 8.6.1 in your textbook, starting from the line: positions = numpy.zeros(np)
  4. NOTE: The book is using 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.

  5. You will need to import the 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.
  6. Call your function using 1 for the number of particles and 10 for the number of steps. Do you get any output? (You shouldn't, since you're not printing or plotting anything yet!)
  7. To see how far along the x-axis the particle moved, add a print statement at the end of the function to display the positions of the particles. Call your function several times with 1 and 10 as parameters and observe the output. Does it agree with what was discussed above in the Stop and Think section?
  8. Test your function with different numbers of particles and steps. Do your results seem reasonable?
  9. When you get many particles moving, it would be nice to see where they all end up. When we have many particles, we would expect more of them to end up closer to the origin. If we were to draw a histogram of the final positions of the particles, for instance, we would expect to see more of a normal distribution. Add code after your print statement to draw a histogram of the postions of the partciles. You may write your own code to do this, or you may use and/or edit the following statements:
              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).

Random Walks in Two Space Dimensions

In this section, we will simulate 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.
  1. Create a function, walk2D that takes the number of particles and the number of steps as parameters.

  2. The code for this function will look very similar to the code for the 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:

  3. If you try to run your function at this point, you will not see anything happening. Add a print statement at the end of your function to print the xpositions and ypositions.

  4. Modify your print statement so that it prints the ending positions of the particles as points, instead of two separate arrays. (Hint: Maybe you can use the zip function to help with this?)

  5. Now we will add a visual piece to this. Add code before your nested loops to display a scatterplot of the positions of the particles. You can write your own code to do this, or you can start with this code:
        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)!

  6. Add code at the end of each step to display the scatter plot of the particles. How many particles appear? Does it make sense why there may not be as many particles shown as you said were in the simulation?

  7. Run your function with different numbers of particles and steps. Run your function several times with the parameters. Is the behavior always the same? Why or why not?

  8. Add a comment to your program to explain why it may not look like all of the particles are showing up in the scatterplot. (Hint: you may be able to see a connection between the list of final positions, and how many particles are shown if you look to see how many times any given final position is repeated.)

Submit