# Problem simulating Matlab ODE15s in Julia

**URL:** <https://discourse.julialang.org/t/problem-simulating-matlab-ode15s-in-julia/79687>\
**Category:** Modelling & Simulations\
**Tags:** diffeq, matlab, ode\
**Created:** [April 19, 2022, 12:29pm UTC](https://discourse.julialang.org/t/problem-simulating-matlab-ode15s-in-julia/79687 "2022-04-19T12:29:32Z")\
**Posts on this page:** 9\
**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 19, 2022, 12:29pm UTC](https://discourse.julialang.org/t/problem-simulating-matlab-ode15s-in-julia/79687/1 "2022-04-19T12:29:32Z")

</div>

Here is the code I have,

```julia
using Pkg
using PyCall

using CSV; using DataFrames; using Dierckx;using DifferentialEquations;using LSODA;using MATLABDiffEq; using ParameterizedFunctions;using IfElse;

p = [
	1.35e-2 #k1
    6.33e-1 #k2
    5.00e-5 #k3
    1.00e-3 #k4
    3.80e-3 #k5
    5.82e-1 #k6
    2.20e-2 #k7
    4.71 #k8
    1.08e-2 #k9
	#2.6 REMOVED
    1.35 #sigma /index 10
    0.63 #Km /index 11
    5.0 #Gb /index 12
    8.58 #Ib /index 13
]    

input = [
        75000, # D
        70, # Mb
        0 # t_meal_start
    ]

c = [0.00551, 
    17/70, 
    0.043,
    1,
    31, 
    3, 
    13/70, 
    9, 
    30, 
    0.1, 
    0.043*(p[11]+p[12])/p[12]-p[5]*1*p[13], 
    p[7]*p[12]/(1*31*p[13])*30 
] 
function differentialequations_1(du,u,p,t)
    Mg,Gpl,Ipl,Int=u
   
    mg = IfElse.ifelse(t> input[3], p[10].*p[1].^p[10].*(t-input[3]).^(p[10]-1) .* exp(-(p[1].*(t-input[3])).^p[10]) .* input[1],0)
    
    mgpl = p[2] .* Mg
    du[1] = mg - mgpl
    
   
    gli = c[3] - p[3] .* (Gpl-p[12])-p[4] .* c[4] .* (Ipl-p[13])

    ggu = p[2] .* (c[1]/(c[2]*input[2])) .* Mg;
    gnoni = c[11] .* (Gpl./(p[11]+Gpl));
    gi = p[5] .* c[4] .* Ipl .* (Gpl./(p[11]+Gpl))
    gre= IfElse.ifelse(Gpl > c[8], c[10] ./ (c[2]*input[2]) .* (Gpl - c[8]), 0 )
    du[2] = gli + ggu - gnoni - gi - gre

    
    
    t_lowerbound= t- c[9];

    if (time > c[9]) && (length(t_saved)>1) && (length(t_saved) == length(Gpl_saved))
        spl=Spline1D(t_saved,Gpl_saved)
        Gpl_lowerbound= spl(t_lowerbound);
    else
        Gpl_lowerbound = Gpl_saved[1]; 
    end 

    du[4] = (Gpl-p[12]) - (Gpl_lowerbound-p[12]);
    ipnc = (c[4].^-1) .* (p[6].*(Gpl-p[12]) +(p[7]/c[5]).*Int + (p[7]/c[5]) .*p[12]* c[9] + (p[8].*c[6]) .* du[2])

    iliv = c[12].*Ipl;
    iif = p[9].*(Ipl-p[13])
    
    du[3] = ipnc - iliv - iif

end

function compute(u0, local_G_saved,local_I_saved)
    
    global time=0
    global t_saved=[0]
    
    convert(Any, local_G_saved)
    convert(Any, local_I_saved)
    while time < 120
        tspan = (time, time+1)
        
        prob = ODEProblem(differentialequations_1,u0,tspan,p)
        sol=solve(prob, MATLABDiffEq.ode15s())
        time= time+1
        convert(Any, sol)
        push!(t_saved, time)
        push!(local_G_saved, last(sol[2,:]))
        push!(local_I_saved,last(sol[3,:]))
        u0=last(sol)
        
        
    end
    
    return local_G_saved, local_I_saved, u0
end 

global Gpl_saved= 5.0
global Ipl_saved=11.5
u0=[0.0,5.0,11.5,0.0]
p[1]=0.00933
p[5]=0.01
p[6]=0.3
p[8]=3.3
simulated_G_saved, simulated_I_saved, u= compute(u0, Gpl_saved,Ipl_saved);

```

which when run provides the error

> Unable to resolve the name mxsol.stats.

Now, I have tried reproducing the error is a smaller version but unfortunately I could not. With every attempt there were some other issues popping up. In some cases, the 4th u variable entirely disappeared in the 2nd iteration. Please run the code and see if you can reproduce the error and see where the flaw is.  
I tried to follow the julia recommended coding practices, but please feel free to provide additional pointers if necessary.

Thanks in advance 🙂

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [April 19, 2022, 12:43pm UTC](https://discourse.julialang.org/t/problem-simulating-matlab-ode15s-in-julia/79687/2 "2022-04-19T12:43:27Z")

</div>

> [@BinaryBot](#):
>
> `using CSV; using DataFrames; using Dierckx;using DifferentialEquations;using LSODA;using MATLABDiffEq; using ParameterizedFunctions;using IfElse;`

Better:

```julia
using CSV, DataFrames, Dierckx, DifferentialEquations, LSODA, MATLABDiffEq, ParameterizedFunctions, IfElse

```

OK, I still have to look at the main issue…

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [April 19, 2022, 12:50pm UTC](https://discourse.julialang.org/t/problem-simulating-matlab-ode15s-in-julia/79687/3 "2022-04-19T12:50:59Z")

</div>

Just realized that you want to use Matlab to solve a differential equation. I do not have a license, so I cannot help you, sorry… Why do you want to use Matlab in the first place?

---

<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 19, 2022, 12:59pm UTC](https://discourse.julialang.org/t/problem-simulating-matlab-ode15s-in-julia/79687/4 "2022-04-19T12:59:51Z")

</div>

I noticed a difference between the simulated results from Matlab and Julia. So by using the MatlabDiffEq package I want to ensure my Julia implementation is actually correct.

---

<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 19, 2022, 1:05pm UTC](https://discourse.julialang.org/t/problem-simulating-matlab-ode15s-in-julia/79687/5 "2022-04-19T13:05:15Z")

</div>

What version of MATLAB is that?

---

<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 19, 2022, 1:16pm UTC](https://discourse.julialang.org/t/problem-simulating-matlab-ode15s-in-julia/79687/6 "2022-04-19T13:16:46Z")

</div>

Matlab R2021b academic use

---

<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 19, 2022, 1:39pm UTC](https://discourse.julialang.org/t/problem-simulating-matlab-ode15s-in-julia/79687/7 "2022-04-19T13:39:52Z")

</div>

Try this branch: [https://github.com/SciML/MATLABDiffEq.jl/pull/31](https://github.com/SciML/MATLABDiffEq.jl/pull/31)

---

<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, 5:08pm UTC](https://discourse.julialang.org/t/problem-simulating-matlab-ode15s-in-julia/79687/8 "2022-04-20T17:08:32Z")

</div>

Thanks for what seems to be a patch, but could someone please tell me how I can try out this patch. Can I just modify MATALABDiffEq.jl locally or do I have to pull the entire branch? Or is there a different procedure?

---

<div class="post-metadata">

**Author:** ![albheim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albheim/32/34660_2.png) [@albheim](https://discourse.julialang.org/u/albheim)\
**Post date:** [April 20, 2022, 5:28pm UTC](https://discourse.julialang.org/t/problem-simulating-matlab-ode15s-in-julia/79687/9 "2022-04-20T17:28:33Z")

</div>

On the phone now, but i think `pkg> add MATLABDiffEq#ChrisRackauckas-patch-1` would add that specifik version.
