Replicating Matlab ode15s results in Julia

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.

How are your “actual” points generated?

First, you should use 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 for a native solver.

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

1 Like

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

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?

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

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?

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())

I get a type error on the if statement when I run this.
typeerror

Does this work?

using IfElse

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

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

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


Does this exception point towards something obvious, or do I have share my code to get some feedback?

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

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

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

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

1 Like

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

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