# How to autodifferentiate the results of NLsolve?

**URL:** https://discourse.julialang.org/t/how-to-autodifferentiate-the-results-of-nlsolve/65680
**Category:** General Usage
**Tags:** autodiff
**Created:** [August 2, 2021, 1:22pm UTC](https://discourse.julialang.org/t/how-to-autodifferentiate-the-results-of-nlsolve/65680 "2021-08-02T13:22:48Z")
**Posts on this page:** 5
**Page:** 2

<div class="post-metadata">

### Author: ![elbert5770](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elbert5770/32/18646_2.png) [@elbert5770](https://discourse.julialang.org/u/elbert5770)
#### Post date: [March 1, 2023, 12:38am UTC](https://discourse.julialang.org/t/how-to-autodifferentiate-the-results-of-nlsolve/65680/21 "2023-03-01T00:38:50Z")

</div>

Addendum: use: u0\_ = eltype(p).(u0) instead of u0\_ = ones(eltype(p)).\*u0

---

<div class="post-metadata">

### Author: ![rveltz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rveltz/32/2707_2.png) [@rveltz](https://discourse.julialang.org/u/rveltz)
#### Post date: [March 1, 2023, 7:14am UTC](https://discourse.julialang.org/t/how-to-autodifferentiate-the-results-of-nlsolve/65680/22 "2023-03-01T07:14:05Z")

</div>

There is also [ImplicitDifferentiation.jl](https://gdalle.github.io/ImplicitDifferentiation.jl/dev/) which nicely takes care of registering the rules. It works with `Zygote` but support for `ForwardDiff` was recently added. See also this [discourse post](https://discourse.julialang.org/t/custom-rule-for-differentiating-through-newton-solver-using-forwarddiff-works-for-gradient-fails-for-hessian/93002)

---

<div class="post-metadata">

### Author: ![elbert5770](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elbert5770/32/18646_2.png) [@elbert5770](https://discourse.julialang.org/u/elbert5770)
#### Post date: [March 6, 2023, 4:40am UTC](https://discourse.julialang.org/t/how-to-autodifferentiate-the-results-of-nlsolve/65680/23 "2023-03-06T04:40:25Z")

</div>

Further addendum, in the example with Diffrules, dxdp is never called even though the Diffrule exists. ForwardDiff must just be working its way through nlsolve. Will update if I figure out why the diffrule is not used.

---

<div class="post-metadata">

### Author: ![elbert5770](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elbert5770/32/18646_2.png) [@elbert5770](https://discourse.julialang.org/u/elbert5770)
#### Post date: [March 6, 2023, 4:41am UTC](https://discourse.julialang.org/t/how-to-autodifferentiate-the-results-of-nlsolve/65680/24 "2023-03-06T04:41:37Z")

</div>

I’ll give this a try.

---

<div class="post-metadata">

### Author: ![elbert5770](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elbert5770/32/18646_2.png) [@elbert5770](https://discourse.julialang.org/u/elbert5770)
#### Post date: [March 8, 2023, 10:20pm UTC](https://discourse.julialang.org/t/how-to-autodifferentiate-the-results-of-nlsolve/65680/25 "2023-03-08T22:20:44Z")

</div>

The reason the answers aren’t the same is that ForwardDiff.jacobian(cd,[1.0,1.0]) is ignoring the DiffRule and just injecting itself right through the nlsolve. You can see this if you put a nonsense symbol in the dxdp function that should crash the function if called. It only crashes when dxdp() is called.

[Previous page](https://discourse.julialang.org/t/how-to-autodifferentiate-the-results-of-nlsolve/65680.md?page=1)
