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
- 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).
- Cost. How many operations, as a function of the problem size or the accuracy wanted?
- Robustness. When does it fail, and does it warn you?
One tiny example shows all three. Approximate for by :
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 smaller first helps (less truncation error), then hurts (more rounding error). The best is about , 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
- Calculus: derivatives, integrals, Taylor series (Calculus Essentials, Taylor and Maclaurin Series).
- Linear algebra: matrices, solving systems, eigenvalues (Linear Algebra & Matrix Theory).
- NumPy arrays and vectorized code.
- The theory of ODEs and PDEs is in Differential Equations; this course is about solving them on a computer.
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 by for small ; (b) 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: , and have no exact binary representation.
Exercise 2: Cost growth
An algorithm takes 0.2 s for and its cost grows like . How long for ?
Answer
12.8 s.
Solution
times longer: 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 . What is the smallest error you can reach, and at which ?
Answer
About , at (versus for the forward difference).
Solution
The central difference has truncation error , so the balance with rounding error () is reached at a larger .
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?
- Rounding error
- Truncation error
- Overflow
- None
Why does the forward-difference error grow again for very small ?
- The function changes
- Rounding errors in are divided by a tiny
- Python is slow
- Truncation error grows
Roughly how many significant decimal digits does a double-precision float carry?
- 7
- 16
- 32
- unlimited