How do get the particles to bounce properly?
Show older comments
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
Accepted Answer
More Answers (0)
Categories
Find more on Operating Points in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!