# Specifying analytical jacobian in NonlinearProblem

**URL:** <https://discourse.julialang.org/t/specifying-analytical-jacobian-in-nonlinearproblem/112932>\
**Category:** Modelling & Simulations\
**Tags:** nonlinearsolve\
**Created:** [April 14, 2024, 6:18pm UTC](https://discourse.julialang.org/t/specifying-analytical-jacobian-in-nonlinearproblem/112932 "2024-04-14T18:18:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![askprash](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/askprash/32/49806_2.png) [@askprash](https://discourse.julialang.org/u/askprash)\
**Post date:** [April 14, 2024, 6:18pm UTC](https://discourse.julialang.org/t/specifying-analytical-jacobian-in-nonlinearproblem/112932/1 "2024-04-14T18:18:36Z")

</div>

Hi! I’m new to using `NonlinearSolve.jl` and trying to understand how to provide an analytical jacobian to the problem. I realize that this is unnecessary if we have autodiff in the mix since that will be enough - but in cases where I have say registered functions (and corresponding registered jacobians) I want to understand how to explicitly supply the jacobian to the problem.

Here’s what I have as an example:

```julia
# Starting with a simple example
function f(u, p)
    @. u*u - p
end
u0 = [1.0, 1.0, 1.0]
p = [2.0, 1.0, 4.0]
prob = NonlinearProblem(f, u0, p)

```

```julia
julia> sol = solve(prob, NewtonRaphson())
retcode: Success
u: 3-element Vector{Float64}:
 1.4142135623730951
 1.0
 2.000000000000002

```

Now if I try to give it the jacobian and setup a `NonlinearFunction` (was not very sure how else to setup a `NonlinearProblem` with a jacobian than doing this):

```julia
df(u,p) = 2.0 .* u
h = NonlinearFunction(f, jac = df)
prob = NonlinearProblem(h, u0, p)

```

If I try solving this problem now I get this:

```julia
julia> sol = solve(prob, NewtonRaphson())
retcode: Unstable
u: 3-element Vector{Float64}:
 1.0
 1.0
 1.0

```

And looking at [this](https://docs.sciml.ai/NonlinearSolve/stable/native/solvers/#NonlinearSolve.NewtonRaphson) I tried specifying to the `NewtonRaphson` algorithm `concrete_jac = true` but that still does not seem to help.

```julia
julia> sol = solve(prob, NewtonRaphson( ;concrete_jac = true))
retcode: Unstable
u: 3-element Vector{Float64}:
 1.0
 1.0
 1.0

```

All this is using the latest `NonlinearSolve` version:

```julia
  [8913a72c] NonlinearSolve v3.9.1

```

Any thoughts or suggestions would be very appreciated!

---

<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 14, 2024, 7:41pm UTC](https://discourse.julialang.org/t/specifying-analytical-jacobian-in-nonlinearproblem/112932/2 "2024-04-14T19:41:40Z")

</div>

> [@askprash](#):
>
> `df(u,p) = 2.0 .* u`

That’s not outputing a matrix. Did you mean `Diagonal(2.0 .* u)`?

---

<div class="post-metadata">

**Author:** ![askprash](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/askprash/32/49806_2.png) [@askprash](https://discourse.julialang.org/u/askprash)\
**Post date:** [April 14, 2024, 8:14pm UTC](https://discourse.julialang.org/t/specifying-analytical-jacobian-in-nonlinearproblem/112932/3 "2024-04-14T20:14:54Z")

</div>

That was silly! Thanks!

---

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [November 13, 2024, 10:03pm UTC](https://discourse.julialang.org/t/specifying-analytical-jacobian-in-nonlinearproblem/112932/4 "2024-11-13T22:03:27Z")

</div>

2 posts were split to a new topic: [NonlinearSolve uses AD Jacobian even when I specify it analytically](https://discourse.julialang.org/t/nonlinearsolve-uses-ad-jacobian-even-when-i-specify-it-analytically/122608)
