# Integrators with analytic derivatives w/r/t initial conditions (especially the independent variable)

**URL:** https://discourse.julialang.org/t/integrators-with-analytic-derivatives-w-r-t-initial-conditions-especially-the-independent-variable/129832
**Category:** Numerics
**Tags:** question, diffeq, ode, differentialequation
**Created:** [June 12, 2025, 12:02pm UTC](https://discourse.julialang.org/t/integrators-with-analytic-derivatives-w-r-t-initial-conditions-especially-the-independent-variable/129832 "2025-06-12T12:02:05Z")
**Posts on this page:** 1
**Showing post:** 3

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [June 13, 2025, 12:43pm UTC](https://discourse.julialang.org/t/integrators-with-analytic-derivatives-w-r-t-initial-conditions-especially-the-independent-variable/129832/3 "2025-06-13T12:43:37Z")

</div>

> [@stevenhughes](#):
>
> For fixed step integration, this is relatively straightforward, but, fixed step is not ideal for obvious reasons.

Do you need to take the discretization into account, i.e. do you need the derivatives to nearly machine precision (including the derivative of the discretization error), or do you only need the derivative to the same accuracy as the ODE solution?

The latter (the “differentiate-then-discretize”) approach is _much_ easier, is independent of the discretization scheme, and is better behaved for adaptive schemes — see [ForwardDiff + Adaptive ODE Solvers: Timestep Issue Leads to Incorrect Derivatives - #4 by ChrisRackauckas](https://discourse.julialang.org/t/forwarddiff-adaptive-ode-solvers-timestep-issue-leads-to-incorrect-derivatives/125870/4)

> [@stevenhughes](#):
>
> This means I need the partials of the solution to odes, w/r/t to initial conditions.

Another question is how many initial conditions you have (i.e. how big is your ODE system), and how many things do you need to differentiate?

- If you have relatively few variables, and/or you want the full Jacobian matrix of the solution with respect to the initial conditions, you are probably better off with forward mode differentiation (whether by hand or using AD), which is simpler and is more efficient in this regime.
- If you have lots of variables (\gtrsim 100), and you only need to differentiate one (or a few) quantities, such as a scalar “loss function” of the solution, you are probably better off with reverse mode / adjoint methods (whether by hand or using AD).

For a gentler introduction to these things, see e.g. [chapter 9 of our _Matrix Calculus_ course notes](https://arxiv.org/abs/2501.14787). For more in-depth coverage, see [Chris’s review article](https://arxiv.org/abs/2406.09699).

I also wrote a [little Julia tutorial](https://github.com/mitmath/matrixcalc/blob/main/notes/double-pendulum-sensitivity.ipynb) implementing forward and reverse mode differentiate-then-discretize, by hand (mostly) following the course notes, to differentiate a small ODE solution with respect to initial conditions. It’s not necessarily any more efficient than using SciMLSensitivity.jl, however, which also uses AD in the analytical differentiate-then-discretize approach.

---

_[View the full topic](https://discourse.julialang.org/t/integrators-with-analytic-derivatives-w-r-t-initial-conditions-especially-the-independent-variable/129832)._
