Skip to content
Tutorials Center
My learning

Topics

Lesson 39 of 40 ยท Projects and Practice

Project: Simulate a Pendulum with scan and grad

Write a differentiable physics simulation in JAX: integrate a pendulum with lax.scan, then differentiate through the whole trajectory to estimate an unknown parameter.

JAX can differentiate not only through a neural network but through an entire simulation. In this project you integrate a damped pendulum with a fixed-step loop, then ask a question gradients are good at: given an observed trajectory, which damping coefficient produced it? Because the simulation is an ordinary pure function, jax.grad of a loss on the trajectory with respect to the damping gives you the answer's direction directly.

JAX does not run in this site's editors. The JAX code is read-only. The runnable blocks simulate the same pendulum in NumPy and estimate the damping with a finite-difference gradient and a one-dimensional search, so the printed numbers come from NumPy.

The physics

A pendulum of angle theta with damping c obeys theta'' = -(g / L) * sin(theta) - c * theta'. Write it as a first-order system on the state (theta, omega):

  • theta' = omega
  • omega' = -(g / L) * sin(theta) - c * omega

We integrate with the semi-implicit (symplectic) Euler method, which keeps energy behavior better than plain Euler: update the velocity first, then use the new velocity to update the angle.

The JAX version

Python
import jax, jax.numpy as jnp
from jax import lax

G_OVER_L = 9.81 / 1.0

def simulate(damping, theta0=1.0, omega0=0.0, dt=0.01, steps=500):
    def step(state, _):
        theta, omega = state
        omega = omega + dt * (-G_OVER_L * jnp.sin(theta) - damping * omega)
        theta = theta + dt * omega
        return (theta, omega), theta         # carry the state, record the angle

    _, thetas = lax.scan(step, (theta0, omega0), None, length=steps)
    return thetas                             # shape (steps,)

observed = simulate(0.4)                      # "ground truth" trajectory

def loss(damping):
    return jnp.mean((simulate(damping) - observed) ** 2)

grad_loss = jax.jit(jax.value_and_grad(loss))
c = 1.0                                       # start with a wrong guess
for i in range(300):
    value, g = grad_loss(c)
    c = c - 5.0 * g
print(float(c))                               # approaches 0.4

Several earlier ideas combine here:

  • lax.scan runs 500 integration steps as one compiled loop, with the state (theta, omega) as a pytree carry (scan lesson).
  • jax.grad through scan backpropagates through every time step automatically; you never write the adjoint equations.
  • jit compiles the whole simulate-and-differentiate computation once.
  • The damping is a single scalar, so grad applies directly.

Running the simulation

Python Simulate the pendulum and compare damping values (NumPy)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

With no damping the swings keep their size (the max angle later on stays near the start value). With stronger damping they shrink faster. The period is close to 2 * pi * sqrt(L / g), about 2 seconds for the numbers above, which is a quick sanity check on the integrator: 500 steps of 0.01 s cover 5 seconds, so you should see two to three peaks.

Recovering the damping

The block below mimics the JAX loop. A finite-difference derivative of the loss replaces jax.grad. The loss is non-convex in general, but near the true value it is a clean bowl.

Python Estimate the damping from a trajectory

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The estimate settles close to 0.4 after enough iterations; noise limits how exactly. In JAX the same loop is faster and more accurate, because grad is exact and the whole thing is compiled.

Why this matters

Differentiable simulation is used for system identification (fitting physical parameters), trajectory optimization, and learning controllers. Things to remember when you scale it up:

  • Long horizons make gradients grow or vanish; stiff systems need smaller steps or implicit integrators.
  • Memory for reverse-mode grows with the number of steps; use jax.checkpoint on the step function if it becomes a problem (see scan, remat and memory).
  • Batches of experiments (many initial angles or dampings) are a vmap over simulate: jax.vmap(simulate)(jnp.linspace(0, 2, 20)) returns a (20, steps) array. See vmap basics.
  • Fixed step counts keep shapes static for jit. A loop that stops "when the pendulum stops" needs lax.while_loop, which cannot be reverse-differentiated.

Exercises

  1. Make the pendulum length a second unknown and estimate both c and L from the same trajectory (parameters as a dict).
  2. Compare the symplectic Euler update to plain Euler on an undamped pendulum over 5,000 steps: which one conserves energy?
  3. Use vmap to simulate 100 initial angles and report the one that takes longest to fall below 0.1 radians.
  4. Add a driving force F * cos(w * t) and explore resonance.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does lax.scan carry through the simulation?

    The carry is the evolving state; the per-step output records the angle.
  2. 2. Why can jax.grad differentiate through the whole simulation?

    scan is differentiable: autodiff builds the backward pass through all iterations.
  3. 3. Which is NOT reverse-differentiable in JAX?

    while_loop supports forward-mode only because its iteration count is not known when tracing.
  4. 4. How do you simulate many dampings at once?

    vmap maps simulate over a batch of parameters and returns stacked trajectories.

Summary

  • Write simulations as pure step functions and run them with lax.scan.
  • jax.grad differentiates through the entire trajectory; no adjoint equations needed.
  • Fit physical parameters by minimizing the mismatch between a simulated and an observed trajectory.
  • Fixed step counts keep shapes static; use remat for memory and vmap for batches of runs.
  • Check integrators against known behavior (period, energy) before trusting gradients.

Next: Practice Problems and a Troubleshooting Guide