School/□ Systems Engineer/Week 4
Rates from data: finite differences and noise
Your robot logs its position 100 times a second. Dividing the differences by 0.01 s should give perfect speed. Instead the speed plot looks like static. Why?
About 40 min · 3 goals · Checked by automated tests
1.Why should I care?
Sensors give samples, not formulas. Estimating velocity from encoder counts, current from charge readings or the D term of a PID controller all use finite differences, and all suffer from the same trade-off between step size and noise.
- ◇ Estimate a derivative from samples with forward and central differences.
- ◇ Explain why forward-difference error ∝ h and central-difference error ∝ h².
- ◇ Explain why noise error grows like 1/h and choose a sensible step.
2.Predict
Positions are measured with ±2 mm noise. If you shrink the sample spacing h, what happens to the velocity estimate?
Guess first. Being wrong here is how the surprise works.
3.Discover
With zero noise, try the forward method at h = 0.5, 0.2, 0.1 s: read the largest error. Switch to central: how much better? Now set noise to ± 5 mm and step h down to 0.01 s. Find the h that gives the smallest error with noise, for each method.
Loading the interactive…
Can't use the interactive? Read a description instead
The finite-difference lab compares the true velocity 3cos(1.5t) + 0.5 with estimates from sampled positions. Without noise, forward errors fall in proportion to h and central errors much faster. With noise of a few millimetres, very small h gives scattered estimates; the readout reports the largest error and both error bounds.
4.Checkpoint: guided practice
Check what you just discovered before going on. Hints appear after a wrong answer.
Practice 1
s(0) = 0 m and s(0.1 s) = 0.29 m. Estimate the velocity at t = 0 with a forward difference, in m/s.
5.Understand
Forward and central
Forward: v ≈ (sᵢ₊₁ − sᵢ)/h. Simple, usable in real time, error ∝ h. Central: v ≈ (sᵢ₊₁ − sᵢ₋₁)/(2h). Error ∝ h², so halving h quarters the error, but it needs the next sample, so it lags by one step in real time.
Differentiation amplifies noise
Noise of ε metres becomes a velocity error of order ε/h. With ε = 2 mm and h = 0.01 s that is up to 0.4 m/s of junk. Differentiating makes noise worse; integrating (week 7) averages it out. This is why the derivative term of a PID controller is usually filtered.
Choosing h
Balance the two errors: truncation falls as h shrinks, noise rises. In practice, either sample fast and then smooth (average several samples or fit a line through a window), or difference over a longer baseline than one sample.
Explain it another way
Measuring speed from two marks 1 mm apart is hopeless if your ruler wobbles by 1 mm. Marks a metre apart give a rougher idea of 'instant' speed but a much steadier number.
Forward difference: O(h) truncation. Central: O(h²). Noise contribution: O(ε/h). Optimal h for central differences scales like (ε/|s‴|)^(1/3).
Minimise E(h) = (h²/6)M + ε/h: dE/dh = hM/3 − ε/h² = 0 gives h* = (3ε/M)^(1/3). Calculus choosing a numerical method's parameter.
Quadrature encoders count discrete ticks, so position noise is ±1 tick. Motor drivers estimate speed by timing the interval between ticks at low speed and counting ticks per period at high speed, switching to keep the error small.
Derivation: Why central differences are more accurate
Taylor expansion: s(t + h) = s(t) + h·s′(t) + (h²/2)·s″(t) + (h³/6)·s‴(t) + …
Forward difference: [s(t + h) − s(t)]/h = s′(t) + (h/2)·s″(t) + … The error is about (h/2)·s″: proportional to h.
Also s(t − h) = s(t) − h·s′(t) + (h²/2)·s″(t) − (h³/6)·s‴(t) + …
Central difference: [s(t + h) − s(t − h)]/(2h) = s′(t) + (h²/6)·s‴(t) + … The s″ terms cancel, so the error is proportional to h².
Noise: if each sample is off by up to ε, the forward difference can be off by up to 2ε/h and the central by ε/h. Total error ≈ C·hᵖ + ε/h, smallest at a moderate h.
Worked example 1
Samples: s(0.9 s) = 1.10 m, s(1.0 s) = 1.32 m, s(1.1 s) = 1.50 m. Estimate v(1.0) by forward and central differences.
Forward: (1.50 − 1.32)/0.1 = 1.8 m/s. Central: (1.50 − 1.10)/0.2 = 2.0 m/s. The central estimate uses information from both sides.
Common mix-ups
- Sampling faster always gives better derivatives.
✓ With noisy sensors, faster sampling makes raw differences noisier. Filter or difference over a longer baseline. - Forward and central differences are equally accurate.
✓ Central is one order more accurate for smooth signals, at the cost of needing the next sample.
6.Practice on your own
Practice, not a test. Wrong answers get a hint and another try.
Practice 2
s(0.9) = 1.10 m and s(1.1) = 1.50 m. Estimate v(1.0) with a central difference, in m/s.
Practice 3
Position noise is up to ±2 mm and h = 0.01 s. What is the largest velocity error the noise could cause in a forward difference, in m/s?
Practice 4
Without noise, you halve h for a central difference. Roughly what happens to its error?
Practice 5 · a fresh version every time
A trolley's position is s(t) = 5t² + 1t metres. What is its average velocity between t = 1 s and t = 1.1 s, in m/s?
7.Apply it
Write a short program (any language) that takes a list of positions sampled every h seconds and returns forward and central velocity estimates. Test it on s = 2·sin(1.5t) + 0.5t with and without added noise and plot the errors against h.
8.Reflect
Concepts: Numerical integration and differentiation · Noise and filtering