TactivergeLite

Lite / Independent Engineering Research / 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 · Full interactive version

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.

Predict

A method has global error order 2. You halve h. What happens to the error at the final time?

  1. Halves
  2. Falls to about a quarter
  3. Falls to about an eighth
Guess first, then reveal

Error ≈ C·h², so halving h multiplies it by (1/2)² = 1/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.

What the interactive shows (text version)

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.

You can also open the interactive on the full site when you have a faster connection.

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

Intuitive

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.

Technical

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.

Applied

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
  1. 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ᵖ⁺¹.

  2. 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.

  3. Euler matches up to h¹ (p = 1). RK4 matches up to h⁴ (p = 4): its local error is O(h⁵), global O(h⁴).

  4. Test: with E(h) ≈ C hᵖ, E(h)/E(h/2) ≈ 2ᵖ, so p ≈ log₂(E(h)/E(h/2)).

  5. 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.

  6. 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

Practice

Practice, not a test. Answers are checked on our server; a wrong answer gets a hint and another try. Nothing is saved.

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?

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.

Reflect

How would you convince a sceptical reviewer that your simulation's step size is small enough?

Write your answer on paper or in your own notes. Lite never stores what you write.

← Euler, semi-implicit Euler and RK4Stability regions and stiffness →