Numerical mathematics – computing with a computer

Most equations in physics and engineering cannot be solved exactly. Numerical mathematics is about finding approximate solutions – and knowing how good they are. In the 1970s calculations were typically run on the university’s central computer and on the first programmable pocket calculators; today every phone has more computing power than all of Sussex’s computers put together had then. The methods are the same. The page has an interactive comparison of methods for root finding and for differential equations.

University of Sussex, Numerical Mathematics (1975–78): One of the mathematics courses in the physics syllabus. Ten years later I was to use the Newton–Raphson and Muller methods to find complex roots in my thesis at DTH. The page describes the subject as it is normally taught; it is not a reproduction of the original course.

Rounding errors

A computer calculates with a finite number of digits. Double-precision floating-point numbers (IEEE 754, standardised in 1985) have 53 binary digits, about 16 decimal places: the machine precision is $\varepsilon \approx 2.2\cdot10^{-16}$. That sounds like a lot, but errors can grow. Subtract two nearly equal numbers, and the leading digits vanish and the relative error explodes – catastrophic cancellation. The familiar formula for the roots of a quadratic therefore gives poor results when $b^2 \gg 4ac$, and it must be rewritten. The other big source of error is truncation error: an infinite process (a series, a limit) is stopped after finitely many steps. The art is to balance the two.

Root finding

To solve $f(x) = 0$ there is a range of iterative methods:

Interactive root finding

Try it: At the top is the function with Newton’s tangents (gold). At the bottom is the error $|x_n - x^*|$ for the three methods step by step on a logarithmic scale: bisection falls in a straight line, Newton plunges. $x^3 - 2x - 5$ is Newton’s own example from 1669. Try $\arctan x$ with a starting value above about 1.4: Newton diverges, while bisection calmly finds the root. At the double root of $(x-1)^2(x+2)$ Newton converges only linearly.

Interpolation

If a function is known only at some points, a polynomial can be passed through them. With $n + 1$ points there is exactly one polynomial of degree $n$ (Lagrange or Newton form). But high degrees are dangerous: Carl Runge showed in 1901 that interpolating $1/(1 + 25x^2)$ at equally spaced points oscillates wildly near the ends, and that it gets worse the more points are taken. The cure is either to crowd the points towards the ends (Chebyshev points) or to use piecewise polynomials – cubic splines, named after the flexible strips shipbuilders used to draw hulls.

Numerical integration

An integral is approximated by a weighted sum of function values. The trapezium rule replaces the function with straight segments and has an error that falls as $h^2$, where $h$ is the step length. Simpson’s rule uses parabolas through three points and has error $h^4$ – halve the step, and the error becomes 16 times smaller. Gaussian quadrature chooses both points and weights optimally and integrates polynomials of degree $2n - 1$ exactly with $n$ points. Richardson extrapolation combines results with different $h$ to remove the leading error terms – the basis of Romberg integration.

Linear systems

Gaussian elimination solves $A\vec x = \vec b$ in about $\tfrac23n^3$ operations. To avoid dividing by small numbers, rows are swapped so that the largest element is on the diagonal – partial pivoting. The condition number $\kappa(A) = \|A\|\,\|A^{-1}\|$ says how much errors in the data are amplified: with $\kappa = 10^{10}$ ten of the sixteen digits are lost. Large, sparse systems from, say, discretised differential equations are better solved iteratively, with Jacobi, Gauss–Seidel or SOR (see the relaxation in applied field theory).

Differential equations

A differential equation $\dot y = f(t, y)$ is solved step by step. Euler’s method $y_{n+1} = y_n + hf(t_n, y_n)$ follows the tangent and has a global error proportional to $h$. The fourth-order Runge–Kutta method (Runge 1895, Kutta 1901) estimates the slope four times per step and has error $h^4$ – it is the workhorse of scientific computing. For mechanical systems it is often more important to conserve energy than to be accurate in the short run: symplectic methods such as Störmer–Verlet (leapfrog) keep the energy error bounded for ever, whereas Euler lets it grow and RK4 lets it drift slowly.

Try it: A harmonic oscillator $\ddot x = -x$ is integrated with three methods and shown in the phase plane, where the exact solution is the unit circle (dashed). Euler’s method spirals outwards – it adds energy at every step. Runge–Kutta stays close to the circle but drifts slowly inwards for large steps. Leapfrog makes a slightly distorted but closed orbit, so the energy never runs away. On the right is the energy error as a function of time. Increase the number of orbits and the step length to see the difference clearly.

Since 1978

Further reading