Solving Differential Equations in "real time"


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?

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

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

dx/dt = f(t,x)

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

@rdeits had to solve this in:

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.

1 Like

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

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

In particular, you might find his PeriodicController useful:

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)

1 Like