# ODE Integrator Function

**URL:** <https://discourse.julialang.org/t/ode-integrator-function/79756>\
**Category:** Modelling & Simulations\
**Tags:** question, ode\
**Created:** [April 20, 2022, 9:06pm UTC](https://discourse.julialang.org/t/ode-integrator-function/79756 "2022-04-20T21:06:40Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![BinaryBot](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/binarybot/32/45258_2.png) [@BinaryBot](https://discourse.julialang.org/u/BinaryBot)\
**Post date:** [April 20, 2022, 9:06pm UTC](https://discourse.julialang.org/t/ode-integrator-function/79756/1 "2022-04-20T21:06:40Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [April 20, 2022, 9:22pm UTC](https://discourse.julialang.org/t/ode-integrator-function/79756/2 "2022-04-20T21:22:21Z")

</div>

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](https://diffeq.sciml.ai/stable/tutorials/dde_example/) what you are after?

---

<div class="post-metadata">

**Author:** ![BinaryBot](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/binarybot/32/45258_2.png) [@BinaryBot](https://discourse.julialang.org/u/BinaryBot)\
**Post date:** [April 20, 2022, 9:30pm UTC](https://discourse.julialang.org/t/ode-integrator-function/79756/3 "2022-04-20T21:30:23Z")

</div>

I’m trying to solve an ode

---

<div class="post-metadata">

**Author:** ![apo383](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/apo383/32/11272_2.png) [@apo383](https://discourse.julialang.org/u/apo383)\
**Post date:** [April 21, 2022, 1:09am UTC](https://discourse.julialang.org/t/ode-integrator-function/79756/4 "2022-04-21T01:09:13Z")

</div>

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.

---

<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:** [April 21, 2022, 4:18am UTC](https://discourse.julialang.org/t/ode-integrator-function/79756/5 "2022-04-21T04:18:29Z")

</div>

> [@BinaryBot](#):
>
> 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.

Use `saveat=1`.

> [@BinaryBot](#):
>
> Be able to use the generated value at timestep n-1 at timestep n.

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

---

<div class="post-metadata">

**Author:** ![BinaryBot](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/binarybot/32/45258_2.png) [@BinaryBot](https://discourse.julialang.org/u/BinaryBot)\
**Post date:** [April 21, 2022, 10:09am UTC](https://discourse.julialang.org/t/ode-integrator-function/79756/6 "2022-04-21T10:09:04Z")

</div>

Thanks for the clarification. I looked at the documentation [https://diffeq.sciml.ai/stable/solvers/dde\_solve/](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.

---

<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:** [April 21, 2022, 10:53am UTC](https://discourse.julialang.org/t/ode-integrator-function/79756/7 "2022-04-21T10:53:05Z")

</div>

> [@BinaryBot](#):
>
> 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/](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!
