All projects

Physics simulations

Three-Body Problem

Three bodies that attract one another through Newtonian gravity have no general solution in closed form, and their motion is generally chaotic. This page integrates the planar problem numerically. It follows eleven classic configurations, from the rotating line of Euler and the rotating triangle of Lagrange to the figure-eight and the other periodic orbits found by computer search, and it lets you release three bodies of your own.
  • Celestial mechanics
  • Chaos
  • Numerical integration

In simple words

Everything in space pulls on everything else. Two things, like the Earth and the Moon, go round each other in the same neat loop forever, and you could write their whole future on a napkin.

Now add a third. Everyone pulls on everyone, and every pull changes all the others. Can you still say where they will be in a hundred years? Will they dance forever, or will someone get thrown out? People have chased that question for more than three hundred years, and the answer turned out to be stranger than anyone expected.

Three-Body Problem SimulationOpen on its own

Why three bodies are hard

Newton solved the motion of two bodies completely. Each one moves on a conic section about their common centre of mass, and the orbit can be written down for all time. Adding a third body destroys this simplicity. In 1887 Heinrich Bruns proved that the problem has no algebraic integrals beyond the ten classical ones [1], and in his prize memoir of 1890 Henri Poincaré showed that its trajectories can depend so sensitively on where they start that no simple formula could ever capture them [2]. That memoir is often counted as the beginning of chaos theory. Karl Sundman did find a convergent series solution in 1912 [3], but it converges so slowly that it is of no practical use, and the problem is studied today by numerical integration, as on this page.

One way to see the difficulty is to count. In the plane each body has a position and a velocity, twelve numbers in all. The conserved quantities fix the motion of the centre of mass and the total angular momentum, and once they are used the planar problem still has three degrees of freedom. A mechanical problem can be solved in closed form when it has an independent conserved quantity for every degree of freedom, and here only the energy is left. Bruns and Poincaré showed that no further conserved quantities of the usual kind exist, so nothing ties the remaining motion down.

Energy alone does not keep the three together either. For two bodies a negative total energy means a bound orbit, an ellipse that repeats forever. For three bodies it does not. The potential energy of a pair, −mimj/rij-m_i m_j/r_{ij}, has no lower limit as the pair moves closer, so a pair that falls into a tighter orbit can release any amount of energy. The third body can carry that energy away as kinetic energy and escape to infinity, while the total stays negative and constant. What a negative total does forbid is that all three end up free of one another, because three separated bodies flying apart have a total energy of zero or more. The typical end of a bound triple is therefore a binary and an escaper: one body leaves for good, and the two that remain are bound more tightly than before.

Close encounters are also where the chaos comes from. When two bodies swing past each other at close range, a tiny change in their approach changes the direction in which they separate, and the difference grows with every new encounter. Two runs that start a millionth apart soon look nothing alike. This sensitive dependence on initial conditions is the hallmark of chaos, and the three-body problem is where Poincaré first met it. It does not stop the motion from being computed. It means that any computation, however careful, eventually loses track of the true orbit, so the useful question is not where a body will be in a thousand years but which kinds of behaviour are possible. The magnetic pendulum shows the same sensitivity in a system that friction eventually brings to rest.

The model and how it is computed

We consider three point masses mim_i moving in a plane, in units where the gravitational constant is G=1G=1. With positions ri\mathbf r_i, the equations of motion read

r¨i=∑j≠imj rj−ri∣rj−ri∣3,i=1,2,3.\ddot{\mathbf r}_i=\sum_{j\neq i} m_j\,\frac{\mathbf r_j-\mathbf r_i}{\lvert\mathbf r_j-\mathbf r_i\rvert^{3}},\qquad i=1,2,3.

All presets use unit masses, except the hierarchical triple, whose third body has m3=0.1m_3=0.1. The motion conserves the total momentum, which we set to zero by working in the centre-of-mass frame, the angular momentum L=∑imi ri×viL=\sum_i m_i\,\mathbf r_i\times\mathbf v_i, and the energy

E=12∑imi ∣vi∣2−∑i<jmimj∣ri−rj∣.E=\frac12\sum_i m_i\,\lvert\mathbf v_i\rvert^2-\sum_{i<j}\frac{m_i m_j}{\lvert\mathbf r_i-\mathbf r_j\rvert}.

The simulation integrates these equations with the classical fourth-order Runge-Kutta method. Close encounters are again the difficulty. When two bodies nearly collide their mutual acceleration grows like the inverse square of their distance, and a fixed step would overshoot, so the step is shortened near encounters,

h=min⁡ ⁣(0.005,  0.02 rmin⁡3/2),h=\min\!\left(0.005,\;0.02\,r_{\min}^{3/2}\right),

where rmin⁡r_{\min} is the smallest distance between two bodies and rmin⁡3/2r_{\min}^{3/2} is, up to a constant, the free-fall time of a pair at that separation. The physics runs on its own clock, independent of the refresh rate of your screen, and the readout ∣ΔE/E0∣\lvert\Delta E/E_0\rvert reports the relative drift of the energy as a running check on the integration. Over thirty time units it stays below 2×10−52\times10^{-5} for every preset.

