Flow Lab

Syllabus / Module 14: Applications: Physics & Engineering

Lesson 14.7

N-Body Simulation

Gravitational N-body systems, softening, energy conservation, complexity O(N²) and Barnes–Hut tree methods.

Written byClaude Sonnet

🌊
Why this matters for simulation: Vortex particle methods use the same O(N²) → tree-code tricks.

By the end of this lesson you can…

  • Write Newton's gravity for $N$ bodies as $6N$ first-order equations and check the conserved energy, momentum and angular momentum along a run.
  • Use the two-body orbit as an exact check: $T = 2\pi\sqrt{a^3/GM}$ and $E = -Gm_1m_2/2a$.
  • Explain Plummer softening, find its maximum force $\frac{2}{3\sqrt3}Gm/\epsilon^2$, and say why a fixed step fails on close encounters.
  • Compare Euler, RK4 and leapfrog on a Kepler orbit and a galaxy collision, by which errors drift and which stay bounded.
  • Derive the Barnes-Hut rule $s/d \lt \theta$ and measure its force error and cost against the $O(N^2)$ direct sum.
  • Say why softening and a tree stop a code conserving the Newtonian energy and momentum, and what fast multipole and particle-mesh methods do instead.
On this page
·
  1. The equations
  2. Two bodies: the exact check
  3. Three bodies: a figure eight
  4. Softening
  5. Euler, RK4 or leapfrog
  6. The cost of all pairs
  7. Barnes-Hut: treat a distant crowd as one body
  8. What the tree and softening break
  9. Beyond the tree
  10. The lab
  11. The exercise: leapfrog for two bodies
  12. Concept checks
  13. Problems
  14. What comes next
  15. Key Takeaways

14.6 Acoustics & Wave Propagation ended on a grid where each cell talks only to its neighbours, so the work per step grows with the number of cells. Gravity has no such mercy: every body pulls on every other one, from any distance. This last problem of the module reuses 12.7 Orbital Mechanics, 4.7 Energy & Symplectic Integrators and the sensitivity of 7.10 Chaos & the Lorenz System. What is new is the scale.

