Hi,
It seems you are trying to make changes in the given code to simulate a plastic collision and add an option to give a solution for both scenarios.
To do so, you can add a conditional statement in the "while" loop when the velocity for particles is calculated. In the conditional, calculate the velocity of the particle based on the type of collision given by the user. To simulate particles for plastic collision, you can make use of the momentum conservation equation to find velocity:
v = (m1u1 + m2u2)/(m1+m2)
Refer to the modified code below for a better understanding:
%%%collision between two particles%%%
%vid = VideoWriter('myVideo.avi');
%open(vid)
%The values of one of the particles
x1 = -8; y1 = 0; v1x = 3;
v1y = 0; m1 = 2;
% The values of the second particle.
t = 0; dt = 0.5; x2 = 0; y2 = 0;
v2x = 0; v2y = 0; m2 = 1;
% Collision type ('elastic' or 'plastic')
collisionType = 'plastic'; % Change this to 'elastic' for elastic collision
% t=0, plot position of particles
subplot(3, 1, 1)
r1 = rectangle('Position', [x2, y2, 1, 1], 'FaceColor', 'g', 'Curvature', [1, 1]); % one particle
r2 = rectangle('Position', [x1, y1, 1, 1], 'FaceColor', 'b', 'Curvature', [1, 1]); % second particle
axis([-10 10 -10 10]);
title('The Collision')
grid on
% The particles will start moving:
while t < 100
% Update previous values
v1xpre = v2x; v2xpre = v1x; v1ypre = v2y; v2ypre = v1y;
% If the distance between the two particles is greater than 1:
if sqrt((x1 - x2)^2 + (y1 - y2)^2) > 1
x2 = x2 + (v2x * dt);
x1 = x1 + (v1x * dt);
y2 = y2 + (v2y * dt);
y1 = y1 + (v1y * dt);
else % If the distance between the two particles is lower than 1, so the particles collide
if strcmp(collisionType, 'elastic')
% Elastic collision equations
v2x = ((m2 - m1) / (m2 + m1)) * v1xpre + ((2 * m1) / (m2 + m1)) * v2xpre;
v2y = ((m2 - m1) / (m2 + m1)) * v1ypre + ((2 * m1) / (m2 + m1)) * v2ypre;
v1x = ((m1 - m2) / (m2 + m1)) * v2xpre + ((2 * m2) / (m2 + m1)) * v1xpre;
v1y = ((m1 - m2) / (m2 + m1)) * v2ypre + ((2 * m2) / (m2 + m1)) * v1ypre;
elseif strcmp(collisionType, 'plastic')
% Plastic collision equations
vFinalX = (m1 * v1xpre + m2 * v2xpre) / (m1 + m2);
vFinalY = (m1 * v1ypre + m2 * v2ypre) / (m1 + m2);
v1x = vFinalX;
v2x = vFinalX;
v1y = vFinalY;
v2y = vFinalY;
end
% Update positions after collision
x2 = x2 + (v2x * dt);
x1 = x1 + (v1x * dt);
y2 = y2 + (v2y * dt);
y1 = y1 + (v1y * dt);
end
% The movement that the particles will move after the collision with the 'walls':
if x2 + dt * v2x > 10
v2x = -v2x;
end
if x1 + dt * v1x > 10
v1x = -v1x;
end
if x2 + dt * v2x < -10
v2x = abs(v2x);
end
if x1 + dt * v1x < -10
v1x = abs(v1x);
end
if y2 + dt * v2y > 10
v2y = -v2y;
end
if y1 + dt * v1y > 10
v1y = -v1y;
end
if y2 + dt * v2y < -10
v2y = abs(v2y);
end
if y1 + dt * v1y < -10
v1y = abs(v1y);
end
% Update the position of the rectangles
set(r1, 'Position', [x2, y2, 0.5, 2]);
set(r2, 'Position', [x1, y1, 0.5, 2]);
% Plot the displacement of the particles
subplot(3, 1, 2)
plot(t, x2, 'go', t, x1, 'bs')
axis([0 100 -10 10])
xlabel('Time')
ylabel('Position-X')
grid on
title('Displacement of the particles')
% Plot the velocity of the particles
subplot(3, 1, 3)
plot(t, v2x, 'go', t, v1x, 'bs')
axis([0 100 -10 10])
yticks(-10:2.5:10)
xlabel('Time')
ylabel('Speed')
title('Velocity of the particles')
grid on
drawnow
% Update time
t = t + dt;
% frame = getframe(gcf);
% writeVideo(vid, frame);
end
% close(vid);