Я создал функцию, которая генерирует N частиц для случайного блуждания по одной за раз.
Я отследил конечное местоположение каждой частицы («результат»). Однако я хотел бы сохранить частицу в конечном положении (то есть, когда y = 99). Когда генерируется новая частица, конечное положение должно быть при y=99 ИЛИ когда частица приземляется поверх конечного положения предыдущей частицы.
Может ли кто-нибудь указать мне направление для достижения этого?
На данный момент мой код останавливает частицу при y=99, но не учитывает, приземляется ли она поверх другой частицы (т.е. если частица уже есть в точке (50,99), новая частица должна остановиться в точке (50,98). ) не (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;
% x(i)=50;
% y(i)=1;
y(i)=1;
x(i)=50;
%for k=1:n
%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