1. Motivation: a probe on a highly eccentric orbit

A probe on an orbit of eccentricity spends 90% of the time far from the planet moving slowly and, for a brief fraction, crosses the pericenter at a velocity times larger than at the apocenter. If we integrate with a small fixed step to resolve the pericenter, we waste almost all the work at the apocenter; if we choose it large, the pericenter is poorly resolved and the orbit opens or closes. This is the problem solved by adaptive step methods: estimate the error of each step and let the integrator itself decide how long the next one can be.
In this lesson we use the planar Kepler problem with gravitational parameter as a test bench, because it has an exact solution (lesson on Kepler’s equation) against which to measure errors. We write and
Units: in the code () everything is dimensionless with , ; the period is . To go to SI it suffices to fix and and use .
2. One-step methods: local and global error

Let with Lipschitz in with constant , that is, . A one-step method advances with an increment function :
Definition. The local error at step is the difference between what the method gives starting from the exact value and the exact value one step later: . The method is of order if . The global error is .
Theorem (convergence). If the method has order and is Lipschitz in with constant , then on a fixed interval
Proof. Subtracting the definition of the local error from that of the method, , hence . Iterating this linear recurrence with ,
using . With the bound follows.
A local error becomes a global because errors are committed. The constant is pessimistic but real: in orbits, phase errors grow linearly with time, and in chaotic systems exponentially (previous lesson).
2.1 Euler
. Comparing with the Taylor series , the local error is : order 1. For a circular orbit, points toward the center and Euler always leaves “along the tangent”: the orbit grows in a spiral (exercise 1). In our table (§5) one needs for a 20% error after a single revolution.
2.2 Order conditions: the two-stage case
An explicit Runge–Kutta method with stages is defined by its Butcher tableau :
For two stages with , (condition , which we always assume), and writing , , evaluated at ,
The exact solution is , so the method is of order 2 if and only if
It is a one-parameter family: gives the midpoint method () and Heun’s method (). For order 3 there are 4 conditions, for order 4 there are 8 and for order 5 there are 17; the number of conditions grows like the rooted trees with vertices (Butcher’s theory; Hairer–Nørsett–Wanner §II.2). This is why the classical RK4 with 4 stages and order 4 is a sweet spot: from order 5 on, more stages than the order are needed ( stages for order 5, for order 6).
2.3 RK4
With , , and the 8 conditions of order 4 are satisfied: local error , global . Applied to it gives with , the Taylor polynomial of : the region of absolute stability cuts the negative real axis at and the imaginary axis at . For an oscillator of frequency this requires ; at the pericenter of an orbit the “local frequency” is , and it is what sets the step.
3. Embedded pairs and step control

3.1 The idea
If we have two approximations (order ) and (order ) that share the same stages (only the weights versus change), the difference
estimates the local error of the lower-order method without any additional evaluation. The two most used pairs:
| Pair | Stages | Orders | Author | Feature |
|---|---|---|---|---|
| RKF45 | 6 | 4(5) | Fehlberg (1969) | designed to propagate the order-4 solution; in practice the order-5 one is propagated (“local extrapolation”) |
| Dormand–Prince 5(4) | 7 | 5(4) | Dormand & Prince (1980) | coefficients optimized to minimize the error of the order-5 method; FSAL property: the seventh stage coincides with the first of the next step, hence it costs 6 evaluations per accepted step |
It is MATLAB’s ode45 and SciPy’s RK45. Both tableaux are in rungekutta.ts; the test checks the identities , , and measures the order 5 of the propagated solution and the order 5 of the error estimator (§5). Our implementation does not exploit FSAL (7 evaluations per step), which inflates the Dormand–Prince evaluation counts in the tables by 17%.
3.2 Step-control law
We want the local error per component not to exceed a mixed tolerance . We define the scaled norm
Since with (the order of the local error of the embedded method of order ), the “optimal” step that would give is . With a safety factor and limits to avoid oscillations:
If the step is accepted and we advance; if not, it is rejected and repeated with . It is a proportional controller (Hairer–Nørsett–Wanner §II.4, eq. 4.13). In integrarAdaptativo we use for both pairs.
One should be honest about what is controlled: the local error per step, not the global one. The accumulated global error can be orders of magnitude larger than the tolerance (table in §5: with the position error after one revolution is ) because the phase errors add up and because the eccentric orbit amplifies perturbations near the pericenter. The practical rule is that the global error scales linearly with the tolerance for a method of order propagated with local extrapolation ( in theory, but in practice the exponent is usually between 0.8 and 1): the table gives and error , that is, a factor in error for in tolerance.
4. Simulator
What to observe. The simulator integrates with a fixed step (RK4 or leapfrog). With “2 bodies, e = 0.8”, measure how many steps per period RK4 needs for the orbit to close visibly: that number is what an adaptive integrator distributes unevenly, concentrating the short steps at the pericenter (in our run with and , the step varies between and : a factor of 2200).
5. Numerical results

