Numerical methods

Interpolation and approximation

The polynomial through a set of points, in Newton's and Lagrange's forms with exact divided differences; the error formula beside the true error; Chebyshev nodes and the Runge phenomenon; natural cubic splines and least-squares polynomials, all with exact rational coefficients.

const p = interpolate([[0, 1], [1, 3], [2, 2]]);
p.polynomial;            // "-3/2*x^2 + 7/2*x + 1"
p.newton;                // "1 + 2*x - 3/2*x*(x - 1)"
p.dividedDifferences;    // [[1, 3, 2], [2, -1], [-3/2]]   exact
p.at("1/2");             // 19/8

const q = interpolate("sin(x)", { nodes: equallySpaced(0, 1, 4) });
q.maxError([0, 1]);      // { value, attained, at, textbook }

Divided differences

Through n+1n + 1 points with distinct xix_i there is exactly one polynomial of degree at most nn. Newton's form builds it one node at a time:

p(x)=f[x0]+f[x0,x1](x−x0)+f[x0,x1,x2](x−x0)(x−x1)+…,f[xi,…,xi+k]=f[xi+1,…,xi+k]−f[xi,…,xi+k−1]xi+k−xi. p(x) = f[x_0] + f[x_0, x_1](x - x_0) + f[x_0, x_1, x_2](x - x_0)(x - x_1) + \dots, \qquad f[x_i, \dots, x_{i+k}] = \frac{f[x_{i+1}, \dots, x_{i+k}] - f[x_i, \dots, x_{i+k-1}]}{x_{i+k} - x_i}.

The table of divided differences is computed in exact rational arithmetic, so for data given as rationals the polynomial is the true one, with no rounding. Lagrange's form writes the same polynomial as ∑yi ℓi(x)\sum y_i\,\ell_i(x), where ℓi\ell_i is 1 at xix_i and 0 at the other nodes.

The error formula

If ff has n+1n + 1 continuous derivatives, then for each xx there is a ξ\xi with

f(x)−p(x)=f(n+1)(ξ)(n+1)! ∏i=0n(x−xi). f(x) - p(x) = \frac{f^{(n+1)}(\xi)}{(n+1)!}\,\prod_{i=0}^{n}(x - x_i).

maxError reports the true maximum of ∣f−p∣|f - p| on an interval, bracketed between a value attained and a proven upper bound, and beside it the bound the formula gives, max⁡∣f(n+1)∣/(n+1)!⋅max⁡∣∏(x−xi)∣\max|f^{(n+1)}| / (n+1)! \cdot \max|\prod (x - x_i)|, with both maxima enclosed.

Runge's example and Chebyshev nodes

More nodes need not mean a better fit. For f(x)=1/(1+25x2)f(x) = 1/(1 + 25x^2) on [−1,1][-1, 1] with equally spaced nodes the error near the ends grows without bound as the degree rises. The product ∏(x−xi)\prod(x - x_i) is the part of the error formula that the choice of nodes controls, and it is smallest in maximum when the nodes are the Chebyshev points

xi=cos⁡(2i+1)π2n,i=0,…,n−1, x_i = \cos\frac{(2i + 1)\pi}{2n}, \qquad i = 0, \dots, n - 1,

which crowd towards the ends. chebyshevNodes(a, b, n) returns them rounded to nearby rationals.

Splines and least squares

A natural cubic spline is a cubic on each interval between nodes, with two continuous derivatives across the nodes and zero second derivative at the two ends. The second derivatives at the nodes solve a tridiagonal system, here exactly. A least-squares polynomial of degree dd minimises ∑(p(xi)−yi)2\sum (p(x_i) - y_i)^2; its coefficients solve the normal equations ATA c=ATyA^{\mathsf T}A\,c = A^{\mathsf T}y, again exactly, and the residual is returned as an exact rational.

callreturns
interpolate(points) or interpolate(f, { nodes }){ nodes, values, dividedDifferences, newton, lagrange, polynomial, coefficients, at(x), error(x), maxError([a, b]) }
equallySpaced(a, b, n), chebyshevNodes(a, b, n)arrays of Rationals
spline(points){ pieces: [{ from, to, coefficients, polynomial }], moments, at(x) }
leastSquares(points, degree = 1){ coefficients, polynomial, residual, at(x) }
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