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

採用された回答

Torsten
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)
  2 件のコメント
Samiha Shimla
Samiha Shimla 2018 年 11 月 16 日
Thank you so much, this has been a big help!!!
Samiha Shimla
Samiha Shimla 2018 年 11 月 16 日
Also, could you help with the question in the picture. I am totalky confused about how to approach this. ould you Cput %comments in your sution so I can understand what's going on .

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by