フィルターのクリア

Jetty Problem

3 ビュー (過去 30 日間)
Reelz
Reelz 2012 年 5 月 1 日
Typical Jetty Problem, problem with my code though? (specifically the line prob = 100 * nsafe / n;) Why doesn't this work?
Also I am trying to graph the probability of survival against the probability of going forward. Help doing this much appreciated!
Here's my code:
% random walk p = input( 'Number of walks: ' );
nsafe = 0; % number of times he makes it
for i = 1:p
steps = 0; % each new walk ...
x = 0; % ... starts at the origin
y = 0;
while x <= 50 & abs(y) <= 10 & steps < 2000
steps = steps + 1; % that’s another step
r = rand; % random number for that step
if r < 0.6 % which way did he go?
x = x + 1; % maybe forward ...
elseif r < 0.8
y = y + 1; % ... or to port ...
else
y = y - 1; % ... or to starboard
end;
end;
if x > 50
nsafe = nsafe + 1; % he actually made it this time!
end;
end;
prob = 100 * nsafe / n;
disp( prob );

回答 (1 件)

Thomas
Thomas 2012 年 5 月 1 日
You have not defined 'n' anywhere in your code... what is 'n'?
EDIT based on the comment hat n=number of steps
Change the prob = 100 * nsafe / n; to the following:
prob = 100 * nsafe / steps;
  2 件のコメント
Reelz
Reelz 2012 年 5 月 1 日
n is the number of steps. Which if she hasn't reached the ship after 2000 steps she dies of thirst. She can go either forward, left, or right. Basically I am just generating probabilities, how would I edit my code to include this though?
Thomas
Thomas 2012 年 5 月 1 日
check my edit above

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

カテゴリ

Help Center および File ExchangeR Language についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by