# Numerical or Auto Differentiation Noob question

Suppose I have a function which calls a nonlinear solver.
For example,

function sqrte(y)
f(x) = x^2 - y
x0 = [0.1]
return nlsolve(f, x0).zero
end

sqrte([4.0])
> 1-element Vector{Float64}:
2.0000


What is the recommended way to get a function sqrte_derivative(y) that gives the derivative of sqrte_derivative at y?

I am new to using derivatives in numerical coding.

In this simple example, the derivative is \partial(\mathrm{sqrte})/\partial y = -(\partial f/\partial x)^{-1} (\partial f / \partial y) (evaluated at the root x = nlsolve(f, x0).zero).

See section 3 of these notes for the underlying math and the generalization to many variables.

There are also packages to help you with this, e.g. GitHub - gdalle/ImplicitDifferentiation.jl: Automatic differentiation of implicit functions

7 Likes

Ok so I’ve created a function that calculates the derivative of the square root using the implicit function theorem formula.

function sqrte_diff(x)
y = sqrte(x)
y_prime_of_x = -(1/F_y(x,y))*F_x(x,y)
return y_prime_of_x
end
sqrte_diff(4.0)
> 0.24999999999999972
sqrte_diff(4.0)  -  (x -> sqrt(x))'(4.0)
> 2e-16  # Success!


But I’m having a bit of trouble with the ImplicitDifferentiation package though. Can someone suggest how to use that package for this square root example? Based on the docs, which use a more complicated example, it looks like the first steps are something like the following, but I’m not sure where to go next:

using ImplicitDifferentiation
function fixed_point_conditions(x,y)
return x^2 - y
end

differentiable_sqrte = ImplicitDifferentiation.ImplicitFunction(
sqrte, fixed_point_conditions)


Hey there! I answered over here on the GitHub issue. Don’t hesitate to ask if you have further questions!

1 Like