Skip to content

Numerical Methods

When exact answers run out — root finding, integration, ODE solvers, and the discipline of error

1. Why numerics — and what can go wrong

Almost every problem you meet outside a textbook has no closed form: the root of x = cos x, the integral of e−x² over a finite interval, a nonlinear ODE, a PDE on an odd-shaped domain, a linear system with a million unknowns. Numerical analysis is the craft of replacing the exact problem by a finite computation and knowing how wrong the answer is.

Three kinds of error enter. Modelling error is the physics you left out. Truncation error comes from the approximation itself — a difference instead of a derivative, a sum instead of an integral, a few terms of a series. Round-off error comes from the computer keeping only about 16 significant digits:

Floating-point model

Round-off is tiny per operation, but subtracting two nearly equal numbers throws away the leading digits they share. In double precision, 1 − cos(10−8) evaluates to exactly 0, while the algebraically equal 2 sin²(x/2) gives the correct 5 × 10−17. The fix is a better algorithm, not more precision.

Try it

In floating point, why is 0.1 + 0.2 == 0.3 false?

2. Root finding: bisection vs Newton

To solve f(x) = 0, bisection needs only a sign change: if f(a) and f(b) have opposite signs, a root is trapped between them; evaluate the midpoint and keep the half that still changes sign. It never fails, but it gains just one binary digit per step: after n steps the error is at most (b − a)/2n.

Newton’s method uses more information — the slope. Replace f by its tangent line at the current guess and jump to where the tangent crosses the axis:

Newton’s method

The error is squared each step (quadratic convergence): once you are close, the number of correct digits doubles. Watch both methods attack x² − 2 = 0.

Worked example: √2 by Newton’s method

For f(x) = x² − 2 the update simplifies to the ancient Babylonian rule xn+1 = ½(xn + 2/xn). From x₀ = 1:

The errors are 8.6 × 10⁻², 2.5 × 10⁻³, 2.1 × 10⁻⁶, 1.6 × 10⁻¹²: each is roughly the previous one squared divided by 2√2, exactly as the error formula predicts. Four steps give twelve digits.

Bisection for comparison: to shrink the bracket [0, 2] to width 10⁻⁶ needs n ≥ log₂(2/10⁻⁶) ≈ 20.9, so 21 steps for what Newton did in about 4.

Try it

Apply Newton’s method to f(x) = x² (a double root at 0) starting from x₀ = 1. How fast does it converge?

3. Integration & the order of accuracy

Numerical integration replaces f by something easy to integrate on each small piece. The trapezoid rule uses straight segments; Simpson’s rule uses parabolas through three points:

Composite trapezoid rule
Composite Simpson rule (n even)

The error bounds explain why Simpson is worth the extra bookkeeping. Both scale as a power of h — the order of accuracy p — so halving h divides the error by 2p:

Error bounds

Worked example: ∫₀^π sin x dx = 2 with n = 4, then n = 8

With n = 4, h = π/4 and the samples sin(0), sin(π/4), sin(π/2), sin(3π/4), sin(π) = 0, 0.7071, 1, 0.7071, 0:

Errors: 0.104 for the trapezoid, 0.0046 for Simpson. Doubling to n = 8 gives T₈ = 1.9742 (error 0.0258, ≈ ¼ of before) and S₈ = 2.00027 (error 0.00027, ≈ 1/16 of before). The ratios 4 and 16 are the signature of orders 2 and 4.

Try it

A Simpson-rule integral has error 3 × 10⁻⁴ with n = 20. Roughly what n gives an error below 10⁻⁷?

4. ODE solvers: from Euler to Runge–Kutta

For y′ = f(t, y), Euler’s method follows the slope field from the ODE chapter in straight steps: stand at (tn, yn), read the slope, walk h. It is first-order: the error per step is O(h²), accumulating to O(h) after 1/h steps. Runge–Kutta methods sample the slope field several times per step and combine the samples so that the error terms cancel. The classical RK4:

Classical Runge–Kutta (RK4)

Four evaluations per step buy fourth-order accuracy. To see what that means, integrate x″ = −x — a circle in the phase plane — with a fairly coarse step:

Euler’s trouble is systematic: for this system each step multiplies the radius by √(1 + h²) > 1, so it manufactures energy. Now measure the order directly. Halve h repeatedly on a test problem with a known answer and plot the final error on log–log axes:

Worked example: one step of Euler vs RK4

Take y′ = y − t² + 1 with y(0) = 0.5 and h = 0.2. The exact solution is y = (t + 1)² − ½eᵗ, so y(0.2) = 0.8292986.

