A4 · Lesson 39 · Numerical derivatives and integrals
Slopes and areas from samples
A scope gives you samples, not formulas. To get the current through a capacitor from its sampled voltage, you need : a derivative from samples. To get the charge delivered by a sampled current, or the energy from sampled power, you need an integral. Both are simple ideas (a slope between neighbors, a sum of thin slices), but how accurately they work depends on the step size in ways that surprise people, including a step that's too small.
Predict
You estimate the derivative of at with . Which gives the most accurate answer in double precision?
About . Shrinking reduces the formula's error, which goes as , but the two sines become nearly equal and their difference loses digits to rounding, an error that grows as . The total is smallest where the two balance, near , with an error around . At the rounding error is about , a million times worse.
Name it
Derivatives
From samples spaced apart:
Taylor series (A6) show their errors: about for forward and for central. Central differences are far more accurate for the same . In NumPy, np.diff(v) / dt gives forward differences between samples and np.gradient(v, dt) gives central differences inside the array.
Rounding sets a floor. Each value of carries a rounding error of about of itself, and dividing a difference by a small magnifies it. Noise in measured data is much larger than , so derivatives of real measurements are noisy unless the data is smoothed first.
Integrals
To integrate samples spaced apart:
Trapezoid rule: join neighbors with straight lines and add the trapezoids.
Its error goes as : halve the step and the error drops by four.
Simpson's rule (with even): fit parabolas through each pair of slices.
Its error goes as : halving the step cuts it sixteenfold.
Running integrals. np.cumsum(y) * h is a quick running sum (rectangles); for a running trapezoid integral, add each new trapezoid, , to the total.
Checking accuracy
The order of a method shows up when you halve the step: the error ratio is for an order- method. That's how you test that an implementation is correct, and how you estimate the error of a result when there's no exact answer to compare with.
Worked example
A capacitor of 1 µF has volts across it, sampled every 0.1 ms. Estimate the current at ms.
Show the solution
- Write the current as a derivative. , estimated with the central difference:
import numpy as np
v = lambda t: 5 * np.sin(2 * np.pi * 50 * t)
h = 1e-4
i = 1e-6 * (v(2e-3 + h) - v(2e-3 - h)) / (2 * h)
print(i) # 1.27059e-03
- Compare with the exact value. so the estimate is 0.016% low.
- Explain the error. The step is a two-hundredth of a period, and the error goes as
Answer about 1.27059 mA, 0.016% below the exact 1.27080 mA.
Worked example
A current A flows for 20 ms. How much charge is delivered? Compare the trapezoid rule with 20 steps against the exact answer.
Show the solution
- Apply the trapezoid rule. With 20 steps: the sum of the samples, minus half of the two ends, times the step.
t = np.linspace(0, 20e-3, 21)
i = 2 * np.exp(-t / 5e-3)
h = t[1] - t[0]
Q = h * (i.sum() - (i[0] + i[-1]) / 2)
exact = 2 * 5e-3 * (1 - np.exp(-4))
print(Q, exact) # 0.0098495, 0.0098168
- The exact charge.
- Compare. The trapezoid rule's 0.0098495 C overestimates by 0.33% here, because the curve sags below each straight segment.
- Halve the step. Doubling the steps cuts the error to 0.083%, a quarter, as an method should.
Answer the exact charge is 0.0098168 C, and the trapezoid rule with 20 steps gives 0.0098495 C, 0.33% high.
Check
Set fwd and ctr to the forward and central difference estimates of the derivative of at x0 = 1 with step h = 0.1, and e_fwd, e_ctr to their absolute errors against .
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
For , compute the central difference error for at x0 = 1. Set errs to the list of 13 errors and best_h to the with the smallest one.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Integrate (I0 = 1 A, tau = 2 ms) from 0 to with the trapezoid rule on n = 40 steps. Set Q to the charge in coulombs and err to its relative error against the exact .
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write simpson(y, h) for an odd number of samples (an even number of slices), and set S to the integral of from 0 to on n = 8 slices, and err to its error against the exact 2.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
The starter samples one period of a waveform v at n + 1 = 20 + 1 equally spaced points (the first and last are the same point in the cycle). Using the trapezoid rule written with NumPy slices (not a library integrator), set rms to . Then set exact to the known value: for a sine, for a square wave and for a triangle.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
A central difference with step has an error of 4 × 10⁻⁶. Its error goes as while rounding is negligible. What should the error be with the step 10 times smaller?
Explain why
Why does making the step in a numerical derivative smaller eventually make the answer worse?
A difference formula has two kinds of error. The truncation error, from replacing the curve with a straight line or a parabola over the step, shrinks as the step shrinks. But the formula subtracts two nearly equal values of the function, each stored with a tiny rounding error, and then divides by the step. As the step shrinks, the difference gets smaller while the rounding errors don't, so their relative effect grows like 1/h. Below some step size the growing rounding error outweighs the shrinking truncation error.
Practice
Keep practicing
8 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.