TASK: What is the average number of times a random walk visits the square labeled 0?
COMMENT: A random walk on a sidewalk of "width" [-n,+n] starts at 0, and then repeatedly steps one unit to the left or right, continuing until reaching -n or +n. The function "walker_1d", given an input "n", will return in "track" the successive locations visited in a sample random walk. The first entry in "track" will be 0, but it's possible that the walk will return to 0 several times before finishing. The purpose of this exercise is to estimate, for a given value of n, how many times this is likely to happen.
INSTRUCTIONS: write a function "hw045" that takes a value n and estimates the number of times a random walk on a sidewalk of "width" [-n,+n] will visit the square labeled "0". You will need to copy the function "walker_1d.m" from the "homework" directory.
write the function header with the output "home", the function name,
and the input "n".
Initialize home to 0.
Use a "for" statement that repeats 1000 times
Call "walker_1d", getting a vector "track".
Set i to a logical vector which is true for each entry of "track" that is 0.
Set visits to be the number of "1" values in "i".
Don't use a FOR statement to count the visits!
Instead, figure out how to use a "logical"
vector of 1's and 0's, and then sum it!
Add "visits" to home.
Once the for statement is complete, average "home" by dividing by 1000.
CHECK: n = 10; home = hw045 ( n ); Because your calculation depends on random experiments, you will get a different answer each time. However, if n = 10, your answer should be roughly 10.
SUBMIT: Your script file should be named "hw045.m", and begin with:
% hw045.m
% YOUR NAME
% This script (describe what it does)