# DifferentialEquationsjl with ForwardDiff.jl

**URL:** <https://discourse.julialang.org/t/differentialequationsjl-with-forwarddiff-jl/75135>\
**Category:** New to Julia\
**Created:** [January 24, 2022, 4:08pm UTC](https://discourse.julialang.org/t/differentialequationsjl-with-forwarddiff-jl/75135 "2022-01-24T16:08:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yinmin\_Liu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yinmin_liu/32/31880_2.png) [@Yinmin\_Liu](https://discourse.julialang.org/u/Yinmin_Liu)\
**Post date:** [January 24, 2022, 4:08pm UTC](https://discourse.julialang.org/t/differentialequationsjl-with-forwarddiff-jl/75135/1 "2022-01-24T16:08:20Z")

</div>

I am trying to solve ODEs using DifferentialEquationsjl while generating jacobian using ForwardDiff.jl.

My current code looks like the following:

 ![Screen Shot 2022-01-24 at 11.03.45 AM](https://global.discourse-cdn.com/julialang/original/3X/1/0/10988cdb10f9e336424e0eec4a68bf352f8f4a6e.png)

It does work, but it is even slower than when I define the function as rober(du,u,p,t) and solve it without passing Jacobian. Therefore, I modified my code as follows. The solution is not correct, all the variables are not changing over time.

 ![Screen Shot 2022-01-24 at 11.07.58 AM](https://global.discourse-cdn.com/julialang/original/3X/1/3/131e5c219bac273464c90a10d2491dc85d7f297a.png)

---

<div class="post-metadata">

**Author:** ![josuagrw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josuagrw/32/1015_2.png) [@josuagrw](https://discourse.julialang.org/u/josuagrw)\
**Post date:** [January 24, 2022, 4:21pm UTC](https://discourse.julialang.org/t/differentialequationsjl-with-forwarddiff-jl/75135/2 "2022-01-24T16:21:24Z")

</div>

> [@Please read: make it easier to help you](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757):
>
> Welcome to the Julia Discourse! We are enthusiastic about helping Julia programmers, both beginner and experienced. This public service announcement (PSA) outlines best practices when asking for help. Following these points makes it easier for us to help you and more likely you’ll get a prompt, useful answer. Keywords are highlighted to make it easier to refer to specific points. Choose a descriptive title that captures the key part of your question, eg “plots with multiple axes” instead of …

---

<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:** [January 24, 2022, 5:33pm UTC](https://discourse.julialang.org/t/differentialequationsjl-with-forwarddiff-jl/75135/3 "2022-01-24T17:33:00Z")

</div>

> [@Yinmin\_Liu](#):
>
> It does work, but it is even slower than when I define the function as rober(du,u,p,t) and solve it without passing Jacobian.

Note that it automatically uses ForwardDiff.jl. But it’s doing something quite a bit smarter because it’s caching the configs and using SparseDiffTools stuff. If you just want ForwardDiff autodiff’d Jacobians, I’d recommend you just use the default and do nothing.

---

<div class="post-metadata">

**Author:** ![Yinmin\_Liu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yinmin_liu/32/31880_2.png) [@Yinmin\_Liu](https://discourse.julialang.org/u/Yinmin_Liu)\
**Post date:** [January 24, 2022, 5:53pm UTC](https://discourse.julialang.org/t/differentialequationsjl-with-forwarddiff-jl/75135/4 "2022-01-24T17:53:23Z")

</div>

Hi Chris,  
I thought Julia ODE solvers are also using finite difference when Jacobian is not provided by users. Are all the Julia solvers automatically using ForwardDiff.jl and SparseDiffTools? Including CVODE\_BDF()?

If I have a very high dimensional and stiff ODE system with no access to the analytical jacobian, the best way to solve it is just to use the default and do not pass a jacobian matrix to the solver. Am I 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:** [January 24, 2022, 5:56pm UTC](https://discourse.julialang.org/t/differentialequationsjl-with-forwarddiff-jl/75135/5 "2022-01-24T17:56:22Z")

</div>

> [@Yinmin\_Liu](#):
>
> I thought Julia ODE solvers are also using finite difference when Jacobian is not provided by users. Are all the Julia solvers automatically using ForwardDiff.jl and SparseDiffTools? Including CVODE\_BDF()?

All of the pure Julia methods default to using ForwardDiff.jl via SparseDiffTools.jl. To turn this off and instead use FiniteDiff.jl, you would set `autodiff=false`, like `Rodas5(autodiff=false)`. Sundials is an outlier since it uses its native algorithm here. This is documented at [https://diffeq.sciml.ai/stable/features/performance\_overloads/](https://diffeq.sciml.ai/stable/features/performance_overloads/)

> [@Yinmin\_Liu](#):
>
> If I have a very high dimensional and stiff ODE system with no access to the analytical jacobian, the best way to solve it is just to use the default and do not pass a jacobian matrix to the solver. Am I correct?

I would recommend reading the tutorial on this topic:

[https://diffeq.sciml.ai/stable/tutorials/advanced\_ode\_example/](https://diffeq.sciml.ai/stable/tutorials/advanced_ode_example/)

Giving a sparsity pattern will automatically specialize the Jacobian calculation on that sparsity pattern via SparseDiffTools.jl, but sometimes analytical solutions to Jacobians can be faster given certain sparsity patterns. That said, using preconditioned Newton-Krylov can also be essential in this domain.