All the numbers in this section come from rungekutta.ts (the test rungekutta.test.ts verifies the orders; the tables were generated with the same code). Orbit , , ; position error after one revolution against the Kepler solution, and relative energy error .
5.1 Measured order, , fixed step
| Steps per revolution | Euler | RK4 | Dormand–Prince (order 5, fixed step) | |
|---|---|---|---|---|
| 100 | ||||
| 200 | ||||
| 400 | ||||
| 800 | ||||
| 1600 | ||||
| 3200 | ||||
| 6400 |
When is divided by 2, Euler divides the error by – (order 1, still outside the asymptotic regime while the error is ), RK4 by – (order 4, ) and Dormand–Prince by – (order 5, ). In the last row Dormand–Prince has already hit the round-off floor in double precision ( after evaluations): reducing the step further improves nothing, because the round-off error grows as .
The energy of RK4 drifts: with the error is after 1 revolution, after 10 and after 100: exactly linear in , as was proved for the oscillator in the previous lesson.
5.2 Adaptive step, , one revolution
| Tolerance | Pair | Accepted steps | Rejected | Evaluations of | Position error | |
|---|---|---|---|---|---|---|
| RKF45 | 53 | 21 | 444 | |||
| DP5(4) | 50 | 20 | 490 | |||
| RKF45 | 123 | 18 | 846 | |||
| DP5(4) | 114 | 24 | 966 | |||
| RKF45 | 302 | 1 | 1818 | |||
| DP5(4) | 278 | 1 | 1953 | |||
| RKF45 | 760 | 1 | 4566 | |||
| DP5(4) | 698 | 1 | 4893 |
Comparison with fixed-step RK4 on the same orbit: to bring the position error down to one needs steps ( evaluations); Dormand–Prince achieves it with tolerance and evaluations, 33 times fewer. The gain is larger the more eccentric the orbit: on a circular one the adaptive step contributes little.
Two observations on the table. (1) At loose tolerance there are many rejections (≈ 30%): the proportional controller overestimates the step after the pericenter; PI controllers (Gustafsson 1991) reduce them. (2) Dormand–Prince conserves energy better than RKF45 at the same cost because its coefficients minimize the error of the order-5 formula, which is the one propagated; RKF45 was optimized for the order-4 one.
6. Error analysis: what the adaptive step does not give

