# DDE running result is different from the result in Matlab

**URL:** <https://discourse.julialang.org/t/dde-running-result-is-different-from-the-result-in-matlab/66769>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [August 21, 2021, 7:47am UTC](https://discourse.julialang.org/t/dde-running-result-is-different-from-the-result-in-matlab/66769 "2021-08-21T07:47:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Liwei17](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/liwei17/32/28423_2.png) [@Liwei17](https://discourse.julialang.org/u/Liwei17)\
**Post date:** [August 21, 2021, 7:47am UTC](https://discourse.julialang.org/t/dde-running-result-is-different-from-the-result-in-matlab/66769/1 "2021-08-21T07:47:38Z")

</div>

I’m using DifferentialEquations.jl package to solve a delay differential equations problem. However, the result is different from what I got in Matlab. Can anybody tell me which part goes wrong? \>\<

* * *

This is my code in Julia:

```julia
using DifferentialEquations

function bc_model(du,u,h,p,t)
    mu0, mu1, mu2, mu3, mu4, k0, k1, k2, k3, k4, k5, k6, n, P0, tau= p
    hist5 = h(p, t-tau)[5]
    hist2 = h(p, t-tau)[2]
    du[1] = -mu0*u[1] - k1*u[1] + k0*(1/1+(hist5/P0)^n)
    du[2] = -mu1*u[2] + k1*u[1]
    du[3] = -mu2*u[3] + 2*k4*u[4] - 2*k3*u[3]^2 + k2*hist2
    du[4] = -mu3*u[4] + k3*u[3]^2 - k4*u[4] - k5*u[4] + k6*u[5]
    du[5] = -mu4*u[5] + k5*u[4] - k6*u[5]
end

h(p, t) = ones(5)

tau = 8
lags = [tau]
P0 = 1250; n = 5;
mu0 = 0.03; mu1 = 0.03; mu2 = 0.3; mu3 = 0.03; mu4 = 0.03;
k0 = 10; k1 = 1; k2 = 10; k3 = 0.01; k4 = 0.001; k5 = 10; k6 = 0;
p = (mu0, mu1, mu2, mu3, mu4, k0, k1, k2, k3, k4, k5, k6, n, P0, tau)
tspan = (0.0, 1000.0)
u0 = [1.0, 1.0, 1.0, 1.0, 1.0]

prob = DDEProblem(bc_model,u0,h,tspan,p, constant_lags=lags)

alg = MethodOfSteps(BS3())

sol = solve(prob,alg)

using Plots; plot(sol)

```

![Screenshot 2021-08-21 at 08.44.36](https://global.discourse-cdn.com/julialang/original/3X/4/5/45de8492bc1945cc4d0e1ddf52c17274de38fa86.png)

* * *

---

<div class="post-metadata">

**Author:** ![Liwei17](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/liwei17/32/28423_2.png) [@Liwei17](https://discourse.julialang.org/u/Liwei17)\
**Post date:** [August 21, 2021, 7:49am UTC](https://discourse.julialang.org/t/dde-running-result-is-different-from-the-result-in-matlab/66769/2 "2021-08-21T07:49:48Z")

</div>

This is my code in Matlab:

```julia
tau1 = 8; tau2 = 8; lags = [tau1 tau2];
tspan = [0 2000];
sol = dde23(@ddefun, lags, @history, tspan);

plot(sol.x, sol.y)
xlabel('Time t');
ylabel('Solution y');
legend('y_1','y_2','y_3','y_4','y_5')

function dydt = ddefun(t, y, Z)

p0 = 1250; n = 5;
mu0 = 0.03; mu1 = 0.03; mu2 = 0.3; mu3 = 0.03; mu4 = 0.03;
k0 = 10; k1 = 1; k2 = 10; k3 = 0.01; k4 = 0.001; k5 = 10; k6 = 0;

ylag1 = Z(:, 1);
ylag2 = Z(:, 2);

dydt = zeros(5, 1);
dydt(1) = -mu0*y(1) + k0*(1/(1 + (ylag1(5)/p0)^n)) - k1*y(1);
dydt(2) = -mu1*y(2) + k1*y(1);
dydt(3) = -mu2*y(3) + k2*ylag2(2) - 2*k3*y(3)^2 + 2*k4*y(4);
dydt(4) = -mu3*y(4) + k3*y(3)^2 - k4*y(4) - k5*y(4) + k6*y(5);
dydt(5) = -mu4*y(5) + k5*y(4) - k6*y(5);
end

function s = history(t)
s = ones(5, 1);
end

```

![Screenshot 2021-08-21 at 08.46.45](https://global.discourse-cdn.com/julialang/original/3X/1/1/113d01d1390d4933724e2d7f36d8af9ca864b53f.png)

---

<div class="post-metadata">

**Author:** ![Liwei17](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/liwei17/32/28423_2.png) [@Liwei17](https://discourse.julialang.org/u/Liwei17)\
**Post date:** [August 21, 2021, 10:20am UTC](https://discourse.julialang.org/t/dde-running-result-is-different-from-the-result-in-matlab/66769/3 "2021-08-21T10:20:27Z")

</div>

**Update:**  
Problem solved.  
The issue is in function du[1]. I typed du[1] wrong. After changing du[1], the Julia code shows the same result as the Matlab code.
