Plotting a moving particle in a box with elastic collisions

Hi, I'm having trouble with this problem:
Develop a Matlab model for the trajectory of a single particle with velocity v = {vx, vy} in a two dimensional square box. Treat the collisions with the walls of the box as elastic, so that when a particle collides with a wall, the sign of its velocity component in the direction normal to the wall is inverted.
I understand the question but I'm just not very good at coding and I have no idea how to make the relevant velocity component switch signs when the particle hits a wall... Please help!
This is what I've come up with so far: (it doesn't work but I can't see why)
n = 100;
x = zeros(1,100); y = zeros(1,100);
v_x = zeros(1,100); v_y = zeros(1,100);
while i=2:n
x(i)=x(i-1)+v_x(i);
y(i)=y(i-1)+v_y(i);
if x(i)<-5
v_x(i)=-v_x(i-1);
elseif x(i)>5
v_x(i)=-v_x(i-1);
elseif y(i)<-5
v_y(i)=-v_y(i-1);
elseif y(i)>5
v_y(i)=-v_y(i-1);
else
v_x(i)=v_x(i-1);
v_y(i)=v_y(i-1);
end
plot(x(i),y(i),'or','MarkerSize',5,'MarkerFaceColor','r')
axis([-5 5 -5 5])
pause(0.1)
end

 Accepted Answer

Hi, there is no initial velocity, the particle will keep staic.
clear;clc;
n = 100;
x = zeros(1,100); y = zeros(1,100);%current position
v_x = zeros(1,100); v_y = zeros(1,100);%current velocity
v0_x = 1; v0_y = 1.5;%initial velocity
v_x(1) = v0_x; v_y(1) = v0_y;
clf;figure(1);
for i=2:n
x(i)=x(i-1)+v_x(i-1);
y(i)=y(i-1)+v_y(i-1);
if x(i)<-5
x(i)=-10-x(i);
v_x(i)=-v_x(i-1);
v_y(i)=v_y(i-1);
elseif x(i)>5
x(i)=10-x(i);
v_x(i)=-v_x(i-1);
v_y(i)=v_y(i-1);
elseif y(i)<-5
y(i)=-10-y(i);
v_x(i)=v_x(i-1);
v_y(i)=-v_y(i-1);
elseif y(i)>5
y(i)=10-y(i);
v_x(i)=v_x(i-1);
v_y(i)=-v_y(i-1);
else
v_x(i)=v_x(i-1);
v_y(i)=v_y(i-1);
end
plot(x(i),y(i),'or','MarkerSize',5,'MarkerFaceColor','r');hold off;
axis([-5 5 -5 5])
pause(0.1)
end

5 Comments

Thank you so much, that's perfect!
Just one question - how did you get the following lines and what do they mean?
x(i)=-10-x(i); x(i)=10-x(i); y(i)=-10-y(i); y(i)=10-y(i);
For example,
if x(i)>5, which means the particle have touched the right boundary, it will reflect from the wall just like the light reflect from a mirror. So we find (x_new+x_old)/2=5.
Oh that makes sense, thanks :)
Hi again, I now have to make this code simulate lots of particles at the same time (they don't interact with each other) and was wondering if you had any suggestions for how to do this? I think I need to make the position and velocity vector elements represent different particles rather than a single particle at different times, but I don't know how to only change the position and velocity of an individual particle when it collides with a wall if the particles are all in one vector. I also don't know how to make the simulations play at the same time. Maybe I need to change my previous script to a function...? Can you please help with this?
Hi, I hope it's not too late to answer. I try to replace vector with matrix. I think it will work well.
clear;clc;
n = 100;%(total time)
m = 2;%(particle numbers)
x = zeros(m,100); y = zeros(m,100);
%current position
%x(i,j) represents the position of the ith particle at the jth loop.
v_x = zeros(m,100); v_y = zeros(m,100);%current velocity
v0_x = [1;1]; v0_y = [1;2];%initial velocity size(v0_x)-->[m,1]
v_x(:,1) = v0_x; v_y(:,1) = v0_y;
clf;figure(1);
for i=2:n
x(:,i)=x(:,i-1)+v_x(:,i-1);
y(:,i)=y(:,i-1)+v_y(:,i-1);
...
end

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!