Lite / Intelligent Robotic Systems / Week 7
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?
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.
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…
- ±0.5 mm/s
- ±1 mm/s
- ±100 mm/s
- ±0.01 mm/s
Guess first, then reveal
±100 mm/s! The difference of two readings can be off by 0.5 + 0.5 = 1 mm, and dividing by h = 0.01 s multiplies that by 100. Small time steps make derivative noise worse, not better.
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.
What the interactive shows (text version)
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.
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
Intuitive
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.
Technical
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.
Mathematical
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.
Applied
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.
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
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.
Reflect
When you next see a jagged speed plot from a robot, what two questions will you ask about how it was computed?
Write your answer on paper or in your own notes. Lite never stores what you write.