Lite / Engineering Complex Systems / 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?
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.
Predict
Euler's method has error roughly proportional to the step h. To make the error 10× smaller, how much more computation?
- About 10×
- About 100×
- About 3×
- No extra cost
Guess first, then reveal
Error ∝ h, so 10× smaller error needs 10× smaller h, so 10× more steps. A second-order method (error ∝ h²) would need only about 3.2× more steps; RK4 (∝ h⁴) about 1.8×. Higher-order methods do more work per step but often win overall.
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.
What the interactive shows (text version)
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.
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
Intuitive
Drawing a circle with straight lines: more, shorter lines look rounder but take longer to draw. A cleverer stroke (a curve) gets round faster.
Technical
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.
Mathematical
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.
Applied
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.
Practice
Practice, not a test. Answers are checked on our server; a wrong answer gets a hint and another try. Nothing is saved.
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.
Reflect
Which part of a system you know is 'good enough' rather than exact, and who decided where good enough was?
Write your answer on paper or in your own notes. Lite never stores what you write.