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)
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

Answers (1)

Image Analyst
Image Analyst on 11 Feb 2018
I've probably done and posted that already. See my attached demos and adapt as needed.
  2 Comments
Kelark Azer Habashi
Kelark Azer Habashi on 11 Feb 2018
my main question is how do I create a vertical bar for this question. I do not believe that any of the questions that you have attached required the creation of a vertical bar.
Image Analyst
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);

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!