How would I start this problem?

4 次查看(过去 30 天)
We consider again the one-dimensional version of the drunkard problem. This time he starts out at position x (e.g., x=10), and takes unit steps left or right with equal probability. Suppose at 0 and a (a>x>0, e.g., a=30) are two bars. When he reaches either one, he is trapped forever.
We want to determine the average time t (the mean number of steps) before the drunkard is trapped. This is a diffusion problem and the 'time' t is called the first passage time. Do a simulation to compute t. (Again note that only the average of t has a meaning and it is that average that we want to compute.)

采纳的回答

Image Analyst
Image Analyst 2017-10-8
See my attached collection of random walk programs/demos I've written. Adapt as needed.
  17 个评论
Gaëtan Poirier
Gaëtan Poirier 2017-10-9
So here it is so far:
for N=1:10000 %number of "walkers"
x(1)=10; %starting position
out_of_bounds = stepNumber;
for stepNumber=20; %number of steps limited to each "walker"
g(n)=rand(); %random number
if g(n)<0.5
x(n+1)=x(n)-1; %if random number is less than, go left
else x(n+1)=x(n)+1; %else go right
end
if x(n+1)>29||x(n+1)<1 %second bar––if "walker" reaches 30, the loop terminates
break
else continue % else it continues
end
end
end
histogram(out_of_bounds);
My distribution is a square, completely filled so I obviously have something wrong. Any ideas?
Image Analyst
Image Analyst 2017-10-9
Look at my code again. Do you see this:
for stepNumber=20
No. That's because your for loop will execute only once, and with a value of only 20, not every integer from 1 up to 20.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by