# Solving Differential Equations in "real time"

**URL:** <https://discourse.julialang.org/t/solving-differential-equations-in-real-time/41546>\
**Category:** General Usage\
**Created:** [June 16, 2020, 7:12pm UTC](https://discourse.julialang.org/t/solving-differential-equations-in-real-time/41546 "2020-06-16T19:12:33Z")\
**Posts on this page:** 7\
**Page:** 1

<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:** [June 16, 2020, 7:12pm UTC](https://discourse.julialang.org/t/solving-differential-equations-in-real-time/41546/1 "2020-06-16T19:12:33Z")

</div>

Hi!

Is there a way to use Differential Equations to solve a system in “real time”? I want to solve a dynamic system that will receive data from the Raspberry Pi to update the simulation. Thus, this system must be solved in “real time”, like, one step every 1ms. How is the best way to do this?

---

<div class="post-metadata">

**Author:** ![rveltz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rveltz/32/2707_2.png) [@rveltz](https://discourse.julialang.org/u/rveltz)\
**Post date:** [June 16, 2020, 7:24pm UTC](https://discourse.julialang.org/t/solving-differential-equations-in-real-time/41546/2 "2020-06-16T19:24:02Z")

</div>

“Solving” an ode is subjective. It depends on tolerance for example.

---

<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:** [June 16, 2020, 7:31pm UTC](https://discourse.julialang.org/t/solving-differential-equations-in-real-time/41546/3 "2020-06-16T19:31:08Z")

</div>

I mean, let’s say you have a problem:

```julia
dx/dt = f(t,x)

```

then I want to step it for 1ms every 1ms. I can use something simple, like RK4.

---

<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:** [June 16, 2020, 9:05pm UTC](https://discourse.julialang.org/t/solving-differential-equations-in-real-time/41546/4 "2020-06-16T21:05:57Z")

</div>

@rdeits had to solve this in:

[![](https://global.discourse-cdn.com/julialang/original/3X/0/b/0b71256d516593ad3b81147b0fb018d0cc526872.jpeg "JuliaRobotics: Making robots walk with Julia | Robin Deits") ](https://www.youtube.com/watch?v=dmWQtI3DFFo)

I don’t recall the solution though. I’d probably do it by making a timer that you then enclose into a discrete callback, and use `sleep` to slow down the solve so the callback finishes at around time `t=n` (by taking a new time and subtracting it from the start time of course). You’d then want to use a counter so that it doesn’t drift.

---

<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:** [June 16, 2020, 9:11pm UTC](https://discourse.julialang.org/t/solving-differential-equations-in-real-time/41546/5 "2020-06-16T21:11:20Z")

</div>

Thanks! The idea of the timer inside a callback is really good 🙂

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [June 16, 2020, 9:18pm UTC](https://discourse.julialang.org/t/solving-differential-equations-in-real-time/41546/6 "2020-06-16T21:18:17Z")

</div>

Actually @tkoolen did all the cool simulation stuff for that–I just got to give the talk 🙂

In particular, you might find his `PeriodicController` useful: [https://github.com/JuliaRobotics/RigidBodySim.jl/blob/master/src/control.jl#L29-L87](https://github.com/JuliaRobotics/RigidBodySim.jl/blob/master/src/control.jl#L29-L87)

---

<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:** [June 16, 2020, 9:20pm UTC](https://discourse.julialang.org/t/solving-differential-equations-in-real-time/41546/7 "2020-06-16T21:20:10Z")

</div>

> [@rdeits](#):
>
> In particular, you might find his `PeriodicController` useful: [https://github.com/JuliaRobotics/RigidBodySim.jl/blob/master/src/control.jl#L29-L87](https://github.com/JuliaRobotics/RigidBodySim.jl/blob/master/src/control.jl#L29-L87)

That was upstreamed to become the PeriodicCallback in the callback library BTW, and indeed that could be useful here (though if you’re using a fixed time step method and calling after each step, a DiscreteCallback with a `condition(u,t,integrator) = true` is all you need)
