# Modelling and Simulation: Regarding DDEs

**URL:** <https://discourse.julialang.org/t/modelling-and-simulation-regarding-ddes/63677>\
**Category:** Modelling & Simulations\
**Tags:** question, first-steps\
**Created:** [June 28, 2021, 7:15am UTC](https://discourse.julialang.org/t/modelling-and-simulation-regarding-ddes/63677 "2021-06-28T07:15:30Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Phonindra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/phonindra/32/26561_2.png) [@Phonindra](https://discourse.julialang.org/u/Phonindra)\
**Post date:** [June 28, 2021, 7:15am UTC](https://discourse.julialang.org/t/modelling-and-simulation-regarding-ddes/63677/1 "2021-06-28T07:15:30Z")

</div>

I was facing a problem regarding delay. For low delay e.g. tau = 0.101 the code was fine as tau increases slightly as tau = 0.104 or more it shows some error not exactly error but warning and shows no promising result. While in Matlab those were fine there were no problem even for higher delay upto tau = 0.15! Why this is happening and how to overcome it!

Here is the .jl file:

###### 

function delay\_model(du,u,h,p,t)

```
a,b,c,tau = p

hist2 = h(p, t-tau)[2]

du[1] = -a*u[1] + b*(hist2)*u[3]

du[2] = a*u[1] - b*(hist2)*u[3] -c*u[2]^2 

du[3] = c*u[2]^2

```

end

h(p, t) = ones(3)

tau = 0.11

lags = [tau]

a = 0.04; b = 10; c = 3

p = (a,b,c,tau)

tspan = (0,20)

u0 = [1.0,0.9,1.0]

using DelayDiffEq

prob = DDEProblem(delay\_model,u0,h,tspan,p; constant\_lags=lags)

alg = MethodOfSteps(Tsit5())

sol = solve(prob,alg)

using Plots; plot(sol)

########

Here are the corresponding errors:  
for tau = 0.104:  
Warning: Interrupted. Larger maxiters is needed.

for tau = 0.108:  
Warning: dt \<= dtmin. Aborting. There is either an error in your model specification or the true solution is unstable.

Here are the attached file regarding various warnings:

 ![delayerror](https://global.discourse-cdn.com/julialang/original/3X/9/3/93f5f9a9c067cbb91a15d423b85e68f96e61b1df.png)

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [June 28, 2021, 4:50pm UTC](https://discourse.julialang.org/t/modelling-and-simulation-regarding-ddes/63677/2 "2021-06-28T16:50:03Z")

</div>

Hello and welcome to the community! Your question needs a better title and tags for the right people to find it and help you. What packages do you need help with? Is it a question about modeling and simulation etc?

---

<div class="post-metadata">

**Author:** ![Phonindra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/phonindra/32/26561_2.png) [@Phonindra](https://discourse.julialang.org/u/Phonindra)\
**Post date:** [June 28, 2021, 4:57pm UTC](https://discourse.julialang.org/t/modelling-and-simulation-regarding-ddes/63677/3 "2021-06-28T16:57:57Z")

</div>

Yes! It is related to modeling and simulation

---

<div class="post-metadata">

**Author:** ![ANasc](https://avatars.discourse-cdn.com/v4/letter/a/41988e/32.png) [@ANasc](https://discourse.julialang.org/u/ANasc)\
**Post date:** [June 28, 2021, 7:14pm UTC](https://discourse.julialang.org/t/modelling-and-simulation-regarding-ddes/63677/4 "2021-06-28T19:14:02Z")

</div>

I can’t try it right now, but while you wait for an expert maybe try a stiff solver. Rosenbrock23 or Rodas5 for a start. What solver were you using in matlab?

---

<div class="post-metadata">

**Author:** ![Phonindra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/phonindra/32/26561_2.png) [@Phonindra](https://discourse.julialang.org/u/Phonindra)\
**Post date:** [June 28, 2021, 7:21pm UTC](https://discourse.julialang.org/t/modelling-and-simulation-regarding-ddes/63677/5 "2021-06-28T19:21:50Z")

</div>

In Matlab I used dde23.

---

<div class="post-metadata">

**Author:** ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)\
**Post date:** [June 28, 2021, 7:48pm UTC](https://discourse.julialang.org/t/modelling-and-simulation-regarding-ddes/63677/6 "2021-06-28T19:48:36Z")

</div>

This post is a great starting point for debugging issues like this: [PSA: How to help yourself debug differential equation solving issues](https://discourse.julialang.org/t/psa-how-to-help-yourself-debug-differential-equation-solving-issues/62489)

The documentation suggests `BS3` may be close to `dde23`: [DDE Solvers · DifferentialEquations.jl](https://diffeq.sciml.ai/stable/solvers/dde_solve/#Nonstiff-DDEs)

---

<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 28, 2021, 10:58pm UTC](https://discourse.julialang.org/t/modelling-and-simulation-regarding-ddes/63677/7 "2021-06-28T22:58:49Z")

</div>

What was the MATLAB code? How did you verify that the rhs was giving exactly the same derivatives?

---

<div class="post-metadata">

**Author:** ![Phonindra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/phonindra/32/26561_2.png) [@Phonindra](https://discourse.julialang.org/u/Phonindra)\
**Post date:** [June 29, 2021, 4:26am UTC](https://discourse.julialang.org/t/modelling-and-simulation-regarding-ddes/63677/8 "2021-06-29T04:26:08Z")

</div>

Let me share corresponding MATLAB code:  
%%Function  
function dydt = Delay(t,y,Z);

global a b c

lagy = Z(:,1);

dydt=[-a_y(1) + b_lagy(2)_y(3);  
a_y(1) - b_lagy(2)y(3) - cy(2)^2;  
c_y(2)^2];

%%%%%

%%Output  
clc;  
clear all;  
global a b c

a = 0.04; b = 10; c = 3;

delay = 0.1;  
Exphst = [1.0 0.9 1.0];  
sol = dde23(@Delay,[delay],Exphst,[0, 20]);  
plot(sol.x,sol.y)

Now let see the corresponding results when tau = 0.1:

MATLAB result:  
 ![Julia_Delay_0.1](https://global.discourse-cdn.com/julialang/original/3X/c/e/ce459615e79ac2d9f7a947817e580bf47af589d2.png)

---

<div class="post-metadata">

**Author:** ![Phonindra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/phonindra/32/26561_2.png) [@Phonindra](https://discourse.julialang.org/u/Phonindra)\
**Post date:** [June 29, 2021, 4:27am UTC](https://discourse.julialang.org/t/modelling-and-simulation-regarding-ddes/63677/9 "2021-06-29T04:27:09Z")

</div>

Julia Result:

 ![Matlab_Delay_0.1](https://global.discourse-cdn.com/julialang/original/3X/e/e/eeb69d649d87ab909281a79c08418ff9ed80fb05.png)

---

<div class="post-metadata">

**Author:** ![Phonindra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/phonindra/32/26561_2.png) [@Phonindra](https://discourse.julialang.org/u/Phonindra)\
**Post date:** [June 29, 2021, 4:28am UTC](https://discourse.julialang.org/t/modelling-and-simulation-regarding-ddes/63677/10 "2021-06-29T04:28:45Z")

</div>

Now if I choose delay upto tau = 0.10519 Matblab shows the result as

 ![Matlab_Delay_0.10519](https://global.discourse-cdn.com/julialang/original/3X/a/f/af42aee3783616aa71449ac817ae0068b6d5fe5d.png)

---

<div class="post-metadata">

**Author:** ![Phonindra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/phonindra/32/26561_2.png) [@Phonindra](https://discourse.julialang.org/u/Phonindra)\
**Post date:** [June 29, 2021, 4:33am UTC](https://discourse.julialang.org/t/modelling-and-simulation-regarding-ddes/63677/11 "2021-06-29T04:33:42Z")

</div>

But for Julia it shows some warning like:  
Warning: Interrupted. Larger maxiters is needed.

while it shows almost similar result for tau = 0.102918!! see the figure  
 ![Julia_Delay_0.102918](https://global.discourse-cdn.com/julialang/original/3X/3/e/3e9c980f5833ada9f7afaf674236bf41645b488e.png)

So the value of the delay is inconsistent for different coding language!! But it should not be happened right?

I would also like to share one thing that the system is unbounded regarding high delay. For example for the maximum time point 20, in MATLAB if tau crosses 0.10519 i.e., if tau = 0.105195 it shows the solution beyond the integration level. But in case of Julia same thing happens for lower delay. From which it is sure that the system is unbounded regarding high delay.

---

<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 29, 2021, 10:10am UTC](https://discourse.julialang.org/t/modelling-and-simulation-regarding-ddes/63677/12 "2021-06-29T10:10:06Z")

</div>

Did you try lower tolerances? Did you try increasing maxiters there?

---

<div class="post-metadata">

**Author:** ![Phonindra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/phonindra/32/26561_2.png) [@Phonindra](https://discourse.julialang.org/u/Phonindra)\
**Post date:** [June 29, 2021, 7:15pm UTC](https://discourse.julialang.org/t/modelling-and-simulation-regarding-ddes/63677/13 "2021-06-29T19:15:38Z")

</div>

I have tried with lower tolerance, found no improvement!! For “increasing of maxiters” I have no idea how to handle this.

---

<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 29, 2021, 7:18pm UTC](https://discourse.julialang.org/t/modelling-and-simulation-regarding-ddes/63677/14 "2021-06-29T19:18:17Z")

</div>

I set a reminder for next week in case someone like @devmotion doesn’t find the time to take a look.

---

<div class="post-metadata">

**Author:** ![Phonindra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/phonindra/32/26561_2.png) [@Phonindra](https://discourse.julialang.org/u/Phonindra)\
**Post date:** [June 29, 2021, 7:21pm UTC](https://discourse.julialang.org/t/modelling-and-simulation-regarding-ddes/63677/15 "2021-06-29T19:21:47Z")

</div>

Thank you and will be waiting.

---

<div class="post-metadata">

**Author:** ![ANasc](https://avatars.discourse-cdn.com/v4/letter/a/41988e/32.png) [@ANasc](https://discourse.julialang.org/u/ANasc)\
**Post date:** [June 29, 2021, 9:18pm UTC](https://discourse.julialang.org/t/modelling-and-simulation-regarding-ddes/63677/16 "2021-06-29T21:18:58Z")

</div>

How are the history and initial conditions set in Matlab? Is Exphst the history? How do you set the initial conditions? If you set `h(p, t) = [1.0,0.9,1.0]` instead of `h(p, t) = ones(3)` (i.e., make initial conditions and history the same) what happens?

---

<div class="post-metadata">

**Author:** ![Phonindra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/phonindra/32/26561_2.png) [@Phonindra](https://discourse.julialang.org/u/Phonindra)\
**Post date:** [June 30, 2021, 5:43am UTC](https://discourse.julialang.org/t/modelling-and-simulation-regarding-ddes/63677/17 "2021-06-30T05:43:52Z")

</div>

Exphst is the history and initial conditions (considered same in MATLAB).

Following @ANasc, initial conditions and history were set same in fact I omitted u0 and set h(p, t) = [1.0 0.9 1.0] and found the result was fine at tau = 0.105 (improved!). Then I set h(p, t) = [1.0 0.1 1.0], surprisingly the result turns even better at high value of tau (=0.14)!! But in this case I found another issue, if time is set tspan = (0,230) then it is fine, once it set as tspan=(0,240) an warning appear as

Warning: Instability detected. Aborting

and simulations stops accordingly!

---

<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 30, 2021, 2:46pm UTC](https://discourse.julialang.org/t/modelling-and-simulation-regarding-ddes/63677/18 "2021-06-30T14:46:35Z")

</div>

what does that instability look like and why?

---

<div class="post-metadata">

**Author:** ![Phonindra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/phonindra/32/26561_2.png) [@Phonindra](https://discourse.julialang.org/u/Phonindra)\
**Post date:** [June 30, 2021, 5:53pm UTC](https://discourse.julialang.org/t/modelling-and-simulation-regarding-ddes/63677/19 "2021-06-30T17:53:49Z")

</div>

Setting h(p, t) = [1.0 0.1 1.0] and for tau = 0.14, it shows better result upto time scale tspan = (0,200)  
See the fig specially time scale 0-200:  
 ![Julia_Delay_ts_200](https://global.discourse-cdn.com/julialang/original/3X/e/e/ee80637a6e266150186259333f8fc265c5d2ffcb.png)

but if time scale set up as tspan = (0,300) it shows that warning:  
┌ Warning: Instability detected. Aborting

See the fig and see time scale was given (0,300) but if runs upto 240 approx:  
 ![Julia_Delay_ts_300](https://global.discourse-cdn.com/julialang/original/3X/a/d/ad0f69582f973750cd4093f2abf27262d92dda13.png)

---

<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:** [July 2, 2021, 10:43pm UTC](https://discourse.julialang.org/t/modelling-and-simulation-regarding-ddes/63677/20 "2021-07-02T22:43:13Z")

</div>

Can you open an issue on DelayDiffEq.jl?
