Skip to content
Back to the surface

Numerical algorithms

Six numerical analysis projects, from the limits of machine numbers to the three-body problem, each method written by hand.

6
projects
SVD
image compression
RK4
and three more solvers
Role
Team
Stack
PythonNumPySciPyLaTeX
Numerical algorithms

Six projects, from the bits to the planets

Six numerical analysis projects for the IS104 course at ENSEIRB-MATMECA (semester 6, 2024–2025), done in teams, in Python with NumPy, each with its report in LaTeX. Every one starts from the mathematics, implements the method by hand, then measures it against NumPy or SciPy.

1 · The limits of the machine

Floats of very low precision are simulated, with four decimal digits and our own machine addition and multiplication, to measure their relative error: it stays around 10⁻ᵖ, and drops to zero when a value is exactly representable. Then ln 2 is summed from its alternating series, stopping as soon as a new term no longer changes the p-th decimal, and the report studies the CORDIC algorithms that calculators use to rebuild ln, exp or tan from a few stored values.

Relative error of x + y with p = 4: a sawtooth that stays below 10⁻⁴.
Relative error of x + y with p = 4: a sawtooth that stays below 10⁻⁴.
The partial sums of the series of ln 2, settling on the final value.
The partial sums of the series of ln 2, settling on the final value.

2 · Linear systems and the heat equation

The Cholesky factorisation, then an incomplete version that keeps the matrix sparse and serves as a preconditioner; the conjugate gradient written by hand and checked against SciPy. Then the heat on a square plate: discretised, the equation becomes a large sparse linear system, solved by the conjugate gradient.

The temperature of the plate, found by our conjugate gradient.
The temperature of the plate, found by our conjugate gradient.
The residual falling with the iterations: our method and SciPy's overlap.
The residual falling with the iterations: our method and SciPy's overlap.
Mean condition number, complete or incomplete Cholesky, against the density of the matrix.
Mean condition number, complete or incomplete Cholesky, against the density of the matrix.

Watch the conjugate gradient heat the plate

A 30 × 30 plate, heated in the middle and held at zero on its edges: 900 unknowns. Slide through the iterations of the conjugate gradient.

3 · Compressing an image with the SVD

The singular value decomposition built by hand: Householder reflections bring the matrix to a bidiagonal form, then QR iterations make it diagonal. An image is then kept with only its k largest singular values: a few numbers per row and per column instead of every pixel.

Rank 5.
Rank 5.
Rank 50.
Rank 50.
Rank 130.
Rank 130.
Reconstruction error against k: most of it is gone after twenty values.
Reconstruction error against k: most of it is gone after twenty values.

Compress an image yourself

One of this site's own fractals, 192 × 120 pixels. Keep the k largest singular values of each colour.

The decomposition is computed in advance with NumPy; the page rebuilds the image from k values as you slide.

4 · Newton-Raphson, Lagrange and Bairstow

Newton's method with a Jacobian for nonlinear systems, used to find the five Lagrange points of a two-body system, where gravity and the rotation of the frame balance out; and Bairstow's method to factorise polynomials, compared with Newton-Raphson on time and on the roots it finds.

The five Lagrange points of the Earth–Moon system.
The five Lagrange points of the Earth–Moon system.
The magnitude of the total force, and where it vanishes.
The magnitude of the total force, and where it vanishes.
Bairstow against Newton-Raphson: time and roots found.
Bairstow against Newton-Raphson: time and roots found.

5 · Splines and integration

Cubic splines through the points of a real NASA airfoil, upper and lower surfaces apart; then the length of curves by numerical integration, with rectangles, midpoints, trapezoids and Simpson, and step doubling until a target precision. They model a laminar airflow around the wing and the pressure it creates.

The airfoil, interpolated by cubic splines.
The airfoil, interpolated by cubic splines.
The laminar airflow around it.
The laminar airflow around it.
The normalised dynamic pressure.
The normalised dynamic pressure.

Bend a cubic spline

Drag the points: the curve through them is a natural cubic spline, smooth up to its second derivative.

6 · Differential equations

Euler, midpoint, Heun and fourth-order Runge-Kutta, written by hand and compared on equations with a known solution. Then the simple and double pendulum, its extreme sensitivity to the starting angles, and the restricted three-body problem, with an asteroid drifting around the L5 point.

The trajectories of the two masses of a double pendulum.
The trajectories of the two masses of a double pendulum.
Two double pendulums started almost alike, then apart.
Two double pendulums started almost alike, then apart.
An asteroid near L5, in the rotating frame.
An asteroid near L5, in the rotating frame.

Euler against Runge-Kutta

The same equation, whose exact solution is a circle, solved for three turns with four methods. Enlarge the step and watch Euler spiral away.

  • Euler
  • Midpoint
  • Heun
  • RK4
  • exact

The double pendulum came back later, in real time and by hand: Chaos Lab

Figures of project 5 regenerated on 27 November 2026 by running the code of the repository.