# Using NonlinearSolve with autodiff

**URL:** https://discourse.julialang.org/t/using-nonlinearsolve-with-autodiff/121333
**Category:** Optimization (Mathematical)
**Tags:** zygote, autodiff, enzyme, nonlinearsolve
**Created:** [October 15, 2024, 2:17pm UTC](https://discourse.julialang.org/t/using-nonlinearsolve-with-autodiff/121333 "2024-10-15T14:17:56Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![albertomercurio](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albertomercurio/32/27051_2.png) [@albertomercurio](https://discourse.julialang.org/u/albertomercurio)
#### Post date: [October 15, 2024, 2:17pm UTC](https://discourse.julialang.org/t/using-nonlinearsolve-with-autodiff/121333/1 "2024-10-15T14:17:57Z")

</div>

Hello,

I was trying to test NonlinearSolve.jl with automatic differentiation like Enzyme.jl or Zygote.jl. But the following simple code fails

```julia
f(x, p) = p.a * x^2 - p.b
prob = NonlinearProblem(f, 0.0, (a=1.0, b=2.0))
solve(prob, NewtonRaphson(autodiff=AutoEnzyme()))

```

It returns zero, which is not the correct solution. Using `NewtonRaphson()` works.

---

<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: [October 15, 2024, 2:43pm UTC](https://discourse.julialang.org/t/using-nonlinearsolve-with-autodiff/121333/2 "2024-10-15T14:43:24Z")

</div>

I think it might be due to you using `u0 = 0.0` instead of `u0 = [0.0]`. I can’t tell from the docs if the scalar form is supported in the general `NonlinearProblem` (but you can [use StaticArrays to speed things up](https://docs.sciml.ai/NonlinearSolve/stable/tutorials/code_optimization/#Further-Optimizations-for-Small-Nonlinear-Solves-with-Static-Arrays-and-SimpleNonlinearSolve))

---

<div class="post-metadata">

### Author: ![tomaklutfu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomaklutfu/32/2411_2.png) [@tomaklutfu](https://discourse.julialang.org/u/tomaklutfu)
#### Post date: [October 15, 2024, 3:47pm UTC](https://discourse.julialang.org/t/using-nonlinearsolve-with-autodiff/121333/3 "2024-10-15T15:47:05Z")

</div>

`0` looks to be a local minima. The difference could be finite differentiation vs exact one.

---

<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: [October 15, 2024, 4:30pm UTC](https://discourse.julialang.org/t/using-nonlinearsolve-with-autodiff/121333/4 "2024-10-15T16:30:05Z")

</div>

Enzyme and Zygote perform exact differentiation, so I don’t think that’s it

---

<div class="post-metadata">

### Author: ![isaacsas](https://avatars.discourse-cdn.com/v4/letter/i/f6c823/32.png) [@isaacsas](https://discourse.julialang.org/u/isaacsas)
#### Post date: [October 15, 2024, 5:46pm UTC](https://discourse.julialang.org/t/using-nonlinearsolve-with-autodiff/121333/5 "2024-10-15T17:46:41Z")

</div>

Note that f'(x\_0) = 0 here so you might want to use another initial guess. Even using the in-place version I get that `NewtonRaphson` stalls on this problem.

---

<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: [October 15, 2024, 6:49pm UTC](https://discourse.julialang.org/t/using-nonlinearsolve-with-autodiff/121333/6 "2024-10-15T18:49:27Z")

</div>

> [@gdalle](#):
>
> I think it might be due to you using `u0 = 0.0` instead of `u0 = [0.0]`. I can’t tell from the docs if the scalar form is supported in the general `NonlinearProblem` (but you can [use StaticArrays to speed things up](https://docs.sciml.ai/NonlinearSolve/stable/tutorials/code_optimization/#Further-Optimizations-for-Small-Nonlinear-Solves-with-Static-Arrays-and-SimpleNonlinearSolve))

All solvers support a scalar form with the out of place version. It uses the same code path as static arrays.

Though generally for a nonlinear solve, if it’s scalar, SimpleNonlinearSolve.jl will be a little bit faster.

---

<div class="post-metadata">

### Author: ![tomaklutfu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomaklutfu/32/2411_2.png) [@tomaklutfu](https://discourse.julialang.org/u/tomaklutfu)
#### Post date: [October 16, 2024, 5:04am UTC](https://discourse.julialang.org/t/using-nonlinearsolve-with-autodiff/121333/7 "2024-10-16T05:04:09Z")

</div>

Why? Since they are exact, they stuck in gradient 0 for Newton-Raphson algorithm. On the other hand, if finite difference(except maybe when central diff used) is used, it can have nonzero gradient.

---

<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: [October 16, 2024, 5:11am UTC](https://discourse.julialang.org/t/using-nonlinearsolve-with-autodiff/121333/8 "2024-10-16T05:11:04Z")

</div>

Oh right, my bad. Usually, when people say “finite differentiation vs exact” they either mean “autodiff is performing finite differentiation” (which is wrong) or “exact differentiation would be better” (which in this case happens to also be wrong due to the flat gradient) but you meant neither of those ^^

---

<div class="post-metadata">

### Author: ![tomaklutfu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomaklutfu/32/2411_2.png) [@tomaklutfu](https://discourse.julialang.org/u/tomaklutfu)
#### Post date: [October 16, 2024, 5:29am UTC](https://discourse.julialang.org/t/using-nonlinearsolve-with-autodiff/121333/9 "2024-10-16T05:29:22Z")

</div>

My comparison was for OPs getting 2 results, one from exact derivatives using autodiff and the other without autodiff in the last sentence but, in retrospect, I ordered my saying the other way around.

---

<div class="post-metadata">

### Author: ![albertomercurio](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albertomercurio/32/27051_2.png) [@albertomercurio](https://discourse.julialang.org/u/albertomercurio)
#### Post date: [October 17, 2024, 9:18pm UTC](https://discourse.julialang.org/t/using-nonlinearsolve-with-autodiff/121333/10 "2024-10-17T21:18:38Z")

</div>

Thanks! That was just a wrong initial condition, together with the use of “exact” differentiation.
