# \[MTK\] Interpolating observed variables

**URL:** https://discourse.julialang.org/t/mtk-interpolating-observed-variables/66308
**Category:** Modelling & Simulations
**Tags:** modelingtoolkit
**Created:** [August 12, 2021, 11:00pm UTC](https://discourse.julialang.org/t/mtk-interpolating-observed-variables/66308 "2021-08-12T23:00:51Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![doppioandante](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/doppioandante/32/28121_2.png) [@doppioandante](https://discourse.julialang.org/u/doppioandante)
#### Post date: [August 12, 2021, 11:00pm UTC](https://discourse.julialang.org/t/mtk-interpolating-observed-variables/66308/1 "2021-08-12T23:00:51Z")

</div>

I am toying around with ModelingToolkit, I have a very simple algebraic model representing the voltage/current relationship in a ideal resistor:

```julia
using ModelingToolkit, OrdinaryDiffEq

@parameters R
@variables t v(t) i(t)
D = Differential(t)

eqs = [
   i ~ v/R
   D(v) ~ 0
]

@named circuit = ODESystem(eqs, t)

u0 = [
   v => 5.0,
]

p = [
   R => 10,
]

tspan = (0.0, 0.01)
# convert the DAE problem to an ODE problem
circuit = structural_simplify(circuit)
prob = ODEProblem(circuit, u0, tspan, p)
sol = solve(prob, Tsit5())

# Interpolation can give the solution inside tspan and also further
#sol(0.009)

```

solve() solves the ODEProblem in the v variable, and I can either v or i as sol[v] or sol[i]. For v(t) I can also find the interpolated values using sol(t). Can the same be done easily for the observed variable i(t)?

---

<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: [August 13, 2021, 12:39am UTC](https://discourse.julialang.org/t/mtk-interpolating-observed-variables/66308/2 "2021-08-13T00:39:38Z")

</div>

`sol(0.009;idxs=v)`

---

<div class="post-metadata">

### Author: ![doppioandante](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/doppioandante/32/28121_2.png) [@doppioandante](https://discourse.julialang.org/u/doppioandante)
#### Post date: [August 13, 2021, 12:54am UTC](https://discourse.julialang.org/t/mtk-interpolating-observed-variables/66308/3 "2021-08-13T00:54:36Z")

</div>

Thanks, that works indeed, even using idxs=i

---

<div class="post-metadata">

### Author: ![TS-CUBED](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ts-cubed/32/25292_2.png) [@TS-CUBED](https://discourse.julialang.org/u/TS-CUBED)
#### Post date: [August 20, 2021, 10:37pm UTC](https://discourse.julialang.org/t/mtk-interpolating-observed-variables/66308/4 "2021-08-20T22:37:21Z")

</div>

Thanks. I was having a hard time finding this as well.

Is there a reason, why it’s `idxs` in some places and `vars` in others?

OK: just seen that `idxs=[statevar1, statevar2]` works in the plot recipe as well. So is `idxs` the preferred way?

---

<div class="post-metadata">

### Author: ![TS-CUBED](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ts-cubed/32/25292_2.png) [@TS-CUBED](https://discourse.julialang.org/u/TS-CUBED)
#### Post date: [August 21, 2021, 1:10pm UTC](https://discourse.julialang.org/t/mtk-interpolating-observed-variables/66308/5 "2021-08-21T13:10:59Z")

</div>

BTW, will the `dump` function create a useable form of the ODE solution that can be reread at a later time?

Since it is ASCII, it may be a way to keep the information even if the binary format changes.

---

<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: [August 26, 2021, 4:26am UTC](https://discourse.julialang.org/t/mtk-interpolating-observed-variables/66308/6 "2021-08-26T04:26:53Z")

</div>

We should fix that. `vars` is in the plot recipe, everything else is `idxs`.

> [@TS-CUBED](#):
>
> BTW, will the `dump` function create a useable form of the ODE solution that can be reread at a later time?

The two-argument show method should?

---

<div class="post-metadata">

### Author: ![TS-CUBED](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ts-cubed/32/25292_2.png) [@TS-CUBED](https://discourse.julialang.org/u/TS-CUBED)
#### Post date: [August 26, 2021, 9:04am UTC](https://discourse.julialang.org/t/mtk-interpolating-observed-variables/66308/7 "2021-08-26T09:04:04Z")

</div>

> [@ChrisRackauckas](#):
>
> The two-argument show method should?

What do you mean by that? Is there a way to read the output from `dump`, e.g. out of a text file, back into a julia object? Like a text-based serializer, so to speak?

---

<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: [August 26, 2021, 10:28am UTC](https://discourse.julialang.org/t/mtk-interpolating-observed-variables/66308/8 "2021-08-26T10:28:59Z")

</div>

Two argument show is supposed to be roundtrippable.

---

<div class="post-metadata">

### Author: ![TS-CUBED](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ts-cubed/32/25292_2.png) [@TS-CUBED](https://discourse.julialang.org/u/TS-CUBED)
#### Post date: [August 26, 2021, 11:43am UTC](https://discourse.julialang.org/t/mtk-interpolating-observed-variables/66308/9 "2021-08-26T11:43:54Z")

</div>

Looks like it (although I don’t know yet how to read it back in).

However, if I look at the output it seems that it does only include the solution vector to the actual ODE problem and the metadata for the solver run, but not the symbolic part that allows calculation of the other state variables.

Looking at the output of `dump` it has a lot of things like

```julia
var"##arg#408" = begin
                    #= /home/thor/.julia/packages/ModelingToolkit/9DDjU/src/structural_transformation/codegen.jl:154 =#
                    (ModelingToolkit.StructuralTransformations.numerical_nlsolve)(var"##fun#405", 0.0, (var"C3₊v(t)", R2₊R, var"C2₊v(t)"))
                end, var"R2₊i(t)" = #= /home/thor/.julia/packages/SymbolicUtils/Kp6RG/src/code.jl:169 =# @inbounds(var"##arg#408"[1]), var"##fun#409" = RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(Symbol("##arg#410"), Symbol("##arg#411")), ModelingToolkit.StructuralTransformations.var"#_RGF_ModTag", ModelingToolkit.StructuralTransformations.var"#_RGF_ModTag", (0xb9251361, 0xd947528e, 0x1c66eb19, 0xd3949734, 0x4cd5ad53)}(quote
    #= /home/thor/.julia/packages/SymbolicUtils/Kp6RG/src/code.jl:282 =#
    #= /home/thor/.julia/packages/SymbolicUtils/Kp6RG/src/code.jl:283 =#
    let var"R3₊i(t)" = #= /home/thor/.julia/packages/SymbolicUtils/Kp6RG/src/code.jl:169 =# @inbounds(var"##arg#410"[1]), var"C3₊v(t)" = #= /home/thor/.julia/packages/SymbolicUtils/Kp6RG/src/code.jl:169 =# @inbounds(var"##arg#411"[1]), R3₊R = #= /home/thor/.julia/packages/SymbolicUtils/Kp6RG/src/code.jl:169 =# @inbounds(var"##arg#411"[2])
        (+)((*)(R3₊R, var"R3₊i(t)"), (*)(-1, var"C3₊v(t)"))
    end

```

in it, which - to the uninitiated, i.e. me - looks like the additional algebraic equations for the other states.

But this gets too far of topic on a closed thread. So maybe better to move the discussion to new topic.

---

<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: [August 26, 2021, 11:55am UTC](https://discourse.julialang.org/t/mtk-interpolating-observed-variables/66308/10 "2021-08-26T11:55:38Z")

</div>

Yes, new topic. You’re looking for prob.f.observed, which is also a RuntimeGeneratedFunction so it will similarly print out it’s code.
