School/✦ Research Engineer/Week 7
Local and global error, and the convergence study
Halve the step and RK4's error drops about sixteen times. How can you prove that from your own code, not from a textbook?
About 50 min · 3 goals · Checked by automated tests
1.Why should I care?
A convergence study is the single most useful test of simulation code. If the observed order matches theory, the method is probably implemented correctly; if not, there is a bug. It is also how you justify the step size in a report.
- ◇ Distinguish local truncation error (per step) from global error (at a fixed final time).
- ◇ Run a convergence study and compute the observed order p = log₂(E(h)/E(h/2)).
- ◇ Use the observed order to check an implementation and to choose a step size.
2.Predict
A method has global error order 2. You halve h. What happens to the error at the final time?
Guess first. Being wrong here is how the surprise works.
3.Understand
Question → method → evidence
Question: is my integrator correct, and is my step size small enough? Method: solve a problem with a known answer at h, h/2, h/4 (and smaller), measure the error at a fixed time, compute ratios. Evidence: ratios close to 2ᵖ confirm the order; a final error below your tolerance justifies the step.
When there is no exact answer
Use the finest run as a reference, or use differences between successive runs: E(h) ≈ |y_h − y_{h/2}|·2ᵖ/(2ᵖ − 1). Richardson extrapolation combines two runs, (2ᵖ y_{h/2} − y_h)/(2ᵖ − 1), to cancel the leading error term.
Limits of the study
Very small h eventually hits round-off error, which grows as steps multiply, so the error curve flattens or turns up. Very large h may be outside the asymptotic range or even unstable. Report the range of h where the ratio is steady, and plot error against h on log–log axes: the slope is the order.
Explain it another way
Like checking a map's scale: if doubling the zoom always doubles the detail, the map is behaving. If RK4 does not give about 16× better results at half the step, something in the code is off.
A common bug is evaluating k₂ or k₃ at the wrong time or state; it usually drops the observed order of RK4 to 1 or 2, which a convergence study catches at once.
Verification standards for engineering simulation ask for exactly this kind of grid- or step-refinement study before results are used.
Derivation: Evidence for RK4's fourth order
Local error: if a method matches the exact Taylor series through the hᵖ term, one step from exact data is off by about C hᵖ⁺¹.
Global error: reaching a fixed time T takes N = T/h steps. Roughly adding the local errors gives N·C hᵖ⁺¹ = C T hᵖ, so global order is p.
Euler matches up to h¹ (p = 1). RK4 matches up to h⁴ (p = 4): its local error is O(h⁵), global O(h⁴).
Test: with E(h) ≈ C hᵖ, E(h)/E(h/2) ≈ 2ᵖ, so p ≈ log₂(E(h)/E(h/2)).
Evidence from this term's code (x″ = −x, x(0) = 1, error in x at t = 10): RK4 with 100, 200, 400 steps gives errors about 3.9×10⁻⁶, 2.6×10⁻⁷, 1.7×10⁻⁸. Ratios 14.9 and 15.5 give p ≈ 3.9 and 3.95, approaching 4.
Explicit Euler on the same test gives 0.57, 0.24, 0.11: ratios 2.3 and 2.2, approaching 2¹ = 2 as h shrinks. The pre-asymptotic ratios are a reminder to test at small enough h.
Worked example 1
Errors at t = 5 are 2.0×10⁻³ (h = 0.1) and 2.6×10⁻⁴ (h = 0.05). What is the observed order?
Ratio 2.0×10⁻³ / 2.6×10⁻⁴ ≈ 7.7; log₂ 7.7 ≈ 2.9. About third order (or a fourth-order method not yet in its asymptotic range: check with h = 0.025).
Common mix-ups
- If two runs agree to three digits, the answer is accurate to three digits.
✓ Agreement shows convergence only if you are in the asymptotic range; check the ratio pattern across several halvings. - Smaller steps are always better.
✓ Round-off and cost grow; choose the largest step that meets your tolerance, with evidence.
4.Discover
Read the convergence table below the energy plot. For each method, divide the h error by the h/2 error and the h/2 error by the h/4 error. Change h and see which ratios settle near 2 and which near 16.
Loading the interactive…
Can't use the interactive? Read a description instead
Below the energy plot, a table lists for each method the error in x at t = 10 for steps h, h/2 and h/4, and the observed order computed from the last two. Changing h updates the table.
5.Checkpoint: guided practice
Check what you just discovered before going on. Hints appear after a wrong answer.
Practice 1 · a fresh version every time
With step h, the midpoint method (global order 2) gives a global error of 0.0032. Predict the error with step h/2.
6.Practice on your own
Practice, not a test. Wrong answers get a hint and another try.
Practice 2 · a fresh version every time
Explicit Euler has global error order 1. By what factor should the global error shrink when h is reduced to h/4?
Practice 3
E(h) = 0.032 and E(h/2) = 0.0021. What is the observed order p = log₂(E(h)/E(h/2))? (2 d.p.)
Practice 4
A method's local error is O(h³). Its global error is…
Practice 5
Your 'RK4' shows observed order 1.0 on a smooth test. Most likely?
7.Apply it
In the Code Lab, run a convergence study for your three integrators on x″ = −x up to t = 10 with h from 0.4 down to 0.0125. Plot log error against log h, fit a line by least squares (week 5) and report the slopes with the range you used.
8.Reflect
Concepts: Numerical integration of ODEs: Euler, RK4 and stiffness · Reproducibility and documentation