A439 of 56
0 XP
  1. Hook
  2. Predict
  3. Name it
  4. Check
  5. Explain

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 Cdv/dt: 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 sin⁡x at x=1 with sin⁡(1+h)−sin⁡(1−h)2h. Which h gives the most accurate answer in double precision?

Name it

Derivatives

From samples spaced h apart:

forward:f(x+h)−f(x)hcentral:f(x+h)−f(x−h)2h

Taylor series (A6) show their errors: about h2f′′ for forward and h26f′′′ for central. Central differences are far more accurate for the same h. 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 f carries a rounding error of about 10−16 of itself, and dividing a difference by a small h magnifies it. Noise in measured data is much larger than 10−16, so derivatives of real measurements are noisy unless the data is smoothed first.

Integrals

To integrate samples y0,y1,…,yn spaced h apart:

Trapezoid rule: join neighbors with straight lines and add the trapezoids.

∫ydx≈h(12y0+y1+⋯+yn−1+12yn)

Its error goes as h2: halve the step and the error drops by four.

Simpson's rule (with n even): fit parabolas through each pair of slices.

∫ydx≈h3(y0+4y1+2y2+4y3+⋯+4yn−1+yn)

Its error goes as h4: 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, h(yk−1+yk)/2, to the total.

Checking accuracy

The order of a method shows up when you halve the step: the error ratio is 2p for an order-p 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 v=5sin⁡(2π⋅50t) volts across it, sampled every 0.1 ms. Estimate the current at t=2 ms.

Show the solution
  1. Write the current as a derivative. i=Cdv/dt, estimated with the central difference: i≈Cv(t+h)−v(t−h)2h
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
  1. Compare with the exact value. i=C⋅5⋅2π⋅50×cos⁡(2π⋅50⋅0.002)=1.27080mA so the estimate is 0.016% low.
  2. Explain the error. The step is a two-hundredth of a period, and the error goes as (ωh)26=0.00016

Answer about 1.27059 mA, 0.016% below the exact 1.27080 mA.

Worked example

A current i=2e−t/5 ms A flows for 20 ms. How much charge is delivered? Compare the trapezoid rule with 20 steps against the exact answer.

Show the solution
  1. 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
  1. The exact charge. Q=2×5ms×(1−e−4)=0.0098168C
  2. Compare. The trapezoid rule's 0.0098495 C overestimates by 0.33% here, because the curve sags below each straight segment.
  3. Halve the step. Doubling the steps cuts the error to 0.083%, a quarter, as an h2 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 sin⁡x at x0 = 1 with step h = 0.1, and e_fwd, e_ctr to their absolute errors against cos⁡x0.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

For h=10−1,10−2,…,10−13, compute the central difference error for sin⁡ at x0 = 1. Set errs to the list of 13 errors and best_h to the h 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 i=I0e−t/τ (I0 = 1 A, tau = 2 ms) from 0 to 4τ with the trapezoid rule on n = 40 steps. Set Q to the charge in coulombs and err to its relative error against the exact I0τ(1−e−4).

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 sin⁡x 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 1T∫0Tv2dt. Then set exact to the known value: A/2 for a sine, A for a square wave and A/3 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 h has an error of 4 × 10⁻⁶. Its error goes as h2 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?

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.