How do I plot a 1-d random walk that generate 1 and -1 with equal probability? I am struggling to plot every individual P that I get against i the number of step.
17 ビュー (過去 30 日間)
古いコメントを表示
P=0; % Starting value
for i=1:10 % i is the number of steps from 1 to 10
R=rand;
if R<0.5
S=-1;
elseif R>0.5
S=1;
end
P=S+P % Gives the next random walk from the new position P every time
end
xlabel('Position')
ylabel('Step Count')
title('1-D Random Walk')
0 件のコメント
採用された回答
Torsten
2018 年 11 月 16 日
n = 10;
P = zeros(n,1);
P(1) = 0; % Starting value
for i=2:n % i is the number of steps from 1 to 10
R = rand;
if R < 0.5
S = -1;
elseif R > 0.5
S = 1;
end
P(i) = S+P(i-1) % Gives the next random walk from the new position P every time
end
ylabel('Position')
xlabel('Step Count')
title('1-D Random Walk')
plot(1:n,P)
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Creating and Concatenating Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!