0

so i need to generate 10 particles per step in random positions inside the circle to be moving in random directions, and when it hits the wall of the main circle it bounces back and without change in velocity.

I defined the large circle:

yc=0;
rc=5;
ang_c=0:0.01:2*pi; 
xpc=rc*cos(ang_c);
ypc=rc*sin(ang_c);
plot(xc+xpc,yc+ypc,'k'); 

and for now just plotting one particle:

r=0.05;
x=rand;
y=rand;

ang=0:0.01:2*pi; 
xp=r*cos(ang);
yp=r*sin(ang);
plot(x+xp,y+yp,'b'); 

now how to i get the particle to move around in the circle?

13python
  • 11
  • 3

1 Answers1

0

Here's an outline of the algorithm

  • Define a circle (origin, radius)
  • Create a nParticles-by-2 array particlePositions. Fill it with random coordinates that lie inside the circle
  • Plot the particle positions, keep the handle returned by plot
  • Create a nParticles-by-2 array particleSpeeds. Fill it with random speed vectors (ie. x- and y- components). Make sure the norm of the vectors is much smaller than the radius of the circle, at least initially.
  • Write a loop that updates particle positions:
    • particlePositions = particlePositions + particleSpeeds;
    • For any particlePosition would lie outside the circle, calculate where the particle intersected the circle, and reflect the particleSpeed for that particle. This takes a bit of fiddling with trigonometry.
    • Update the plot: set(plotHandle,'xData',particlePositions(:,1),'yData',particlePositions(:,2))
Jonas
  • 74,690
  • 10
  • 137
  • 177