Simulating orbits using laws of physics

若如初见. 提交于 2019-11-30 20:08:33

You must give the planet initial velocity v = (vx, vy, vz) tangent to the desired orbit. If the position of the sun is s and planet is p, then there is always a force acting between the two: the one on the planet points toward the sun, vector t=(s - p) and vice versa. The magnitude of this force is g Ms Mp / (t dot t), where "dot" is the dot product, g is the standard acceleration due to gravity, and Ms, Mp are the respective masses.

If you are doing a detailed model where all bodies can exert pull on all other bodies, then the algorithm is to accumulate all the pairwise forces to get a single resultant force vector acting on each body (planet or sun). Otherwise you might settle for an approximation where only the sun pulls on planets and other forces are deemed too small to matter.

So the algorithm is:

Choose dt, the time step, a small interval.
Set initial positions and velocities 
    (for planets, velocity is tangent to desired orbit. Sun has velocity zero.)
loop
  Accumulate all forces on all bodies with current positions.
  For each body position=p, velocity=v, net resultant force=f, mass=m, 
    update its velocity: v_new = v + f / m dt
    update position p_new = p + 0.5 * (v + v_new) 
    v = v_new; p = p_new
  Render
end loop

As has been mentioned, Euler is simple but requires a very small time step to get even reasonable accuracy. Sometimes you can introduce just a tiny bit of drag in the system (multiply velocity by a factor just a tad below 1) to keep things stable where otherwise they blow up.

Consult the project "Moving stars around", there is an old C and a modernized Ruby version. (And now a C++ version?)

Short advice: Eulers methods are bad for energy conservation. explicit Euler increases energy, implicit Euler reduces energy. Just check it on the phase space picture of the harmonic oscillator y''+y=0.

Use symplectic integrators, the most simple and famous is the Leapfrog or Verlet method that already Newton used to reason about planetary movement.

You could try Runge Kutta 4, but there will still be some "drift" in the orbits. Keeping the total energy of the system constant is needed to stabilize the system, but I'm not sure how this is done. A better method for celestial mechanics is symplectic integrator.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!