Help trying to create a Bean Machine Random Walk script
3 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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
0 Commenti
Risposte (2)
Reen
il 23 Ago 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.
Image Analyst
il 24 Ago 2017
Just before the call to plot() add this line:
y(y < -100) = -100; % Clip y to no less than -100.
0 Commenti
Vedere anche
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!