# Define coupled ODE solution as differentiable

**URL:** <https://discourse.julialang.org/t/define-coupled-ode-solution-as-differentiable/81708>\
**Category:** General Usage\
**Tags:** ode, forwarddiff\
**Created:** [May 26, 2022, 10:11am UTC](https://discourse.julialang.org/t/define-coupled-ode-solution-as-differentiable/81708 "2022-05-26T10:11:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![weymouth](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/weymouth/32/15839_2.png) [@weymouth](https://discourse.julialang.org/u/weymouth)\
**Post date:** [May 26, 2022, 10:11am UTC](https://discourse.julialang.org/t/define-coupled-ode-solution-as-differentiable/81708/1 "2022-05-26T10:11:09Z")

</div>

I have a variable `A` defined by an ODE, and I want to treat the solution as an explicit differentiable function of time so I can tie it in with some other code.

For example

```julia
m*Ä+k*A = f(A,t)

```

where `m,k` are the mass and stiffness and `f` is the forcing giving by a coupled system of equations.

Typically, I would define the oscillator state `(A,Ȧ,t)`, and then do discrete time stepping for both systems, but this would require changing the other system quite a bit. I was hoping I could define something that _acts_ like an explicit function so that it will play nice with `ForwardDiff` and other Julia packages.

Naively, I guess I could define a function `A(τ) = A+Ȧ(τ-t)` , and then just keep updating the internal state? Maybe this is a generator function kind of thing?

Suggestions are welcome.

---

<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:** [May 26, 2022, 12:28pm UTC](https://discourse.julialang.org/t/define-coupled-ode-solution-as-differentiable/81708/2 "2022-05-26T12:28:55Z")

</div>

The differential equation solver is differentiable, so if you just take what comes out of the solver you should be fine?

> [@weymouth](#):
>
> Typically, I would define the oscillator state `(A,Ȧ,t)` , and then do discrete time stepping for both systems, but this would require changing the other system quite a bit.

Are you describing a symplectic integrator here? You can choose to use a symplectic integrator.

---

<div class="post-metadata">

**Author:** ![weymouth](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/weymouth/32/15839_2.png) [@weymouth](https://discourse.julialang.org/u/weymouth)\
**Post date:** [May 26, 2022, 1:38pm UTC](https://discourse.julialang.org/t/define-coupled-ode-solution-as-differentiable/81708/3 "2022-05-26T13:38:12Z")

</div>

> Are you describing a symplectic integrator here?

Sorry I wasn’t clear. This is a [coupled fluid & structural system](https://en.wikipedia.org/wiki/Fluid%E2%80%93structure_interaction). The unsteady Navier-Stokes PDE for the fluid depends on the structural displacement and velocity. The structural ODE depends on the fluid force `f`. Typically, these systems are solved using a fixed-point method to update both solid and fluid states until they satisfy their GEQs and then you move on to the next time step. I’m certainly happy to hear a Julian approach to this chestnut.

> The differential equation solver is differentiable, so if you just take what comes out of the solver you should be fine?

Neat! I see this bit on [Interpolating](https://diffeq.sciml.ai/stable/basics/solution/#Interpolations-and-Calculating-Derivatives). Is that what you mean? I tried to find the code for this functionality in the repo, but couldn’t track it down.
