# ODE solver with too many function calls

**URL:** <https://discourse.julialang.org/t/ode-solver-with-too-many-function-calls/90686>\
**Category:** Performance\
**Tags:** differentialequation\
**Created:** [November 23, 2022, 7:10am UTC](https://discourse.julialang.org/t/ode-solver-with-too-many-function-calls/90686 "2022-11-23T07:10:05Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![c\_sell](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/c_sell/32/38854_2.png) [@c\_sell](https://discourse.julialang.org/u/c_sell)\
**Post date:** [November 23, 2022, 7:10am UTC](https://discourse.julialang.org/t/ode-solver-with-too-many-function-calls/90686/1 "2022-11-23T07:10:05Z")

</div>

Hello,  
I am using an Ode solver to simulate a mechanical system with a mass and a spring. Besides the spring and an external force there is a nonlinear force acting on the system. Each computation takes around 4 ms.  
When im am simulating the exact same system in Matlab, the ode23t needs around 200 function evaluations.  
In Julia all solver I tried (non stiff and stiff solvers) are calling the ode\_function at least 2200 times. The one performing the best is the Rosenbrock23.  
I am using the same tolerances in matlab as in julia. Is there a solver option I am missing?  
I am also using the same initial step size.

Thanks for your help.

---

<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:** [November 23, 2022, 11:18am UTC](https://discourse.julialang.org/t/ode-solver-with-too-many-function-calls/90686/2 "2022-11-23T11:18:48Z")

</div>

Is it exactly the same ODE? Did you check at random values? Can you share your validation?

---

<div class="post-metadata">

**Author:** ![c\_sell](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/c_sell/32/38854_2.png) [@c\_sell](https://discourse.julialang.org/u/c_sell)\
**Post date:** [November 23, 2022, 2:51pm UTC](https://discourse.julialang.org/t/ode-solver-with-too-many-function-calls/90686/3 "2022-11-23T14:51:46Z")

</div>

I am sorry. I didnt saw a typo in the computation of the jacobi for 1 week.  
Thanks for your answer

---

<div class="post-metadata">

**Author:** ![c\_sell](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/c_sell/32/38854_2.png) [@c\_sell](https://discourse.julialang.org/u/c_sell)\
**Post date:** [November 27, 2022, 7:59pm UTC](https://discourse.julialang.org/t/ode-solver-with-too-many-function-calls/90686/4 "2022-11-27T19:59:10Z")

</div>

Hi, i have a follow up problem to my last one above.  
I compared most of the stiff solvers avaible against the ode15s in Matlab. As my equation gets more and more stiff, the julia solvers still have at least twice as much function calls.  
If I reduce the error tollerance, the solution i obtain is not acceptable. Since the function calls are expensive I really would like to reduce them.

I checked the ode\_function and the ode\_jacobian with random inputs against my Matlab code. The results are exactly the same.  
A list of the solvers I tried:

- Some SDIRKT Methods ( but my jacobian is changing alot)
- RadauIIA3 and RadauIIA5
- ROS3P
- Rodas3,Rodas4, Rodas4P2, Rodas5P, Rodsa4P and 5P
- Rosenbrock23

Thanks for your time. Tell me if you have an idea or you need some more informations.

---

<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:** [November 27, 2022, 8:03pm UTC](https://discourse.julialang.org/t/ode-solver-with-too-many-function-calls/90686/5 "2022-11-27T20:03:38Z")

</div>

What about QNDF and FBDF? These are the solvers most similar to ode15s. In general the stiff solvers that are worth trying first are Rosenbrok23, Rodas5P, QNDF, and FBDF.

---

<div class="post-metadata">

**Author:** ![c\_sell](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/c_sell/32/38854_2.png) [@c\_sell](https://discourse.julialang.org/u/c_sell)\
**Post date:** [November 27, 2022, 9:27pm UTC](https://discourse.julialang.org/t/ode-solver-with-too-many-function-calls/90686/6 "2022-11-27T21:27:35Z")

</div>

Ahh yes. I tried them. For the same tolerances I get 4 times as many evals. And the result in Matlab is even more accurate.  
If i am reducing the maximum step size I am getting compareable results.  
But this should not be the way.

---

<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:** [November 27, 2022, 9:36pm UTC](https://discourse.julialang.org/t/ode-solver-with-too-many-function-calls/90686/7 "2022-11-27T21:36:59Z")

</div>

You probably shouldn’t be touching the maximum step sizes (and reducing the maximum step size should strictly increase the number of evaluations).

---

<div class="post-metadata">

**Author:** ![c\_sell](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/c_sell/32/38854_2.png) [@c\_sell](https://discourse.julialang.org/u/c_sell)\
**Post date:** [November 27, 2022, 9:40pm UTC](https://discourse.julialang.org/t/ode-solver-with-too-many-function-calls/90686/8 "2022-11-27T21:40:16Z")

</div>

I meant, if I am using a smaller max. step size I can reducde the tolerance.  
But as you said, this is no solution^^

---

<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:** [November 27, 2022, 9:50pm UTC](https://discourse.julialang.org/t/ode-solver-with-too-many-function-calls/90686/9 "2022-11-27T21:50:07Z")

</div>

how are you measuring accuracy? two different solvers with the same tolerance can yield very different accuracy.

---

<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:** [November 27, 2022, 11:54pm UTC](https://discourse.julialang.org/t/ode-solver-with-too-many-function-calls/90686/10 "2022-11-27T23:54:15Z")

</div>

And there’s no code to share?

---

<div class="post-metadata">

**Author:** ![c\_sell](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/c_sell/32/38854_2.png) [@c\_sell](https://discourse.julialang.org/u/c_sell)\
**Post date:** [November 28, 2022, 7:35am UTC](https://discourse.julialang.org/t/ode-solver-with-too-many-function-calls/90686/11 "2022-11-28T07:35:39Z")

</div>

I compared the results to a Simultion with a lot smaller tolerances.

---

<div class="post-metadata">

**Author:** ![c\_sell](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/c_sell/32/38854_2.png) [@c\_sell](https://discourse.julialang.org/u/c_sell)\
**Post date:** [November 28, 2022, 7:38am UTC](https://discourse.julialang.org/t/ode-solver-with-too-many-function-calls/90686/12 "2022-11-28T07:38:38Z")

</div>

Its hard, since the computation of the nonlinear force is done in many files. I also dindt wrote all the parts by my selfe. So I am not sure if I am allowed to share them public.  
I could share the rest if its helpfull.

---

<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:** [November 28, 2022, 12:56pm UTC](https://discourse.julialang.org/t/ode-solver-with-too-many-function-calls/90686/13 "2022-11-28T12:56:31Z")

</div>

Well it’s really hard to help if there’s no code to compare. But the #1 issue always tends to be that the translation is not 1-1. If you run all of the functions with random (u,p,t) do you get the same exact answer from MATLAB and Julia? In almost all cases like this, I’ve found that to be false, so that’s the first thing I would look into detail with.

Other than that `ode23t` is very different from solve of the solvers you’re discussing here. Did you try the `Trapezoidal` counterpart? L-stability is good for “most” equations, but not ones for which the dampening is not beneficial. In which case, the reversibility (and “close to symplecticness”) can be beneficial. So it’s good to try methods that are similar to what you’ve seen work well before.

Lastly, you may want to watch this video which highlights some of the most common issues people have when translating, particularly from MATLAB:

[![](https://global.discourse-cdn.com/julialang/original/3X/5/d/5d8be9be2d861eecc910a9bdda15b5b546dd839f.jpeg "How to debug Julia simulation codes (ODEs, optimization, etc.!)") ](https://www.youtube.com/watch?v=g-iOOhh2U6o)

---

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [November 28, 2022, 3:13pm UTC](https://discourse.julialang.org/t/ode-solver-with-too-many-function-calls/90686/14 "2022-11-28T15:13:32Z")

</div>

Hi @c_sell !

Just one question: are you simulating the system using a .m file or simulink?

---

<div class="post-metadata">

**Author:** ![c\_sell](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/c_sell/32/38854_2.png) [@c\_sell](https://discourse.julialang.org/u/c_sell)\
**Post date:** [November 28, 2022, 3:40pm UTC](https://discourse.julialang.org/t/ode-solver-with-too-many-function-calls/90686/15 "2022-11-28T15:40:21Z")

</div>

In matlab I am simulating using a .m file

---

<div class="post-metadata">

**Author:** ![c\_sell](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/c_sell/32/38854_2.png) [@c\_sell](https://discourse.julialang.org/u/c_sell)\
**Post date:** [November 28, 2022, 3:42pm UTC](https://discourse.julialang.org/t/ode-solver-with-too-many-function-calls/90686/16 "2022-11-28T15:42:01Z")

</div>

Thanks for your answer. I used in Matlab the ode15s. The function outputs are the same for random inputs.  
I have already seen your video before and I tried the steps there.

---

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [November 28, 2022, 3:42pm UTC](https://discourse.julialang.org/t/ode-solver-with-too-many-function-calls/90686/17 "2022-11-28T15:42:12Z")

</div>

Ok, many many years ago, when @ChrisRackauckas helped me a lot by adding amazing features to DifferentialEquations.jl ecosystem, I was doing exactly the same comparison. I see results similar to yours, but it was caused by simulink. It seems it were automatically changing the tolerances, yielding less evaluations. However, everything was very similar when using .m and the function `ode45`.

---

<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:** [November 28, 2022, 3:46pm UTC](https://discourse.julialang.org/t/ode-solver-with-too-many-function-calls/90686/18 "2022-11-28T15:46:01Z")

</div>

> [@c\_sell](#):
>
> I used in Matlab the ode15s.

If you used `ode15s`, then you should try `QNDF` and `FBDF` which are the most similar (`QNDF` should be fairly similar).

---

<div class="post-metadata">

**Author:** ![c\_sell](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/c_sell/32/38854_2.png) [@c\_sell](https://discourse.julialang.org/u/c_sell)\
**Post date:** [November 28, 2022, 3:46pm UTC](https://discourse.julialang.org/t/ode-solver-with-too-many-function-calls/90686/19 "2022-11-28T15:46:39Z")

</div>

Thats what I tried.

---

<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:** [November 28, 2022, 3:48pm UTC](https://discourse.julialang.org/t/ode-solver-with-too-many-function-calls/90686/20 "2022-11-28T15:48:48Z")

</div>

Interesting. With or without autodiff? Does the difference there matter? Are there complex valued variables? And the function is exactly the same on random values?

[Next page](https://discourse.julialang.org/t/ode-solver-with-too-many-function-calls/90686.md?page=2)
