# Replicating Matlab ode15s results in Julia

**URL:** <https://discourse.julialang.org/t/replicating-matlab-ode15s-results-in-julia/79280>\
**Category:** Modelling & Simulations\
**Tags:** plotting, diffeq, matlab, ode\
**Created:** [April 9, 2022, 10:02pm UTC](https://discourse.julialang.org/t/replicating-matlab-ode15s-results-in-julia/79280 "2022-04-09T22:02:15Z")\
**Posts on this page:** 16\
**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 9, 2022, 10:02pm UTC](https://discourse.julialang.org/t/replicating-matlab-ode15s-results-in-julia/79280/1 "2022-04-09T22:02:15Z")

</div>

I’m new to Julia and I have been working on ODE solvers for my first project .I recently got my hands onto some data which was generated using Matlab. I have been trying to replicate the results using the Julia DifferentialEquation.jl package, but unfortunately there is some difference in the outputs. You see the difference in the image below. The golden line represents the Matlab results, while the other lines represents the results obtained from different solvers. So far I have set the reltol to the same as in Matlab, but I am not sure if I need to change any other parameters.

Can someone with experience in Matlab and Julia help me achieve same or very similar results as observed in Matlab using the ode15s solver.

 ![juliaforum](https://global.discourse-cdn.com/julialang/original/3X/f/1/f17b828db4321a58dbf33b023a9641b338dae28f.png)

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [April 9, 2022, 10:08pm UTC](https://discourse.julialang.org/t/replicating-matlab-ode15s-results-in-julia/79280/2 "2022-04-09T22:08:52Z")

</div>

How are your “actual” points generated?

---

<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:** [April 9, 2022, 10:09pm UTC](https://discourse.julialang.org/t/replicating-matlab-ode15s-results-in-julia/79280/3 "2022-04-09T22:09:40Z")

</div>

First, you should use [https://github.com/SciML/MATLABDiffEq.jl](https://github.com/SciML/MATLABDiffEq.jl) to ensure your re-implementation of the integrand is correct.

Once you can get the same answer using the same integrator, then this page has some advice [ODE Solvers · DifferentialEquations.jl](https://diffeq.sciml.ai/stable/solvers/ode_solve/#Translations-from-MATLAB/Python/R) for a native solver.

If that doesn’t help, post an example of the code for people to dig in to!

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [April 9, 2022, 10:15pm UTC](https://discourse.julialang.org/t/replicating-matlab-ode15s-results-in-julia/79280/4 "2022-04-09T22:15:14Z")

</div>

> [@BinaryBot](#):
>
> I have been trying to replicate the results using the Julia DifferentialEquation.jl package, but unfortunately there is some difference in the outputs.

Welcome @BinaryBot, I’m slightly puzzled: we are not even able to replicate your results?

---

<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 10, 2022, 10:51am UTC](https://discourse.julialang.org/t/replicating-matlab-ode15s-results-in-julia/79280/5 "2022-04-10T10:51:04Z")

</div>

> [@BinaryBot](#):
>
> So far I have set the reltol to the same as in Matlab, but I am not sure if I need to change any other parameters.

And the abstol? Did you do a convergence test, bringing it down to 1e-12 or 1e-14 to see if the two converge to the same values? And did you test using MATLABDiffEq.jl?

---

<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 10, 2022, 12:28pm UTC](https://discourse.julialang.org/t/replicating-matlab-ode15s-results-in-julia/79280/6 "2022-04-10T12:28:52Z")

</div>

Thanks, I did not know that the MATLABDiffEq.jl package existed. Will look into that and come back here.

---

<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 11, 2022, 2:31pm UTC](https://discourse.julialang.org/t/replicating-matlab-ode15s-results-in-julia/79280/7 "2022-04-11T14:31:13Z")

</div>

Correct me if I’m wrong here, the MATLABDiffEq.jl package expects it’s differential equation function to be a Parameterized function, but unfortunately you cannot use if statements with the passed u values. My actual diffeq has multiple such if-statements. Is there a work around?

```julia
function diffeq(du,u,p,t)
    if u[1]>0
        du[1]= u[1]+10
    end    
end
u0=[0.0,1.0]
tspan= (0,1)
prob= ODEProblem(diffeq,u0,tspan,p)
sol= solve(prob, MATLABDiffEq.ode15s())

```

 ![paramfunc](https://global.discourse-cdn.com/julialang/original/3X/3/4/34ad04c6a115b42cfb00bb1e0d02fdec756fba49.png)

---

<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 11, 2022, 2:32pm UTC](https://discourse.julialang.org/t/replicating-matlab-ode15s-results-in-julia/79280/8 "2022-04-11T14:32:29Z")

</div>

I get a type error on the if statement when I run this.  
 ![typeerror](https://global.discourse-cdn.com/julialang/original/3X/6/8/68b95a4783b0b195f248cc21aecfb72e1de9a8c2.png)

---

<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:** [April 11, 2022, 3:20pm UTC](https://discourse.julialang.org/t/replicating-matlab-ode15s-results-in-julia/79280/9 "2022-04-11T15:20:42Z")

</div>

Does this work?

```julia
using IfElse

function diffeq(du,u,p,t)
    du[1] = IfElse.ifelse(u[1]>0, u[1]+10, u[1])
end

```

---

<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 11, 2022, 8:58pm UTC](https://discourse.julialang.org/t/replicating-matlab-ode15s-results-in-julia/79280/10 "2022-04-11T20:58:30Z")

</div>

It does, but how parameterised functions work is beyond me. If you have any learning sources please do share.

---

<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 11, 2022, 9:01pm UTC](https://discourse.julialang.org/t/replicating-matlab-ode15s-results-in-julia/79280/11 "2022-04-11T21:01:24Z")

</div>

Although the diff equation function seems to be working, the Matlab solver does not seem to be happy yet.

 ![matlabexception](https://global.discourse-cdn.com/julialang/original/3X/c/3/c306dd589c0471fb22894a29e7d127d8b31e36f2.png)  
Does this exception point towards something obvious, or do I have share my code to get some feedback?

---

<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:** [April 11, 2022, 9:39pm UTC](https://discourse.julialang.org/t/replicating-matlab-ode15s-results-in-julia/79280/12 "2022-04-11T21:39:14Z")

</div>

It uses ModelingToolkit to create a symbolic version of your julia code:

[https://mtk.sciml.ai/stable/mtkitize\_tutorials/modelingtoolkitize/](https://mtk.sciml.ai/stable/mtkitize_tutorials/modelingtoolkitize/)

And then Symbolics.jl to turn that symbolic form into MATLAB code:

[https://symbolics.juliasymbolics.org/stable/manual/build\_function/#build\_function-1](https://symbolics.juliasymbolics.org/stable/manual/build_function/#build_function-1)

---

<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:** [April 11, 2022, 9:44pm UTC](https://discourse.julialang.org/t/replicating-matlab-ode15s-results-in-julia/79280/13 "2022-04-11T21:44:15Z")

</div>

I don’t understand that one, but it looks like a problem communicating with MATLAB. I don’t have copy to test with, but if you can make a small example that exhibits that error it would likely be worth opening an issue with MATLABDiffEq.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:** [April 12, 2022, 2:14am UTC](https://discourse.julialang.org/t/replicating-matlab-ode15s-results-in-julia/79280/14 "2022-04-12T02:14:31Z")

</div>

Did MATLAB change how you get these stats in some version? [https://github.com/SciML/MATLABDiffEq.jl/blob/master/src/MATLABDiffEq.jl#L69](https://github.com/SciML/MATLABDiffEq.jl/blob/master/src/MATLABDiffEq.jl#L69)

---

<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:20pm UTC](https://discourse.julialang.org/t/replicating-matlab-ode15s-results-in-julia/79280/15 "2022-04-19T12:20:17Z")

</div>

To the best of my knowledge no, although if there is a way to reset everything and retry please do let me know.

---

<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:31pm UTC](https://discourse.julialang.org/t/replicating-matlab-ode15s-results-in-julia/79280/16 "2022-04-19T12:31:32Z")

</div>

I have opened a new discussion here:[https://discourse.julialang.org/t/problem-simulating-matlab-ode15s-in-julia/79687](https://discourse.julialang.org/t/problem-simulating-matlab-ode15s-in-julia/79687). Please have a look and see if you can spot something.
