flowchart LR
A("2(x-2) = 2λx <br> 2y = -10λy <br> x²-5y² = 1")
A --> B("2(x-λx-2) = 0 <br> 2y (1+5λ) = 0 <br> x²-5y² = 1")
B -->|"y=0"| C("2(x-λx-2) = 0 <br> x² = 1")
B -->|"λ=-1/5"| D("2(6x/5-2) = 0 <br> x²-5y² = 1")
C -->|"x=1"| E("-2(1+λ) = 0")
C -->|"x=-1"| F("2(-3+λ) = 0")
E --> G("(x,y,λ) = (1,0,-1)"):::solution
F --> H("(x,y,λ) = (-1,0,3)"):::solution
D --> I("x = 5/3 <br> 25/9-5y² = 1")
I --> J("(x,y,λ) = (5/3, -4/(3√5), -1/5)"):::solution
I --> K("(x,y,λ) = (5/3, 4/(3√5), -1/5)"):::solution
%% GENERATED from _cells.scss and _palette.scss by mermaid-classes.py; do not edit.
%% Node classes: solution, contradiction, engine, source, auxiliary, log, final.
%%{init: {"themeCSS": ".node.solution :is(rect, circle, polygon, path, ellipse) { fill: color-mix(in srgb, var(--ctp-green, #40a02b) 15%, var(--ctp-base, #eff1f5)) !important; stroke: var(--ctp-green, #40a02b) !important; stroke-width: 2px !important; } .node.contradiction :is(rect, circle, polygon, path, ellipse) { fill: color-mix(in srgb, var(--ctp-red, #d20f39) 15%, var(--ctp-base, #eff1f5)) !important; stroke: var(--ctp-red, #d20f39) !important; stroke-width: 3px !important; } .node.engine :is(rect, circle, polygon, path, ellipse) { fill: color-mix(in srgb, var(--ctp-red, #d20f39) 15%, var(--ctp-base, #eff1f5)) !important; stroke: var(--ctp-red, #d20f39) !important; stroke-width: 2px !important; } .node.source :is(rect, circle, polygon, path, ellipse) { fill: color-mix(in srgb, var(--ctp-blue, #1e66f5) 15%, var(--ctp-base, #eff1f5)) !important; stroke: var(--ctp-blue, #1e66f5) !important; stroke-width: 2px !important; } .node.auxiliary :is(rect, circle, polygon, path, ellipse) { fill: color-mix(in srgb, var(--ctp-peach, #fe640b) 15%, var(--ctp-base, #eff1f5)) !important; stroke: var(--ctp-peach, #fe640b) !important; stroke-dasharray: 4 4 !important; stroke-width: 2px !important; } .node.log :is(rect, circle, polygon, path, ellipse) { fill: color-mix(in srgb, var(--ctp-mauve, #8839ef) 15%, var(--ctp-base, #eff1f5)) !important; stroke: var(--ctp-mauve, #8839ef) !important; stroke-dasharray: 4 4 !important; stroke-width: 2px !important; } .node.final :is(rect, circle, polygon, path, ellipse) { fill: color-mix(in srgb, var(--ctp-green, #40a02b) 20%, var(--ctp-base, #eff1f5)) !important; stroke: var(--ctp-green, #40a02b) !important; stroke-width: 2.5px !important; } .label, .nodeLabel, .cluster span { font-family: var(--mermaid-font-family, inherit); font-weight: var(--mermaid-font-weight, inherit); }"}}%%
21 Lagrange Multipliers II
21.1 Lagrangian formulation
Remark 21.1. The Lagrangian combines the objective function \(f\) and the constraint function \(g\):
\[ \mathcal{L}(x_1,\dots,x_n,\lambda) = f(x_1,\dots,x_n) - \lambda ( g(x_1,\dots,x_n) - c ). \]
This auxiliary function is useful because its critical points encode the \(n+1\) Lagrange equations:
\[ \Gradient{\mathcal{L}} = \langle f_{x_1} - \lambda g_{x_1}, \cdots, f_{x_n} - \lambda g_{x_n}, c - g \rangle. \]
The critical points (the stationary points) of the Lagrangian are the solutions to our optimization problem.
Method of Lagrange multipliers (Stewart et al. 2020, 14.8.1)
We want to optimize \(f(x_1,\dots,x_n)\) subject to a constraint \(g(x_1,\dots,x_n) = c\).
- Compute the gradient of the Lagrangian \(\mathcal{L}(x_1,\dots,x_n,\lambda) = f - \lambda (g-c)\),
- Set \(\nabla \mathcal{L} = 0\) (a system of equations) and solve to find its stationary points, and
- Plug the stationary points into \(f(x_1,\dots,x_n)\) to determine extreme values.
If you prefer to solve constrained optimization problems without using the Lagrangian formulation, that’s fine!
21.2 More examples
Example 21.1 (The nearest point on \(x^2 - 5 y^2 = 1\) to \(P = (2,0)\).) We want to find the nearest point on a hyperbola to a given point \(P\). We take \(f(x,y) = (x - 2)^2 + y^2\) (since the square of a function, namely the distance function, will inherit its critical points), \(g(x,y) = x^2 - 5 y^2\), and \(c = 1\).
We can check our work with Sage, using the Lagrangian formulation to simplify writing down the equations.
var('x y λ')
f = (x - 2)^2 + y^2
g = x^2 - 5*y^2
c = 1
L(x,y,λ) = f - λ*(g-c)
gradL = list(L.gradient())
solutions = solve(gradL, [x, y, λ])
real_solutions = [sol for sol in solutions
if all(eq.rhs() in RR for eq in sol)]
print("Stationary points:")
for sol in real_solutions:
val = f.subs(sol)
print(f"f = {val} when {sol}")Stationary points:
f = 9 when [x == -1, y == 0, λ == 3]
f = 1 when [x == 1, y == 0, λ == -1]
f = 7/15 when [x == (5/3), y == 4/15*sqrt(5), λ == (-1/5)]
f = 7/15 when [x == (5/3), y == -4/15*sqrt(5), λ == (-1/5)]
Writing list() in the above converts the result of L.gradient() into an object which Sage interprets as each entry as being set equal to \(0\). Regardless of how we solve, there are two absolute minima (\(f=\frac{7}{15}\)), and all are visualized below in red:
constraint_curve = implicit_plot(
g == c,
(x, -2.5, 2.5),
(y, -2.5, 2.5),
color='cyan',
zorder=1
)
stationary_points = point([
(x.subs(sol), y.subs(sol)) for sol in real_solutions],
color='red',
size=50,
zorder=2
)
P = point((2,0), color='black', size=40)
show(constraint_curve + stationary_points + P)
What is the significance of the points that did not give the absolute minimum?
Example 21.2 (\(f(x,y) = x y e^{-x^2 - y^2}\), subject to \(g(x,y) = 2 x - y\) and \(c = 0\).) We have \[ \begin{aligned} \Gradient{f} & = \Vector{(1-2x^2) y e^{-x^2 - y^2}, (1-2y^2) x e^{-x^2 - y^2}} \\ \Gradient{g} & = \Vector{2, -1}. \end{aligned} \]
Since the constraint is quite simple, a nice trick is to rearrange \(g = c\) to get \(y = 2x\). Then we can substitute into the other Lagrange equations: \[ e^{-5x^2} \langle 2x(2x^2-1), x(4x^2-1) \rangle = \lambda \langle 2, -1 \rangle. \] Solving for \(\lambda\) in both equations and equating gives \(x(2x^2-1) e^{-5x^2} = x(1-8x^2) e^{-5x^2}.\) Hence we have \[ x(2x^2-1) = x(1-8x^2) \implies 2 x (5 x^2 - 1) = 0 \implies x = 0 \text{ or } x = \pm \tfrac{1}{\sqrt{5}}. \] Thus the stationary points of the Lagrangian are: \[ (x,y,\lambda) = (0, 0, 0), (\tfrac{1}{\sqrt{5}}, \tfrac{2}{\sqrt{5}}, \tfrac{3}{5 \sqrt{5} e}), (-\tfrac{1}{\sqrt{5}}, -\tfrac{2}{\sqrt{5}}, -\tfrac{3}{5 \sqrt{5} e}). \] The first is the minimum (\(f=0\)) and the last two are maxima (\(f=\frac{2}{5e}\)).
Just to remind ourselves what Lagrange multipliers do, let’s plot the gradients of our objective \(f\) and constraint \(g\) and see how these line up with our stationary points:
