# ForwardDiff with Rosenbrock in DifferentialEquations.jl

**URL:** <https://discourse.julialang.org/t/forwarddiff-with-rosenbrock-in-differentialequations-jl/132605>\
**Category:** Modelling & Simulations\
**Tags:** sciml, differentialequation\
**Created:** [September 23, 2025, 7:09pm UTC](https://discourse.julialang.org/t/forwarddiff-with-rosenbrock-in-differentialequations-jl/132605 "2025-09-23T19:09:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bill\_Holmes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bill_holmes/32/47921_2.png) [@Bill\_Holmes](https://discourse.julialang.org/u/Bill_Holmes)\
**Post date:** [September 23, 2025, 7:09pm UTC](https://discourse.julialang.org/t/forwarddiff-with-rosenbrock-in-differentialequations-jl/132605/1 "2025-09-23T19:09:40Z")

</div>

Hello all. I am having an issue using Rosenbrock methods in DifferentialEquations.jl. Below is a MWE. My general problem is that I am trying to optimize parameters of a stiff system of ODEs. This is a relatively small problem so something like Rodas4P with ForwardDiff is suitable. However I am running into issues computing gradients with respect to parameters. When I attempt to do so, I get the following error: **type LinearProblem has no field f** (see example below). The error tracks back to the _solve_ line. I’m not seeing any dual number errors in the stack trace and I did attempt to type the initial condition (if it is necessary) to ensure the solver inferred the dual type for du.

If I replace Rodas4P with Tsit5(), everything is fine. If I use **Rodas4P(autodiff = false)**, I get the error once again. So it is something about the linear solve in the Rosenbrock method that is causing the issue.

Any thoughts? A search on this specific error doesn’t seem to pull anything up.

```julia-auto
function ODE_tmp!(du,u,p,t)

    a = p[1]
    b = p[2]

    du[1] = a - u[1]
    du[2] = a + b - u[2]

    return nothing
end

function run_sim(x_change, odeprob_in)

    u0 = eltype(x_change[1]).(odeprob_in.u0)

    println("u0 = ", u0)
    println("x_change = ", x_change[1])
    println(typeof(x_change[1]))

    odeprob_new = remake(odeprob_in , p = x_change, u0 = u0);
    sol = solve(odeprob_new, Rodas4P(), reltol=1e-4, abstol=1e-4);

    tend = odeprob_new.tspan[2];
    return_val = sol(tend)[1] + sol(tend)[2];

    return return_val
end

u0 = [0.0, 0.0];
tspan = (0.0, 10.0);
p = [2.0,3.0];

odeprob = ODEProblem(ODE_tmp!, u0, tspan, p);

run_sim(p,odeprob) # Runs ok

ForwardDiff.gradient(x -> run_sim(x, odeprob), p) # Error with Rodas4P but not Tsit5()

```

---

<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:** [September 23, 2025, 8:20pm UTC](https://discourse.julialang.org/t/forwarddiff-with-rosenbrock-in-differentialequations-jl/132605/2 "2025-09-23T20:20:41Z")

</div>

What package versions are you using? Have you tried updating them?

Also as a side note, `Rodas5P` is usually faster than `Rodas4P`. It’s probably worth switching (although it won’t change anything here).

---

<div class="post-metadata">

**Author:** ![Bill\_Holmes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bill_holmes/32/47921_2.png) [@Bill\_Holmes](https://discourse.julialang.org/u/Bill_Holmes)\
**Post date:** [September 23, 2025, 8:51pm UTC](https://discourse.julialang.org/t/forwarddiff-with-rosenbrock-in-differentialequations-jl/132605/3 "2025-09-23T20:51:39Z")

</div>

Thanks for the response. The proverbial turn it off / on trick! My environment and all packages were about a year old. Updating corrected the issue.

Thank you for taking the time. I really appreciate that you all respond to these!