- It is not symplectic. Changing according to the state turns the map into , which in general does not conserve the symplectic form even if does (Hairer–Lubich–Wanner §VIII.2). The energy drifts again: in the table, grows with time as in RK4. For integrations of thousands of periods a fixed-step symplectic method is preferred (or one with a reversible variable step, which uses a time transformation and recovers conservation).
- It controls the local error, not the global one. We saw that the global one can be times the tolerance. To estimate the global error one must integrate twice (with and ) and compare, or integrate forward and backward.
- Round-off. The total error is ; there is an optimal and asking for in double precision is useless (row 6400 of table 5.1).
- Close encounters. The adaptive step resolves them (its great advantage over the fixed step), but the cost explodes: . For nearly exact collisions one must regularize (Kustaanheimo–Stiefel) or use coordinates that remove the singularity.
Problems
1. (Euler on a circular orbit). For with , and circular velocity , show that one Euler step increases by and the specific energy by , so that the orbit opens in a spiral.
Show solution
We start from , , . Euler gives , . Then and . The specific energy goes from to . At each step the energy rises by (always with the same sign: that is why it is secular), and after steps it rises by : linear in , order 1. In the table, with the energy error after one revolution is ; the factor 2 comes from the fact that on the orbit the contribution of the pericenter dominates.
2. (Order-2 conditions). Show that there is no explicit 2-stage Runge–Kutta method of order 3.
Show solution
With the expansion of §2.2 carried to : the exact solution contains the term , whereas the method contributes and no term proportional to (that term only appears when evaluating at a point that already contains a “nested” evaluation of , which requires a third stage). For general both types of terms are independent, so the condition is impossible. The conditions for order 3 with three stages are , , and .
3. (Linear stability of RK4). Show that for RK4 gives and deduce the stability limit .
Show solution
With , . Squaring and adding (computation of the previous lesson, exercise 3) gives . The condition is equivalent to , that is, , . For small , : weak damping per step, which accumulated over steps gives the decay . This is the secular energy drift of RK4.
4. (Error estimator). For an embedded pair of orders and , show that is an asymptotically correct estimate of the local error of but not of that of , and explain why, even so, is propagated.
Show solution
Let be the local exact solution. Then and with the leading term of the local error of the method of order . Subtracting, : it coincides with the error of to leading order, whereas that of is of a higher order and is masked. One propagates (“local extrapolation”) because it is more accurate; the price is that the step is controlled with a conservative estimate: the actual error committed is smaller than the estimated one. This is what makes the global error in table 5.2 look more like than like .
5. (Control law). Suppose that exactly. Show that, after a rejection, the new step is always accepted, and compute how much the factor 0.9 wastes in the number of steps.
Show solution
: accepted. Without the factor the step would give exactly , at the limit. With factor 0.9, the step is 10% smaller and 11% more steps are needed (). In reality varies along the orbit (it grows toward the pericenter) and the model fails just where it matters: hence the rejections in the table at loose tolerance.
6. (Computational: order table). Implement fixed-step Euler, RK4 and Dormand–Prince and reproduce table 5.1 for . Then estimate the empirical order row by row and explain the deviations from 1, 4 and 5.
Show solution
Pseudocode (Python):
import numpy as np
mu = 1.0
def f(t, y):
r3 = np.hypot(y[0], y[1])**3
return np.array([y[2], y[3], -mu*y[0]/r3, -mu*y[1]/r3])
def rk4(f, t, y, h):
k1 = f(t, y); k2 = f(t+h/2, y+h/2*k1); k3 = f(t+h/2, y+h/2*k2); k4 = f(t+h, y+h*k3)
return y + h/6*(k1 + 2*k2 + 2*k3 + k4)
def orbita(paso, n, e=0.5):
y = np.array([1-e, 0, 0, np.sqrt((1+e)/(1-e))]); h = 2*np.pi/n
for k in range(n): y = paso(f, k*h, y, h)
return np.hypot(y[0]-(1-e), y[1]) # after T the probe returns to the pericenter
for n in [100, 200, 400, 800, 1600]:
print(n, orbita(rk4, n), orbita(rk4, n)/orbita(rk4, 2*n))For Dormand–Prince the coefficients are copied from rungekutta.ts (table 2 of Dormand & Prince 1980). Empirical orders from our table: Euler 0.52, 0.56, 0.64, 0.76, 0.88, 0.96 (tends to 1 from below: the error is and the asymptotic expansion does not hold); RK4 4.4, 4.2, 4.1, 4.1, 4.0, 4.0; DP5 4.9, 5.2, 5.2, 5.1, 5.2 and then 0.2 (round-off). The values somewhat larger than in RK4 and DP5 at coarse step come from the higher-order terms of the error, which decay faster.
7. (Computational: adaptive step). Implement the controller of §3.2 on Dormand–Prince and integrate the orbit for one revolution with . Record as a function of the true anomaly and check that approximately. Why that exponent?
Show solution
The local error of a method of order 5 scales as and the derivatives of the Keplerian solution scale with the local frequency : . Imposing a constant local error gives const, that is, . Between the apocenter () and the pericenter () the expected step ratio is ; the measured ratio between the maximum and minimum step was 2200 because the minimum occurs right at the pericenter, where in addition the components of are small and the relative tolerance bites harder, and the maximum is set by the growth limit of per step when moving away. (It is a good exercise to plot on a logarithmic scale.)
✦ Self-assessment
Further reading
- Hairer, Nørsett & Wanner (1993), Solving ODEs I: §II.1 (Runge–Kutta and Butcher theory), §II.3 (convergence), §II.4 (error estimation, step control, Gustafsson’s PI controller), §II.5 (Dormand–Prince tableau).
- Press et al. (2007), Numerical Recipes, 3rd ed.: §17.1–17.2 (
StepperDopr5, step control). - Dormand & Prince (1980), J. Comput. Appl. Math. 6, 19–26 (table 2: RK5(4)7M coefficients). Fehlberg (1969), NASA TR R-315 (table III).
- Hairer, Lubich & Wanner (2006), Geometric Numerical Integration: §VIII.2 (why the variable step destroys symplecticity and how to recover it with time transformations).
- Murray & Dermott (1999), ch. 2: exact solution of the two-body problem used as reference.
🍎 Teacher guide
- Time
- 2 classes of 90 min
- Learning outcome
- By the end, the student proves the relation between local and global error, derives the order-2 conditions, implements an embedded pair with step control and experimentally evaluates the order and the cost of Euler, RK4 and Dormand–Prince on a Keplerian orbit.
- Evidence of learning
- Submission of problem 6 (order table with empirical orders) and of problem 7 (plot of h versus the true anomaly), with a half-page discussion of what the adaptive step does not control.
Materials
- Python 3 with NumPy (or Julia); optionally SciPy to compare with solve_ivp(method="RK45")
- Reference code: src/lib/math/rungekutta.ts and its test
- The portal's N-body simulator
Suggested sequence
Class 1 (90 min). Opening (15 min): the problem of the eccentric probe, estimating by hand with how many RK4 steps would be needed at the pericenter and at the apocenter. Development (60 min): proof of the convergence theorem on the board; order-2 conditions derived by the students in pairs (§2.2); presentation of RK4 and its stability region. Closing (15 min): problem 1 (Euler in a spiral) with a quick calculation and check in the simulator.
Class 2 (90 min). Opening (10 min): “how does the integrator know it is wrong?” (idea of the embedded pair). Development (60 min): programming workshop: implement Dormand–Prince with the given coefficients, verify the tableau sums, reproduce the order table (problem 6) and add the step controller (problem 7). Closing (20 min): share the plots of , discuss the loss of symplecticity and when each family of methods is preferred; mini-quiz.
Common mistakes
- Believing that the tolerance is the final error. Show table 5.2: with tol = 10⁻⁶ the global error is 10⁻³. Ask them to integrate twice with different tolerances to estimate the global error.
- Confusing the order of the local and global error. Insist on the theorem of §2: one power is lost when adding up T/h steps.
- Reducing the step “until it converges” without noticing round-off. Row 6400 of table 5.1 shows it: the error stops decreasing.
- Assuming that Dormand–Prince, being of order 5, conserves energy better than an order-2 leapfrog at long times. Compare with the previous lesson: high order reduces the error per step, but the secular drift remains.
- Forgetting the condition Σⱼ aᵢⱼ = cᵢ when copying a Butcher tableau. Make the sum test (as in rungekutta.test.ts) part of the submission.