The acceleration of one body is
These iteration techniques approximate the successive positions and time-derivatives after each small time increment
Only the first approximated position and speed is written below, for the
For our projectile example, the gravitational acceleration is constant and only non-zero in the
Let
$x_1 = x_0 + v_{x0} dt$ $v_{x1} = v_{x0} + a_{x0} dt$ $a_{x1} = -G m / x_1^2$
This method is a second-order Runge-Kutta (RK2) method, that provides significantly better accuracy than the Euler method, by evaluating the derivative at the midpoint of each time step.
Calculate midpoint estimates:
$x_{0.5} = x_0 + v_{x0} dt / 2$ $v_{x0.5} = v_{x0} + a_{x0} dt / 2$ $a_{x0.5} = -G m / x_{0.5}^2$
These are then used to find the successive parameters:
$x_1 = x_0 + v_{x0.5} dt$ $v_{x1} = v_{x0} + a_{x0.5} dt$ $a_{x1} = -G m / x_1^2$
Heun’s method is another RK2 technique, which is more accurate than the Euler method because it averages the initial gradient with the gradient predicted by Euler's method.
Euler's method predicts:
$x_{pred} = x_0 + v_{x0} dt$ $v_{xpred} = v_{x0} + a_{x0} dt$ $a_{xpred} = -G m / x_{pred}^2$
Average the above with the initial parameters to get:
$x_1 = x_0 + (v_{xpred} + v_{x0}) dt / 2$ $v_{x1} = v_{x0} + (a_{xpred} + a_{x0}) dt / 2$ $a_{x1} = -G m / x_1^2$
RK4 is a fourth-order method that evaluates acceleration and velocity at multiple intermediate points, improving both accuracy and energy conservation. Four intermediate estimates are calculated, and a weighted average is used to more-accurately determine the successive positions and velocities.
Startpoint gradients:
$k_{1x} = v_{x0} dt$ $k_{1vx} = a_{x0} dt$
Second intermediate gradients:
$x_{1mid} = x_0 + k_{1x} / 2$ $v_{1midx} = v_{x0} + k_{1vx} / 2$ $a_{1midx} = -G m / x_{1mid}^2$ $k_{2x} = v_{1midx} dt$ $k_{2vx} = a_{1midx} dt$
Third intermediate gradients:
$x_{2mid} = x_0 + k_{2x} / 2$ $v_{2midx} = v_{x0} + k_{2vx} / 2$ $a_{2midx} = -G m / x_{2mid}^2$ $k_{3x} = v_{2midx} dt$ $k_{3vx} = a_{2midx} dt$
Endpoint gradients:
$x_{end} = x_0 + k_{3x}$ $v_{endx} = v_{x0} + k_{3vx}$ $a_{endx} = -G m / x_{end}^2$ $k_{4x} = v_{endx} dt$ $k_{4vx} = a_{endx} dt$
Taking weighted averages of all the gradients:
$x_1 = x_0 + (k_{1x} + 2k_{2x} + 2k_{3x} + k_{4x}) / 6$ $v_{x1} = v_{x0} + (k_{1vx} + 2k_{2vx} + 2k_{3vx} + k_{4vx}) / 6$ $a_{x1} = -G m / x_1^2$
Velocity Verlet is a second-order symplectic method. It is especially useful for orbital dynamics because it updates positions and velocities in a way that tends to keep long-term energy error bounded rather than letting it drift monotonically.
Using the current acceleration
$x_1 = x_0 + v_{x0} dt + \frac{1}{2} a_{x0} dt^2$ $a_{x1} = -G m / x_1^2$ $v_{x1} = v_{x0} + \frac{1}{2}(a_{x0} + a_{x1})dt$
The method is lower order than RK4, but its symplectic structure makes it an important comparison method for long-running conservative systems.
This repository includes several special and illustrative three-body cases. Some are analytically structured initial conditions, then RK4 is used to test whether the numerical trajectory preserves that structure over one period. Others intentionally perturb a solution or use the restricted three-body approximation to show nearby behavior.
The figure-8 solution is generated by minimising the action which describes three identical masses that follow each other
The Lagrange solution places the three bodies at the vertices of an equilateral triangle.
The triangle rotates rigidly about its center of mass with angular speed
The Euler collinear solution places three equal masses on a single rotating line.
For positions
These solutions are special cases, not generic outcomes. Like other three-body trajectories, they remain sensitive to perturbations and numerical error over longer integrations.
Perturbed figure-8 and perturbed Lagrange examples start from known periodic solutions and apply a small displacement or velocity change. These examples are not new exact solutions; they show sensitivity to initial conditions while conserving total energy numerically.
The circular restricted three-body problem (CR3BP) assumes two massive primaries on circular orbits and a third body whose mass is negligible.
In the rotating frame, the primaries are fixed and the third body obeys normalized equations with mass ratio
The Sitnikov problem is a spatial restricted case.
Two equal primaries orbit in the plane while the third body moves along the perpendicular axis through their center of mass.
For circular primaries separated symmetrically by radius
Hierarchical triples model a close binary plus a much more distant third body. This is not generally periodic, but it is a common astrophysical configuration and usually remains stable when the outer orbit is much wider than the binary separation.
The Butterfly I choreography uses cataloged equal-mass periodic initial conditions with
The n-body engine stores positions and velocities in arrays with dimensions
step x body x coordinate.
For each body, acceleration is computed by summing the pairwise gravitational
contribution from every other body.
This makes the same solver usable for two, three, four, or more bodies without
duplicating the force equations for each system size.
The specialized two-body and three-body files are kept because they are easier to read as educational derivations. The n-body engine is a general-purpose parallel implementation for larger examples and parity checks.
Two special four-body central configurations are included as n-body examples.
In the equal-mass square, the four bodies sit at the corners of a square and
rotate rigidly about the center. For half-side length
The triangular central configuration places one mass at the center and three equal outer masses at the vertices of an equilateral triangle. The central body remains fixed by symmetry while the outer triangle rotates rigidly around it.
- Euler Method
- Midpoint Method
- Heun's Method
- Runge-Kutta Method
- Three-Body Problem Solutions
- Periodic Three-Body Initial Conditions
📘 Author: Sid Richards (SidRichardsQuantum)
LinkedIn: https://www.linkedin.com/in/sid-richards-21374b30b/
This project is licensed under the MIT License - see the LICENSE file for details.