▶ Video Newton's N-body equations, the Kepler orbit as an exact check, the figure-eight orbit, Plummer softening, Euler against RK4 against leapfrog, the quadtree of Barnes and Hut, and a galaxy collision with an energy readoutcaptions only
Transcript
  1. Gravity has no mercy: every body pulls on every other one. Newton's laws for N bodies are 6N first-order equations in 3D, 4N in the plane, with G equal to 1. Momentum, angular momentum and energy are conserved, and each is a test. They are among the ten classical integrals, from memory, and there is no closed form, so we simulate.
  2. Subtract the two equations and the relative position obeys the one-body problem, with a conic orbit. Take two unit masses, GM equal to 2, a equal to 1, eccentricity 0.5, started at periapsis. Then r p is 0.5 and the relative speed is root 6, 2.449. Kinetic energy 1.5, potential minus 2, total minus one half; the period is 4.4429. Every integrator must reproduce these.
  3. In the figure eight, three equal masses chase one another along one curve shaped like an 8: Moore 1993, Chenciner and Montgomery 2000, from memory. The energy is minus 1.28714, and RK4 with h equal to T over 1000, T equal to 6.32591398, returns every body to within 3.4 times 10 to the minus 8 of its start. Kick body 3 by 0.1 percent and the distance after one, two and ten periods is 0.011, 0.033 and 0.153: roughly linear, a stable orbit, from memory.
  4. A fixed step fails on a close encounter: the crossing time is 1000 times shorter at 0.01. Plummer softening replaces r squared by r squared plus epsilon squared. The force peaks at epsilon over root 2 with value 0.385 G m over epsilon squared, and is bounded; but inside epsilon gravity is wrong, and only the softened energy is conserved. In the lab galaxy at epsilon 0.05 the error is of order 0.1, about 1.2 times 10 to the minus 3 at half the step. For the e equals 0.9 pair, 0.683 becomes 0.062.
  5. Now the three integrators, 100 steps per orbit. Euler spirals out of the frame, with an energy error of 1.14 over 100 orbits; RK4 and leapfrog stay on the ellipse.
  6. Over 100 orbits the largest energy error is 1.14 for Euler, 2.98 times 10 to the minus 3 for RK4 and 1.08 times 10 to the minus 2 for leapfrog. Angular momentum errors are 0.58, 4.6 times 10 to the minus 4 and 1.4 times 10 to the minus 14. Over 1000 orbits RK4 reaches 2.99 times 10 to the minus 2 while leapfrog stays put; the lines cross near 360 orbits. But bounded energy is not accurate phase: leapfrog's return time is off by plus 1.7 percent at 100 steps and plus 0.10 at 400.
  7. The same story in a galaxy: N equal to 302, two cores of mass 1 with 150 light particles each, epsilon 0.1, step 0.01. The energy error at t equal to 5 and 20 is plus 2.28 and plus 2.43 for Euler, minus 1.7 times 10 to the minus 3 and minus 3.9 times 10 to the minus 3 for RK4, and plus 1.1 times 10 to the minus 4 and plus 5.4 times 10 to the minus 4 for leapfrog. Late-time numbers differ between my two codes, because the motion is chaotic.
  8. A million bodies mean 5 times 10 to the 11 pairs per force evaluation. Barnes and Hut, 1986, from memory, treat a distant crowd as one body. Divide the square into four quadrants, and any quadrant holding more than one body again, storing each cell's mass and centre of mass. Accept a cell of side s at distance d when s over d is less than theta, otherwise open it. The error is of relative order s over d squared, and the cost is order N log N.
  9. For N equal to 1000, theta 0.5 uses 91.8 terms per body with a median force error of 1.1 times 10 to the minus 3; theta 0.3 uses 169.5 for 3.4 times 10 to the minus 4, theta 1 uses 37.0 for 4.1 times 10 to the minus 3. Speed is another matter: in this lab's JavaScript the ratio of direct over tree is roughly 0.3 at 250 bodies, 1.1 at 2000 and 5.5 at 16000, one machine, rough. The tree wins only beyond about 2000 bodies.
  10. What does the tree break? Total momentum, exactly 0 at the start, is 3.6 times 10 to the minus 3 at t equal to 5 with theta 0.5, against 10 to the minus 15 for the direct sum. The energy error sits near 2 times 10 to the minus 3, twenty times the direct sum's. Cell approximations are not symmetric, so the third law fails. Softening conserves the softened energy, not the Newtonian one. Beyond the tree, from memory: fast multipole and particle-mesh.
  11. That closes Module 14. The toolkit was the same each time: write the model, find what it conserves, choose an integrator that respects that, check against an exact case, and know what each approximation breaks. Population models leave mechanics. An N-body system conserves energy, while a population is dissipative and settles toward an equilibrium. Module 15 turns to dissipative growth.

The equations

Bodies i=1,…,Ni = 1,\dots,N have masses mim_i, positions ri\mathbf r_i and velocities vi\mathbf v_i. Newton’s laws give

r˙i=vi,v˙i=G∑j≠imj rj−ri∣rj−ri∣3.\dot{\mathbf r}_i = \mathbf v_i, \qquad \dot{\mathbf v}_i = G\sum_{j\ne i} m_j\,\frac{\mathbf r_j - \mathbf r_i}{|\mathbf r_j - \mathbf r_i|^3}.

That is 6N6N first-order equations in 3D, or 4N4N in a plane, which is what the lab simulates (with the 3D inverse-square law). We set G=1G = 1. This is the Hamiltonian system of 4.7 Energy & Symplectic Integrators.

Three things are conserved, and each is a test. Momentum: the pull of jj on ii is minus the pull of ii on jj, so ∑imiv˙i=0\sum_i m_i\dot{\mathbf v}_i = 0. Angular momentum: each force acts along the line between its two bodies. Energy: the kinetic plus potential energy HH is constant. With the centre-of-mass motion these are the ten classical integrals (from memory), and for N≥3N\ge3 no more are known. There is no closed form, so we simulate.

Two bodies: the exact check

Subtract the two equations of motion. The relative position r=r2−r1\mathbf r = \mathbf r_2 - \mathbf r_1 obeys r¨=−G(m1+m2) r/∣r∣3\ddot{\mathbf r} = -G(m_1 + m_2)\,\mathbf r/|\mathbf r|^3, the one-body problem of 12.7 Orbital Mechanics with GM=G(m1+m2)GM = G(m_1+m_2). The relative orbit is a conic, the period is T=2πa3/GMT = 2\pi\sqrt{a^3/GM}, and the energy is E=−Gm1m2/(2a)E = -Gm_1m_2/(2a).

