A5 · Lesson 12 · Lab: derivatives from numbers
How a computer finds a slope, and how it goes wrong
A computer can't take a limit. When a simulator needs a slope, or a scope computes the current through a capacitor from its sampled voltage, it divides a small difference by a small step. Make the step too big and the answer is off because the curve bends; make it too small and the answer is off because the computer rounds. In this lab you'll write the difference formulas in Python, find the step that balances those two errors, and turn a sampled voltage into a current.
- The curve is the error of a central difference for the slope of at , against the step , on log axes. The dashed line is the truncation error, ; the dotted line is the rounding error, about with .
- The total error is smallest where the two lines cross. Below that, a smaller step makes things worse.
- Change how many digits the computer keeps (the exponent of ) and watch the best step move.
Predict
A central difference for the slope of at 1 is tried with and with , in standard double precision. Which is more accurate?
, by about seven digits. Its error is the truncation error, . At , the two function values agree in their first 12 digits, so their difference keeps only about 4 good digits, and dividing by the tiny magnifies the rounding: the error is around . The best step for a central difference in double precision is near .
Name it
Difference formulas
- Forward: , with error about : it shrinks in proportion to .
- Central: , with error about : it shrinks as , because the even terms cancel.
Rounding
A double-precision number keeps about 16 significant digits (). Each function value carries a rounding error near , and the difference formula divides it by . So the total error of a central difference is about
which is smallest near , about for values near 1.
Sampled data
When the data are samples taken every , the step is fixed. The central difference at sample is ; the first and last samples need a one-sided formula, or are dropped. Noise in the samples is divided by too, so differentiating noisy data magnifies the noise; that's why scopes smooth before they differentiate.
In Python, lists hold the samples, and a list comprehension builds the differences: [(v[k+1] - v[k-1]) / (2*dt) for k in range(1, len(v) - 1)].
Worked example
Estimate the slope of at with , forward and central. (The true slope is .)
Show the solution
- Forward difference. an error of .
- Central difference. an error of .
- Compare. For the same step, the central difference is almost 50 times better, as the against errors predict.
Answer the forward difference gives 0.497 364 (error ) and the central difference gives 0.539 402 (error ), almost 50 times better.
Worked example
A 10 µF capacitor's voltage is sampled every 1 ms: 2.00, 2.18, 2.33 V. Estimate the current at the middle sample.
Show the solution
- Slope at the middle sample. Use the central difference:
- Current.
- Effect of noise. If each reading had a 0.01 V error, the slope could be off by 6% of the answer: the noise is magnified by .
Answer about 1.65 mA flows at the middle sample.
Check
About how big is the truncation error of a central difference for the slope of at with = 0.01?
Check
A 10 µF capacitor's voltage is sampled every 0.5 ms. Two samples on either side of a moment differ by 0.33 V. What current flows at that moment?
Write units the way you normally would: 1.5 A, 1500 mA and 0.0015 kA are the same answer.
Check
Write forward(f, x, h) and central(f, x, h), the two difference formulas. The tests compare their errors for at 1.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write best_step(f, df, x), which tries the steps in a central difference and returns the one whose error against the exact slope df(x) is smallest. Set h = best_step(math.exp, math.exp, 1.0).
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
The starter has voltage samples v of a 10 µF capacitor taken every dt = 1 ms. Write current(v, dt, C) returning the list of currents at the interior samples, , with C in farads. Set i = current(v, 1e-3, C * 1e-6).
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Noise hurts differences. The starter adds random noise of size s = 0.005 V to samples of taken every 0.1 ms. Set raw to the largest error of the central differences against the exact slope, and smooth to the largest error after first averaging each sample with its two neighbors (a three-point moving average) and then differencing.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write richardson(f, x, h), which combines two central differences, and , into . Since the central error goes as , this cancels it. Set e_central and e_rich to the errors for the slope of at 1 with .
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Explain why
Why does making the step smaller eventually make a numerical derivative worse?
A difference quotient has two errors that pull in opposite directions. The truncation error comes from the curve bending within the step; it shrinks as (forward) or (central). The rounding error comes from the computer storing each function value to about 16 digits: when is tiny, and agree in most of their digits, their difference keeps only the last few, and dividing by the tiny magnifies that loss. Rounding error grows as . So the total error falls as shrinks, reaches a minimum where the two are equal, then rises. For a central difference in double precision the best step is near , and steps below that give worse answers.
Practice
Keep practicing
6 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.