Asked by Lauren Nixon
on 25 Aug 2019 at 4:21

I am trying to simulate a biased 2d random walk in a 99 x 99 unit domain. I have created a function that generates N particles to do a random walk, 1 at a time. I can't get the particles to stick once they have either reached the bottom row or an occupied square.

I have tracked the end location of each particle ('result'). However, I would like to keep the particle in the end position (which is when y=99). When the new particle is generated the end position should be when y=99 OR when a particle lands on top of the end location of the previous particle.

Can anyone please point me in the direction to achieve this?

At the moment my code stops the particle when y=99 but does not consider if it is landing on top of another particle (ie.if there is already a particle at (50,99) the new particle should stop at (50,98) not (50,99))

function [result]=random_walk_gravity(N,s,w,e)

%N (the number of particles), and s, w, e (the probabilities of moving south, west and east).

result = zeros(1,N);

for k=1:N

%start particle i at x0, y0

%In this case, all particles will start in column 50 in the top row

%step with stepsize 1 either South, West or East

%sample u(0,1) with the equal probability of moving in the east, west or

%south direction

i=1;

%initital postions

y(i)=1;

x(i)=50;

%generate random number between 0 and 1, N times

u=rand();

while y(i)<99 %we want it to stop at y=99 and look at the x-value

if (0<=u)&&(u<=e) && (x(i)<99) %Check that the x position is less than 99

%Move east, therefore xposition is increased by 1 unit

x(i+1)= x(i)+1;

y(i+1)= y(i);

i=i+1;

u=rand();

elseif (0<=u)&&(u<=e) && (x(i)>=99) %Check if the x position is greater than or equal to 99

%particle hits the side boundary it moves back in the direction

%it came from (west)

x(i+1)= x(i)-1;

y(i+1)= y(i);

i=i+1;

u=rand();

elseif (e<u)&&(u<=(e+w))&& (x(i)>0) %Check that the x position is greater than 0

%Move west, therefore xposition is increased by 1 unit

x(i+1)= x(i)-1;

y(i+1)= y(i);

i=i+1;

u=rand();

elseif (e<u)&&(u<=(e+w))&& (x(i)<0)

%Check if the x position is less than or equal to 0

%particle hits the side boundary it moves back in the direction

%it came from (east)

x(i+1)= x(i)+1;

y(i+1)= y(i);

i=i+1;

u=rand();

else %Move south, therefore yposition is increased by 1 unit

x(i+1)= x(i);

y(i+1)= y(i)+1;

i=i+1;

u=rand();

end

end

result(k) = x(i);

histogram(result)

end

Opportunities for recent engineering grads.

Apply Today
## 1 Comment

## Walter Roberson (view profile)

## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/477476-bias-random-walk-under-gravity-how-to-count-how-many-particles-end-in-the-same-y-column-and-stac#comment_738720

Sign in to comment.