You are on page 1of 5

EA-1 Fall 2021

Homework Program 3: Random Walk


In this assignment you will write a Matlab script file that simulates a random walk in the x − y plane.
Imagine someone who decides to take a walk to explore a neighborhood partitioned into blocks in the
following random way. Starting at the origin, the person chooses a random direction: north, west, east,
or south with equal probability, and walks along that direction for one block. At the end of the block the
person then chooses another random direction (which could be the reverse direction) with equal probability,
walks for one block, then chooses another random direction, walks another block, and so forth. This type
of random path approximates what is known as Brownian motion, and is used to model many physical
processes, including the diffusion of tobacco smoke in air, noise in electrical circuits, firing of neurons in the
brain, as well as the movement of prices in different types of financial markets.
We will assume that the random walk takes place in the x − y plane starting at the origin, and that
each step has length one, and consists of moving up, down, left, or right with equal probability. Here is an
example of a possible sequence of steps:

(0, 0) → (−1, 0) → (0, 0) → (0, 1) → (1, 1) → (1, 2) → (1, 1) → (1, 2) → (0, 2) (1)

In particular, it is not allowed to move from (0, 0) to (1, 1) or another diagonal point.
The assignment consists of two tasks:

• Plot a particular random walk for a specified number of steps. This illustrates an easy way to animate
plots in Matlab.
• Simulate several random walks where each walk terminates when the distance from the origin reaches
or crosses a given threshold. In each case count the number of steps it takes to reach the threshold and
plot the corresponding histogram, which indicates the probability that the boundary will be crossed
after a given number of steps.

To complete the assignment you will need to write Matlab code that implements each of the following
steps.

1. Prompt the user to input the following parameters:


• For task one, the number of steps in the random walk;
• For task two, the number of random walks generated (or “trials”), the distance threshold, and
the maximum number of steps in each random walk.
In the second task we limit the number of steps in each random walk so that the program does not run
indefinitely. Note that you can finish task one before prompting the user for the latter three inputs for
task two.
2. For the first task initialize a position variable at the origin, then update and plot each new position
for the specified number of steps. Note that given a particular position (x, y), the next position is
(x, y)+(s1 , s2 ) where the step (s1 , s2 ) is chosen at random from the set {(1, 0), (−1, 0), (0, 1), (0, −1)}.
An easy way to update the position is to create an array that contains these four steps and to choose
a row or column (depending on how you arrange the array) using the Matlab function randi.
To animate the series of points in the plot, you can use the hold on command, which will superimpose
each new step in the plot. Plot each step using the command
quiver(position(1),position(2),new direction(1),new direction(2), 1.0,...
'r', 'LineWidth', 3,'MaxHeadSize', .8);
where position(1) and position(2) are the x and y coordinates of the current position, and
new direction(1) and new direction(2) are the step sizes in the x and y directions for the next

1 of 5
EA-1 Fall 2021

step, that is, the new position will be position(1) + new direction(1) for the x-coordinate and
position(2) + new direction(2) for the y-coordinate. To slow down the animation insert the
pause command to pause the program for a predetermined amount of time (e.g., at least 0.01 second,
see help pause). Also, you can use the command axis equal to make the x and y distances look
the same. An example plot is shown in Fig. 1.
Print a sentence that states the final position (x, y) and the Euclidean distance from the origin d =
p
x2 + y 2 .
3. For the second task you will need to write a loop to generate N different random walks (trials). Each
of the trial random walks continues until either the distance of the current position from the origin
reaches or exceeds the maximum distance, or the number of steps (num steps) exceeds the maximum,
both specified by the user. This is illustrated in Fig. 2, which shows a random walk with a distance
threshold of 30. In this example, the boundary is reached in 1,755 steps.
For this task you need to store the number of steps for each trial in an array. After completing the
trials use the commands figure and histogram to generate a histogram showing the number of times
num steps falls within different intervals. (You can instead use hist as in Homework 2 if you like.
Here we do not need the features of hist used in Homework 2.)
To complete this task you can use the following nested loop (sketch of Matlab code):

loop over number of random walks (trials)


loop over the position updates
update position
increment number of steps for this trial
end inner loop
if number of steps exceeds the distance threshold, print a warning message
end outer loop
plot histogram for number of steps
print the average of number of steps over the trials

As indicated, for each trial print a warning message if num steps exceeds the maximum number of
steps, and once the trials are completed, compute and print the average number of steps over the N
trials (note that mean computes an average, or you can use sum). You do not need to plot each
of the random walks in this task. That will take too long when running a large number of trials.
You need only count the number of steps to hit the boundary.

