Skip to content
Tutorials Center
My learning

Topics

Lesson 43 of 47 ยท Projects and Practice

Project: gravity and the N-body problem

Simulate Kepler orbits and the three-body figure-eight with Euler, velocity Verlet and RK4, and learn to audit energy and angular momentum.

Newton solved the problem of two bodies attracted by gravity in the 1680s: ellipses, hyperbolas and parabolas. Add a third body and no general closed-form solution exists. The "three-body problem" occupied Euler, Lagrange, Poincarรฉ and many others, and it is one of the places where computers changed physics: we cannot write the answer, but we can calculate it. In this project you will simulate gravity with three different integrators and use conservation laws to decide which results to trust.

Left: a Kepler orbit with Euler spiralling outward and Verlet staying closed. Right: the figure-eight orbit of three equal masses
The same law of gravity, two integrators: only one keeps the orbit closed. On the right, three bodies chasing each other along a single figure-eight.

What you will build

  • A general N-body acceleration function.
  • Three integrators: explicit Euler, velocity Verlet and RK4.
  • Energy and angular-momentum monitors.
  • A reproduction of the figure-eight orbit found by Cristopher Moore (1993) and proven to exist by Chenciner and Montgomery (2000).

Key terms

Newton's law of gravitation: F = G mโ‚ mโ‚‚ / rยฒ, attractive, along the line joining the masses.

Kepler orbit: the elliptical path of one body around another; period T = 2ฯ€ โˆš(aยณ/(GM)).

Conserved quantity: total energy, momentum and angular momentum of an isolated system.

Softening: replacing rยฒ by rยฒ + ฮตยฒ in the force to avoid huge forces during close encounters.

Choreography: a periodic orbit in which all bodies follow the same curve, shifted in time.

The model

For bodies i = 1โ€ฆN with masses m_i at positions r_i:

Text
a_i = G ยท ฮฃ_{j โ‰  i} m_j (r_j โˆ’ r_i) / |r_j โˆ’ r_i|ยณ

In natural units we take G = 1. For a central mass M = 1, a body at distance r = 1.5 (the aphelion) with tangential speed v = โˆš(2/r โˆ’ 1/a) where a = 1 follows an ellipse with semi-major axis 1, eccentricity 0.5 and period 2ฯ€ โ‰ˆ 6.28. The energy per unit mass is โˆ’1/(2a) = โˆ’0.5, and the angular momentum per unit mass is rยทv.

Three integrators compared

  • Explicit Euler (x += ฮ”tยทv, v += ฮ”tยทa): first order, and for orbits it always adds energy, so the orbit spirals outward.
  • Velocity Verlet: as in chapter 6, symplectic. Energy oscillates around the true value and the orbit shape is preserved, but the ellipse slowly rotates (a numerical precession).
  • RK4: fourth order. Very accurate for a few orbits but not symplectic; for thousands of orbits, the energy drifts.

A rule of thumb: for long orbital integrations choose a symplectic scheme (or a high-order one such as Yoshida or Wisdom-Holman); for short high-precision runs choose RK4 or an adaptive method.

The figure-eight

In 1993 Moore found by numerical search a periodic orbit of three equal masses chasing each other along a figure-eight curve; Chenciner and Montgomery proved its existence in 2000. With G = 1 and masses 1, one set of initial conditions is

Text
rโ‚ = โˆ’rโ‚‚ = (โˆ’0.97000436, 0.24308753),   rโ‚ƒ = (0, 0)
vโ‚ƒ = (โˆ’0.93240737, โˆ’0.86473146),        vโ‚ = vโ‚‚ = โˆ’vโ‚ƒ/2
period T โ‰ˆ 6.32591398

The bodies return to their starting positions after one period. The orbit is stable, which is why it could be found numerically, but it is not forgiving: a small nudge grows steadily, as the last experiment below shows.

Common misconceptions

  • "Gravity is chaotic, so the integrator does not matter." Chaos limits long-term prediction of one path; but a poor integrator destroys even the statistical features, such as bound orbits staying bound.
  • "A smaller step always fixes a spiralling orbit." It slows the spiral. Euler keeps adding energy at any step size.
  • "Conserved energy proves the orbit is right." Energy and angular momentum are necessary tests, not sufficient ones; compare with an analytic case like the Kepler period.
  • "The figure-eight is just a lucky picture." It is an exact periodic solution of Newton's equations, and it is stable against small perturbations.

Try it

We compare the three integrators on the eccentric Kepler orbit for 10 revolutions with ฮ”t = 0.01. The program prints relative energy and angular-momentum errors, and the final distance from the center.

Python Euler, Verlet and RK4 on a Kepler orbit

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Now the figure-eight. We integrate three equal masses for one period with RK4, check that the bodies return home, and plot the curve.

Python The figure-eight choreography

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Finally, how delicate is the orbit? We nudge the position of body 1 by 1e-4 and by 1e-2, run five periods, and report how far the bodies have wandered from the unperturbed motion.

Python Perturbing the figure-eight

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Why does the explicit Euler method make an orbit spiral outward?

    Euler's local error always has the same sign for orbital motion, so the energy keeps rising.
  2. 2. For a central mass M = 1 and orbit with semi-major axis a = 1 (G = 1), the period is...

    T = 2ฯ€โˆš(aยณ/GM) = 2ฯ€.
  3. 3. Which integrator is the best choice for millions of orbits?

    Symplectic methods keep the energy error bounded over very long times.
  4. 4. What is a gravitational softening length used for?

    Replacing rยฒ by rยฒ + ฮตยฒ limits the force when two bodies nearly collide.
  5. 5. In the figure-eight orbit, the three bodies...

    A choreography is a periodic orbit where all bodies follow the same curve.

Summary

  • Gravity: a_i = G ฮฃ m_j (r_j โˆ’ r_i)/|r_j โˆ’ r_i|ยณ; monitor energy and angular momentum.
  • Euler adds energy and spirals out; velocity Verlet stays closed over long runs; RK4 is very accurate for short ones.
  • The figure-eight is an exact periodic three-body solution; check that every body returns home after T โ‰ˆ 6.326.
  • Tests with known answers (Kepler period, conservation) beat a pretty picture.

Next: Practice set