Worked example. Two unit masses, GM=2GM = 2, a=1a = 1, e=0.5e = 0.5, started at periapsis (the lab’s setup). Then rp=0.5r_p = 0.5 and the relative speed is 6=2.449\sqrt6 = 2.449, so each body moves at 6/2\sqrt6/2. The kinetic energy is 1.51.5, the potential energy is −1/0.5=−2-1/0.5 = -2, and the total is −12=−Gm1m2/2a-\tfrac12 = -Gm_1m_2/2a as promised. The period is 2π/2=4.44292\pi/\sqrt2 = 4.4429. Every integrator must reproduce these.

Three bodies: a figure eight

For three bodies there is no formula, but there are special solutions. In the figure eight (Moore, 1993, and Chenciner and Montgomery, 2000, from memory) three equal masses chase one another along one curve shaped like an 8. The initial data as I remember them are r1,2=±(0.97000436,−0.24308753)\mathbf r_{1,2} = \pm(0.97000436, -0.24308753), r3=0\mathbf r_3 = 0, v1=v2=(0.466203685,0.43236573)\mathbf v_{1} = \mathbf v_2 = (0.466203685, 0.43236573), v3=−2v1\mathbf v_3 = -2\mathbf v_1, with T=6.32591398T = 6.32591398. We can check them. The energy is E=−1.28714E = -1.28714, and RK4 with h=T/1000h = T/1000 returns every body to within 3.4×10−83.4\times10^{-8} of its start after one period (numpy agrees), so the data are right to about eight digits.

Leapfrog at h=T/1000h = T/1000 closes to 8.1×10−58.1\times10^{-5}, and at T/4000T/4000 to 5.1×10−65.1\times10^{-6} (a factor 1616: second order); after ten periods the T/1000T/1000 error is 1.5×10−31.5\times10^{-3}.

Now kick it. Raise the speed of body 3 by 0.1%0.1\% (and remove the centre-of-mass drift). With RK4 at T/4000T/4000 the largest distance of a body from its start after one, two and ten periods is 0.0110.011, 0.0330.033 and 0.1530.153. A 1%1\% kick gives 0.1090.109 after one period and 1.191.19 after ten. That is roughly linear growth, not the exponential growth of 14.3 Pendulums: Linear, Nonlinear, Driven & Double, because the figure eight is a stable orbit (from memory: linearly stable). Generic three-body motion is not so kind (Poincare found chaos, from memory).

◆ Interactive Figure eight: three equal masses, an integrator selector, a speed kick, and the distance from the start after each period

Loading simulation…

Softening

Why does a fixed step fail on a close encounter? A pair at separation rr crosses it in time about r3/Gm\sqrt{r^3/Gm}, which shrinks like r3/2r^{3/2}. A step fine at r=1r = 1 is hopeless at r=0.01r = 0.01, where the time is 10001000 times shorter, and in a galaxy some pair is always passing close.

The standard remedy is Plummer softening (from memory, after Plummer’s 1911 cluster model). Replace r2r^2 by r2+ϵ2r^2 + \epsilon^2:

r¨i=G∑j≠imj rj−ri(∣rj−ri∣2+ϵ2)3/2,Φij=−Gmimjrij2+ϵ2.\ddot{\mathbf r}_i = G\sum_{j\ne i} m_j\,\frac{\mathbf r_j - \mathbf r_i}{\left(|\mathbf r_j - \mathbf r_i|^2 + \epsilon^2\right)^{3/2}}, \qquad \Phi_{ij} = -\frac{Gm_im_j}{\sqrt{r_{ij}^2 + \epsilon^2}}.

The force is still the gradient of the softened potential, so the softened energy is conserved by the true flow, with momentum and angular momentum intact. The pair force Gm r/(r2+ϵ2)3/2Gm\,r/(r^2+\epsilon^2)^{3/2} is zero at r=0r = 0, acts like a spring while r≪ϵr\ll\epsilon, peaks at r=ϵ/2r = \epsilon/\sqrt2 with value 233Gm/ϵ2=0.385 Gm/ϵ2\frac{2}{3\sqrt3}Gm/\epsilon^2 = 0.385\,Gm/\epsilon^2, then falls like 1/r21/r^2. The acceleration is bounded, so the step need only resolve a time of order ϵ3/2/Gm\epsilon^{3/2}/\sqrt{Gm}.

