# DifferentialEquations.jl -- store intermediate variables?

**URL:** https://discourse.julialang.org/t/differentialequations-jl-store-intermediate-variables/68928
**Category:** Modelling & Simulations
**Tags:** question
**Created:** [September 29, 2021, 12:39pm UTC](https://discourse.julialang.org/t/differentialequations-jl-store-intermediate-variables/68928 "2021-09-29T12:39:37Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)
#### Post date: [September 29, 2021, 12:39pm UTC](https://discourse.julialang.org/t/differentialequations-jl-store-intermediate-variables/68928/1 "2021-09-29T12:39:37Z")

</div>

Is there a way to store _intermediate_ variables in ODEs using the `DifferentialEquations` package?

MWE:

```julia
using DifferentialEquations
#
function sys!(dx,x,p,t)
    r = 1
    u = 5*(r-x[1])
    dx[1] = 0.1*(u-x[1])
end
#
x0 = [-1.0]
tspan = (0.0,10.0)
prob = ODEProblem(sys!,x0,tspan)
sol = solve(prob)
#
plot(sol)

```

So… I’d like to be able to store `u` (and possibly also `r`).

- Is this possible? (I know it is possible in `ModelingToolkit` by converting the DAE to an ODE; I’ll test my problem with ModelingToolkit later – it is rather big/ugly, so I’d prefer to handle it directly in `DifferentialEquations` first.)
- If possible, how can I specify the variable in the `plot(sol)` command? (I know how to do that if only states are stored.)

Suggestions are appreciated!

---

<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: [September 29, 2021, 3:50pm UTC](https://discourse.julialang.org/t/differentialequations-jl-store-intermediate-variables/68928/2 "2021-09-29T15:50:41Z")

</div>

It’s actually really ugly to do optimally in DifferentialEquations.jl because you don’t want to store them but instead lazily instantiate them to limit the computational cost 😅 . You can use SavingCallback or something if you want to. If you think you’re redoing computations for the save, well you’re not actually recomputing anything in most common cases because you generally don’t save at steps.

---

<div class="post-metadata">

### Author: ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)
#### Post date: [October 8, 2021, 6:47am UTC](https://discourse.julialang.org/t/differentialequations-jl-store-intermediate-variables/68928/3 "2021-10-08T06:47:25Z")

</div>

Hm… could I store the algebraic variables in a pure integrator state, and — once solved — plot the _derivative_ of this integral state?

Something like:

```julia
using DifferentialEquations
#
function sys!(dx,x,p,t)
    r = 1
    u = 5*(r-x[1])
    dx[1] = 0.1*(u-x[1])
    dx[2] = u
end
#
r = 1
x0 = [-1.0, 5*(r+1.0)]
tspan = (0.0,10.0)
prob = ODEProblem(sys!,x0,tspan)
sol = solve(prob)
#
plot(sol,vars=(0,1)) # gives "real" state x
plot(sol.t,Array(sol(sol.t,Val{1}))[2,:]) # gives u

```

- Is there a simpler way to plot the derivative of variable 2 (u)?
- Are there downsides to this procedure, compared to computing u in a post processing step?

---

<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: [October 8, 2021, 10:15am UTC](https://discourse.julialang.org/t/differentialequations-jl-store-intermediate-variables/68928/4 "2021-10-08T10:15:49Z")

</div>

If you solve it as a DAE then that derivative would be calculated
