Help with a code writing

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?

6 Comments

you can post a snapof ur code or your script, it will be helpfull to tell u the changes. Where to add what...
PS
PS on 27 Mar 2020
Edited: PS on 27 Mar 2020
OK sorry I forgot about it thank you.
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:1:np
deltat(1,i)=(R-r(1,i))/v(1,i); deltat(2,i)=(a-R-r(1,i))/v(1,i);
deltat(3,i)=(R-r(2,i))/v(2,i); deltat(4,i)=(b-R-r(2,i))/v(2,i);
if deltat(1,i)<=4*eps
deltat(1,i)=1e6;
end
if deltat(2,i)<=4*eps
deltat(2,i)=1e6;
end
if deltat(3,i)<=4*eps
deltat(3,i)=1e6;
end
if deltat(4,i)<=4*eps
deltat(4,i)=1e6;
end
deltat_wall=[deltat(1,i),deltat(2,i),deltat(3,i),deltat(4,i)];
[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
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
Hope its better now
See this question
PS
PS on 27 Mar 2020
Thank you for your answer but I still cannot understand how to do it for more particles using my script
I can't run your script. I have some errors. Do you have errors?
PS
PS on 27 Mar 2020
yes I have.If you take the cicle for and the i's on deltat you should see the animation with one particle.

Sign in to comment.

Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Asked:

PS
on 27 Mar 2020

Commented:

PS
on 27 Mar 2020

Community Treasure Hunt

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

Start Hunting!