22 views (last 30 days)

I want to create a code that simulates multiple particles coliding with the walls of a box but I dont want the particles to colide with each other.

I alredy made a script for one particle, but Im having trouble making one for multiple ones.

Can someone help me please?

So far I got this:

Here's the main script:

a=20;

b=10;

R=0.5;

dt=0.1;

np=3; %Number of particles

rx=a*rand(1,np);

ry=b*rand(1,np);

r=[rx;ry];

v=5*(2*rand(2,np)-1);

tmax=100;

t=0;

while (t<tmax)

for i=1:np

deltat(i,1)=(R-r(1,i))/v(1,i); deltat(i,2)=(a-R-r(1,i))/v(1,i);

deltat(i,3)=(R-r(2,i))/v(2,i); deltat(i,4)=(b-R-r(2,i))/v(2,i);

if deltat(i,1)<=4*eps

deltat(i,1)=1e6;

end

if deltat(i,2)<=4*eps

deltat(i,2)=1e6;

end

if deltat(i,3)<=4*eps

deltat(i,3)=1e6;

end

if deltat(i,4)<=4*eps

deltat(i,4)=1e6;

end

end

deltat_wall=[deltat(:,1),deltat(:,2),deltat(:3),deltat(:,4)]; %must be a matrix of np particles and 4 columns

[deltat, column]=min(deltat_wall,[],2);

[deltat,particle]=min(deltat);

wall=column(particle);

animacion(r,v,a,b,R,dt,deltat) % the function is just to plot the box and the posicion of the particles

r=r+v*deltat;

ep=[[1;0],[-1;0],[0;1],[0;-1]];

v=v-2*dot(v,ep(:,wall))*ep(:,wall);

t=t+deltat;

end

Here´s the function:

function []= animacion(r,v,a,b,R,dt,deltat)

x1=0;

x2=a;

y1=0;

y2=b;

side_a=[x1,x2,x2,x1,x1];

side_b=[y1,y1,y2,y2,y1];

for t=0:dt:deltat

plot(side_a,side_b,'b-','Linewidth',2)

hold on

plot(r(1,:),r(2,:),'ko','MarkerFaceColor','k', 'MarkerSize',28*R)

xlim([-10,30])

ylim([-10,20])

drawnow

hold off

r=r+v*dt;

end

end

Geoff Hayes
on 27 Mar 2020

If the last step is to just calculate a new speed after the shock then this would apply to a single particle only. So we would modify the velocity of just that one particle instead of trying to modify the velocities for each. Try changing

v=v-2*dot(v,ep(:,wall))*ep(:,wall);

to

v(:, particle) = v(:, particle) - 2*dot(v(:, particle),ep(:,wall))*ep(:,wall);

where the only difference is how we index into v.

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 2 Comments

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/513404-help-ploting-different-particles-in-a-box#comment_816575

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/513404-help-ploting-different-particles-in-a-box#comment_816575

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/513404-help-ploting-different-particles-in-a-box#comment_816584

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/513404-help-ploting-different-particles-in-a-box#comment_816584

Sign in to comment.