ODE Integrator Function

My ODE runs from tspan(1,120). It needs an integrator function that can do the following:

  • Store the solutions at timestep 2,3,4…120 (i.e integral numbers) in a list. Although step size does not have to be 1, it just has to step on the integral numbers and only store results of integral numbers.

  • Be able to use the generated value at timestep n-1 at timestep n. (i.e inorder to calculate the value at time step 10, it needs to access the value at time step 9 which would be the last entry added to the list storing the solution at every time step of size 1.)

Please let me know how I can implement this using a integrator function.
Currently I have a looping implementation that would simulate at timesteps with an interval of 1. I am certain that this is not the most efficient implementation.

From your problem description it is not immediately clear for me if you are trying to solve an ‘ordinary’ ODE (otherwise you probably might be able to provide us with more hints). Is it a DDE what you are after?

1 Like

I’m trying to solve an ode

There are multiple ways to handle this. One is to augment the state to include the integer time states. Probably easiest to do this with discrete callbacks, where you can set a new state at discrete times. You pay a price because you’ve doubled the states.

Another way is to think of this as a hybrid dynamical system, with a discrete time feedback control and a continuous system. The control is updated at integer times, to serve as a memory of the discrete time states. Inject this as an exogenous input. In control systems you have xdot =f(x, u, t) where x is state and u is control. Then ui=g(x(ti)), a piecewise constant function of state sampled at appropriate times i. In fact this scheme is quite common in sampled data systems, where a microcontroller might perform discrete time control on a motor or other device with continuous time dynamics. Here again it would probably be best to use discrete callbacks. Callbacks alert the integrator of known times that you want to save and update the control, and should be efficient.

If you don’t want to use callbacks, there are probably ways to do it, but there are all sorts of complications for a variable step integrator, because things go weird when you overstep an integer point. The state derivative function has to do the right thing for times both before and after the discrete change in behavior. You could just do forward Euler with small time steps, but I’m guessing you didn’t want to do that.

Use saveat=1.

That’s not an ODE. That’s a DDE.

1 Like

Thanks for the clarification. I looked at the documentation https://diffeq.sciml.ai/stable/solvers/dde_solve/, could someone point me towards some sample code so I can understand the syntax and the parameters that I can pass better. For eg, I don’t know how I can access the result of tstep n-1 at tstep n using a DDE.

Well , time step is not a concept of a differential equation, so it cannot be referenced in the equation. But x(t-tau) is a well-defined concept.

https://diffeq.sciml.ai/stable/tutorials/dde_example/

So you’d need to do something like that.

The problem with what you’re saying is that your model would not have a unique solution. Changing the time step would change not just the numerical solution, but the analytical solution as well! You can think of it as a delay differential equation with x(t-tau) terms, but tau is dependent on the solver in your case, so as you try to converge you’re actually changing the equation!