TExcelTAD
Home › Numerical Methods › Numerical Methods Intro

Numerical Methods Intro

Behind np.linalg.solve, scipy.optimize.brentq, np.interp and solve_ivp sit a few dozen algorithms, each with its own accuracy, cost and failure modes. Numerical analysis is the study of those algorithms: how to compute answers to continuous mathematical problems with finite-precision arithmetic, how accurate the answers are, and how fast. This course builds the core algorithms from scratch in NumPy and compares them with the library versions.

The reference book

The topic order of this course follows Fundamentals of Numerical Computation by Tobin A. Driscoll and Richard J. Braun (SIAM, 2017; the authors publish an open online edition at fncbook.com). Thanks to the authors for making it openly available.

The book is used only as a map of topics. All explanations, examples, code and exercises in these lessons are written from scratch for this site.

What is in the course

Part Topics
1. Floating point and conditioning how numbers are stored, rounding error, cancellation, condition numbers, stable algorithms
2. Nonlinear equations bisection, fixed-point iteration, Newton and secant methods, systems, quasi-Newton
3. Linear systems triangular solves, LU, operation counts, pivoting, norms, conditioning, banded and SPD matrices
4. Least squares data fitting, normal equations, QR, Householder reflections
5. Eigenvalues conditioning, power and inverse iteration, the QR algorithm, the SVD
6. Interpolation polynomials, Runge's phenomenon, Chebyshev points, piecewise linear, cubic splines
7. Differentiation and integration finite differences, truncation versus rounding error, Newton-Cotes, Gaussian and adaptive quadrature
8. Initial value problems Euler, Runge-Kutta, adaptive steps, multistep methods, stiffness, symplectic integrators
9. Boundary value problems and PDEs shooting, finite differences, heat, wave and Poisson equations
10. Iterative methods sparse matrices, Jacobi and Gauss-Seidel, conjugate gradients, preconditioning, GMRES

Three questions for every method

  1. Accuracy. How close is the computed answer to the true one? Two kinds of error compete: truncation error (the method approximates, e.g. a derivative by a difference quotient) and rounding error (the computer stores only about 16 significant digits).
  2. Cost. How many operations, as a function of the problem size nn or the accuracy wanted?
  3. Robustness. When does it fail, and does it warn you?

One tiny example shows all three. Approximate f′(1)f'(1) for f(x)=exf(x) = e^x by f(1+h)−f(1)h\frac{f(1 + h) - f(1)}{h}:

python · try it yourself
import numpy as np

exact = np.exp(1.0)
for h in (1e-1, 1e-4, 1e-8, 1e-12, 1e-15):
    approx = (np.exp(1 + h) - np.exp(1)) / h
    print(f"h = {h:.0e}: approx {approx:.12f}, error {abs(approx - exact):.1e}")
h = 1e-01: approx 2.858841954874, error 1.4e-01
h = 1e-04: approx 2.718417747083, error 1.4e-04
h = 1e-08: approx 2.718281821856, error 6.6e-09
h = 1e-12: approx 2.718714142702, error 4.3e-04
h = 1e-15: approx 3.108624468950, error 3.9e-01

Making hh smaller first helps (less truncation error), then hurts (more rounding error). The best hh is about 10−810^{-8}, and even then we only get about 8 correct digits. Understanding this trade-off is the subject of Error and Step Size in Finite Differences.

What you should know first

How lessons work

Each lesson explains one method, writes it as a short NumPy function, measures its error and cost, compares it with SciPy, and ends with an In software note, exercises with answers and worked solutions, and a short quiz. All examples run in your browser.

Tip: Always test a numerical method on a problem whose exact answer you know, and look at how the error changes when you change the step size or the number of iterations. A method that converges at the predicted rate is very likely implemented correctly.

Learning paths

Your goal Suggested route
Scientific computing / simulation parts 1, 3, 7, 8, 9, 10
Machine learning engineering parts 1, 2, 3, 4, 5, then Math for Machine Learning
Game and graphics programming parts 1, 6, 8 → Game Physics
Data analysis parts 1, 4, 6, 7 → SciPy

Exercises

Exercise 1: Truncation or rounding?

Classify each error: (a) approximating sin⁡x\sin x by xx for small xx; (b) 0.1+0.2≠0.30.1 + 0.2 \ne 0.3 in Python; (c) stopping Newton's method after 3 iterations.

Answer

(a) truncation; (b) rounding; (c) truncation.

Solution

Truncation errors come from the mathematical approximation (dropping Taylor terms, stopping an iteration) and would remain even with exact arithmetic. Rounding errors come from finite precision: 0.10.1, 0.20.2 and 0.30.3 have no exact binary representation.

Exercise 2: Cost growth

An algorithm takes 0.2 s for n=1000n = 1000 and its cost grows like n3n^3. How long for n=4000n = 4000?

Answer

12.8 s.

Solution

43=644^3 = 64 times longer: 0.2⋅64=12.80.2 \cdot 64 = 12.8 s. Gaussian elimination is such an algorithm (Operation Counts and Efficiency).

Exercise 3: A better difference (code)

Repeat the lesson's experiment with the central difference f(1+h)−f(1−h)2h\frac{f(1 + h) - f(1 - h)}{2h}. What is the smallest error you can reach, and at which hh?

Answer

About 6×10−116 \times 10^{-11}, at h=10−5h = 10^{-5} (versus 7×10−97 \times 10^{-9} for the forward difference).

Solution

The central difference has truncation error O(h2)O(h^2), so the balance with rounding error (≈ϵ/h\approx \epsilon/h) is reached at a larger h≈ϵ1/3h \approx \epsilon^{1/3}.

python · try it yourself
import numpy as np
exact = np.exp(1.0)
for h in (1e-3, 1e-4, 1e-5, 1e-6, 1e-8):
    err = abs((np.exp(1 + h) - np.exp(1 - h)) / (2 * h) - exact)
    print(f"h = {h:.0e}: error {err:.1e}")
h = 1e-03: error 4.5e-07
h = 1e-04: error 4.5e-09
h = 1e-05: error 5.9e-11
h = 1e-06: error 1.6e-10
h = 1e-08: error 6.6e-09

Test yourself

Which error would remain even with exact (infinite-precision) arithmetic?

  1. Rounding error
  2. Truncation error
  3. Overflow
  4. None

Why does the forward-difference error grow again for very small hh?

  1. The function changes
  2. Rounding errors in f(1+h)−f(1)f(1 + h) - f(1) are divided by a tiny hh
  3. Python is slow
  4. Truncation error grows

Roughly how many significant decimal digits does a double-precision float carry?

  1. 7
  2. 16
  3. 32
  4. unlimited