# Saving time-dependent variable inside ODE

**URL:** <https://discourse.julialang.org/t/saving-time-dependent-variable-inside-ode/84552>\
**Category:** Specific Domains\
**Tags:** plotting, differentialequation\
**Created:** [July 20, 2022, 6:23pm UTC](https://discourse.julialang.org/t/saving-time-dependent-variable-inside-ode/84552 "2022-07-20T18:23:41Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jpro](https://avatars.discourse-cdn.com/v4/letter/j/9fc29f/32.png) [@jpro](https://discourse.julialang.org/u/jpro)\
**Post date:** [July 20, 2022, 6:23pm UTC](https://discourse.julialang.org/t/saving-time-dependent-variable-inside-ode/84552/1 "2022-07-20T18:23:41Z")

</div>

Hi, I’d like to extract time-dependent variable from inside an ODE, but am having trouble doing so given that it is not in the state vector. I have my `du` function (called `pendulum!`) inside a `system` function. I use this setup so that I can pass parameters as arguments to the `system` function rather than hard coding them. See my code below:

```julia
using DifferentialEquations
using Plots

function system(l, m, g)
    function pendulum!(du, u, p, t)
        θ, ω = u
        l, m, g = p
        M = 0.1sin(ω*t)
        du[1] = ω
        du[2] = -3g/(2l)*sin(θ) + 3/(m*l^2)*M
    end

    θ_0 = 0.01
    ω_0 = 0.0
    u_0 = [θ_0, ω_0]
    tspan = (0.0, 10.0)
    params = [l, m, g]
    prob = ODEProblem(pendulum!, u_0, tspan, params)
    sol = solve(prob)
    return sol
end

mySol = system(1.0, 1.0, 9.81)
plot(mySol, linewidth = 2, xaxis = "t", label = ["θ" "ω"], layout = (2,1))

```

This all works great until I’d like to see how `M = 0.1sin(ω*t)` variable changes overtime (ideally plot it). I haven’t found a way to save that data outside of the `pendulum` function, so in a way it is trapped in there.

I have thought of appending the value of M to a local variable at each time step and returning it after the integration, however the local variable gets reset at each time step.

Additionally, I have considered making a global variable outside of the function but it cannot be recognized inside the `pendulum` function. Is there an elegant way to do this? Any ideas would be much appreciated. Thanks in advance!

---

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [July 20, 2022, 7:04pm UTC](https://discourse.julialang.org/t/saving-time-dependent-variable-inside-ode/84552/2 "2022-07-20T19:04:22Z")

</div>

For discrete variables, that are updated only in certain times, you can use callbacks and a vector inside the parameters `p` to store them. However, for continuous values as you want, I am not sure what is the best approach.

---

<div class="post-metadata">

**Author:** ![gnicolosi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gnicolosi/32/48031_2.png) [@gnicolosi](https://discourse.julialang.org/u/gnicolosi)\
**Post date:** [July 20, 2022, 7:40pm UTC](https://discourse.julialang.org/t/saving-time-dependent-variable-inside-ode/84552/3 "2022-07-20T19:40:13Z")

</div>

Hi, I added the following to your code. See if it helps and if this is what you want. mySol(t) can be treated as a function of time itself. I also removed your `pendulum` function from within the `system` function (perhaps it doesn’t matter?).

```julia
function M(t)
    ω = mySol(t)[1]
    M = 0.1*sin(ω*t)
    return M
end

plot(t->M(t), xlims=(0,10), label="M(t)")

```

![JuliaDiscourseReply](https://global.discourse-cdn.com/julialang/original/3X/c/1/c1dc93f8cfc1e04c74f115048347724ee12e5ea2.png)

Gabriel

---

<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:** [July 20, 2022, 7:41pm UTC](https://discourse.julialang.org/t/saving-time-dependent-variable-inside-ode/84552/4 "2022-07-20T19:41:11Z")

</div>

> [@jpro](#):
>
> This all works great until I’d like to see how `M = 0.1sin(ω*t)` variable changes overtime (ideally plot it). I haven’t found a way to save that data outside of the `pendulum` function, so in a way it is trapped in there.

The easiest way is to use ModelingToolkit.

```julia
eqs = [
        M ~ 0.1sin(ω*t)
        D(θ) ~ ω
        D(ω) ~ -3g/(2l)*sin(θ) + 3/(m*l^2)*M
]

```

`structural_simplify` with that set of equations will eliminate the extra equation, but `sol[M]` will grab it from the observed, or `plot(sol,vars=M)` would plot it.

See the first MTK tutorial:

[https://mtk.sciml.ai/dev/tutorials/ode\_modeling/](https://mtk.sciml.ai/dev/tutorials/ode_modeling/)

The other way is a post analysis as @gnicolosi shows

---

<div class="post-metadata">

**Author:** ![jpro](https://avatars.discourse-cdn.com/v4/letter/j/9fc29f/32.png) [@jpro](https://discourse.julialang.org/u/jpro)\
**Post date:** [July 20, 2022, 10:43pm UTC](https://discourse.julialang.org/t/saving-time-dependent-variable-inside-ode/84552/6 "2022-07-20T22:43:45Z")

</div>

Thanks for your reply! This works well in the example I’ve provided, but quickly becomes unideal when M is more than just one line (e.g. interpolating several variables, algebraically manipulating several variables in a long state vector, etc). I wanted to avoid having to re-write the definition of the desired variable `M` because theoretically it could take hundreds of lines to define.

---

<div class="post-metadata">

**Author:** ![jpro](https://avatars.discourse-cdn.com/v4/letter/j/9fc29f/32.png) [@jpro](https://discourse.julialang.org/u/jpro)\
**Post date:** [July 20, 2022, 11:09pm UTC](https://discourse.julialang.org/t/saving-time-dependent-variable-inside-ode/84552/7 "2022-07-20T23:09:26Z")

</div>

Thank you! This is exactly what I was looking for

---

<div class="post-metadata">

**Author:** ![jpro](https://avatars.discourse-cdn.com/v4/letter/j/9fc29f/32.png) [@jpro](https://discourse.julialang.org/u/jpro)\
**Post date:** [July 21, 2022, 5:40pm UTC](https://discourse.julialang.org/t/saving-time-dependent-variable-inside-ode/84552/8 "2022-07-21T17:40:18Z")

</div>

From my understanding ModelingToolkit cannot handle vector-valued variables. Is this correct? I would like a way to both grab sol[M] but also allow my variables to be vectors-- just wanted to see if this was possible in Julia

---

<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:** [July 23, 2022, 11:30pm UTC](https://discourse.julialang.org/t/saving-time-dependent-variable-inside-ode/84552/9 "2022-07-23T23:30:50Z")

</div>

If you make `M` a vector variable that will work. It doesn’t internally use it, but it externally allows one to define equations with them.
