ladderAICalculus
Building the derivative from its definition
The slope of a secant, then a secant centered on the point, then two of those combined: three estimates of the derivative of , each with an error the algebra predicts exactly, and the tests check the algebra. The last rung tries the same three on , where nothing is exact and the orders show up as ratios: halve and the three errors fall by factors of 2, 4 and 16.
Open in the workbenchDownload .vivOutputs below are from the site build’s run; Run all runs it again in your browser.
Look inside this program
- Atlas
The program’s map: every function and which calls which.
There’s no recorded run of this program yet.
A ladder is one program built in steps called rungs. Each rung adds a definition and a test; the program at any rung is everything up to it, and every earlier rung's tests run again. The derivative is a good thing to build this way because each improvement is a few lines, and each has an error you can work out by hand before running anything.
The slope of a secant
The derivative of at is the limit, as , of the slope of the line through and . Before the limit that slope is an ordinary number, and for the algebra gives it exactly: . The derivative is the ; the rest, , is the error, and it shrinks like : halve and the error roughly halves.
forward takes the function as an argument, so each estimate on this ladder is written once and works for any function.
new: cube, forward
Rung 1 tests 2 of 2 pass
- the forward difference of x^3 is 3x^2 + 3xh + h^2, exactly (passes)
- halving h halves the error, to within 1% (passes)
A secant centered on the point
Take the secant from to instead. In every even power of cancels, and . The term is gone. What is left is , so halving quarters the error. Rung 1's two tests still run here, unchanged, and still pass; a ladder requires every earlier test to keep passing. The new estimate gets a new name, centered, so forward and its tests stay as they were.
new: centered
Rung 2 tests 2 of 2 pass
- the centered difference of x^3 is 3x^2 + h^2, exactly (passes)
- halving h quarters the error (passes)
Combining two centered slopes to cancel the h^2 term
The centered error is at step and at step . So four times the finer estimate, minus the coarser one, over three, has no term at all:
For a cubic that is the derivative exactly, to rounding. This is Richardson extrapolation, and it uses the same idea as the previous rung: combine two estimates so that their leading error terms cancel.
new: richardson
Rung 3 tests 1 of 1 pass
- for x^3 the extrapolated slope is 3x^2 to rounding (passes)
On a function where nothing is exact
For the Taylor series never ends, so none of the three is exact and what remains is the order of each: halve and the forward error falls by about 2, the centered by about 4, the extrapolated by about 16. At , the three errors are , and . On a log-log plot they are lines of slope 1, 2 and 4, and the figure draws them from down to .
Rung 4 tests 3 of 3 pass
- halving h shrinks the three errors by factors of 2, 4 and 16, each to within 2% (passes)
- the figure's steps run from h = 0.4 down to h = 0.0125, five halvings (passes)
- the log-log slopes are 1, 2 and 4 between the two finest steps (passes)
Printed