Speed from position, distance from speed: calculus on samples
Your encoder gives position every 10 ms. Differentiate to get speed and the plot turns into hair. Integrate speed to get distance and it's beautifully smooth. Why the difference?
About 40 min · 3 goals · Checked by automated tests
1.Why should I care?
Robots measure in samples, not formulas. Speed comes from differencing positions; distance and energy come from summing speeds and powers. Knowing how each operation treats noise tells you which results to trust and when to filter first.
- ◇ Estimate a derivative from samples with forward and central differences.
- ◇ Estimate an integral from samples with the trapezium rule.
- ◇ Explain why differentiation amplifies noise and integration smooths it.
2.Discover
In the Code Lab, make clean positions x = 0.2·t² for t from 0 to 2 s every 0.01 s, then add random noise of ±0.5 mm. Compute speed with forward differences, and distance from the true speed with the trapezium rule. Compare both with the exact answers. Then try h = 0.05 s.
Can't use the interactive? Read a description instead
The Code Lab activity can be done on paper: for x = 0.2t² sampled every 0.01 s, the forward difference at t = 1 s gives 0.402 m/s, the central difference 0.400 m/s, and with ±0.5 mm noise the forward difference can be off by up to 0.1 m/s. The trapezium rule on the speed samples gives the exact distance 0.8 m.
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
A linear-axis encoder logs position every 0.1 s: x = [11, 20, 29, 32, 38, 45] mm. Estimate the speed at sample index 4 using a forward difference (x[i+1] - x[i]) / h, in mm/s.
Practice 2 · a fresh version every time
A rover logs wheel speed every 2 s: v = [1, 1, 5, 5.5] m/s. Using the trapezium rule, how far did it travel between the first and last sample, in metres?
4.Apply it
From the Code Lab experiment, make a table of the maximum speed error against h (0.005, 0.01, 0.02, 0.05, 0.1 s) for noisy data. Find the h with the smallest error and explain the trade-off you see between noise and truncation error.
5.Understand
Derivatives from differences
Forward difference: (x[i+1] - x[i]) / h, an estimate of the speed between two samples. Central difference: (x[i+1] - x[i-1]) / (2 * h), centred on sample i and usually more accurate for smooth data (its error shrinks like h² rather than h).
Integrals from trapezia
Join neighbouring samples with straight lines and add the areas: h * (v[0]/2 + v[1] + ... + v[-2] + v[-1]/2). For speed samples this gives distance; for power samples, energy. Its error shrinks like h² for smooth data.
Noise changes the advice
On clean data, smaller h is always better. On noisy data, differencing divides the noise by h, so too-small steps make speed estimates useless. Integrate when you can, smooth before you differentiate, and always state the sample interval in your report.
Explain it another way
Speed is about how much something changed between two looks. If each look is a bit shaky, comparing two looks very close together mostly measures the shake.
Truncation error: forward O(h), central O(h²), trapezium O(h²). Noise error: differences O(ε/h); trapezium total about ε·h·√N for N random readings. The best h for a derivative balances the two.
For x = 0.2t², the exact speed is 0.4t. A forward difference gives 0.4t + 0.2h: exactly linear in h. A central difference gives 0.4t with no truncation error for a quadratic.
Motor controllers often estimate speed by counting encoder ticks over a longer window rather than differencing every sample, trading a little delay for much less noise.
Derivation: Why differences amplify noise
Each reading is x_i + n_i, where the noise n_i is at most ε in size.
Forward difference: ((x_{i+1} + n_{i+1}) − (x_i + n_i))/h = (x_{i+1} − x_i)/h + (n_{i+1} − n_i)/h.
The noise term can be as large as 2ε/h. Halving h doubles it.
The trapezium rule multiplies each reading by h and adds them, so noise enters as h·Σn_i. Random noise partly cancels in the sum: integration smooths, differentiation sharpens.
So choose h large enough that 2ε/h is acceptable, or smooth the readings first (for example with a moving average), or use a central difference over 2h.
Worked example 1
Differentiate and integrate clean and noisy samples of x = 0.2t² (metres).
import random
random.seed(1)
h = 0.01
t = [i * h for i in range(201)] # 0 to 2 s
x = [0.2 * ti ** 2 for ti in t]
x_noisy = [xi + random.uniform(-0.0005, 0.0005) for xi in x]
i = 100 # t = 1.0 s, true speed 0.4 m/s
fwd = (x[i + 1] - x[i]) / h
cen = (x[i + 1] - x[i - 1]) / (2 * h)
fwd_noisy = (x_noisy[i + 1] - x_noisy[i]) / h
print(round(fwd, 4), round(cen, 4), round(fwd_noisy, 3))
v = [0.4 * ti for ti in t] # true speed samples
distance = h * (v[0] / 2 + sum(v[1:-1]) + v[-1] / 2)
print(round(distance, 6)) # 0.8, exact: 0.2 * 2**2 = 0.8The clean forward difference is 0.402 (off by 0.2h), the central difference is 0.4, and the noisy forward difference can be off by up to 0.1 m/s.
Common mix-ups
- Sampling faster always gives a better speed estimate.
✓ With noise, faster sampling makes simple differences noisier. Filter or difference over longer windows. - The trapezium rule is exact.
✓ Its error shrinks like h², and it is exact only for straight-line segments.
6.Practice on your own
Practice, not a test. Wrong answers get a hint and another try.
Practice 3 · a fresh version every time
A rover logs wheel speed every 1 s: v = [3.5, 3.5, 2.5, 1, 0] m/s. Using the trapezium rule, how far did it travel between the first and last sample, in metres?
Practice 4 · a fresh version every time
A linear-axis encoder logs position every 0.5 s: x = [15, 24, 29, 35, 40, 41] mm. Estimate the speed at sample index 3 using a central difference (x[i+1] - x[i-1]) / (2*h), in mm/s.
Practice 5
Readings have noise up to ±0.2 mm and are 0.004 s apart. What is the largest possible noise in a forward-difference speed, in mm/s?
Practice 6
Speed from a noisy encoder is too jumpy. Which change helps most without new hardware?
7.Predict
Positions are logged every 0.01 s and each reading has up to ±0.5 mm of noise. A forward-difference speed estimate could be wrong by up to about…
Guess first. Being wrong here is how the surprise works.
8.Reflect
Concepts: Numerical integration and differentiation · Noise and filtering · Measurement error and uncertainty