The equations have no preferred scale. Stretching all lengths by a factor λ\lambda and all times by λ3/2\lambda^{3/2} maps one solution onto another,

r↦λ r,t↦λ3/2 t,v↦λ−1/2 v,\mathbf r\mapsto\lambda\,\mathbf r,\qquad t\mapsto\lambda^{3/2}\,t,\qquad \mathbf v\mapsto\lambda^{-1/2}\,\mathbf v,

which is Kepler's third law in another guise. Every orbit shown here therefore stands for a whole family of systems, from compact stellar triples to planets around their stars.

Inside the code

A few short pieces of the simulation do most of the work. The force on each body comes from a single pass over the three pairs. Each pair's force is computed once and added to both bodies with opposite signs, which is Newton's third law and halves the work.

// One pass over the three pairs. Each pair's force is computed once
// and applied to both bodies with opposite signs (Newton's third law).
for (let p = 0; p < 3; p++) {
  for (let q = p + 1; q < 3; q++) {
    const dx = s[2*q] - s[2*p], dy = s[2*q+1] - s[2*p+1];
    const r2 = dx*dx + dy*dy;
    const inv = G / (r2 * Math.sqrt(r2));        // G / r^3
    acc[2*p] += m[q] * dx * inv;  acc[2*p+1] += m[q] * dy * inv;
    acc[2*q] -= m[p] * dx * inv;  acc[2*q+1] -= m[p] * dy * inv;
  }
}

The step size is chosen inside the integrator from the closest pair, so the integration slows down only during close encounters and takes large steps the rest of the time.

// Advance by dtau in Runge-Kutta substeps that shrink near close
// encounters: never longer than 0.005, nor than 0.02 r_min^(3/2).
let rem = dtau;
while (rem > 1e-12) {
  const rmin = Math.max(minSep(y), 1e-6);
  const h = Math.min(rem, 0.005, 0.02 * rmin * Math.sqrt(rmin));
  rk4(deriv, y, h);
  rem -= h;
}

The animation never advances the physics by the time between two frames of the screen, because that time varies from one machine to another. Elapsed time is collected instead and spent in fixed ticks, so every screen computes exactly the same trajectory.

// The kit's animation loop: real time is collected and spent in fixed
// physics ticks, so a 60 Hz and a 144 Hz screen compute the same orbit.
acc += dtReal * speed;
while (acc >= fixedDt) {
  update(fixedDt);
  acc -= fixedDt;
}
render();

Finally, the initial conditions are moved into the centre-of-mass frame by subtracting the mass-weighted mean position and velocity. The total momentum is then zero to rounding error and the picture does not drift off the screen. In the free mode only the velocity is corrected, so that the bodies stay exactly where you placed them.

// Move to the centre-of-mass frame: subtract the mass-weighted mean
// position and velocity, so the total momentum is exactly zero.
const M = m[0] + m[1] + m[2];
for (let i = 0; i < 3; i++) {
  c.x += m[i] * r0[i].x;   c.vx += m[i] * v0[i].x;
  c.y += m[i] * r0[i].y;   c.vy += m[i] * v0[i].y;
}
for (let k = 0; k < 3; k++) {
  r[k] = keepPositions ? r0[k] : { x: r0[k].x - c.x / M, y: r0[k].y - c.y / M };
  v[k] = { x: v0[k].x - c.vx / M, y: v0[k].y - c.vy / M };
}

The orbits in the menu

Every preset is defined by its starting positions and velocities. On the first frame, and again after every Reset, each body carries an arrow for its velocity, scaled to the fastest of the three, and the arrows fade as the motion begins. The Velocity arrows control keeps them on throughout or turns them off.

The oldest exact solutions are the central configurations, in which the shape never changes and only its size and orientation evolve. Euler found the collinear family in 1767 [4] and Lagrange the equilateral family in 1772 [5]. Their speeds follow from balancing gravity against the centripetal force. For unit masses at the corners of an equilateral triangle of side aa, the two other bodies pull each one towards the centre with a force 3/a2\sqrt3/a^2, and each body moves on a circle of radius a/3a/\sqrt3. In the collinear arrangement the middle body rests at the centre and the outer ones, at distance dd from it, feel 1/d2+1/(2d)2=5/(4d2)1/d^2+1/(2d)^2=5/(4d^2). The circular speeds are therefore

v△=1a ,v∣=54d .v_{\triangle}=\sqrt{\frac{1}{a}}\,,\qquad v_{|}=\sqrt{\frac{5}{4d}}\,.

The presets use these speeds with a=6a=6 and d=1d=1. Given a different velocity, each body follows a Kepler ellipse instead and the shape breathes as it turns, which is what the two elliptic presets show. Neither family is stable for equal masses. Routh showed that the equilateral solution is linearly stable only when

27 (m1m2+m2m3+m3m1)<(m1+m2+m3)2,27\,(m_1m_2+m_2m_3+m_3m_1)<(m_1+m_2+m_3)^2,