Run the program with the following inputs:

• For task 1, show 6000 random steps;


• For task 2, show histograms for dmax equal to 20 and 40. with N = 20, 000 trials. Enter a sufficiently
large value for the maximum number of steps so that it is not exceeded.

Note that it may take a few minutes for your program to run when the number of random steps and trials
are this large. Test your program with smaller values! Monitor the progress of the program by printing out
the number of trials (outer loop variable) in increments of 500. (You will need to insert this in the previous
sketch of the Matlab code.) A convenient way to do this is to use the mod command.
The output of an example program run is shown below along with example plots. For this run the input
parameters are different from the preceding inputs. In particular, the maximum number of steps in task 2
is too small. Note that the shape of the histogram in task 2 generally becomes smoother with more trials.
Also note the interpretation of the histogram: in this example with dmax = 30, the histogram shown in Fig.
3 shows that the random walk hits the boundary in less than 1000 steps in more than half of the trials; also,
there is a small chance (less than 50 trials out of 10,000, or < 0.5%) that the number of steps exceeds 4000.

2 of 5
EA-1 Fall 2021

Submit your program as a single script (.m file), comment the output of the program for the preceding
example with dmax = 40, and append it to your script. Also, run the program for the values of dmax equal
to 10, 20, 30, and 40, and note the average values of num steps. Do this for a large number of trials (say,
20,000) and again, make sure that you are not getting warnings that the maximum number of steps has been
exceeded. Based on these results, give an approximate estimate for the average number of steps to reach the
distance dmax = 1000. Explain your answer – you do not need to run your program with this value.

Enter number of steps for example. 1000

The final position is (16, 36).


The distance from the origin is 39.40.

Enter distance threshold: 30


Enter number of trials. 10000
Enter maximum number of steps. 4000
Warning: maximum number of steps exceeded!
Warning: maximum number of steps exceeded!
Warning: maximum number of steps exceeded!
Warning: maximum number of steps exceeded!
trial= 500
Warning: maximum number of steps exceeded!
trial= 1000
Warning: maximum number of steps exceeded!
Warning: maximum number of steps exceeded!
Warning: maximum number of steps exceeded!
Warning: maximum number of steps exceeded!
trial= 1500
Warning: maximum number of steps exceeded!
Warning: maximum number of steps exceeded!
trial= 2000
trial= 2500
Warning: maximum number of steps exceeded!
Warning: maximum number of steps exceeded!
trial= 3000
Warning: maximum number of steps exceeded!
trial= 3500
Warning: maximum number of steps exceeded!
Warning: maximum number of steps exceeded!
Warning: maximum number of steps exceeded!
trial= 4000
Warning: maximum number of steps exceeded!
Warning: maximum number of steps exceeded!
Warning: maximum number of steps exceeded!
trial= 4500
Warning: maximum number of steps exceeded!
Warning: maximum number of steps exceeded!
Warning: maximum number of steps exceeded!
trial= 5000
Warning: maximum number of steps exceeded!
Warning: maximum number of steps exceeded!
trial= 5500
trial= 6000
Warning: maximum number of steps exceeded!

3 of 5
EA-1 Fall 2021

Warning: maximum number of steps exceeded!


trial= 6500
Warning: maximum number of steps exceeded!
trial= 7000
Warning: maximum number of steps exceeded!
trial= 7500
Warning: maximum number of steps exceeded!
trial= 8000
Warning: maximum number of steps exceeded!
Warning: maximum number of steps exceeded!
Warning: maximum number of steps exceeded!
trial= 8500
Warning: maximum number of steps exceeded!
Warning: maximum number of steps exceeded!
Warning: maximum number of steps exceeded!
trial= 9000
Warning: maximum number of steps exceeded!
trial= 9500
Warning: maximum number of steps exceeded!
Warning: maximum number of steps exceeded!
Warning: maximum number of steps exceeded!
trial= 10000

Average number of steps to reach distance 30 is 910.05.

Figure 1: Task 1 example: 2-D random walk with 1000 steps.

4 of 5
EA-1 Fall 2021

Random Walk with Distance Threshold


30

20

10

-10

-20

-30
-30 -20 -10 0 10 20 30

Figure 2: Random walk with circular boundary representing the distance threshold.

Figure 3: Task 2 example: histogram of num steps with 10,000 trials, dmax = 30.

5 of 5

You might also like