# DifferentialEquations.jl discrete delay problem?

**URL:** https://discourse.julialang.org/t/differentialequations-jl-discrete-delay-problem/62829
**Category:** New to Julia
**Tags:** diffeq
**Created:** [June 13, 2021, 8:32am UTC](https://discourse.julialang.org/t/differentialequations-jl-discrete-delay-problem/62829 "2021-06-13T08:32:01Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![iHany](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihany/32/18151_2.png) [@iHany](https://discourse.julialang.org/u/iHany)
#### Post date: [June 13, 2021, 8:32am UTC](https://discourse.julialang.org/t/differentialequations-jl-discrete-delay-problem/62829/1 "2021-06-13T08:32:01Z")

</div>

In DifferentialEquations.jl (abbrev.: DiffEq.jl), [DDE (delay differential equations)](https://diffeq.sciml.ai/stable/tutorials/dde_example/) is supported.

But what I want is close to delay discrete equation; that is, get an external signal `h(t)`, then the state `x(t) = h(t - \tau)` with delay constant (`\tau > 0`).

What is the best practice in this case?

Note: I would like to keep using DiffEq.jl rather than utilising other packages.

---

<div class="post-metadata">

### Author: ![iHany](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihany/32/18151_2.png) [@iHany](https://discourse.julialang.org/u/iHany)
#### Post date: [June 13, 2021, 8:44am UTC](https://discourse.julialang.org/t/differentialequations-jl-discrete-delay-problem/62829/2 "2021-06-13T08:44:34Z")

</div>

One remedy would be to literally define a function `x(t) = h(t - tau)` by receiving the external signal function `h` and calling `x` within ODE function.

Is there any other way?

---

<div class="post-metadata">

### Author: ![SteffenPL](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steffenpl/32/206270_2.png) [@SteffenPL](https://discourse.julialang.org/u/SteffenPL)
#### Post date: [June 13, 2021, 9:07am UTC](https://discourse.julialang.org/t/differentialequations-jl-discrete-delay-problem/62829/3 "2021-06-13T09:07:27Z")

</div>

The equation \dot x(t) = h(t - \tau) is an ordinary differential equation.  
(Only if you access the state x in the past, i.e. x(t - \tau), you have a delay differential equation.)

Is h a function with a closed-form expression? Or is it something where you only know h(t\_i) (with t\_i being the timesteps?)

---

<div class="post-metadata">

### Author: ![iHany](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihany/32/18151_2.png) [@iHany](https://discourse.julialang.org/u/iHany)
#### Post date: [June 13, 2021, 10:15am UTC](https://discourse.julialang.org/t/differentialequations-jl-discrete-delay-problem/62829/4 "2021-06-13T10:15:03Z")

</div>

Not \dot{x}(t) = h(t-\tau) but x(t) = h(t - \tau).

~~It is possible to access the values of `h` at any instants, but it would be desirable if the access is limited only at the time instants when solver calls.~~ For now, suppose that we can access the values of `h` any desired time instants.

EDIT:  
the following remedy would work properly, but for the extensibility of my custom functions, I hope that the delayed state `x(t)` can be dealt with as the solution of an ODE.

> [@iHany](#):
>
> One remedy would be to literally define a function `x(t) = h(t - tau)` by receiving the external signal function `h` and calling `x` within ODE function.

---

<div class="post-metadata">

### Author: ![SteffenPL](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steffenpl/32/206270_2.png) [@SteffenPL](https://discourse.julialang.org/u/SteffenPL)
#### Post date: [June 13, 2021, 10:18am UTC](https://discourse.julialang.org/t/differentialequations-jl-discrete-delay-problem/62829/5 "2021-06-13T10:18:43Z")

</div>

That’s not like a differential equation at all?

It looks like just copying values? What is the complete problem you want to solve?

---

<div class="post-metadata">

### Author: ![iHany](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihany/32/18151_2.png) [@iHany](https://discourse.julialang.org/u/iHany)
#### Post date: [June 13, 2021, 10:26am UTC](https://discourse.julialang.org/t/differentialequations-jl-discrete-delay-problem/62829/6 "2021-06-13T10:26:24Z")

</div>

Thanks for your attention, @SteffenPL !

Yes, it’s actually now a DE 🙂  
To avoid confusion, I’ll explain my situation below:

I’m trying to add a fault detection and isolation (FDI) module in dynamical system. Basically, this receives a true effectiveness signal h(t) and produces delayed (and possibly noisy) estimation of the effectiveness (namely, \hat{h}(t) ).  
There are many models of the FDI. In my mind, the implementation of two simple FDI models are the primary task.

1. (Simple delay model) \hat{h} (t) = h(t-\tau)
2. (Low-pass-filter-like model) \dot{\hat{h}} (t) = (h(t) - \hat{h}(t)) / \tau

And I wanna deal with these models in the same manner. For the second model (and other parts of dynamical systems, e.g., multicopter in my case), my code is highly based on DifferentialEquations.jl.  
In this regard, I wanna implement the first model as a part of differential equation.

That’s why I’m looking forward to ways of realising FDI models in ODE manner (although it would not be expressed as an ODE).

---

<div class="post-metadata">

### Author: ![SteffenPL](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steffenpl/32/206270_2.png) [@SteffenPL](https://discourse.julialang.org/u/SteffenPL)
#### Post date: [June 13, 2021, 10:56am UTC](https://discourse.julialang.org/t/differentialequations-jl-discrete-delay-problem/62829/7 "2021-06-13T10:56:12Z")

</div>

Have you tried this? [Discrete Problems · DifferentialEquations.jl](https://diffeq.sciml.ai/stable/types/discrete_types/)  
You could define the ODE function as

```julia
u0 = [0]
p = ( h(t) = t^2, 0.2 )
tspan = (0, 100)

function f(du, u, p, t) 
  h, tau = p
  du[:] .= h(t - tau)
  nothing
end

prob = DiscreteProblem(f, u0, p, tspan)

```

(I haven’t tried the code, it’s just an idea)

Is it ok for you if you define the two different models with different RHS functions?

---

<div class="post-metadata">

### Author: ![iHany](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihany/32/18151_2.png) [@iHany](https://discourse.julialang.org/u/iHany)
#### Post date: [June 13, 2021, 11:25am UTC](https://discourse.julialang.org/t/differentialequations-jl-discrete-delay-problem/62829/8 "2021-06-13T11:25:20Z")

</div>

Can I compose `DiscreteProblem` and `ODEProblem`?  
If so, it may be possible.

EDIT:  
Roughly speaking,

1. For the first model

```julia
x0 = [0]
tspan = (0, 100)
h = t -> t
function f(dx, x, p, t)
    dx .= (h(t) - x) / tau
end
prob = ODEProblem(f, x0, tspan)

```

1. For the second model

```julia
x0 = [0]
tspan = (0, 100)
h = t -> t
function f(dx, x, p, t)
    dx .= h(t - tau)
end
prob = DiscreteProblem(f, x0, tspan)

```

Then, it seems hard to be dealt with the same manner; when changing models, I may have to write codes twice with different `DEProblem`…

---

<div class="post-metadata">

### Author: ![dawbarton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dawbarton/32/215461_2.png) [@dawbarton](https://discourse.julialang.org/u/dawbarton)
#### Post date: [June 13, 2021, 12:11pm UTC](https://discourse.julialang.org/t/differentialequations-jl-discrete-delay-problem/62829/9 "2021-06-13T12:11:44Z")

</div>

The discrete delay equation as you call it is usually known as a difference equation (assuming you are interested in continuous time; the DiscreteProblem appears to be for discrete time of I’m reading it correctly). My best guess for implementing it with DiffEq is to use DelayDiffEq with a singular mass matrix (you need to make sure that you pick a solver that is compatible with singular mass matrices). This will allow you to mix differential terms and difference terms directly.

I’m pretty sure there is an example in the DiffEq docs of using singular mass matrices. (I’m on my phone otherwise I’d look it up.)

---

<div class="post-metadata">

### Author: ![iHany](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihany/32/18151_2.png) [@iHany](https://discourse.julialang.org/u/iHany)
#### Post date: [June 13, 2021, 12:26pm UTC](https://discourse.julialang.org/t/differentialequations-jl-discrete-delay-problem/62829/10 "2021-06-13T12:26:20Z")

</div>

@dawbarton Your solution is exactly what I’ve wanted!

I saw [Handling Mass Matrices](https://diffeq.sciml.ai/stable/tutorials/advanced_ode_example/#Handling-Mass-Matrices) and adopt the code for my case.

- Example code

```julia
using DifferentialEquations
using LinearAlgebra

function test()
    function dynamics!(du,u,p,t)
        du .= u - ones(size(u)) * t
        nothing
    end
    n = 3
    x0 = zeros(n)
    M = zeros(n) |> Diagonal |> Matrix
    f = ODEFunction(dynamics!, mass_matrix=M)
    tspan = (0, 10.0)
    prob = ODEProblem(f, x0, tspan)
    sol = solve(prob)
end

```

- Result

```julia

julia> test()
retcode: Success
Interpolation: specialized 3rd order "free" stiffness-aware interpolation
t: 9-element Vector{Float64}:
  0.0
  1.0e-6
  1.1e-5
  0.00011099999999999999
  0.0011109999999999998
  0.011110999999999996
  0.11111099999999996
  1.1111109999999995
 10.0
u: 9-element Vector{Vector{Float64}}:
 [0.0, 0.0, 0.0]
 [1.0e-6, 1.0e-6, 1.0e-6]
 [1.1e-5, 1.1e-5, 1.1e-5]
 [0.00011099999999999999, 0.00011099999999999999, 0.00011099999999999999]
 [0.0011109999999999998, 0.0011109999999999998, 0.0011109999999999998]
 [0.011110999999999996, 0.011110999999999996, 0.011110999999999996]
 [0.11111099999999996, 0.11111099999999996, 0.11111099999999996]
 [1.1111109999999995, 1.1111109999999995, 1.1111109999999995]
 [10.0, 10.0, 10.0]

```

# Notes

- This is independent of the initial state (you can choose any `x0`). Even `nothing` can be assigned.

---

<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 13, 2021, 12:42pm UTC](https://discourse.julialang.org/t/differentialequations-jl-discrete-delay-problem/62829/11 "2021-06-13T12:42:15Z")

</div>

> [@iHany](#):
>
> Can I compose `DiscreteProblem` and `ODEProblem` ?  
> If so, it may be possible.

For automatic composition, it’s coming through ModelingToolkit, and you can follow this issue for ongoing information:

> <https://github.com/SciML/ModelingToolkit.jl/issues/894>
>
> There is a discussion about how to implement hybrid system, i.e. a combination o…f a continuous system (in my domain of control-systems engineering often called the "plant" or the "physical system") with a discrete-time system ("controller"), \[on Discourse\](https://discourse.julialang.org/t/diffeqs-hybrid-continuous-discrete-system-periodic-callback/23791). The question came up whether MTK does already, will in the future, or should at all support discrete-time systems.
> 
> Please don't cosider this a "request to implement it", but a discussion on whether it makes sense, how it could be done, what effort it would take, etc. I know too little about the inner workings of MTK (and DiffEqs.jl) to judge any of this.

2/4 chunks have been knocked out from it. I assume this is for optimal control, and yeah 😅 I’ll make ya’ll happy in a bit.

---

<div class="post-metadata">

### Author: ![iHany](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihany/32/18151_2.png) [@iHany](https://discourse.julialang.org/u/iHany)
#### Post date: [June 13, 2021, 12:46pm UTC](https://discourse.julialang.org/t/differentialequations-jl-discrete-delay-problem/62829/12 "2021-06-13T12:46:05Z")

</div>

Thanks a lot!

Yes, it is quite often necessary to compose discrete- and continuous-time dynamical systems (especially for discrete-input controller, e.g., model predictive control (MPC)).

As a student of control engineering, SciML is a very rich ecosystem for various kinds of simulation! 🙂  
I would be so pleased if the composition functionality is once provided.

EDIT: for other control engineers, I highly recommended you to take a look at [ComponentArrays.jl](https://github.com/jonniedie/ComponentArrays.jl) to construct nested and complex dynamical systems.  
Also, I’ve tried to find a good way to construct dynamical system and control simulator; [FlightSims.jl](https://github.com/JinraeKim/FlightSims.jl).

---

<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 13, 2021, 12:55pm UTC](https://discourse.julialang.org/t/differentialequations-jl-discrete-delay-problem/62829/13 "2021-06-13T12:55:42Z")

</div>

Yeah the whole MPC story is coming together. The extra pieces are all about feeding the solver extra information, new solvers specifically designed for better handling of discontinuities, etc… next year will be a fun one around optimal control. But until then, I’d recommend using DiffEq directly, and yes ComponentArrays.jl is an amazing abstraction for making the “by hand” approach very clean (I plan to add ComponentArrays.jl to the DifferentialEquations.jl first ODE tutorial to promote it even more).

---

<div class="post-metadata">

### Author: ![iHany](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihany/32/18151_2.png) [@iHany](https://discourse.julialang.org/u/iHany)
#### Post date: [June 13, 2021, 1:16pm UTC](https://discourse.julialang.org/t/differentialequations-jl-discrete-delay-problem/62829/14 "2021-06-13T13:16:00Z")

</div>

@ChrisRackauckas  
BTW, I have a plan of implementing discrete-time control via DiscreteCallback by considering parameters of parameterised ODE as control input.  
Will there be a distinction more than the difference between state variable of ODE (upcoming supports as I understand) and parameters of parameterised ODE (my plan)?
