How do get the particles to bounce properly?

5 Ansichten (letzte 30 Tage)
Alastair Martin
Alastair Martin am 4 Mai 2015
Kommentiert: Animesh Biswas am 29 Mai 2022
Hi i am trying to model gas particles when they collide with each other and a wall using elastic collision trying to keep it simple. But once they have collide they only change direction for 1 loop the go back to the original direction and collide again and so on.
Here is my code
clear all
figure(1)
%Constants
r = 0.5;
C = 0;
n = 5; %Number of Moles
N = n; % Number of molecules
x = 10*rand(N,1)-10;
y = 10*rand(N,1)-10;
velocityx = 15*rand(N,1)-5; %realsqrt((3*K*T)/MaO)
velocityy = 15*rand(N,1)-5; %realsqrt((3*K*T)/MaO)
dt = 0.1;
for t = 1:1:60
for i=1:1:N
x(i) = x(i)+velocityx(i)*dt;
y(i) = y(i)+velocityy(i)*dt;
%Collisions
for i=1:1:N
for j=(i+1):1:N
dx = x(i)-x(j);
dy = y(i)-y(j);
if sqrt((dx*dx)+(dy*dy)) <= 2*r %Collision with other particle
C = C+1;
x(i) = x(i)-velocityx(i)*dt;
y(i) = y(i)-velocityy(i)*dt;
for i=1:1:N;
x(i) = x(i)-velocityx(i)*dt;
y(i) = y(i)-velocityy(i)*dt;
end
end
end
%Collisions with the Y-axis bounds
if 20 <= x(i)+r %Collision with chamber wall
C= C+1;
x(i) = x(i)-velocityx(i)*dt;
y(i) = y(i)-velocityy(i)*dt;
for i=1:1:N
x(i) = x(i)-velocityx(i)*dt;
y(i) = y(i)+velocityy(i)*dt;
end
end
if -20 >= x(i)-r %Collision with chamber wall
C = C+1;
x(i) = x(i)-velocityx(i)*dt;
y(i) = y(i)-velocityy(i)*dt;
for i=1:1:N
x(i) = x(i)-velocityx(i)*dt;
y(i) = y(i)+velocityy(i)*dt;
end
end
% Collisions with the X-axis bounds
if 20 <= y(i)+r %Collision with chamber wall
C= C+1;
x(i) = x(i)-velocityx(i)*dt;
y(i) = y(i)-velocityy(i)*dt;
for i=1:1:N
x(i) = x(i)+velocityx(i)*dt;
y(i) = y(i)-velocityy(i)*dt;
end
end
if -20 >= y(i)-r %Collision with chamber wall
C = C+1;
x(i) = x(i)-velocityx(i)*dt;
y(i) = y(i)-velocityy(i)*dt;
for i=1:1:N
x(i) = x(i)+velocityx(i)*dt;
y(i) = y(i)-velocityy(i)*dt;
end
end
end
end
plot(x,y,'o','markersize',11);
axis([-20,20,-20,20]);
hold on
pause(dt);
hold off
end
display(C)
Any suggestions Thank you

Akzeptierte Antwort

James Tursa
James Tursa am 4 Mai 2015
Bearbeitet: James Tursa am 4 Mai 2015
You have multiple nested loops that use i for the index variable. These are going to clash and will not work. Change those inner loops to some other variable, e.g. k.
I would have expected the velocity to change when collisions occur, but I don't see that. I.e., I don't see anywhere in your code lines that begin with velocityx(i) = etc or velocityy(i) = etc. And once you do code this, I would expect the change in velocity to depend on the impact angles involved. I.e., don't just negate the velocities involved, but go through the geometry of the impact to see how things will bounce off of each other.
EDIT #1:
I still don't see the changes I suggested, so let's start with the simplest collision with the +x direction wall and agree that for this 1st cut we are only going to do the simplest result:
if 20 <= x(i)+r %Collision with chamber wall
C= C+1;
x(i) = x(i)-velocityx(i)*dt;
y(i) = y(i)-velocityy(i)*dt;
for i=1:1:N
x(i) = x(i)-velocityx(i)*dt;
y(i) = y(i)+velocityy(i)*dt;
end
end
The only thing that should change as a result of this collision is the velocity of the particle that had the collision, velocityx(i) and velocityy(i). None of the other particles should change anything as a result of this (so no inner loop should be used here), and none of the positions of any particle should change including the particle that had the collision, so you shouldn't be altering any x and y values. So this is how to code this collision:
if 20 <= x(i)+r %Collision with chamber wall
C= C+1;
velocityx(i) = -velocityx(i);
end
The only thing that happens is the x velocity of this particle reverses sign. No other particle has any changes. No particles have any position changes. And the y velocity of this particle does not change.
The other wall collisions are similar:
if -20 >= x(i)-r %Collision with chamber wall
C = C+1;
velocityx(i) = -velocityx(i);
end
if 20 <= y(i)+r %Collision with chamber wall
C= C+1;
velocityy(i) = -velocityy(i);
end
if -20 >= y(i)-r %Collision with chamber wall
C = C + 1;
velocityy(i) = -velocityy(i);
end
For your particle-to-particle collisions, the only things that change are the velocities of the two particles (again we are ignoring the geometry of the collision for this 1st simple cut). E.g.,
if sqrt((dx*dx)+(dy*dy)) <= 2*r %Collision with other particle
C = C+1;
velocityx(i) = -velocityx(i);
velocityy(i) = -velocityy(i);
velocityx(j) = -velocityx(j);
velocityx(j) = -velocityy(j);
end
Also, I think you want to end your loop of position updates before entering the collision code. E.g.,
for i=1:1:N
x(i) = x(i)+velocityx(i)*dt;
y(i) = y(i)+velocityy(i)*dt;
end % <-- ADDED THIS LINE
%Collisions
for i=1:1:N
  4 Kommentare
Salike Anuraag
Salike Anuraag am 24 Mai 2017
Hi I would be glad if you can send me the modified code, I am working on the same particle-particle collision but here we are using superquadric structures which are complex in nature. I can go through your program as a reference, My email address is salikeanuraag@gmail.com. Thanks in advance
Animesh Biswas
Animesh Biswas am 29 Mai 2022
Could you please post the modified version of the code. I have similar problem doing the collision simularion.
Thank you

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu General Applications finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by