Plotting a random walk
显示 更早的评论
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?
回答(1 个)
Image Analyst
2015-4-11
1 个投票
I have two random walk programs, both attached. Study each and adapt as needed.
类别
在 帮助中心 和 File Exchange 中查找有关 Random Number Generation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!