# Convert continuous time (ODESystem) to discrete time system?

**URL:** <https://discourse.julialang.org/t/convert-continuous-time-odesystem-to-discrete-time-system/114597>\
**Category:** Modelling & Simulations\
**Created:** [May 22, 2024, 11:58pm UTC](https://discourse.julialang.org/t/convert-continuous-time-odesystem-to-discrete-time-system/114597 "2024-05-22T23:58:26Z")\
**Posts on this page:** 5\
**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:** [May 22, 2024, 11:58pm UTC](https://discourse.julialang.org/t/convert-continuous-time-odesystem-to-discrete-time-system/114597/1 "2024-05-22T23:58:26Z")

</div>

I’d like to take a continuous time ODE, and convert it to a discrete time system, along the lines of `c2d` in ControlSystems.jl.

Here’s the ODE version:

> <https://github.com/epirecipes/sir-julia/blob/master/markdown/ode/ode.md>

Here’s the discrete-time equivalent:

> <https://github.com/epirecipes/sir-julia/blob/master/markdown/function_map/function_map.md>

I suspect that this is easy using ModelingToolkit (now that it has DiscreteProblem support), and that there is something out-of-the-box to do this, but I can’t find it.

A slightly harder version is to take a ReactionSystem and convert it to a discrete time Markov process - any pointers here would also be great!

---

<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:** [May 23, 2024, 6:08am UTC](https://discourse.julialang.org/t/convert-continuous-time-odesystem-to-discrete-time-system/114597/2 "2024-05-23T06:08:20Z")

</div>

I’m not sure exactly what you would like the result to be, a discrete-time simulation, or the discrete-time symbolic equations?

In either case, there is no support for this at the moment. For general MTK models, this “conversion” implies integration, so what you’re asking for would require the ability to choose the integration method etc. Looking at the discrete-time equations is pretty meaningless already for simple integrators like RK4, so I assume that you are interested in a numerical simulation with a particular integrator? You can of course already achieve this by `adaptive=false` and `dt=δt` when you call `solve`

---

<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:** [May 23, 2024, 4:21pm UTC](https://discourse.julialang.org/t/convert-continuous-time-odesystem-to-discrete-time-system/114597/3 "2024-05-23T16:21:36Z")

</div>

To clarify, f you look at the specific examples I gave, there is an integration of the rates in the ODE to give proportions in the map; my question was whether this can be done symbolically in the current MTK. I’ll think about it some more (c2d does much the same thing in a different setting…)

---

<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:** [May 23, 2024, 4:30pm UTC](https://discourse.julialang.org/t/convert-continuous-time-odesystem-to-discrete-time-system/114597/4 "2024-05-23T16:30:24Z")

</div>

I don’t recognize the integration scheme used in your code.

There is no feature to carry out this transformation at the moment, but if you describe it as an algorithm it can certainly be automated.

---

<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:** [May 23, 2024, 7:46pm UTC](https://discourse.julialang.org/t/convert-continuous-time-odesystem-to-discrete-time-system/114597/5 "2024-05-23T19:46:50Z")

</div>

The example there is just a discrete time model? the new way to do this is to use clocks:

> **[(Experimental) Modeling Discrete Systems · ModelingToolkit.jl](https://docs.sciml.ai/ModelingToolkit/stable/tutorials/discrete_system/)**
>
> Documentation for ModelingToolkit.jl.

There isn’t an automatic conversion of an ODESystem into a discrete time clocked system at this time, but if it’s just for simulating then `Euler` with the right step size is the same thing.
