Obtain jacobian used by NLsolve

Hello, I have a function, let’s call it f!, whose zero I’m trying to find by using the Newton method. I know that the package NLsolve allows me to do this either giving it the jacobian, with something of the form
nlsolve(f!, j!, initial_x),
or just allow it to calculate the jacobian itself, with
nlsolve(f!, initial_x).
My issue is that I have a function j! that calculates the jacobian, but I seem to have a mistake on the jacobian because when I use mine it does not converge, but when I let NLsolve calculate it’s own jacobian it functions perfectly. I would like to be able to retrieve the jacobian calculated by the function nlsolve, to compare it with my own and find mistakes, is there a way to do so?.

To find an error in your code you don’t need the Jacobian computed by NLsolve.jl specifically: any library computing the right Jacobian will do.
Depending on what your function looks like, various autodiff frameworks like ForwardDiff.jl or Zygote.jl could work, but your safest bet is probably FiniteDifferences.jl

Thanks, I found FiniteDifferences shortly before your answer and it did the trick. I tried using ForwardDiff before but it just didn’t work for me, the function uses auxiliary vectors and I had a lot of trouble propagating the Dual types inside it.

1 Like