# Save additional quantities using DifferentialEquations.jl pkg

**URL:** https://discourse.julialang.org/t/save-additional-quantities-using-differentialequations-jl-pkg/34011
**Category:** Modelling & Simulations
**Tags:** diffeq
**Created:** [January 31, 2020, 8:35am UTC](https://discourse.julialang.org/t/save-additional-quantities-using-differentialequations-jl-pkg/34011 "2020-01-31T08:35:12Z")
**Posts on this page:** 7
**Page:** 2

<div class="post-metadata">

### Author: ![jt1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jt1/32/38331_2.png) [@jt1](https://discourse.julialang.org/u/jt1)
#### Post date: [March 5, 2021, 8:08pm UTC](https://discourse.julialang.org/t/save-additional-quantities-using-differentialequations-jl-pkg/34011/21 "2021-03-05T20:08:38Z")

</div>

I guess we can also add observed quantities ourselves, without using modelingtoolkit?  
I was looking for some documentation to do this, but i couldn’t find any.

If possible I would like to define a function like (x,p) → some quantity.  
Which can be accessed via sol like in the docs now for the DAE and then use interpolation on x etc.

I found that the code that adds such equations for ODAEPoblem is [this](https://github.com/SciML/ModelingToolkit.jl/blob/f42e3f8855f36a3fb799369f819100fca9a9b6b2/src/structural_transformation/codegen.jl#L256-L328), but it is a bit over my head.

I noticed ODEFunction has an ‘observed’ field. But couldn’t get it to work.

---

<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: [March 8, 2021, 6:07am UTC](https://discourse.julialang.org/t/save-additional-quantities-using-differentialequations-jl-pkg/34011/22 "2021-03-08T06:07:04Z")

</div>

> [@jt1](#):
>
> I noticed ODEFunction has an ‘observed’ field. But couldn’t get it to work.

It would be a slightly horrifying function to build by hand. It’s shown here though:

> <https://github.com/SciML/SciMLBase.jl/pull/9#issuecomment-770289063>
>
> \`\`\`julia
> using ModelingToolkit, OrdinaryDiffEq
> 
> @parameters t σ ρ β
> @variabl…es x(t) y(t) z(t) δ(t) Ω(t)
> D = Differential(t)
> 
> eqs = \[D(x) ~ σ\*(y-x),
> D(y) ~ x\*(ρ-z)-y,
> D(z) ~ x\*y - β\*z\]
> 
> lorenz1 = ODESystem(eqs,name=:lorenz1)
> lorenz2 = ODESystem(eqs,name=:lorenz2)
> 
> @variables a
> @parameters γ
> connections = \[0 ~ lorenz1.x + lorenz2.y + a\*γ\]
> connected = ODESystem(connections,t,\[a\],\[γ\],systems=\[lorenz1,lorenz2\])
> 
> u0 = \[lorenz1.x =\> 1.0,
> lorenz1.y =\> 0.0,
> lorenz1.z =\> 0.0,
> lorenz2.x =\> 0.0,
> lorenz2.y =\> 1.0,
> lorenz2.z =\> 0.0,
> a =\> 2.0\]
> 
> p = \[lorenz1.σ =\> 10.0,
> lorenz1.ρ =\> 28.0,
> lorenz1.β =\> 8/3,
> lorenz2.σ =\> 10.0,
> lorenz2.ρ =\> 28.0,
> lorenz2.β =\> 8/3,
> γ =\> 2.0\]
> 
> tspan = (0.0,100.0)
> prob = ODEProblem(connected,u0,tspan,p)
> sol = solve(prob,Rodas5())
> 
> sol\[lorenz1.x\]
> sol\[lorenz1.x,2\]
> sol\[lorenz1.x,:\]
> sol\[lorenz1.x,1:5\]
> sol\[δ,1:5\] # Indexing symbol δ is unknown.
> 
> \# Assume δ = a\*lorenz1.x + lorenz2.y
> function SciMLBase.DEFAULT\_OBSERVED(sym, u)
> if sym === δ
> u\[7\]\*u\[1\] + u\[5\]
> elseif sym === Ω
> u\[7\]
> else
> error("Indexing symbol $sym is unknown.")
> end
> end
> 
> sol\[δ,1:5\] # success!
> sol\[Ω\] # success!
> 
> using Plots
> plot(sol,vars=(lorenz2.x,lorenz2.z))
> savefig("plot1.png")
> plot(sol,vars=(δ,lorenz2.z))
> savefig("plot2.png")
> plot(sol,vars=(δ,Ω)) # made a heart!
> savefig("plot3.png")
> plot(sol,vars=Ω)
> savefig("plot4.png")
> plot(sol,vars=(t,Ω))
> savefig("plot5.png")
> \`\`\`
> 
> !\[plot1\](https://user-images.githubusercontent.com/1814174/106369395-fe353280-631e-11eb-8d67-583267ae5573.png)
> !\[plot2\](https://user-images.githubusercontent.com/1814174/106369399-fffef600-631e-11eb-91b3-a28fb75a4796.png)
> !\[plot3\](https://user-images.githubusercontent.com/1814174/106369402-02615000-631f-11eb-87a3-0e78db266cd0.png)
> !\[plot4\](https://user-images.githubusercontent.com/1814174/106369404-03927d00-631f-11eb-905d-53b1e0a3da1a.png)
> !\[plot5\](https://user-images.githubusercontent.com/1814174/106369406-055c4080-631f-11eb-9670-d33774a99558.png)

---

<div class="post-metadata">

### Author: ![wallyxie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wallyxie/32/8141_2.png) [@wallyxie](https://discourse.julialang.org/u/wallyxie)
#### Post date: [March 18, 2021, 5:35am UTC](https://discourse.julialang.org/t/save-additional-quantities-using-differentialequations-jl-pkg/34011/23 "2021-03-18T05:35:16Z")

</div>

Hi all, not sure this is exactly related, but after googling for how to save non-state variable quantities in DifferentialEquations.jl, I ended up here and feel that my question corresponds to this thread.

I’m currently trying to do something resembling the following simplifying pseudocode:

```julia
using DifferentialEquations

param1 = 0.1
param2 = 0.11

function some_parameter_changing_func(param, time)
    modified_parameter = param + 0.001 * time
end

function f(du, u, p, t)
    param1, param2 = p
    du[1] = modified_parameter(param1, time) * u[2] - 0.09 * u[1] 
    du[2] = modified_parameter(param2, time) * u[1] - 0.1 * u[2]
    desired_export = (1 - modified_parameter(param1, time)) * u[2] + (1 - modified_parameter(param2, time)) * u[1]
end

```

and then extract a vector for the `desired_export` values over some `tspan`. Coming into the thread in media res, I got a little lost, so I was wondering what was the best means of going about that? I could compute the value after the solutions have been obtained from the `sol` object, of course, but the actual system I’m working with is more tedious and would make computing a `desired_export` vector based on `sol.u` and `sol.t` less convenient.

Thanks!

---

<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: [March 18, 2021, 12:10pm UTC](https://discourse.julialang.org/t/save-additional-quantities-using-differentialequations-jl-pkg/34011/24 "2021-03-18T12:10:50Z")

</div>

> [@wallyxie](#):
>
> and then extract a vector for the `desired_export` values over some `tspan` . Coming into the thread in media res, I got a little lost, so I was wondering what was the best means of going about that? I could compute the value after the solutions have been obtained from the `sol` object, of course, but the actual system I’m working with is more tedious and would make computing a `desired_export` vector based on `sol.u` and `sol.t` less convenient.

Just calculate it algebraically. It’s more efficient. See all.

---

<div class="post-metadata">

### Author: ![wallyxie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wallyxie/32/8141_2.png) [@wallyxie](https://discourse.julialang.org/u/wallyxie)
#### Post date: [March 18, 2021, 3:06pm UTC](https://discourse.julialang.org/t/save-additional-quantities-using-differentialequations-jl-pkg/34011/25 "2021-03-18T15:06:18Z")

</div>

> [@ChrisRackauckas](#):
>
> See all.

Sorry, what is the all here referring to?

---

<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: [March 18, 2021, 6:25pm UTC](https://discourse.julialang.org/t/save-additional-quantities-using-differentialequations-jl-pkg/34011/26 "2021-03-18T18:25:05Z")

</div>

There’s this thread and a few others all about control variables, and I’m writing a new system in the new month that will improve it, so I’m not taking the time to write another version of an answer I intend to obsolete soon. But if you search control variables and DEDataArray stuff you’ll get how to do it with callbacks and all of that.

---

<div class="post-metadata">

### Author: ![wallyxie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wallyxie/32/8141_2.png) [@wallyxie](https://discourse.julialang.org/u/wallyxie)
#### Post date: [March 18, 2021, 7:44pm UTC](https://discourse.julialang.org/t/save-additional-quantities-using-differentialequations-jl-pkg/34011/27 "2021-03-18T19:44:25Z")

</div>

I had no idea they were called explicit algebraic variables! Always glad to benefit from your expertise, thanks again for your time.

[Previous page](https://discourse.julialang.org/t/save-additional-quantities-using-differentialequations-jl-pkg/34011.md?page=1)
