Plotting a random walk

7 views (last 30 days)
Sofia
Sofia on 11 Apr 2015
Answered: Image Analyst on 11 Apr 2015
so I need to create a function that plots location versus time for an individual who makes successive random jumps. Each jump is one unit to the right with probability R, and otherwise its one unit to the left. The arguments need to be R = probably of jumping one unit to the right; a = the initial location; and numjumps = number of jump the individual makes. I also need to use the
binornd() function.
What I've coded so far is:
function plot_sim(a,numjumps,R)
loc = a;
time = 0;
for i = 1:numjumps;
loc = loc + (2*binornd(1,R)-1);
time = time + 1;
hold on;
plot(time,loc,'-')
end
And I have to evaluate it with plot_sim(0,25,0.5). And I am just confused because even though I have plot(time,loc,'-'), it doesn't plot as connecting lines, it just plots as separate dots. I've tried including the plot function outside of the for loop and that doesn't work. I've even tried changing the colours of the dots and that doesn't even work. Am I coding this wrong?

Answers (1)

Image Analyst
Image Analyst on 11 Apr 2015
I have two random walk programs, both attached. Study each and adapt as needed.

Community Treasure Hunt

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

Start Hunting!