Loading the package
using Plots
gr()
Plots.GRBackend()
Using the difference equation to demonstrating how to solve the single particle problem
Initial the parameters
x0=0.5;y0=7.; #The initial position
Vx0=1;Vy0=2; #The initial velocity
m=2; #The mass of the particle
g=-9.8; #The acceleration
Fx(x,y)=0;Fy(x,y)=g.*m; #The Force in different direction
Δt=0.1;t=0:Δt:1; #The simulation time
xt=zeros(1,length(t)+1);yt=zeros(1,length(t)+1); #Initialization of the position vs time xt(t) and yt(t)
xt[1]=x0;yt[1]=y0;
Vxt=zeros(1,length(t)+1);Vyt=zeros(1,length(t)+1); #Initialization of the position vs time xt(t) and yt(t)
Vxt[1]=Vx0;Vyt[1]=Vy0;
We can get the trajectory by numerical way
for i=1:length(t)
ax=Fx(xt[i],yt[i])/m; ay=Fy(xt[i],yt[i])/m; #The acceleration
Vxt[i+1]=Vxt[i]+ax*Δt;Vyt[i+1]=Vyt[i]+ay*Δt; #The velocity in next step
xt[i+1]=xt[i]+Vxt[i]*Δt+ax*Δt^2/2; #The numerical trajectory
yt[i+1]=yt[i]+Vyt[i]*Δt+ay*Δt^2/2;
end
Meanwhile, we can also obtain the analytic solution
xp(t)=x0+Vx0.*t;yp(t)=y0+Vy0.*t+g.*t.^2/2; #Numerical solution
Comparing the numerical and analytic solution
plot(xt',yt',w=2,c=:black)
plot!(xp,yp,0,1,c=:blue)
The friction has the form $\vec{F}=\kappa \vec{V}$
Initial the parameter
x0=0.5;y0=7.; #The initial position
Vx0=1;Vy0=2; #The initial velocity
m=2; #The mass of the particle
g=-9.8; #The acceleration
κ=-0.8; #The friction coefficient
Fx(vx)=κ*vx;Fy(vy)=m*g+κ*vy; #The Force in different direction
Δt=0.01;t=0:Δt:1; #The simulation time
xt=zeros(1,length(t)+1);yt=zeros(1,length(t)+1); #Initialization of the position vs time xt(t) and yt(t)
xt[1]=x0;yt[1]=y0;
Vxt=zeros(1,length(t)+1);Vyt=zeros(1,length(t)+1); #Initialization of the position vs time xt(t) and yt(t)
Vxt[1]=Vx0;Vyt[1]=Vy0;
We can get the trajectory by numerical way
for i=1:length(t)
ax=Fx(Vxt[i])/m; ay=Fy(Vyt[i])/m; #The acceleration
Vxt[i+1]=Vxt[i]+ax*Δt;Vyt[i+1]=Vyt[i]+ay*Δt; #The velocity in next step
xt[i+1]=xt[i]+Vxt[i]*Δt+ax*Δt^2/2; #The numerical trajectory
yt[i+1]=yt[i]+Vyt[i]*Δt+ay*Δt^2/2;
end
Comparing the numerical and analytic solution
plot(xt',yt',w=2,c=:black)
plot!(xp,yp,0,1,c=:blue)
Numerically demonstration of the eclipsal motion under universal force
Initial the parameter
x0=1;y0=1; #The initial position
Vx0=1;Vy0=0; #The initial velocity
m=2; #The mass of the particle
Fx(x,y)=-m*x/(sqrt(x.^2+y.^2))^3;Fy(x,y)=-m*y/(sqrt(x.^2+y.^2))^3; #The Force in different direction
Δt=0.0002;t=0:Δt:5; #The simulation time
xt=zeros(1,length(t)+1);yt=zeros(1,length(t)+1); #Initialization of the position vs time xt(t) and yt(t)
xt[1]=x0;yt[1]=y0;
Vxt=zeros(1,length(t)+1);Vyt=zeros(1,length(t)+1); #Initialization of the position vs time xt(t) and yt(t)
Vxt[1]=Vx0;Vyt[1]=Vy0;
We can get the trajectory by numerical way
for i=1:length(t)
ax=Fx(xt[i],yt[i]); ay=Fy(xt[i],yt[i]); #The acceleration
Vxt[i+1]=Vxt[i]+ax*Δt;Vyt[i+1]=Vyt[i]+ay*Δt; #The velocity in next step
xt[i+1]=xt[i]+Vxt[i]*Δt+ax*Δt^2/2; #The numerical trajectory
yt[i+1]=yt[i]+Vyt[i]*Δt+ay*Δt^2/2;
end
Comparing the numerical and analytic solution
plot!(xt',yt',w=2,c=:black,lab="\\Delta t=0.0002")
Try to demonstrate the coriolic force
θ(t)=0;r(t)=v*t;
v=0.5;
plot(θ,r,0,2π,proj=:polar,w=2,c=:red)
θ₂(t)=θ(t)-ω*t;r₂(t)=r(t);
ω=2;
plot(θ₂,r₂,0,2π,proj=:polar,w=2,c=:red)