# Lagrangian Function

**URL:** <https://discourse.julialang.org/t/lagrangian-function/38287>\
**Category:** Optimization (Mathematical)\
**Created:** [April 27, 2020, 8:01am UTC](https://discourse.julialang.org/t/lagrangian-function/38287 "2020-04-27T08:01:26Z")\
**Posts on this page:** 1\
**Showing post:** 19

<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:** [April 27, 2020, 1:04pm UTC](https://discourse.julialang.org/t/lagrangian-function/38287/19 "2020-04-27T13:04:43Z")

</div>

Note that, given any optimization algorithm, you can easily compute the Lagrange multipliers yourself given the optimum — it’s just a system of linear equations (from the KKT equations) given the gradients of the objective and constraints at the optimum, which you can solve with a backslash operation in Julia.

For example, if you are minimizing f\_0(x) subject to h\_i(x)=0 for i=1\ldots p, and your optimization algorithm gives you an approximate minimum x^\*, then to find the Lagrange multipliers \nu\_i for your constraints you would solve

\nu = \begin{bmatrix} \nabla h\_1 & \cdots & \nabla h\_m \end{bmatrix} \setminus -\nabla f\_0 

where we have a matrix whose columns are the constraint gradients (evaluated at x^\*) and `\` denotes the Julia backslash operation, which will solve the equations in the least-square sense assuming you have fewer constraints than variables x (this is necessary since x^\* will only be an approximate minimum).

If you have inequality constraints, it is the same thing except that you need only include active constraints in your linear system; the Lagrange multipliers of inactive constraints are zero (“complementary slackness”).

---

_[View the full topic](https://discourse.julialang.org/t/lagrangian-function/38287)._
