Help trying to create a Bean Machine Random Walk script

3 Ansichten (letzte 30 Tage)
Joshua Muller
Joshua Muller am 23 Aug. 2017
Beantwortet: Image Analyst am 24 Aug. 2017
I'm creating this script which simulates 120 particles falling one by one down a 100x100 grid. Particles should stop when they either collide with the edges of the plot or collide with older particles. I've managed to get a single particle to fall and stop when it hits the edges, however I'm stuck trying now to repeat this for 120 particles.
Can anyone see why the particles don't stop when they reach the bottom?
%%Initialisation
clear all
M = 120; % the number of particles
N = 400; % the number of jumps to take
Stepx = 1; % the size of the jumps in x
Stepy = 1; % the size of the jumps in y
e = 1/3;
s = 1/3;
w = 1/3;
x = zeros(N,M); % set all x positions to zero initially
y = zeros(N,M); % set all y positions to zero initially
%%Simulation
for n = 1:N % for each of the N jumps
if y(n+1,M) ~= -100 || x(n+1,M) ~=-50 || x(n+1) ~= 50
r = rand(1, M);
east_mask = r <= e;
x(n+1, east_mask) = x(n, east_mask) + Stepx;
y(n+1, east_mask) = y(n, east_mask);
south_mask = r >= e & r <= e + s;
x(n+1, south_mask)= x(n, south_mask);
y(n+1, south_mask)= y(n, south_mask)- Stepy;
west_mask = r>= e + s & r <=1;
x(n+1, west_mask) = x(n, west_mask) - Stepx;
y(n+1, west_mask) = y(n, west_mask);
else
break
hold on
end
plot(x(n,:), y(n,:), '.', 'MarkerSize', 20)
axis equal
axis([-50, 50, -100, 0]);
drawnow
end

Antworten (2)

Reen
Reen am 23 Aug. 2017
I see a few thing wrong with you're code. The first thing that sticks out to me is the line:
if y(n+1,M) ~= -100 || x(n+1,M) ~=-50 || x(n+1) ~= 50
This line isn't doing anything. It's making sure the bean isn't at all of the sides simultaneously, which will never happen. The OR operators should be AND operators for it to work properly.
One other thing I see is that you're only making that check for the LAST bean (index M=120). You should probably use another for loop to run through this check on every bean.
It also looks like the else condition isn't doing much of anything. You should really be trying to stick the previous values, so something like:
else
x(n+1,m) = x(n,m);
y(n+1,m) = y(n,m);
end
This way the beans will stick to whatever edge they hit. I suspect there will be some more bugs after fixing these, but hopefully this will send you in the right direction.
  1 Kommentar
Joshua Muller
Joshua Muller am 23 Aug. 2017
How would I go about running a check for every particle? I used the else statement you suggested and changed my if statement
for n = 1:N % for each of the N jumps
r = rand(1, M);
east_mask = r < e;
x(n+1, east_mask) = x(n, east_mask) + Stepx;
y(n+1, east_mask) = y(n, east_mask);
south_mask = r >= e & r < e + s;
x(n+1, south_mask)= x(n, south_mask);
y(n+1, south_mask)= y(n, south_mask)- Stepy;
west_mask = r>= e + s & r <=1;
x(n+1, west_mask) = x(n, west_mask) - Stepx;
y(n+1, west_mask) = y(n, west_mask);
if y(n+1,:) ~= -100 & x(n+1,:) ~=-50 & x(n+1,:) ~= 50
else
x(n+1,m) = x(n,m);
y(n+1,m) = y(n,m);
end
%j = j+1;
for j=1:M
set(plotHandle(j),'XData',x(1:n,j),'YData',y(1:n,j));
end
end
However this stops every particle once the first particle hits 100, so many of the particles are still midfield.

Melden Sie sich an, um zu kommentieren.


Image Analyst
Image Analyst am 24 Aug. 2017
Just before the call to plot() add this line:
y(y < -100) = -100; % Clip y to no less than -100.

Kategorien

Mehr zu Programming finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by