The cost is honest: inside ϵ\epsilon gravity is wrong, which one justifies by letting each particle stand for a cloud of stars.

What it buys, in the lab’s galaxy (N=302N = 302, Δt=0.01\Delta t = 0.01, leapfrog, largest ∣ΔE/E0∣|\Delta E/E_0| among the values at t=1,…,10t=1,\dots,10; a single step can spike higher, and the ϵ=0.1\epsilon=0.1 direct run touches 0.110.11 at t=3.78t=3.78 on one close pass, then relaxes): at ϵ=0\epsilon = 0 the error is of order 10510^5, and at Δt=0.005\Delta t = 0.005 my two codes disagree by orders of magnitude, since one chaotic encounter decides. At ϵ=0.05\epsilon = 0.05 it is 0.0650.065 in the lab’s JavaScript and 0.0980.098 in numpy (only the order 0.10.1 is robust), and 1.2×10−31.2\times10^{-3} at Δt=0.005\Delta t = 0.005 in both. At ϵ=0.1\epsilon = 0.1 and 0.20.2 it is between 10−410^{-4} and 3×10−43\times10^{-4}. For the eccentric Kepler pair (e=0.9e = 0.9, 400400 steps per orbit, 2020 orbits) it is 0.6830.683 unsoftened and 0.0620.062 with ϵ=0.1\epsilon = 0.1.

Euler, RK4 or leapfrog

4.7 Energy & Symplectic Integrators compared these methods on a pendulum and a Kepler orbit. Here is the test on the two-body orbit above (e=0.5e = 0.5, 100100 steps per orbit), with the largest relative errors over 100100 orbits and, for the energy, 10001000 orbits:

methodmax⁡∣ΔE/E0∣\max\lvert\Delta E/E_0\rvert, 100 orbits1000 orbitsangular momentum, 100 orbits
Euler1.141.140.580.58
RK42.98×10−32.98\times10^{-3}2.99×10−22.99\times10^{-2}4.6×10−44.6\times10^{-4}
leapfrog1.08×10−21.08\times10^{-2}1.08×10−21.08\times10^{-2}1.4×10−141.4\times10^{-14}

Euler spirals out; RK4 loses 3×10−53\times10^{-5} per orbit: ten times the orbits, ten times the error. Leapfrog’s error does not grow at all, which is the shadow-Hamiltonian guarantee, so the lines cross near 360360 orbits (an estimate from these numbers). Leapfrog keeps angular momentum to rounding.

Bounded energy is not accurate phase. The orbit’s return time is off by +1.7%+1.7\% for leapfrog at 100100 steps per orbit, +0.41%+0.41\% at 200200 and +0.10%+0.10\% at 400400, a factor of four per halving. RK4’s is only −5.0×10−5-5.0\times10^{-5} at 100100.

◆ Interactive Two-body Kepler orbit: Euler, RK4 and leapfrog side by side with the running maximum of the energy error; try e = 0.9

Loading simulation…

Close encounters again. Set e=0.9e = 0.9, so rp=0.1r_p = 0.1. At 200200, 400400, 16001600 and 64006400 steps per orbit the leapfrog energy error over 2020 orbits is 4.14.1, 0.680.68, 3.9×10−23.9\times10^{-2} and 2.4×10−32.4\times10^{-3}. The factor 1616 appears only once the periapsis pass is resolved. Softening (ϵ=0.1\epsilon = 0.1) gives 0.290.29, 6.2×10−26.2\times10^{-2}, 3.7×10−33.7\times10^{-3} and 2.3×10−42.3\times10^{-4}, for a different orbit. The other cure is individual or adaptive time steps that shrink near periapsis (from memory, production codes use block steps in powers of two), but a step chosen from the state breaks the symplectic argument.

The cost of all pairs

Each of the NN bodies needs a sum over the other N−1N-1. The third law lets us visit only N(N−1)/2N(N-1)/2 pairs, each costing a square root and a division. A million bodies mean 5×10115\times10^{11} pairs per force evaluation. Doubling NN quadruples the work. The lab’s JavaScript direct sum takes (rough, one machine) 0.270.27, 1.11.1 and 2222 ms per force evaluation for N=500N = 500, 10001000 and 40004000.

Barnes-Hut: treat a distant crowd as one body

