Differentiable Simulation

ODEs

Let’s consider ODEs of the form . Assuming a fixed step size , the most common integration schemes are basically functions such that

For instance, Euler integration uses .

The function is applied recursively to produce a series of points, starting from some initial condition .

We can write this more compactly as the recurrence relation

By differentiating both sides of this relation and applying the chain rule, we find that

Note that we can take the partial derivative of any component of with respect to any component of . So the equation above relates Jacobian matrices. The left hand side is the Jacobian of of as a function of . The right hand side is the product of the Jacobian of as a function of with the Jacobian of as a function of . Note that necessarily involves the gradient of as a function of time (i.e. , the object that normal non-differential simulation integrates forward), so the Jacobian of will involve second order derivatives of (first evaluated with respect to time, then with respect to the prior state).

Assuming that we can compute on demand, this gives a convenient recurrence relation for in terms of . So to differentiate through such a simulation, all we need to do is figure out , and compute the series of derivate values alongside the values .

Euler

As mentioned above, for Euler integration . Thus

And

N-Body Problem

A simple test case is the n-body problem. Let’s take our phase space coordinates to be position and velocity. Given positions and masses , Newton’s law of universal gravitation (along with Newton’s second law of motion) tells us how to compute the acceleration experienced by a particular particle.

To start, let’s consider two particles. I’ll make one 100 times heavier than the other, and our goal will be to find a stable circular orbit by varying the initial velocity of the satellite. As a baseline, here’s a really bad trajectory, in which the satellite gains escape velocity and doesn’t end up orbiting at all.

Differentiating through the simulation makes it very easy to find the desired solution. This orbit was found by gradient descent, but Nelder Mead finds the same optimum.