# Numerical or Auto Differentiation Noob question

**URL:** <https://discourse.julialang.org/t/numerical-or-auto-differentiation-noob-question/85750>\
**Category:** General Usage\
**Tags:** differentiation\
**Created:** [August 15, 2022, 12:12am UTC](https://discourse.julialang.org/t/numerical-or-auto-differentiation-noob-question/85750 "2022-08-15T00:12:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![samerb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/samerb/32/9242_2.png) [@samerb](https://discourse.julialang.org/u/samerb)\
**Post date:** [August 15, 2022, 12:12am UTC](https://discourse.julialang.org/t/numerical-or-auto-differentiation-noob-question/85750/1 "2022-08-15T00:12:04Z")

</div>

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

```julia
function sqrte(y)
    f(x) = x[1]^2 - y[1]
    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.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [August 15, 2022, 1:34am UTC](https://discourse.julialang.org/t/numerical-or-auto-differentiation-noob-question/85750/2 "2022-08-15T01:34:55Z")

</div>

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](https://math.mit.edu/~stevenj/18.336/adjoint.pdf) 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](https://github.com/gdalle/ImplicitDifferentiation.jl)

---

<div class="post-metadata">

**Author:** ![samerb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/samerb/32/9242_2.png) [@samerb](https://discourse.julialang.org/u/samerb)\
**Post date:** [August 15, 2022, 7:01pm UTC](https://discourse.julialang.org/t/numerical-or-auto-differentiation-noob-question/85750/3 "2022-08-15T19:01:39Z")

</div>

> [@stevengj](#):
>
> GitHub - gdalle/ImplicitDifferentiation.jl: Automatic differentiation of implicit functions

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

```julia
function sqrte_diff(x)
    F_x(x_new,y) = gradient(g,x_new,y)[1]
    F_y(x_new,y) = gradient(g,x_new,y)[2]
    y = sqrte(x[1])[1]
    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:

```julia
using ImplicitDifferentiation
function fixed_point_conditions(x,y)
    return x[1]^2 - y[1]
end

differentiable_sqrte = ImplicitDifferentiation.ImplicitFunction(
    sqrte, fixed_point_conditions)

```

---

<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:** [August 16, 2022, 3:24pm UTC](https://discourse.julialang.org/t/numerical-or-auto-differentiation-noob-question/85750/4 "2022-08-16T15:24:38Z")

</div>

Hey there! I answered over here on the [GitHub issue](https://github.com/gdalle/ImplicitDifferentiation.jl/issues/17#issuecomment-1216789508). Don’t hesitate to ask if you have further questions!