A star far from a cluster feels only its total pull. Let masses mkm_k sit at sk\mathbf s_k relative to their centre of mass, so ∑mksk=0\sum m_k\mathbf s_k = 0, with the group at distance dd from the target. Expanding 1/∣d−s∣1/|\mathbf d - \mathbf s| for ∣s∣≪d|\mathbf s|\ll d,

Φ=−G∑kmk∣d−sk∣=−GMd[1+0+O ⁣(s2d2)].\Phi = -G\sum_k\frac{m_k}{|\mathbf d - \mathbf s_k|} = -\frac{GM}{d}\left[1 + 0 + O\!\left(\frac{s^2}{d^2}\right)\right].

The first-order term ∑mksk⋅d/d3\sum m_k\mathbf s_k\cdot\mathbf d/d^3 vanishes by the choice of centre. So replacing the group by one body at its centre of mass is wrong only at relative order (s/d)2(s/d)^2.

The Barnes-Hut tree (1986, from memory) applies this recursively. Put the bodies in a square, divide it into four quadrants, divide any quadrant holding more than one body, and so on (an octree in 3D). Each cell stores its total mass and centre of mass. For the force on a body, start at the root. If a cell of side ss has its centre of mass at distance dd with

sd<θ,\frac{s}{d} \lt \theta,

use it as one point mass; otherwise open it and look at its four children. My implementation also opens every cell containing the target. At θ=0\theta = 0 nothing is accepted and the result is the direct sum. A body meets O(log⁡N)O(\log N) cells, so the cost is O(Nlog⁡N)O(N\log N).

◆ Interactive Barnes-Hut quadtree of a galaxy snapshot: click a body to see the cells used for its force, then measure force error and speed against theta

Loading simulation…

Measurements. For N=1000N = 1000 bodies (two disks, seed 77, ϵ=0.1\epsilon = 0.1), the error is the relative ∣Δa∣/∣a∣|\Delta\mathbf a|/|\mathbf a| against the direct sum. An independent numpy tree reproduces every entry.

θ\thetaterms per bodymedian error99th percentilerms
0.30.3169.5169.53.4×10−43.4\times10^{-4}1.1×10−31.1\times10^{-3}1.5×10−41.5\times10^{-4}
0.50.591.891.81.1×10−31.1\times10^{-3}3.5×10−33.5\times10^{-3}5.0×10−45.0\times10^{-4}
0.70.760.060.02.1×10−32.1\times10^{-3}7.3×10−37.3\times10^{-3}1.0×10−31.0\times10^{-3}
1.01.037.037.04.1×10−34.1\times10^{-3}3.7×10−23.7\times10^{-2}2.6×10−32.6\times10^{-3}

A few bodies have relative errors of 0.10.1 to 2.72.7, where the true force nearly cancels. Typical errors are far below the worst case: Problem 3 shows a two-body cell at s/d=0.5s/d = 0.5 can be 17%17\% wrong, yet the median at θ=0.5\theta = 0.5 is 0.1%0.1\%, because real cells have bodies at all positions and the errors average out.

The logarithm is visible: terms per body at θ=0.5\theta = 0.5 are 54.554.5 at N=250N = 250, 91.891.8 at 10001000 and 171.3171.3 at 1600016000, about 1919 more per doubling. Speed is another matter. In this lab’s JavaScript, which rebuilds the tree at every evaluation, the ratio direct over tree at θ=0.5\theta = 0.5 is roughly 0.30.3 at N=250N = 250, 0.60.6 at 10001000, 1.11.1 at 20002000, 2.42.4 at 40004000 and 5.55.5 at 1600016000 (one machine, rough). The tree wins here only beyond about 20002000 bodies.

What the tree and softening break

Run the galaxy with the tree (leapfrog, Δt=0.01\Delta t = 0.01, ϵ=0.1\epsilon = 0.1, N=302N = 302). The total momentum, exactly 00 at the start, is 3.6×10−33.6\times10^{-3} at t=5t = 5 and 5.5×10−35.5\times10^{-3} at t=10t = 10 for θ=0.5\theta = 0.5, against 10−1510^{-15} for the direct sum. It is 3.2×10−43.2\times10^{-4} for θ=0.3\theta = 0.3 and 3.6×10−23.6\times10^{-2} for θ=1\theta = 1. At t=3,…,10t=3,\dots,10 the energy error sits near 2×10−32\times10^{-3}, twenty times the direct sum’s (both runs spike for one step near t=3.78t=3.78, to 0.140.14 and 0.110.11). The cell approximation is not symmetric: body ii may see jj as part of a far cell while jj sees ii individually, so the third law fails pair by pair. The tree force is also not the gradient of any potential, so the shadow-Hamiltonian argument of 4.7 Energy & Symplectic Integrators no longer applies.

