Hello, so basically I want to code beads which move left and write independently to each other unless if they collide. I haven't written the collision algorithm so far, but right now i've gotten a problem with my plotting: When running my code, instead of having 2 balls simply move along the screen (left and right), i get the balls leaving a trail behind them while moving (as seen in attached pic.)
Part of my code is this (which i believe causes the problem since the other is just sorting out):
for t = 0:500 for n=1:(nMolecules-1) %MMCollision check
m=n+1; endfor n = 1:nMolecules % Wall collision check (Only 1st and last molecules)
if (Molecule(1,n)<= 0.5*Molecule(3,n)) Molecule(4,n) = -Molecule(4,n); %Elastic Collision
elseif (Molecule(1,n)>= Right-0.5*Molecule(3,n)) Molecule(4,n) = -Molecule(4,n); %Elastic Collision
end Molecule(1,n) = Molecule(1,n) + Molecule(4,n);endhold onfor n=1:nMoleculesxlim([Left Right]);ylim([Down Up]);if (Molecule(2,n)==4) %Drawing different balls based on rng
h = plot(Molecule(1,n),Molecule(5,n),'o','Color','black','MarkerFaceColor','red','MarkerSize',100*Molecule(3,n)); elseh = plot(Molecule(1,n),Molecule(5,n),'o','Color','black','MarkerFaceColor','blue','MarkerSize',100*Molecule(3,n)); endendhold offdrawnowend
Basically my code randomly attributes a size to a ball and its "properties" like mass and radius data (not in provided code). The provided image shows both being of the same size and yes, there's supposed to be only 2 balls on the screen, instead of 10+
Anybody can help me?
Best Answer