Help with 2d random walk
1 view (last 30 days)
Show older comments
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).
Answers (1)
David Sanchez
on 28 Oct 2014
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
0 Comments
See Also
Categories
Find more on Random Number Generation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!