Softening is subtler. The softened energy is conserved by leapfrog to bounded error, which is what the lab reports. The Newtonian energy with 1/r1/r is not conserved, because you are simulating a smoother system. Know which quantity your code actually conserves.

Beyond the tree

Two further ideas, both from memory. The fast multipole method (Greengard and Rokhlin, 1987) keeps the tree but expands each cell’s far field to higher order, and lets groups of bodies interact with groups. The cost is O(N)O(N) at fixed accuracy, raised through the expansion order. The particle-mesh method deposits mass on a grid, solves ∇2Φ=4πGρ\nabla^2\Phi = 4\pi G\rho with the FFT (the Poisson solve of 10.12 Spectral Methods & the FFT), differentiates, and interpolates the force back: O(Mlog⁡M)O(M\log M) for MM cells, but with no resolution below a cell.

The lab

◆ Interactive Galaxy collision lite: two softened disks, integrator and force selectors, softening and step sliders, tree opening angle, and live energy and momentum readouts

Loading simulation…

  1. Defaults (leapfrog, direct, ϵ=0.1\epsilon = 0.1, Δt=0.01\Delta t = 0.01): the disks interact and shed tails. The energy error stays near 10−410^{-4} and ∣P∣|\mathbf P| at rounding.
  2. Choose Euler: the energy error jumps by order one. RK4 looks fine but its error creeps downward.
  3. Set ϵ=0.05\epsilon = 0.05 at Δt=0.01\Delta t = 0.01: the error becomes about 0.10.1. Halve Δt\Delta t and it falls to about 10−310^{-3}.
  4. Choose the Barnes-Hut tree and watch ∣P∣|\mathbf P| leave zero; lower θ\theta and it shrinks.

The exercise: leapfrog for two bodies

The playground runs the figure eight with a numpy direct sum and leapfrog, then compares the eccentric pair with and without softening. In the exercise you write the pairwise acceleration, the energy and the leapfrog step yourself, and the tests check the energy, the period and the symmetries against the exact two-body values.

🐍 PythonFigure eight and a softened close pair
⌘/Ctrl + Enter
🧪 Coding exerciseExercise: leapfrog for the Kepler two-body problem
⌘/Ctrl + Enter

Concept checks

✅ Concept check

Two bodies of mass $1$ each attract with $G = 1$ and move on a relative orbit with semi-major axis $a = 1$. What is the period?

✅ Concept check

How many distinct pairs of bodies must a direct-sum force evaluation visit for $N = 2000$ bodies?

✅ Concept check

A leapfrog galaxy simulation uses a Barnes-Hut tree. Compared with the direct sum at the same step, which statement is true?

✅ Concept checkselect all that apply

Select all true statements about Plummer softening with length $\epsilon$.

Problems

✏️ Pen & paperThe softened close pair●○○

Two bodies of mass m=1m = 1, G=1G = 1, ϵ=0.1\epsilon = 0.1. (a) At what separation is the pair force largest, and how large is it? (b) For r≪ϵr\ll\epsilon show the relative motion is harmonic and find its period.

Show worked solution

(a) With f(r)=r(r2+ϵ2)−3/2f(r) = r(r^2+\epsilon^2)^{-3/2}, f′=(r2+ϵ2)−5/2(ϵ2−2r2)=0f' = (r^2+\epsilon^2)^{-5/2}(\epsilon^2 - 2r^2) = 0 gives r=ϵ/2=0.0707r = \epsilon/\sqrt2 = 0.0707, where f=2/(33 ϵ2)=0.3849/ϵ2f = 2/(3\sqrt3\,\epsilon^2) = 0.3849/\epsilon^2, so the force is 38.4938.49.

(b) For r≪ϵr\ll\epsilon, f≈r/ϵ3f\approx r/\epsilon^3 and the separation obeys r¨=−2r/ϵ3\ddot r = -2r/\epsilon^3. So ω2=2000\omega^2 = 2000, ω=44.7\omega = 44.7, and the period is 2π/ω=0.14052\pi/\omega = 0.1405: about 1414 steps of 0.010.01.

✏️ Pen & paperUnequal masses●●○

