Differences and quadrature rules
The classical formulas for derivatives and integrals, with the true error of each beside the textbook estimate: difference quotients and the step size at which rounding takes over, Richardson extrapolation, the composite midpoint, trapezoid and Simpson rules, Romberg's table, and Gauss-Legendre rules with certified nodes and weights.
const d = differences("exp(x)", 1, { kind: "central" });
d.rows; // [{ h, value, error, truncation }, …] for h = 1e-1 … 1e-12
d.best; // the h with the smallest true error
quadratureRule("exp(x)", [0, 1], { rule: "simpson", n: 4 });
// { value, exact, error, bound, order: 4 }
gaussLegendre(3); // { polynomial: "5/2*x^3 - 3/2*x", nodes, weights } enclosures
Difference quotients
| kind | formula | truncation error |
|---|---|---|
| forward | ||
| central | ||
| second |
The truncation error shrinks with . The rounding error grows: the numerator is a difference of nearly equal numbers, accurate to about with , and dividing by makes that . The total is smallest near for the forward difference and for the central one. differences evaluates the quotient in doubles for a range of and reports the true error of each against the certified derivative, so the turn is visible in the table, with the predicted truncation term beside it.
Richardson extrapolation
The central difference has an error expansion in even powers, . Then cancels the term, and repeating the idea cancels , , and so on. richardson builds the triangular table; romberg does the same for the trapezoid rule, whose error has the same kind of expansion (Euler-Maclaurin).
Composite rules
| rule | on each panel of width h | error bound |
|---|---|---|
| midpoint | ||
| trapezoid | ||
| Simpson |
quadratureRule evaluates the rule with certified arithmetic, so error is the error of the method alone, with no rounding mixed in. exact is the certified integral and bound the textbook bound with the maximum of the derivative enclosed. The true error is always inside the bound, and usually well inside.
Darboux sums
The lower and upper Darboux sums of a partition use the inf and the sup of on each piece, and , and every integral lies between them. darboux(f, partition) takes a list of points or { interval, n }. On a piece where is proven to have one sign, is monotone and its inf and sup are its end values, so for a polynomial or rational function the sums are exact fractions: for on with four pieces, and . Where monotonicity is not proven, the inf and sup come from the end values and the certified critical points inside, the sums are intervals that contain the true sums, and exact is false.
Gauss rules
With points chosen freely, a rule can be exact for all polynomials of degree below . The points are the roots of the Legendre polynomial , and the weights are . gaussLegendre(n) builds with exact coefficients, isolates its roots with proof, and returns nodes and weights as enclosures to 25 digits.
| call | returns |
|---|---|
| differences(f, x, { kind = "central", steps }) | { exact, order, rows: [{ h, value, error, truncation }], best } |
| richardson(f, x, { h = 0.5, levels = 5 }) | { table, errors, exact } |
| quadratureRule(f, [a, b], { rule, n }) | { value, exact, error, bound, order, h } |
| darboux(f, points) or darboux(f, { interval: [a, b], n }) | { lower, upper, exact, pieces: [{ from, to, monotone, inf, sup }], integral }; Rationals when exact, else RationalIntervals |
| romberg(f, [a, b], { levels = 5 }) | { table, errors, exact } |
| gaussLegendre(n), legendre(n) | { polynomial, nodes, weights }; coefficients of Pₙ |
| gaussRule(f, [a, b], n = 3) | { value, exact, error, nodes, weights } |
@ratmath/analytic and Core's R, Rational, RationalInterval in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.