Random Walks in the Plane
This in-class demonstration shows how to use the built-in random number generators to simulate and visualize two-dimensional random walks in the xy-plane, and begins to study the distribution of distances from the starting point. A homework assignment is included to continue analyzing the distribution of distances, study how the average distance depends on the number of steps, and generalize to three-dimensional random walks.
Students will learn how to simulate random walks using the built-in random number generators randi( ) and rand( ), together with for loops and conditional statements. Students will be exposed to basic plotting tools scatter( ), plot( ) and histogram( ) to visualize results. They will also gain experience in how user-defined functions can call each other, and be called by scripts to perform increasingly sophisticated analysis.
Context for Use
This activity is used in an introductory MATLAB course offered by a math department to students with no assumed prior coding experience, near the beginning of the course, immediately after the introduction of for loops, conditional statements, and basic plotting tools. The activity is the first of the semester to utilize all of the above programming techniques and plotting tools together towards a single goal. The in-class portion is comfortably completed in 50 minutes, and the homework assignment is due one week from the in-class presentation.
Description and Teaching Materials
two_d_walk_UDLR.m – function that performs "UDLR" random walks.
two_d_walk_angle.m – function that performs "angle" random walks.
visualize_walk.m – script that plots a UDLR and angle random walk of a given length.
distance_of_walkers.m – function that simulates multiple random walks of each type and calculates the distance from the starting in each case.
distribution_of_distances.m – script that visualizes the distribution of distances for each type of walk using a histogram and calculates the mean distance.
walk_assignment.pdf – PDF file summarizing the above functions and assigning four homework problems.
two_d_walk_UDLR.m (Matlab File 1kB Sep17 21)
two_d_walk_angle.m (Matlab File 1kB Sep17 21)
visualize_walk.m (Matlab File 635bytes Sep17 21)
distance_of_walkers.m (Matlab File 1013bytes Sep17 21)
walk_assignment.pdf (Acrobat (PDF) 310kB Sep17 21)
Teaching Notes and Tips
Since most students will not have experience with random walks, it's important to first clarify how one could simulate an UDLR walk using a 4-sided die, including recording the sequences of coordinates in vectors. Once the concept of a random walk is clear, it's a good opportunity to make clear how for loops and if statements are the perfect tools for efficiently implementing the walk. When introducing the angle walk, it's useful to spend a moment on how the output of rand( ) can be scaled to provide random numbers in a desired interval. The .m file distance of walkers is incomplete, to be worked on by students in small groups for 5-10 minutes, before discussing and sharing a solution with the class.
Point totals are indicated in the homework assignment for each problem, which are graded based on the correctness of the code or solution, as well as clarity of comments, and required attributes of the figures, when appropriate.
References and Resources