Write a script that computes the path(s) of a set of N random walkers which are confined by a pair of barriers at +B units and -B units from the origin, i.e., 0 (where the walkers all start from). If a random walk hits or exceeds the wall positions,
3 views (last 30 days)
Show older comments
A random walk can be instantiated by repeatedly performing the calculation x(n+1) = x(n) + s where s is a number drawn from the standard normal distribution (randn in MATLAB). Your script should have two parameters at the top of the script – the number of random walkers (Nwalk) and the distance to the barriers (B). When testing this program, I would use a value of B around 10.
The statistics of interest here is the mean and distribution of the number of steps the walk must take before crossing one of the barriers. This provides a model for how long it takes to make a choice between the two alternatives. Your script should make a histogram of the distribution of lifetimes, and indicate the mean lifetime with a vertical bar.
So far for this question this is my code but im struggling on how to create a vertical bar.
for walk=1:100;
steps=1;
x=0;
while x(steps)<10 & x(steps)>-10;
x(steps+1)= x(steps)+randn;
steps=steps+1;
end
numb_steps(walk)=steps;
end
meanofnumb_steps= mean(numb_steps);
figure(1)
hist(numb_steps);
hold on
2 Comments
Jan
on 11 Feb 2018
I've formatted you code to make it readable: Mark it with the mouse and hit the "{} Code" button.
Answers (1)
Image Analyst
on 11 Feb 2018
I've probably done and posted that already. See my attached demos and adapt as needed.
2 Comments
Image Analyst
on 11 Feb 2018
Once you've plotted the histogram with histogram() or bar() or whatever, you can plot a bar at the mean lifetime value like this:
hold on;
line([meanLifeTime, meanLifeTime], ylim, 'Color', 'r', 'LineWidth', 2);
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!