Numerical methods

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

kindformulatruncation error
forwardf(x+h)−f(x)h\dfrac{f(x + h) - f(x)}{h}12f′′(x) h\tfrac12 f''(x)\,h
centralf(x+h)−f(x−h)2h\dfrac{f(x + h) - f(x - h)}{2h}16f(3)(x) h2\tfrac16 f^{(3)}(x)\,h^2
secondf(x+h)−2f(x)+f(x−h)h2\dfrac{f(x + h) - 2f(x) + f(x - h)}{h^2}112f(4)(x) h2\tfrac1{12} f^{(4)}(x)\,h^2

The truncation error shrinks with hh. The rounding error grows: the numerator is a difference of nearly equal numbers, accurate to about ε∣f∣\varepsilon|f| with ε≈10−16\varepsilon \approx 10^{-16}, and dividing by hh makes that ε∣f∣/h\varepsilon|f|/h. The total is smallest near h≈εh \approx \sqrt\varepsilon for the forward difference and h≈ε1/3h \approx \varepsilon^{1/3} for the central one. differences evaluates the quotient in doubles for a range of hh 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, D(h)=f′(x)+c2h2+c4h4+…D(h) = f'(x) + c_2h^2 + c_4h^4 + \dots. Then (4D(h/2)−D(h))/3\bigl(4D(h/2) - D(h)\bigr)/3 cancels the h2h^2 term, and repeating the idea cancels h4h^4, h6h^6, 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

ruleon each panel of width herror bound
midpointh f(mid)h\,f(\text{mid})(b−a)h224max⁡∣f′′∣\dfrac{(b - a)h^2}{24}\max|f''|
trapezoidh2(f(left)+f(right))\dfrac h2\bigl(f(\text{left}) + f(\text{right})\bigr)(b−a)h212max⁡∣f′′∣\dfrac{(b - a)h^2}{12}\max|f''|
Simpsonh6(f(left)+4f(mid)+f(right))\dfrac h6\bigl(f(\text{left}) + 4f(\text{mid}) + f(\text{right})\bigr)(b−a)h42880max⁡∣f(4)∣\dfrac{(b - a)h^4}{2880}\max|f^{(4)}|

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 ff on each piece, L=∑mi ΔxiL = \sum m_i\,\Delta x_i and U=∑Mi ΔxiU = \sum M_i\,\Delta x_i, and every integral lies between them. darboux(f, partition) takes a list of points or { interval, n }. On a piece where f′f' is proven to have one sign, ff is monotone and its inf and sup are its end values, so for a polynomial or rational function the sums are exact fractions: for x2x^2 on [0,1][0, 1] with four pieces, L=732L = \tfrac{7}{32} and U=1532U = \tfrac{15}{32}. 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 nn points chosen freely, a rule can be exact for all polynomials of degree below 2n2n. The points are the roots of the Legendre polynomial PnP_n, and the weights are wi=2/((1−xi2) Pn′(xi)2)w_i = 2/\bigl((1 - x_i^2)\,P_n'(x_i)^2\bigr). gaussLegendre(n) builds PnP_n with exact coefficients, isolates its roots with proof, and returns nodes and weights as enclosures to 25 digits.

callreturns
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 }
Try itEdit the code and run it with @ratmath/analytic and Core's R, Rational, RationalInterval in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.
Ctrl/⌘ + Enter runs