How to autodifferentiate the results of NLsolve?

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

There is also ImplicitDifferentiation.jl which nicely takes care of registering the rules. It works with Zygote but support for ForwardDiff was recently added. See also this discourse post

2 Likes

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.

I’ll give this a try.

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.