# Generating derived variables from ModelingToolkit.jl - variable of interest eliminated during structural\_simplify

**URL:** https://discourse.julialang.org/t/generating-derived-variables-from-modelingtoolkit-jl-variable-of-interest-eliminated-during-structural-simplify/93939
**Category:** Modelling & Simulations
**Created:** [February 2, 2023, 4:30pm UTC](https://discourse.julialang.org/t/generating-derived-variables-from-modelingtoolkit-jl-variable-of-interest-eliminated-during-structural-simplify/93939 "2023-02-02T16:30:03Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![sdwfrost](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sdwfrost/32/2831_2.png) [@sdwfrost](https://discourse.julialang.org/u/sdwfrost)
#### Post date: [February 2, 2023, 4:30pm UTC](https://discourse.julialang.org/t/generating-derived-variables-from-modelingtoolkit-jl-variable-of-interest-eliminated-during-structural-simplify/93939/1 "2023-02-02T16:30:03Z")

</div>

Hi everyone,

I’m trying to generate variables that are functions of my state variables in an ODE. Here’s my example.

```julia
using ModelingToolkit
using OrdinaryDiffEq

@parameters t β c γ
@variables S(t) I(t) R(t) λ(t)
D = Differential(t)
N=S+I+R
eqs = [D(S) ~ -λ*S,
       D(I) ~ λ*S-γ*I,
       D(R) ~ γ*I,
       λ ~ β*c*I/N];
@named sys = ODESystem(eqs) # 4 equations: S,I,R,λ
simpsys = structural_simplify(sys) # 3 equations: S,I,R

```

Defining λ not only makes the ODEs easier to read, but it’s also a variable that I want access to, in order to plug it in to other models. λ gets eliminated (as it should) during `structural_simplify`, but then I lose the ability to access it. Is there any way to run the ODE problem and get back all the variables (S,I,R,λ)?

---

<div class="post-metadata">

### Author: ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)
#### Post date: [February 2, 2023, 4:43pm UTC](https://discourse.julialang.org/t/generating-derived-variables-from-modelingtoolkit-jl-variable-of-interest-eliminated-during-structural-simplify/93939/2 "2023-02-02T16:43:46Z")

</div>

\lambda has likely been moved to `observed(simsys)`. These variables are still accessible in the solution when you have simulated, but it’s also possible to build a function that computes the observed variables given the states.

The following should do what you want

```julia
outputs = [λ]
syso, _ = ModelingToolkit.io_preprocessing(sys, [], outputs;)
obsf = ModelingToolkit.build_explicit_observed_function(syso, outputs)

lambda = obsf[1](x, p, t) # where x is the state of the simplified system

```

* * *

Or perhaps better yet, without using internals, construct an `ODEProblem` and then

```julia
λfun = prob.f.observed(λ)
lambda = λfun(x, p, t) 

```

In both of these approaches, `x` and `p` need to be numerical arrays in the correct order. The order is given by `states(simpsys)` and `parameters(simpsys)`

---

<div class="post-metadata">

### Author: ![xtalax](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xtalax/32/35293_2.png) [@xtalax](https://discourse.julialang.org/u/xtalax)
#### Post date: [February 2, 2023, 4:52pm UTC](https://discourse.julialang.org/t/generating-derived-variables-from-modelingtoolkit-jl-variable-of-interest-eliminated-during-structural-simplify/93939/3 "2023-02-02T16:52:06Z")

</div>

It’s worth mentioning that `sol[λ]` will still get you the solution for lambda, even if it’s eliminated

---

<div class="post-metadata">

### Author: ![sdwfrost](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sdwfrost/32/2831_2.png) [@sdwfrost](https://discourse.julialang.org/u/sdwfrost)
#### Post date: [February 2, 2023, 6:41pm UTC](https://discourse.julialang.org/t/generating-derived-variables-from-modelingtoolkit-jl-variable-of-interest-eliminated-during-structural-simplify/93939/4 "2023-02-02T18:41:55Z")

</div>

Thanks for these tips! Is there any way to add those observed values to the solution, so I can call `sol_ode(10.0)` (say) and get the states and the derived values from the interpolation?

---

<div class="post-metadata">

### Author: ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)
#### Post date: [February 2, 2023, 6:48pm UTC](https://discourse.julialang.org/t/generating-derived-variables-from-modelingtoolkit-jl-variable-of-interest-eliminated-during-structural-simplify/93939/5 "2023-02-02T18:48:57Z")

</div>

It’s already in there, I can never remember the syntax to obtain the interpolated value, but I know it is possible 😅

---

<div class="post-metadata">

### Author: ![xtalax](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xtalax/32/35293_2.png) [@xtalax](https://discourse.julialang.org/u/xtalax)
#### Post date: [February 2, 2023, 6:55pm UTC](https://discourse.julialang.org/t/generating-derived-variables-from-modelingtoolkit-jl-variable-of-interest-eliminated-during-structural-simplify/93939/6 "2023-02-02T18:55:56Z")

</div>

```julia
λfun = prob.f.observed(λ)
lambda = λfun(x, p, t) 

state_interp = sol(10.0)
complete_interp = [state_interp..., λfun(state_interp, [], 10.0)]

```

This is the first time I have noticed that the observed aren’t automatically included in the interp, this is probably an oversight. Please post an issue to SciMLBase, and I may get to it soon.

---

<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: [February 2, 2023, 9:44pm UTC](https://discourse.julialang.org/t/generating-derived-variables-from-modelingtoolkit-jl-variable-of-interest-eliminated-during-structural-simplify/93939/7 "2023-02-02T21:44:08Z")

</div>

> [@sdwfrost](#):
>
> `sol_ode(10.0)` (say) and get the states and the derived values from the interpolation?

`sol_ode(10.0, idxs = λ)`

And note you can create new derived variables on the fly and it will compute it.

`sol_ode(10.0, idxs = λ + R^2)`

---

<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: [February 2, 2023, 9:45pm UTC](https://discourse.julialang.org/t/generating-derived-variables-from-modelingtoolkit-jl-variable-of-interest-eliminated-during-structural-simplify/93939/8 "2023-02-02T21:45:25Z")

</div>

> [@baggepinnen](#):
>
> ```julia
> λfun = prob.f.observed(λ)
> lambda = λfun(x, p, t) 
> 
> ```

Please don’t use that 😅 . I mean, Fredrik can, but it’s internals most people shouldn’t know and it’s not guaranteed to keep the same syntax.

---

<div class="post-metadata">

### Author: ![sdwfrost](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sdwfrost/32/2831_2.png) [@sdwfrost](https://discourse.julialang.org/u/sdwfrost)
#### Post date: [February 2, 2023, 10:56pm UTC](https://discourse.julialang.org/t/generating-derived-variables-from-modelingtoolkit-jl-variable-of-interest-eliminated-during-structural-simplify/93939/9 "2023-02-02T22:56:04Z")

</div>

Thanks Chris!