which requires one mass to dominate the other two [6]. For three equal masses the left side is nine times larger than the right. The criterion is met by the Sun, Jupiter and the Trojan asteroids, which share the orbit of Jupiter sixty degrees ahead of it and behind it. The collinear solution is unstable for any masses. In the simulation the equal-mass shapes hold together for a while and then break apart, driven by nothing more than rounding errors in the last digits.

In 1993 Cristopher Moore found, by a numerical search, an orbit in which three equal masses chase one another around a single figure-eight curve, each a third of a period behind the next [7]. Alain Chenciner and Richard Montgomery proved its existence in 2000 [8], and the preset starts from the initial conditions given there,

r1=−r3=(−0.97000436,  0.24308753),r2=0,v1=v3=−12 v2=(0.46620369,  0.43236573).\mathbf r_1=-\mathbf r_3=(-0.97000436,\;0.24308753),\quad \mathbf r_2=0,\qquad \mathbf v_1=\mathbf v_3=-\tfrac12\,\mathbf v_2=(0.46620369,\;0.43236573).

Orbits in which all bodies share one curve are called choreographies. The figure-eight has zero angular momentum and a period of T≈6.326T\approx 6.326, and unlike most three-body orbits it survives small perturbations.

For two centuries only a handful of periodic orbits were known. In 2013 Milovan Šuvakov and Veljko Dmitrašinović searched the space of initial conditions systematically and found thirteen new ones for equal masses and zero angular momentum, which they named after the shapes they trace [9]. All of them start from the same symmetric arrangement, with the two outer bodies moving with the same velocity and the middle one with minus twice that velocity, so that the total momentum vanishes,

r1,2=(∓1, 0),r3=(0, 0),v1=v2=(vx, vy),v3=−2 (vx, vy).\mathbf r_{1,2}=(\mp1,\,0),\quad \mathbf r_3=(0,\,0),\qquad \mathbf v_1=\mathbf v_2=(v_x,\,v_y),\quad \mathbf v_3=-2\,(v_x,\,v_y).

A single pair (vx,vy)(v_x,v_y) selects the orbit. The menu contains six of them, with the values of Table I of Ref. [9]. Started from these values the simulation comes back to its initial state after the published period to within one part in a thousand.

Orbitvxv_xvyv_yPeriod TT
Butterfly I0.306890.125516.24
Moth I0.464440.3960614.89
Dragonfly0.080580.5888421.27
Moth III0.383440.3773625.84
Moth II0.439170.4529728.67
Bumblebee0.184280.5871963.53

The hierarchical triple is not taken from the literature. It is built here to show what astronomers usually find: an equal-mass binary on a circular orbit of separation 2.5, circled by a light body with m3=0.1m_3=0.1 on a circular orbit at six times that distance, each given its circular speed. Seen from so far away the inner pair acts almost like a single mass, and the light body moves on a nearly Keplerian orbit about it. Most triple stars in our Galaxy are arranged in this way, because only hierarchical triples survive for long.

Try it yourself

In the free mode you place the three bodies yourself and give each a velocity by dragging before you release it. The simulation removes the motion of the centre of mass and integrates from there. Can you build a pattern that lasts? Random starts rarely do. Within a few dozen time units two bodies pair up and the third is thrown out, just as the energy argument above predicts. Two recipes help. The first is hierarchy: put two bodies close together with opposite sideways velocities so that they circle each other, and place the third far away with a gentle sideways push. The second is symmetry: three bodies at the corners of an equilateral triangle, each pushed sideways in the same sense of rotation, make a Lagrange triangle that holds for a while before it breaks. Watch the closest-pair readout and the energy error as you experiment. Finding a periodic orbit by hand is all but impossible, which is why so many of them were discovered only by computer search, two centuries after Euler and Lagrange.

References

  1. [1] H. Bruns, Über die Integrale des Vielkörper-Problems, Acta Math. 11, 25 (1887).
  2. [2] H. Poincaré, Sur le problème des trois corps et les équations de la dynamique, Acta Math. 13, 1 (1890).
  3. [3] K. F. Sundman, Mémoire sur le problème des trois corps, Acta Math. 36, 105 (1912).
  4. [4] L. Euler, De motu rectilineo trium corporum se mutuo attrahentium, Novi Comment. Acad. Sci. Petropol. 11, 144 (1767).
  5. [5] J.-L. Lagrange, Essai sur le problème des trois corps, Prix de l’Académie Royale des Sciences de Paris, Vol. IX (1772).
  6. [6] E. J. Routh, On Laplace’s three particles, with a supplement on the stability of steady motion, Proc. London Math. Soc. 6, 86 (1875).
  7. [7] C. Moore, Braids in classical dynamics, Phys. Rev. Lett. 70, 3675 (1993).
  8. [8] A. Chenciner and R. Montgomery, A remarkable periodic solution of the three-body problem in the case of equal masses, Ann. of Math. 152, 881 (2000).
  9. [9] M. Šuvakov and V. Dmitrašinović, Three classes of Newtonian three-body planar periodic orbits, Phys. Rev. Lett. 110, 114301 (2013).