Bodies of mass m1=3m_1 = 3 and m2=1m_2 = 1 (G=1G = 1) have a relative orbit with a=2a = 2. Find the period, the energy, and each body’s semi-major axis about the centre of mass.

Show worked solution

GM=4GM = 4, so T=2π8/4=2π2=8.886T = 2\pi\sqrt{8/4} = 2\pi\sqrt2 = 8.886. The energy is E=−Gm1m2/(2a)=−3/4E = -Gm_1m_2/(2a) = -3/4. Since m1r1+m2r2=0m_1\mathbf r_1 + m_2\mathbf r_2 = 0, r1=−m2Mr\mathbf r_1 = -\frac{m_2}{M}\mathbf r and r2=m1Mr\mathbf r_2 = \frac{m_1}{M}\mathbf r, so the heavy body has a1=14⋅2=0.5a_1 = \frac14\cdot2 = 0.5 and the light one a2=34⋅2=1.5a_2 = \frac34\cdot2 = 1.5, both with period 8.8868.886.

✏️ Pen & paperHow wrong is the monopole?●●○

Two bodies of mass mm sit at ±s/2\pm s/2 on a line, and a target on the line is at distance dd from the centre with s/d=0.5s/d = 0.5. Compare the exact force with the monopole force 2Gm/d22Gm/d^2. Why is the measured median error at θ=0.5\theta = 0.5 so much smaller?

Show worked solution

With u=s/(2d)=0.25u = s/(2d) = 0.25 the exact force is Gmd2[1(1−u)2+1(1+u)2]=2.4178 Gmd2\dfrac{Gm}{d^2}\left[\dfrac{1}{(1-u)^2} + \dfrac{1}{(1+u)^2}\right] = 2.4178\,\dfrac{Gm}{d^2}, so the monopole is low by the factor 1.2091.209 (17.3%17.3\%). The expansion 2(1+3u2+5u4+… )2(1 + 3u^2 + 5u^4 + \dots) has leading error 3u2=0.18753u^2 = 0.1875, as the O(s2/d2)O(s^2/d^2) argument says. The measured median is 1.1×10−31.1\times10^{-3} because real cells hold bodies spread through the cell, and their quadrupole terms have no common sign.

What comes next

That closes Module 14. Oscillators gave eigenmodes, pendulums brought nonlinearity and chaos, heat and beams were boundary-value problems, and acoustics derived a wave equation. This lesson ran a many-body system. The toolkit was the same each time: write the model, find what it conserves, choose an integrator that respects that, check against an exact special case, and know what each approximation breaks. Here the approximations broke energy and momentum, and three-body chaos limits prediction whatever the integrator.

15.1 Population Models leaves mechanics. The syllabus lists exponential, logistic and Gompertz growth, harvesting, the Allee effect, least-squares fitting and a glimpse of delay equations. The contrast is the point: an N-body system conserves energy, while a population is dissipative and settles toward an equilibrium.

Key Takeaways

  • Newton’s gravity is 6N6N first-order equations (4N4N in a plane) with conserved energy, momentum and angular momentum; the two-body orbit is the exact check, and the figure eight closes to 3.4×10−83.4\times10^{-8} with RK4 at h=T/1000h = T/1000.
  • A close encounter demands a step shrinking like r3/2r^{3/2}; Plummer softening bounds the force at the price of changing the physics inside ϵ\epsilon.
  • On the Kepler orbit (e=0.5e = 0.5, 100100 steps per orbit) Euler gains energy (1.141.14), RK4 loses 3×10−53\times10^{-5} per orbit, and leapfrog stays within 1.08×10−21.08\times10^{-2} with angular momentum exact.
  • Barnes-Hut accepts a cell when s/d<θs/d\lt\theta, an error of relative order (s/d)2(s/d)^2, giving O(Nlog⁡N)O(N\log N). At θ=0.5\theta = 0.5 the median force error is 1.1×10−31.1\times10^{-3}, and it beat the direct sum in my code only above about 20002000 bodies.
  • The tree breaks the third law, so momentum (3.6×10−33.6\times10^{-3} at t=5t = 5 against 10−1510^{-15}) and the energy guarantee are lost; softening conserves only the softened energy.
  • Fast multipole and particle-mesh methods are the next steps; Module 15 turns to dissipative growth.
Finished this lesson?

Mark it complete to track your progress (saved in this browser). Reading every section does this for you.