Euler: y₁ = 0.5 + 0.2 · 1.5 = 0.8000 (error 0.029).

RK4: k₁ = 1.5, k₂ = 1.64, k₃ = 1.654, k₄ = 1.7908, so

The RK4 error is 5 × 10⁻⁶ — about 5000 times smaller for four times the work.

Stability and stiff equations

Accuracy is not enough. Apply Euler to y′ = −λy: each step multiplies y by (1 − λh), so the numerical solution decays only if |1 − λh| < 1, i.e. h < 2/λ. For a fast-decaying component (large λ) that forces a tiny step even when the fast component is long gone and you only care about the slow behaviour. Such problems are called stiff, and they are solved with implicit methods like backward Euler, yn+1 = yn + h f(tn+1, yn+1), which cost an equation solve per step but stay stable for any h.

Try it

For y′ = −50y, Euler is run with h = 0.05. What happens, and does backward Euler fix it?

5. PDEs on a grid: finite differences & stability

To solve the heat equation ut = α²uxx from the PDE chapter on a computer, lay a grid of spacing Δx and time step Δt, and replace each derivative by a difference:

Explicit finite-difference scheme (FTCS)

It is cheap and easy — and it can explode. Test a single Fourier mode ujn = GⁿeikjΔx(von Neumann analysis): each step multiplies that mode by the amplification factor

Amplification factor

Worked example: how big a time step is allowed?

Take α² = 1 and Δx = 0.1. Stability needs Δt ≤ Δx²/(2α²) = 0.005.

Refine the grid to Δx = 0.05: the limit drops to Δt ≤ 0.00125. In one space dimension that is 2× more nodes and 4× more steps — 8× the work for 2× the resolution. This is why implicit schemes and fast linear solvers matter.

Try it

A rod is simulated with α² = 4 and Δx = 0.02. What is the largest stable Δt for the explicit scheme?

6. Linear systems: direct vs iterative

Every grid method above ends in a linear system Ax = b. Direct methods (Gaussian elimination / LU, see the Subspaces & Least Squares chapter) cost about (2/3)n³ operations: fine for a few thousand unknowns, hopeless for a million. But PDE matrices are sparse (a few nonzeros per row), and iterative methods exploit that by repeatedly improving a guess:

Jacobi and Gauss–Seidel

Gauss–Seidel reuses the freshly updated components immediately, which typically halves the iteration count. Both are guaranteed to converge when A is strictly diagonally dominant, |aii| > Σj≠i|aij|. Industrial solvers use their descendants: conjugate gradients and multigrid.

Worked example: Jacobi vs Gauss–Seidel on a 3 × 3 system

Start from x = 0. The first three sweeps give:

kJacobiGauss–Seidel
1(0.5, 1, 2.5)(0.5, 1.125, 2.781)
2(0.75, 1.75, 2.75)(0.781, 1.891, 2.973)
3(0.938, 1.875, 2.938)(0.973, 1.986, 2.997)

To bring the residual below 10⁻⁶, Jacobi needs 16 iterations and Gauss–Seidel 9. Row 1 is diagonally dominant (4 > 1), row 2 (4 > 2) and row 3 (4 > 1), so convergence was guaranteed.

7. Practice & where it shows up

Try it: which method?

You must find the root of a smooth function you can only evaluate (no formula for f′), and you know a bracket [a, b] with a sign change. Which method would you reach for first, and why?

Try it: explain the numbers

A student halves the step in an RK4 solver and sees the error drop from 8 × 10⁻⁶ to 5 × 10⁻⁷. Then, halving again, it only drops to 4 × 10⁻⁷. What is going on?

Circuit & system simulators

SPICE-style tools integrate stiff ODEs with implicit methods; MATLAB’s ode45 is an adaptive Runge–Kutta pair that picks the step size for you.

CFD, FEM & weather

Every PDE solver is a grid, a stable time-stepping rule, and a big sparse linear solve per step. The stability limits in this chapter set the time step.

Machine learning

Gradient descent xₖ₊₁ = xₖ − η∇f(xₖ) is Euler’s method on x′ = −∇f. For f = (L/2)x² it is stable only if η < 2/L — the same story as r ≤ ½.

Everyday floating point

Never compare floats with ==, avoid subtracting nearly equal numbers, and sum small terms first. Most “mystery bugs” in numerical code are one of these.

8. Resources

This chapter follows the numerical-analysis threads of MIT 18.330 (root finding, quadrature, ODE solvers) and 18.085 (finite differences, iterative solvers), and the scientific-computing content of Stanford’s CME 108.

Site soundtrack