# Expressions for partial derivatives using AD tools

**URL:** <https://discourse.julialang.org/t/expressions-for-partial-derivatives-using-ad-tools/45741>\
**Category:** Optimization (Mathematical)\
**Created:** [August 29, 2020, 12:53pm UTC](https://discourse.julialang.org/t/expressions-for-partial-derivatives-using-ad-tools/45741 "2020-08-29T12:53:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![JLHTM](https://avatars.discourse-cdn.com/v4/letter/j/bbce88/32.png) [@JLHTM](https://discourse.julialang.org/u/JLHTM)\
**Post date:** [August 29, 2020, 12:53pm UTC](https://discourse.julialang.org/t/expressions-for-partial-derivatives-using-ad-tools/45741/1 "2020-08-29T12:53:50Z")

</div>

I have two bivariate functions (more generally, n n-variable functions) f\_1(x\_1,x\_2) and f\_2(x\_1,x\_2). I need to solve the following system of equations: \frac{\partial f\_1}{\partial x\_1} = 0 and \frac{\partial f\_2}{\partial x\_2} = 0 (the context here is a Cournot-type duopoly model). The functions are too complex to differentiate by hand, so I would like to use AD tools to solve this. However, **I cannot come up with a good way to grab the partial derivatives and use them as expressions inside tools such as NLsolve or JuMP. What would be the correct way to do this?**

My current approach is to calculate the Jacobian matrix, and point to the corresponding entries when solving the system. This gives me the correct solution, but feels more like a clumsy workaround than an actual solution, in particular for larger problems.

```julia
using ForwardDiff
using NLsolve

f1(x1,x2) = x1*(50-2*(x1+x2))-10-2x1
f2(x1,x2) = x2*(50-2*(x1+x2))-10-2x2

function jac(f1,f2,a)
       F(x1,x2) = [f1(x1,x2), f2(x1,x2)]
       return ForwardDiff.jacobian(x -> F(x[1],x[2]),a)
end

function f!(F,x)
    F[1] = jac(f1,f2,x)[1,1] # df1/dx1 == 0
    F[2] = jac(f1,f2,x)[2,2] # df2/dx2 == 0
end

x0 = [5.,5.] # Initial guess

res = nlsolve(f!, x0)
println(res.zero) # Correct solution: [8.0,8.0]

```

The example above is from: [http://faculty.econ.ucdavis.edu/faculty/bonanno/teaching/200C/Cournot.pdf](http://faculty.econ.ucdavis.edu/faculty/bonanno/teaching/200C/Cournot.pdf)

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [August 29, 2020, 1:12pm UTC](https://discourse.julialang.org/t/expressions-for-partial-derivatives-using-ad-tools/45741/2 "2020-08-29T13:12:44Z")

</div>

Use a closure. Eg \partial f\_1 / \partial x\_1 at x\_1, x\_2 is

```julia
ForwardDiff.derivative(x1 -> f1(x1, x2), x1)

```

---

<div class="post-metadata">

**Author:** ![JLHTM](https://avatars.discourse-cdn.com/v4/letter/j/bbce88/32.png) [@JLHTM](https://discourse.julialang.org/u/JLHTM)\
**Post date:** [August 29, 2020, 1:53pm UTC](https://discourse.julialang.org/t/expressions-for-partial-derivatives-using-ad-tools/45741/3 "2020-08-29T13:53:51Z")

</div>

Ah, nice and simple. Thank you. This does the trick.
