@Phonindra (Phonindra Nath Das) posted a new topic, subject "Modelling and Simulation: Regarding DDEs". It read:

   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)
    [code] 
        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
        
    [/code]
    
    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:  
    
    
    delayerror1241×327 37.1 KB

To which @baggepinnen (Fredrik Bagge Carlson) replied

    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?

Then @Phonindra (Phonindra Nath Das) said

    Yes! It is related to modeling and simulation

@ANasc (Alex Nascou) replied

    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?

@Phonindra (Phonindra Nath Das) replied

    In Matlab I used dde23.

@contradict (Russel Howe) replied

    This post is a great starting point for debugging issues like this: PSA: How to help yourself debug differential equation solving issues
    
    The documentation suggests `BS3` may be close to `dde23`: DDE Solvers · DifferentialEquations.jl

To which @ChrisRackauckas (Christopher Rackauckas) replied

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

So @Phonindra (Phonindra Nath Das) replied

    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) - c_ y(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:

To which @Phonindra (Phonindra Nath Das) replied

    Julia Result:  
    
    
    Matlab_Delay_0.1840×630 6.7 KB

So @Phonindra (Phonindra Nath Das) replied

    Now if I choose delay upto tau = 0.10519 Matblab shows the result as  
    
    
    Matlab_Delay_0.10519840×630 15.6 KB

After which, @Phonindra (Phonindra Nath Das) added

    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  
    
    
    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.

@ChrisRackauckas (Christopher Rackauckas) replied

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

So @Phonindra (Phonindra Nath Das) replied

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

@ChrisRackauckas (Christopher Rackauckas) replied

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

Then @Phonindra (Phonindra Nath Das) said

    Thank you and will be waiting.

@ANasc (Alex Nascou) replied

    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?

After which, @Phonindra (Phonindra Nath Das) added

    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!

Then @ChrisRackauckas (Christopher Rackauckas) said

    what does that instability look like and why?

@Phonindra (Phonindra Nath Das) replied

    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:  
    
    
    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:

Then @ChrisRackauckas (Christopher Rackauckas) said

    Can you open an issue on DelayDiffEq.jl?


{'source': 'discourse', 'url': 'https://discourse.julialang.org/t/modelling-and-simulation-regarding-ddes/63677', 'title': 'Modelling and Simulation: Regarding DDEs', 'views': 676, 'has_accepted_answer': False, 'word_count': 939, 'like_count': 1, 'reply_count': 13, 'participant_count': 5, 'posts': 20, 'tags': 'question first-steps', 'author': '@Phonindra (Phonindra Nath Das)', 'participants': '@Phonindra (Phonindra Nath Das), @baggepinnen (Fredrik Bagge Carlson), @ANasc (Alex Nascou), @contradict (Russel Howe), @ChrisRackauckas (Christopher Rackauckas)', 'image_url': ['https://global.discourse-cdn.com/business5/uploads/julialang/optimized/3X/6/5/650fa50688e21baa33b5c4a1f79e241739a81117_2_1024x269.png'], 'created_at': '2021-06-28T07:15:30.834Z', 'last_posted_at': '2021-07-02T22:43:13.835Z'}