School/□ Systems Engineer/Week 31
Time, memory, accuracy: pick your trade
Halve the step size and the answer gets better. Halve it again and it gets slower. When do you stop?
About 45 min · 3 goals · Checked by automated tests
1.Why should I care?
A rover's flight computer has a fraction of a phone's speed and memory. Every model it runs is a deliberate compromise between how right, how fast and how small. Engineers make those compromises with numbers, not hunches.
- ◇ Trade accuracy against computation by choosing a numerical step size and method order.
- ◇ Compare algorithms by growth rate and decide when a slower but simpler method is acceptable.
- ◇ Replace floating-point with fixed-point arithmetic and quantify the precision lost.
2.Discover
In the Code Lab, integrate dv/dt = −k·v (k = 0.5, v₀ = 10) to t = 4 s with Euler's method at h = 0.4, 0.2, 0.1, 0.05. Print the error against the exact 10·e^(−2) and the number of steps. Then repeat with values stored as integers scaled by 256 (Q8 fixed point) and compare the error floor.
Can't use the interactive? Read a description instead
In the Code Lab the learner integrates exponential decay with Euler's method at four step sizes and prints the error and step count: each halving of h roughly halves the error and doubles the steps. Storing values in Q8 fixed point adds an error floor of a few thousandths that does not shrink with h.
3.Checkpoint: guided practice
Check what you just discovered before going on. Hints appear after a wrong answer.
Practice 1 · a fresh version every time
The trapezoid rule has global error roughly proportional to h^2. With step h = 0.2 s the error is 0.8 mm. Estimate the error with step h = 0.05 s, in mm. (4 significant figures.) The run will take about 4× as long.
4.Apply it
Produce an error-versus-cost plot (log–log) for Euler and a second-order method on the same problem. Mark the cheapest run that meets an error target of 0.01. Add a fixed-point Q8 Euler run and mark its error floor.
5.Understand
Order of accuracy
A method of order p has global error ≈ C·h^p. Halving h divides the error by 2^p and doubles the work. Plot error against h on log–log axes: the slope is p, which is a powerful check that your code is correct. Below some h, rounding error in each step starts to accumulate and the curve turns back up.
Growth rates
An O(n) algorithm on a million items does about a million operations; O(n log n) about twenty million; O(n²) a trillion. For small n, a simple O(n²) method may be faster and far easier to verify. Choose by the sizes you actually face, measured, and by the cost of getting it wrong.
Memory as a resource
Storing results can save time (a lookup table replaces a slow sin(x)) at the cost of memory, and the table's spacing sets its accuracy. A microcontroller with 32 KB of RAM can't hold a 64 K-entry table, so you interpolate a smaller one. This is time versus memory versus accuracy in one decision.
Fixed point: precision you can count
Fixed-point stores x as an integer X = round(x·2^f) with f fractional bits. Resolution is 2^−f; rounding error is at most half of that per operation. Q8 (f = 8) resolves 1/256 ≈ 0.0039. It is fast and predictable on chips without a floating-point unit, but you must check the range (overflow) and the accumulated rounding in long loops.
Explain it another way
Drawing a circle with straight lines: more, shorter lines look rounder but take longer to draw. A cleverer stroke (a curve) gets round faster.
Euler is O(h); trapezoid/Heun O(h²); classical RK4 O(h⁴). Cost per step: 1, 2 and 4 function evaluations. Fixed-point Qm.f: range ±2^(m), resolution 2^(−f), quantisation error uniformly distributed with SD 2^(−f)/√12.
If error E(h) = C·h^p, then E(h/r) = E(h)/r^p. From two runs, p ≈ log(E₁/E₂)/log(h₁/h₂). Richardson extrapolation combines them to cancel the leading error term.
Game physics engines often pick a fixed, cheap step and accept small energy errors, because a guaranteed frame rate matters more than perfect accuracy; spacecraft trajectory tools pick high-order methods with adaptive steps because a metre at Mars matters.
Worked example 1
Euler gives error 0.32 at h = 0.4 and 0.16 at h = 0.2. Estimate p, then predict the error at h = 0.05.
p = log(0.32/0.16)/log(2) = 1. At h = 0.05 (8× smaller than 0.4) the error ≈ 0.32/8 = 0.04, for 8× the steps.
Common mix-ups
- Smaller steps are always more accurate.
✓ Truncation error falls but rounding error grows with the number of steps; there is an optimum, and a cost. - The algorithm with the best big-O is always the right choice.
✓ For the sizes you actually face, constant factors, memory and verifiability can matter more.
6.Practice on your own
Practice, not a test. Wrong answers get a hint and another try.
Practice 2
A sensor value is stored in Q8 fixed point (8 fractional bits). What is the largest possible rounding error for one stored value?
Practice 3
You sort 40 sensor readings once per second on a microcontroller. Is a simple O(n²) insertion sort acceptable?
Practice 4 · a fresh version every time
You grid-search a design with 4 parameters, trying 10 values of each. One simulation takes 1 s. How long does the full grid take, in seconds?
Practice 5
Shrinking h keeps reducing the error, until at very small h the error starts to grow. Why?
7.Predict
Euler's method has error roughly proportional to the step h. To make the error 10× smaller, how much more computation?
Guess first. Being wrong here is how the surprise works.
8.Reflect
Concepts: Algorithms, searching and complexity · Simulation and time stepping · Units, scaling and fixed-point arithmetic