Professional Documents
Culture Documents
HW 3
HW 3
(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 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):
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.
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.
3 of 5
EA-1 Fall 2021
4 of 5
EA-1 Fall 2021
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