# Automatic State Transition Matrix

**URL:** <https://discourse.julialang.org/t/automatic-state-transition-matrix/115657>\
**Category:** Numerics\
**Tags:** ode, forwarddiff, sciml\
**Created:** [June 14, 2024, 2:46pm UTC](https://discourse.julialang.org/t/automatic-state-transition-matrix/115657 "2024-06-14T14:46:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![afossa](https://avatars.discourse-cdn.com/v4/letter/a/eb9ed0/32.png) [@afossa](https://discourse.julialang.org/u/afossa)\
**Post date:** [June 14, 2024, 2:46pm UTC](https://discourse.julialang.org/t/automatic-state-transition-matrix/115657/1 "2024-06-14T14:46:11Z")

</div>

Hello,

When solving an ODE with the `DifferentialEquations` package, is there a way to automatically retrieve the state transition matrix, i.e. the sensitivity of the solution w.r.t. the initial conditions, along the entire trajectory?

I came across [Forward-Mode Automatic Differentiation with ForwardDiff.jl](https://docs.sciml.ai/SciMLSensitivity/stable/getting_started/#Forward-Mode-Automatic-Differentiation-with-ForwardDiff.jl), but this just gives me the STM at a given point in time, and `f(x)` must return an array-like object.

I would need something similar to `ODEForwardSensitivityProblem` in [Direct Forward Sensitivity Analysis of ODEs](https://docs.sciml.ai/SciMLSensitivity/stable/manual/direct_forward_sensitivity/), but capable of computing the sensitivities w.r.t. `u0` rather than `p`.

Thank you!

---

<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 14, 2024, 3:37pm UTC](https://discourse.julialang.org/t/automatic-state-transition-matrix/115657/2 "2024-06-14T15:37:35Z")

</div>

Can’t you just put in dual numbers (via ForwardDiff.jl) as the initial conditions?

---

<div class="post-metadata">

**Author:** ![afossa](https://avatars.discourse-cdn.com/v4/letter/a/eb9ed0/32.png) [@afossa](https://discourse.julialang.org/u/afossa)\
**Post date:** [June 14, 2024, 4:00pm UTC](https://discourse.julialang.org/t/automatic-state-transition-matrix/115657/3 "2024-06-14T16:00:27Z")

</div>

Yes, but this require a bit of machinery to extract the STM, something like

```julia
stack([vcat([[x.partials...] for x in u]'...) for u in sol(t).u],dims=3)

```

I was wondering if there was a more straightforward way to achieve the same.

---

<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 15, 2024, 10:02am UTC](https://discourse.julialang.org/t/automatic-state-transition-matrix/115657/4 "2024-06-15T10:02:53Z")

</div>

Right now there isn’t a nice high level utility for this. I’ve been asked to extend `ODEForwardSensitivityProblem` for this but just haven’t gotten around to it yet. As Steven suggests, just using dual numbers on u0 and extracting it manually is the current technique, though I do hope to just make `ODEForwardSensitivityProblem` handle this sometime in the future.
