# 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:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![stevenhughes](https://avatars.discourse-cdn.com/v4/letter/s/4da419/32.png) [@stevenhughes](https://discourse.julialang.org/u/stevenhughes)
#### Post date: [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/1 "2025-06-12T12:02:05Z")

</div>

Hello,

I am working on an application that requires derivatives odes to compute partials for solvers. The approach I am taking is to prefer analytic partials (for performance, which is key), and fallback to AD or finite difference only when needed and at the lowest level. This means I need the partials of the solution to odes, w/r/t to initial conditions. For fixed step integration, this is relatively straightforward, but, fixed step is not ideal for obvious reasons.

Is there any work in the extensive DifferentialEquations ecosystem on ode integrators with analytic derivatives (especially w/r/t the independent variable). I could make do with fixed step, but adaptative step is better (and much, much harder))

---

<div class="post-metadata">

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

</div>

> [@stevenhughes](#):
>
> Is there any work in the extensive DifferentialEquations ecosystem on ode integrators with analytic derivatives (especially w/r/t the independent variable). I could make do with fixed step, but adaptative step is better (and much, much harder))

You mean with respect to the dependent variable? W.r.t. the independent variable is just `f`. W.r.t. `u0` is included in the SciMLSensitivity codes.

> **[SciMLSensitivity: Automatic Differentiation and Adjoints for (Differential)...](https://docs.sciml.ai/SciMLSensitivity/stable/)**
>
> Documentation for SciMLSensitivity.jl.

> [@stevenhughes](#):
>
> nd fallback to AD or finite difference only when needed and at the lowest level

Note that AD actually uses the continuous adjoints by default. This is discussed in the SciMLSensitivity documentation. You can directly choose how the function is differentiated via the sensealg keyword:

> **[Sensitivity Algorithms for Differential Equations with Automatic...](https://docs.sciml.ai/SciMLSensitivity/stable/manual/differential_equation_sensitivities/)**
>
> Documentation for SciMLSensitivity.jl.

or by using the direct interface

> **[Direct Adjoint Sensitivities of Differential Equations · SciMLSensitivity.jl](https://docs.sciml.ai/SciMLSensitivity/stable/manual/direct_adjoint_sensitivities/)**
>
> Documentation for SciMLSensitivity.jl.

but we recommend just using the defaults in most scenarios.

---

<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.
