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

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 ex at x=1, against the step h, on log axes. The dashed line is the truncation error, h26e; the dotted line is the rounding error, about εeh with ε=2.2×10−16.
  • 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 ex at 1 is tried with h=10−4 and with h=10−12, in standard double precision. Which is more accurate?

Name it

Difference formulas

  • Forward: f′(x)≈f(x+h)−f(x)h, with error about h2f′′: it shrinks in proportion to h.
  • Central: f′(x)≈f(x+h)−f(x−h)2h, with error about h26f′′′: it shrinks as h2, because the even terms cancel.

Rounding

A double-precision number keeps about 16 significant digits (ε≈2.2×10−16). Each function value carries a rounding error near ε|f|, and the difference formula divides it by h. So the total error of a central difference is about

h26|f′′′|+ε|f|h

which is smallest near h≈ε1/3, about 6×10−6 for values near 1.

Sampled data

When the data are samples v0,v1,… taken every Δt, the step is fixed. The central difference at sample k is vk+1−vk−12Δt; the first and last samples need a one-sided formula, or are dropped. Noise in the samples is divided by 2Δt 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 sin⁡x at x=1 with h=0.1, forward and central. (The true slope is cos⁡1=0.540302.)

Show the solution
  1. Forward difference. m=sin⁡1.1−sin⁡10.1=0.891207−0.8414710.1=0.497364 an error of −0.0429.
  2. Central difference. m=sin⁡1.1−sin⁡0.90.2=0.891207−0.7833270.2=0.539402 an error of −0.000900.
  3. Compare. For the same step, the central difference is almost 50 times better, as the h against h2 errors predict.

Answer the forward difference gives 0.497 364 (error −0.0429) and the central difference gives 0.539 402 (error −0.000900), 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
  1. Slope at the middle sample. Use the central difference: dvdt≈2.33−2.002×0.001=165V/s
  2. Current. i=Cdvdt=10−5×165=1.65mA
  3. Effect of noise. If each reading had a 0.01 V error, the slope could be off by 0.020.002=10V/s 6% of the answer: the noise is magnified by 1/(2Δt).

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 ex at x=1 with h = 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 sin⁡ 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 10−1,10−2,…,10−13 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, Cvk+1−vk−12Δt, 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 sin⁡(2π⋅50t) 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, D(h) and D(h/2), into 4D(h/2)−D(h)3. Since the central error goes as h2, this cancels it. Set e_central and e_rich to the errors for the slope of ex at 1 with h=0.1.

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?

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.