Numerical Computing

and Scientific Simulation

ML for Science - Lecture 5

From differential equations to predictions on a computer

The Big Picture: Learning vs Inference

Data
(high complexity)
Model
(low complexity)
Predictions
(high complexity)
Learning
Data → Model
Compression / Distillation
Statistical
Inference
Model → Predictions
Reconstruction / Generation
Computational / Recursive

Today: From Models to Predictions

We've seen how to extract models from data (learning/fitting).

Now: How do we use models to make predictions?

Key insight: Both learning and inference require computing, but they're fundamentally different:
  • Learning: Compress many observations into simple rules
  • Inference: Apply rules recursively to generate predictions

Computing as Recursive Rule Application

The most basic form of computing: apply a rule repeatedly

Peano Axioms: Generating Natural Numbers

$0, \; S(0), \; S(S(0)), \; S(S(S(0))), \; \ldots$

Start with 0, apply successor function S repeatedly → all natural numbers

This is just a for loop:

i = 0
for _ in range(n):
    i = i + 1  # successor

Example: Fibonacci Sequence

$F_n = F_{n-1} + F_{n-2}$

Simple rule → Complex, interesting sequence

def fib(n):
    a, b = 0, 1
    for _ in range(n):
        a, b = b, a + b
    return a

Properties emerge:

  • Ratios converge to golden ratio $\phi$
  • Which are prime? Even?
  • Open questions in number theory!
Pattern: Simple recursive rule → Generate sequence → Ask questions about properties

Historical Example: Titius-Bode Law

Planetary distances follow a pattern:

$a_n = 0.4 + 0.3 \times 2^n$ (in AU)

The process:

  1. Observe data (planetary distances)
  2. Find the rule (the formula)
  3. Use the rule to predict → missing planet?
This predicted the asteroid belt and helped discover Uranus!

Cellular Automata: Game of Life

Von Neumann's idea (1940s) + Conway's rules (1970): simple local rules → complex emergent behavior

Click cells to toggle | Generation: 0

Rules (each cell):
  • Alive + 2-3 neighbors → survives
  • Alive + <2 neighbors → dies
  • Alive + >3 neighbors → dies
  • Dead + 3 neighbors → born
Turing complete! Can simulate any computation.

From Differential Equations to Discrete

Recall Galileo's projectile: $\displaystyle\frac{d^2x}{dt^2} = -g$

We integrated analytically: $x(t) = -\frac{1}{2}gt^2 + v_0 t + x_0$

Problem: Most equations can't be integrated analytically!

Solution: Solve on a computer by going back to discrete

Discretization

Turn derivatives back into differences:

$\frac{dx}{dt} \approx \frac{x_{n+1} - x_n}{\Delta t}$

For $\frac{d^2x}{dt^2} = -g$, introduce velocity $v = \frac{dx}{dt}$:

$v_{n+1} = v_n - g \cdot \Delta t$
$x_{n+1} = x_n + v_n \cdot \Delta t$
Like Fibonacci: given previous values, compute the next one recursively!

Interactive: Step-by-Step Integration

0.20
Initial conditions:
$x_0 = 0$ m
$v_0 = 15$ m/s (upward)
$g = 9.8$ m/s²
Click "Step" to begin...

Blue dots = Numerical (Euler) | Green dashed = Analytical solution

The Role of $\Delta t$

$\Delta t$ controls the accuracy of the approximation

  • Smaller $\Delta t$ → more accurate, but slower
  • Larger $\Delta t$ → faster, but less accurate
  • Too large → solution can become unstable!
Key trade-off: Speed vs Accuracy

Case Study: The Hudson Bay Fur Records

The Fur Trade Business:

  • 1670-1920s: Hudson's Bay Company was a fur trading giant
  • Beaver and lynx pelts were highly prized for European fashion
  • Company kept detailed ledgers of pelts purchased from trappers
  • Why track pelts? Inventory, pricing, trapper payments
1924: Ecologist Charles Elton analyzed 90 years of pelt records
Discovered striking ~10-year cycles in lynx and hare populations
The puzzle: Why do populations oscillate so regularly?
🐰 Snowshoe Hare
🐱 Canadian Lynx

Hudson Bay Company records (1900-1920)
■ Hare | ■ Lynx (thousands)

The Lotka-Volterra Model

Lotka (1925) and Volterra (1926) independently proposed:

The logic:

  • More prey → predators thrive
  • More predators → eat more prey
  • Fewer prey → predators starve
  • Fewer predators → prey recover
  • Oscillation!
