Help with 2d random walk

3 次查看(过去 30 天)
DDDD
DDDD 2014-10-28
A particle moving in a sheet where -1<y<1 and 0<x<5. It will start at x=0 and between -0.5<y<0.5. After each step, it will move a distance defined by d=0.2*log(rand()) and a random angle from –pi/4 to pi/4. Plot a sample movement. Find after 1000 iterations how many particles make it to the end of the sheet (ie. X>5).

回答(1 个)

David Sanchez
David Sanchez 2014-10-28
This may be of help:
% Initial position
x = 0;
y = 0;
for k = 1:1000
% -1<y<1 and 0<x<5.
d = 0.2*log(rand);
% random angle between –pi/4 to pi/4
% Generate values from the uniform distribution on the interval [a,b]:
a = -pi/4;
b = pi/4;
r = a + (b-a)*rand;
% Position:
x = x + d*cos(r);
y = y + d*sin(r);
end

类别

Help CenterFile Exchange 中查找有关 Random Number Generation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by