Leapfrog integration with multiple particles

3 views (last 30 days)
Hi all i'm new to programming and I wanted to ask a quick question: Consider the following leapfrog integration for one particle:
time=100;
steps=1000000;
dt=time/(steps-1);
v0=5
theta=2*pi*rand(1);
vx(1)=v0*sin(theta);
vy(1)=v0*cos(theta);
t(1)=0;
x(1)=50;
y(1)=50;
for i=1:steps-1
t(i+1)=t(i)+dt;
x(i+1)=x(i)+vx(1)*dt;
y(i+1)=y(i)+vy(1)*dt;
end
How do I increase the number of particles in the leapfrog integration? eg 1000? Thanks in advance!
Some info (edited after request): Ok so, basically the particle starts at time t=0, at position x,y and at velocity vx,vy.
The simulation lasts for 100 seconds and consists in a million step. Leapfrog integration, in this case, consists in the for loop that updates the particle position every 100/1000000.
The problem is that i dont know how to scale this for loop for multiple particles. Thanks!
  2 Comments
Geoff Hayes
Geoff Hayes on 8 Sep 2018
Frederico - how is the single particle used in the above code? I guess some background on leapfrog integration would help. :)
AFHG
AFHG on 8 Sep 2018
Edited: AFHG on 8 Sep 2018
Ok so, basically the particle starts at time t=0, at position x,y and at velocity vx,vy.
The simulation lasts for 100 seconds and consists in a million step. Leapfrog integration, in this case, consists in the for loop that updates the particle position every 100/1000000.
The problem is that i dont know how to scale this for loop for multiple particles. Thanks!

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 8 Sep 2018
Edited: Star Strider on 8 Sep 2018
This works (with a much smaller value for ‘steps’):
time=100;
steps=1000000;
% steps = 100;
dt=time/(steps-1);
v0=5;
NrParticles = 1000;
theta=2*pi*rand(1,NrParticles);
vx=v0*sin(theta);
vy=v0*cos(theta);
t(1)=0;
x(1,1:NrParticles)=50;
y(1,1:NrParticles)=50;
for k = 1:NrParticles
for i=1:steps-1
t(i+1)=t(i)+dt;
x(i+1,k+1)=x(i,k)+vx(i)*dt;
y(i+1,k+1)=y(i,k)+vy(i)*dt;
end
end
figure
hold all
for k1 = 1:NrParticles
plot(x(:,k1), y(:,k1))
end
hold off
You will likely have to experiment with your code. I changed it slightly to use different values for ‘vx’ and ‘vy’ in the loop.
EDIT Corrected error in ‘k’ loop.
  12 Comments
AFHG
AFHG on 8 Sep 2018
This was the correct loop, btw:
e
for k = 1:NrParticles
for i=1:steps-1
t(i+1)=t(i)+dt;
x(i+1,k)=x(i,k)+vx(1,k)*dt;
y(i+1,k)=y(i,k)+vy(1,k)*dt;
end
end
Star Strider
Star Strider on 8 Sep 2018
As always, my pleasure.
I was not certain how to scale up ‘vx’ and ‘vy’.

Sign in to comment.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!