$\displaystyle\frac{dx}{dt} = \color{#98c379}{\alpha}\, x - \color{#e5c07b}{\beta}\, xy$

$\displaystyle\frac{dy}{dt} = \color{#c678dd}{\delta}\, xy - \color{#56b6c2}{\gamma}\, y$

$x$ = prey (hares), $y$ = predators (lynx)
$\color{#98c379}{\alpha}$ = prey growth | $\color{#e5c07b}{\beta}$ = predation rate
$\color{#c678dd}{\delta}$ = predator efficiency | $\color{#56b6c2}{\gamma}$ = predator death

Discretizing the Equations

Apply Euler's method to each equation:

Continuous:
$\frac{dx}{dt} = \alpha x - \beta xy$

$\frac{dy}{dt} = \delta xy - \gamma y$
Discrete (Euler):
$x_{n+1} = x_n + \Delta t (\alpha x_n - \beta x_n y_n)$

$y_{n+1} = y_n + \Delta t (\delta x_n y_n - \gamma y_n)$
The recipe: Given $(x_n, y_n)$ at time $t_n$, compute $(x_{n+1}, y_{n+1})$ at time $t_{n+1} = t_n + \Delta t$

Fitting the Model to Data

$\displaystyle\frac{dx}{dt} = \color{#98c379}{\alpha}\, x - \color{#e5c07b}{\beta}\, xy$

$\displaystyle\frac{dy}{dt} = \color{#c678dd}{\delta}\, xy - \color{#56b6c2}{\gamma}\, y$
0.55
0.028
0.018
0.84

Dots = Real data | Lines = Model simulation | Hare | Lynx

Case Study: The Pendulum

The equation of motion:

$\displaystyle\frac{d^2\theta}{dt^2} = -\frac{g}{L}\sin\theta$

For small angles: $\sin\theta \approx \theta$ → simple harmonic motion

For large angles: nonlinear! Must solve numerically.

Discretizing the Pendulum

Introduce angular velocity $\omega = \frac{d\theta}{dt}$, then apply Euler:

Continuous:
$\displaystyle\frac{d\theta}{dt} = \omega$

$\displaystyle\frac{d\omega}{dt} = -\frac{g}{L}\sin\theta$
Discrete (Euler):
$\theta_{n+1} = \theta_n + \Delta t \cdot \omega_n$

$\omega_{n+1} = \omega_n - \Delta t \cdot \frac{g}{L}\sin\theta_n$
Same pattern: Given $(\theta_n, \omega_n)$ at time $t_n$, compute $(\theta_{n+1}, \omega_{n+1})$ at $t_{n+1} = t_n + \Delta t$

Pendulum Phase Space

0.5

Left: Pendulum animation | Right: Phase space (θ, ω)

Phase Space: The Full Picture

Multiple trajectories in phase space for different initial conditions

Small θ₀: circles | Large θ₀: distorted "eye" shapes

Error Analysis: How Accurate?

Unlike Fibonacci (exact), numerical solutions have error. Use Taylor expansion:

$x(t + \Delta t) = x(t) + \Delta t \cdot \dot{x}(t) + \frac{(\Delta t)^2}{2}\ddot{x}(t) + O((\Delta t)^3)$

Rearranging for the derivative:

$\frac{x(t + \Delta t) - x(t)}{\Delta t} = \dot{x}(t) + \underbrace{\frac{\Delta t}{2}\ddot{x}(t) + O((\Delta t)^2)}_{\text{Error} = O(\Delta t)}$

Key insight: Euler method has error $O(\Delta t)$ — halving step size halves the error

Finite Difference Schemes

Forward
$\displaystyle\frac{x_{n+1} - x_n}{\Delta t}$

Error: $O(\Delta t)$
Backward
$\displaystyle\frac{x_n - x_{n-1}}{\Delta t}$

Error: $O(\Delta t)$
Central
$\displaystyle\frac{x_{n+1} - x_{n-1}}{2\Delta t}$

Error: $O((\Delta t)^2)$

Central difference is more accurate for the same $\Delta t$!

Second Derivative

$\frac{d^2 x}{dt^2} \approx \frac{x_{n+1} - 2x_n + x_{n-1}}{(\Delta t)^2}$

Error: $O((\Delta t)^2)$

The pattern: Higher-order schemes = more neighbors used = better accuracy = more computation

PDEs: Functions of Space AND Time

Now $u(x, t)$ depends on both space and time

Diffusion Equation: $\displaystyle\frac{\partial u}{\partial t} = D\frac{\partial^2 u}{\partial x^2}$

Examples where diffusion appears:

  • Heat conduction: Temperature spreads through a material
  • Chemical diffusion: Ink spreading in water, pollutants in air
  • Electrical signals: Current spreading in neural axons
  • Population dynamics: Species spreading into new territory
  • Information: Rumors spreading through a network

Discretizing Space and Time

Define a grid with spacing $\Delta x$ in space and $\Delta t$ in time:

$u_i^n \equiv u(x_i, t_n)$ where $x_i = i \cdot \Delta x$ and $t_n = n \cdot \Delta t$

Apply finite differences to both derivatives:

Time: $\frac{\partial u}{\partial t} \approx \frac{u_i^{n+1} - u_i^n}{\Delta t}$
Space: $\frac{\partial^2 u}{\partial x^2} \approx \frac{u_{i+1}^n - 2u_i^n + u_{i-1}^n}{(\Delta x)^2}$
Update rule: $u_i^{n+1} = u_i^n + r(u_{i+1}^n - 2u_i^n + u_{i-1}^n)$   where $r = D\frac{\Delta t}{(\Delta x)^2}$

PDE Discretization: Step by Step

Current stencil:
Click "Step" to begin...

u value (0 to 1)

"Step Stencil" moves one cell in space | "Next Row" completes a time step

The Stencil

Each new value depends on neighboring values:

FTCS

(Forward Time,
Central Space)

$u_i^{n+1}$
$u_{i-1}^n$
$u_i^n$
$u_{i+1}^n$

$u_i^{n+1} = u_i^n + r(\Delta u)$

BTCS

(Backward Time,
Central Space)

$u_{i-1}^{n+1}$
$u_i^{n+1}$
$u_{i+1}^{n+1}$
$u_i^n$

Solve: $Au^{n+1} = u^n$

Crank-Nicolson

(Average
FTCS + BTCS)

$O((\Delta t)^2, (\Delta x)^2)$ accuracy

Stability: The CFL Condition

0.40
CFL Condition:

$r = \frac{D \cdot \Delta t}{(\Delta x)^2} \leq \frac{1}{2}$
r = 0.40: Stable ✓
Solution remains bounded

Try r > 0.5 to see instability!

Courant-Friedrichs-Lewy condition →

Implicit schemes (BTCS, Crank-Nicolson) are unconditionally stable but require solving linear systems.

Physics-Informed Numerical Solvers

How do we build numerical methods that respect physical laws?

  • Better integrators (Runge-Kutta)
  • Conservation laws (Finite Volume)
  • Stability & incompressibility (Navier-Stokes)

Better Integrators: Runge-Kutta

The workhorse of scientific computing

RK4:   $x_{n+1} = x_n + \frac{\Delta t}{6}(k_1 + 2k_2 + 2k_3 + k_4)$
$k_1 = f(t_n, x_n)$
$k_2 = f(t_n + \frac{\Delta t}{2}, x_n + \frac{\Delta t}{2}k_1)$
$k_3 = f(t_n + \frac{\Delta t}{2}, x_n + \frac{\Delta t}{2}k_2)$
$k_4 = f(t_n + \Delta t, x_n + \Delta t \cdot k_3)$

Sample the slope at 4 points, take a weighted average → 4th-order accuracy!

Why RK4?

  • Error: $O((\Delta t)^4)$ — much better than Euler!
  • Evaluates $f$ at multiple points per step
  • Good balance of accuracy and efficiency
  • Used in most ODE solvers (MATLAB's ode45, SciPy's solve_ivp)
Adaptive methods: Automatically adjust $\Delta t$ based on estimated error

Preserving Physics: Finite Volume Methods

When equations have conservative form:

$\frac{\partial \rho}{\partial t} + \frac{\partial F}{\partial x} = 0$

Finite volume methods ensure conservation is preserved in the discretization.

Key idea: What flows out of one cell flows into the next.
No mass/energy is created or destroyed numerically.

Theme in Scientific ML: Encode physical knowledge into numerical schemes

Finite Volume: The Flux Picture

$\rho_{i-1}$
$\rho_i$
$\rho_{i+1}$
$F_{i-\frac{1}{2}}$
$F_{i+\frac{1}{2}}$
$\rho_i^{n+1} = \rho_i^n - \frac{\Delta t}{\Delta x}(F_{i+\frac{1}{2}} - F_{i-\frac{1}{2}})$
Upwind Scheme:
If $u > 0$ (flow right):
  $F_{i+\frac{1}{2}} = u \cdot \rho_i$

If $u < 0$ (flow left):
  $F_{i+\frac{1}{2}} = u \cdot \rho_{i+1}$
Why upwind?
Information travels with the flow.
Using "downwind" values is unstable!

Conservation: What enters one cell exits the neighbor. No numerical mass loss.

Interactive: 2D Fluid Dynamics

Navier-Stokes: $\begin{aligned} \frac{\partial \mathbf{u}}{\partial t} + (\mathbf{u} \cdot \nabla)\mathbf{u} &= -\frac{1}{\rho}\nabla p + \nu \nabla^2 \mathbf{u} \\ \nabla \cdot \mathbf{u} &= 0 \quad \text{(incompressible)} \end{aligned}$
Solver:
  1. Add source
  2. Diffuse ($\nu$)
  3. Project
  4. Advect
  5. Project
1e-4
1e-4
15
$\nu$ = viscosity (momentum diffusion)
$D$ = dye diffusion coefficient
$|u_0|$ = inlet velocity magnitude

Red = source | Mouse = obstacle (blocks flow) | Real Navier-Stokes in browser!

The Discretized Equations

Each step solves part of Navier-Stokes numerically:

1. Diffusion (implicit, stable)

$(I - \nu \Delta t \nabla^2) u^{n+1} = u^n$

Gauss-Seidel iteration:
$u_{i,j}^{k+1} = \frac{u_{i,j}^n + a \sum_{\text{neighbors}}}{1 + 4a}$
2. Advection (semi-Lagrangian)

$q^{n+1}(\mathbf{x}) = q^n(\mathbf{x} - \mathbf{u}\Delta t)$

"Where did this particle come from?"
Trace backward, interpolate
Key insight: Implicit diffusion + semi-Lagrangian advection = unconditionally stable!

Reference: Jos Stam, "Stable Fluids" (SIGGRAPH 1999)

Enforcing Incompressibility

After diffusion and advection, velocity may not satisfy $\nabla \cdot \mathbf{u} = 0$

Helmholtz decomposition: $\mathbf{u} = \mathbf{u}_{\text{div-free}} + \nabla p$

Solve Poisson equation for pressure:

$\nabla^2 p = \nabla \cdot \mathbf{u}$   →   $\mathbf{u}_{\text{div-free}} = \mathbf{u} - \nabla p$
Algorithm: Add sources → Diffuse → Project → Advect → Project

Rayleigh-Bénard Convection

HOT COLD warm ↑ cool ↓

The physics: Fluid heated from below, cooled from above.

  • Warm fluid rises (less dense)
  • Cool fluid sinks (more dense)
  • Creates convection cells
  • Basic mechanism of weather!
Many equations describe this: velocity, temperature, pressure fields in 3D...

From 7 Equations to 3: The Saltzman-Lorenz Story

Barry Saltzman (Yale, 1961) developed a 7-equation model for convection.

He showed it to Edward Lorenz at MIT — one solution "refused to settle down."

Lorenz noticed: 4 variables quickly became tiny. Only 3 were "keeping each other going."

Lorenz's insight:

"Barry gave me the go-ahead signal, and back at MIT the next morning I put the three equations on the computer..."

"...and sure enough, there was the same lack of periodicity."

Saltzman-Lorenz Exchange, 1961 →

The Lorenz Equations

Saltzman's 7 equations → Lorenz's 3 equations:

$\dot{x} = \sigma(y - x)$    $\dot{y} = x(\rho - z) - y$    $\dot{z} = xy - \beta z$
Variables:
$x$ = intensity of convection
$y$ = horizontal temperature diff
$z$ = vertical temperature deviation
Parameters (chaotic regime):
$\sigma = 10$, $\rho = 28$, $\beta = 8/3$

Just 3 coupled ODEs, yet the dynamics are incredibly complex

The Lorenz Attractor

x = 1.00
y = 1.00
z = 1.00
3
1500

The trajectory never repeats but stays on this strange "butterfly" shape

The Accident

Lorenz wanted to extend a simulation. Instead of starting over, he typed in the values from a printout:

Printout showed:

0.506
Computer stored:

0.506127

A difference of about 0.0001 — surely that can't matter?

He goes for a coffee, comes back, and...

The Weather is Completely Different!

The two simulations start nearly identical, then completely diverge.

Wait... what's happening here?
The equations are deterministic. Same input → same output, right?

Sensitivity to Initial Conditions

10-4

■ Original vs ■ Perturbed by ε

No matter how small ε is, the trajectories eventually diverge completely.

The Butterfly Effect

"Does the flap of a butterfly's wings in Brazil set off a tornado in Texas?"
— Edward Lorenz, 1972

Lorenz's discovery:

  • Deterministic ≠ Predictable
  • Tiny errors grow exponentially fast
  • Long-term weather prediction has a fundamental limit (~2 weeks)
This is chaos: sensitivity to initial conditions in deterministic systems.

Summary

  1. Computing = recursive rule application (from Peano to PDEs)
  2. Discretization: Turn differential equations into algebraic ones
  3. Error analysis: Taylor expansion tells us accuracy ($O(\Delta t)$, $O(\Delta t^2)$, ...)
  4. Stability: Not all $\Delta t$ work; implicit vs explicit schemes
  5. Runge-Kutta: The workhorse of ODE solving
  6. Finite volume: Preserve physics (conservation) in numerics
  7. Chaos: Sensitivity to initial conditions limits predictability

Looking Ahead

If systems are chaotic and sensitive...

How do we account for uncertainty?

Next lecture: Probabilistic Modeling and Uncertainty Quantification
  • Probability distributions over predictions
  • Bayesian inference
  • Monte Carlo methods
  • Connecting uncertainty in ML and scientific computing

Questions?

See you next time!