If you're seeing this message, it means we're having trouble loading external resources on our website.

If you're behind a web filter, please make sure that the domains *.kastatic.org and *.kasandbox.org are unblocked.

## Multivariable calculus

### Course: Multivariable calculus>Unit 3

Lesson 6: Constrained optimization (articles)

# Lagrange multipliers, introduction

The "Lagrange multipliers" technique is a way to solve constrained optimization problems.  Super useful!

## What we're building to:

• The Lagrange multiplier technique lets you find the maximum or minimum of a multivariable function $f\left(x,y,\dots \right)$ when there is some constraint on the input values you are allowed to use.
• This technique only applies to constraints that look something like this:
$g\left(x,y,\dots \right)=c$
Here, $g$ is another multivariable function with the same input space as $f$, and $c$ is some constant.
• The core idea is to look for points where the contour lines of $f$ and $g$ are tangent to each other.
• This is the same as finding points where the gradient vectors of $f$ and $g$ are parallel to each other.
• The entire process can be boiled down into setting the gradient of a certain function, called the Lagrangian, equal to the zero vector.

## Motivating example

Suppose you want to maximize this function:
$f\left(x,y\right)=2x+y$
But let's also say you limited yourself to inputs $\left(x,y\right)$ which satisfy the following equation:
${x}^{2}+{y}^{2}=1$
In other words, for which point $\left(x,y\right)$ on the $\text{unit circle}$ is the value $2x+y$ biggest?
This is what's known as a constrained optimization problem. The restriction to points where ${x}^{2}+{y}^{2}=1$ is called a "constraint", and $f\left(x,y\right)=2x+y$ is the function that needs to be optimized.
Here's one way to visualize this: First draw the graph of $f\left(x,y\right)$, which looks like a slanted plane since $f$ is linear. Next, project the circle ${x}^{2}+{y}^{2}=1$ from the $xy$-plane vertically onto the graph of $f$. The maximum we are seeking corresponds with the highest point of this projected circle on the graph.

## More general form

In general, constrained optimization problems involve maximizing/minimizing a multivariable function whose input has any number of dimensions:
$f\left(x,y,z,\dots \right)$
Its output will always be one-dimensional, though, since there's not a clear notion of "maximum" with vector-valued outputs.
The type of constraints that the Lagrange multiplier technique applies to must take the form of some other multivariable function $g\left(x,y,z,\dots \right)$ being set equal to a constant $c$.
$g\left(x,y,z,\dots \right)=c$
Since this is meant to be a constraint on the input of $f$, the number of dimensions in the input of $g$ is the same as that of $f$. For example, the example outlined above fits this general form as follows:
$f\left(x,y\right)=2x+y$
$g\left(x,y\right)={x}^{2}+{y}^{2}$
$c=1$

## Using contour maps

Reasoning about this problem becomes easier if we visualize $f$ not with a graph, but with its contour lines.
As a reminder, a contour line of $f\left(x,y\right)$ is the set of all points where $f\left(x,y\right)=k$ for some constant $k$. The following interactive tool shows how this line (drawn in blue) changes as the constant $k$ changes. The circle $g\left(x,y\right)=1$ is also shown (in red). Try to make $k$ as big/small as possible while still allowing contour line of $f$ to intersect the circle.
Concept check: What does it mean if for a particular value of $k$, the blue line representing $f\left(x,y\right)=k$ does not intersect the red circle representing $g\left(x,y\right)=1$?

Notice, the circle where $g\left(x,y\right)=1$ can be thought of as a particular contour line of the function $g$. So with that, here's the clever way to think about constrained optimization problems:
Key observation: The maximum and minimum values of $f$, subject to the constraint $g\left(x,y\right)=1$, correspond with contour lines of $f$ that are tangent to the contour representing $g\left(x,y\right)=1$.
If $f$ were a different function, its contours might not always be straight lines. This is unique to our example since $f$ is linear. For example, take a look at this function:
$f\left(x,y\right)=2{x}^{2}+\sqrt{5y}$,
Its contour lines look like this:
That said, the key observation still holds, and is worth repeating: When $k$ is a maximum or minimum of $f$ subject to the constraint, the contour line for $f\left(x,y\right)=k$ will be tangent to contour representing $g\left(x,y\right)=1$.

## Where the gradient comes into play

How do you put the idea of two contour lines being tangent into a formula you can solve?
To answer this, we turn to our loyal friend the gradient. There are many ways to interpret $\mathrm{\nabla }f$: The direction of steepest ascent, a tool for computing directional derivatives, etc. But for our purposes here, the property we care about is that the gradient of $f$ evaluated at a point $\left({x}_{0},{y}_{0}\right)$ always gives a vector perpendicular to the contour line passing through that point.
This means when the contour lines of two functions $f$ and $g$ are tangent, their gradient vectors are parallel. Here's what that might look like for arbitrary functions $f$ and $g$:
The fact that contour lines are tangent tells us nothing about the magnitude of each of these gradient vectors, but that's okay. When two vectors point in the same direction, it means we can multiply one by some constant to get the other. Specifically, let $\left({x}_{0},{y}_{0}\right)$ represent a particular point where the contour lines of $f$ and $g$ are tangent (writing ${x}_{0}$ and ${y}_{0}$ with a $0$ subscripts just indicates that we are considering constant values, and hence a specific point). Since this tangency means their gradient vectors align, here's what you might write down:
$\begin{array}{r}\mathrm{\nabla }f\left({x}_{0},{y}_{0}\right)={\lambda }_{0}\mathrm{\nabla }g\left({x}_{0},{y}_{0}\right)\end{array}$
Here, ${\lambda }_{0}$ represents some constant. Some authors use a negative constant, $-{\lambda }_{0}$, but I personally prefer a positive constant, as it gives a cleaner interpretation of ${\lambda }_{0}$ down the road.
Let's see what this looks like in our example where $f\left(x,y\right)=2x+y$ and $g\left(x,y\right)={x}^{2}+{y}^{2}$. The gradient of $f$ is
$\begin{array}{r}\mathrm{\nabla }f\left(x,y\right)=\left[\begin{array}{c}\frac{\partial }{\partial x}\left(2x+y\right)\\ \\ \frac{\partial }{\partial y}\left(2x+y\right)\end{array}\right]=\left[\begin{array}{c}2\\ 1\end{array}\right]\end{array}$
and the gradient of $g$ is
$\begin{array}{r}\mathrm{\nabla }g\left(x,y\right)=\left[\begin{array}{c}\frac{\partial }{\partial x}\left({x}^{2}+{y}^{2}-1\right)\\ \\ \frac{\partial }{\partial y}\left({x}^{2}+{y}^{2}-1\right)\end{array}\right]=\left[\begin{array}{c}2x\\ 2y\end{array}\right]\end{array}$
Therefore, the tangency condition ends up looking like this:
$\begin{array}{r}\left[\begin{array}{c}2\\ 1\end{array}\right]={\lambda }_{0}\left[\begin{array}{c}2{x}_{0}\\ 2{y}_{0}\end{array}\right]\end{array}$

## Solving the problem in the specific case

To sum up where we are so far, we are looking for input points $\left({x}_{0},{y}_{0}\right)$ with the following properties:
• $g\left({x}_{0},{y}_{0}\right)=1$, which for our example means
$\phantom{\rule{1em}{0ex}}{x}_{0}^{2}+{y}_{0}^{2}=1$
• $\mathrm{\nabla }f\left({x}_{0},{y}_{0}\right)={\lambda }_{0}\mathrm{\nabla }g\left({x}_{0},{y}_{0}\right)$ for some constant ${\lambda }_{0}$, which for our example means
$\begin{array}{rl}\phantom{\rule{1em}{0ex}}2& =2{\lambda }_{0}{x}_{0}\\ 1& =2{\lambda }_{0}{y}_{0}\end{array}$
There are $3$ equations and $3$ unknowns, so this is a perfectly solvable situation.

## The Lagrangian function

In the 1700's, our buddy Joseph Louis Lagrange studied constrained optimization problems of this kind, and he found a clever way to express all of our conditions into a single equation.
You can write these conditions generally by saying we are looking for constants ${x}_{0}$, ${y}_{0}$ and ${\lambda }_{0}$ that satisfy the following conditions:
• The constraint:
$g\left({x}_{0},{y}_{0}\right)=c$
• The tangency condition:
$\mathrm{\nabla }f\left({x}_{0},{y}_{0}\right)={\lambda }_{0}\mathrm{\nabla }g\left({x}_{0},{y}_{0}\right)$.
This can be broken into its components as follows:
• ${f}_{x}\left({x}_{0},{y}_{0}\right)={\lambda }_{0}{g}_{x}\left({x}_{0},{y}_{0}\right)$
• ${f}_{y}\left({x}_{0},{y}_{0}\right)={\lambda }_{0}{g}_{y}\left({x}_{0},{y}_{0}\right)$
Lagrange wrote down a special new function which takes in all the same input variables as $f$ and $g$, along with the new kid in town $\lambda$, thought of now as a variable rather than a constant.
$\mathcal{L}\left(x,y,\lambda \right)=f\left(x,y\right)-\lambda \left(g\left(x,y\right)-c\right)$
For example, consider our example above.
$\begin{array}{rl}\phantom{\rule{1em}{0ex}}f\left(x,y\right)& =2x+y\\ g\left(x,y\right)& ={x}^{2}+{y}^{2}\\ c& =1\end{array}$
Here's how this new function would look:
$\mathcal{L}\left(x,y,\lambda \right)=2x+y-\lambda \left({x}^{2}+{y}^{2}-1\right).$
Notice, the partial derivative of $\mathcal{L}$ with respect to $\lambda$ is $-\left(g\left(x,y\right)-c\right)$:
$\begin{array}{rl}\phantom{\rule{1em}{0ex}}{\mathcal{L}}_{\lambda }\left(x,y,\lambda \right)& =\frac{\partial }{\partial \lambda }\left(f\left(x,y\right)-\lambda \left(g\left(x,y\right)-c\right)\\ & =0-\left(g\left(x,y\right)-c\right)\end{array}$
So we can translate the condition $g\left(x,y\right)=c$ as
$\begin{array}{r}\phantom{\rule{1em}{0ex}}{\mathcal{L}}_{\lambda }\left(x,y,\lambda \right)=-g\left(x,y\right)+c=0\end{array}$
What's more, look at what we get when we set one of the other partial derivatives equal to $0$:
$\begin{array}{rl}\phantom{\rule{1em}{0ex}}{\mathcal{L}}_{x}\left(x,y,\lambda \right)& =0\\ \\ \frac{\partial }{\partial x}\left(f\left(x,y\right)-\lambda \left(g\left(x,y\right)-c\right)\right)& =0\\ \\ {f}_{x}\left(x,y\right)-\lambda {g}_{x}\left(x,y\right)& =0\\ \\ {f}_{x}\left(x,y\right)& =\lambda {g}_{x}\left(x,y\right)\end{array}$
That just so happens to be another one of our conditions! Almost identically, the condition ${\mathcal{L}}_{y}\left(x,y,\lambda \right)=0$ unravels to become
$\begin{array}{r}\phantom{\rule{1em}{0ex}}{f}_{y}\left(x,y\right)=\lambda {g}_{y}\left(x,y\right)\end{array}$
Together, these conditions are the same as saying.
$\begin{array}{r}\phantom{\rule{1em}{0ex}}\mathrm{\nabla }f\left(x,y\right)=\lambda \mathrm{\nabla }g\left(x,y\right)\end{array}$
Therefore, the three conditions we need to solve to find $x,y$ and $\lambda$ come down to the various partial derivatives of $\mathcal{L}$ being equal to $0$. This can be written extremely compactly by setting the gradient of $\mathcal{L}$ equal to the zero vector:
$\begin{array}{r}\phantom{\rule{1em}{0ex}}\mathrm{\nabla }\mathcal{L}=\mathbf{\text{0}}\end{array}$
For example, using our specific functions from above, we see how this encodes the system of equations we need to solve:
$\begin{array}{r}\phantom{\rule{1em}{0ex}}\mathrm{\nabla }\mathcal{L}=\left[\begin{array}{c}\frac{\partial }{\partial x}\left(2x+y-\lambda \left({x}^{2}+{y}^{2}-1\right)\right)\\ \\ \frac{\partial }{\partial y}\left(2x+y-\lambda \left({x}^{2}+{y}^{2}-1\right)\right)\\ \\ \frac{\partial }{\partial \lambda }\left(2x+y-\lambda \left({x}^{2}+{y}^{2}-1\right)\right)\\ \end{array}\right]=\left[\begin{array}{c}2-2\lambda x\\ 1-2\lambda y\\ -{x}^{2}-{y}^{2}+1\end{array}\right]=\left[\begin{array}{c}0\\ 0\\ 0\end{array}\right]\end{array}$
As a tribute to ol' Joey Lou, we call this function $\mathcal{L}$ the "Lagrangian", and the new variable $\lambda$ that we introduce is called a "Lagrange multiplier". Imagine if someone added "-ian" the end of your last name and made it the name of a function everybody uses. Pretty sweet!
Warning: Some authors use a convention where the sign of $\lambda$ is reversed:
$\begin{array}{r}\phantom{\rule{1em}{0ex}}\mathcal{L}\left(x,y,\lambda \right)=f\left(x,y\right)+\lambda \left(g\left(x,y\right)-c\right)\end{array}$
This doesn't make any difference when it comes to solving the problem, but you should keep it in mind in case the course you are taking or the text you are reading follows this convention.

## Summary

When you want to maximize (or minimize) a multivariable function $f\left(x,y,\dots \right)$ subject to the constraint that another multivariable function equals a constant, $g\left(x,y,\dots \right)=c$, follow these steps:
• Step 1: Introduce a new variable $\lambda$, and define a new function $\mathcal{L}$ as follows:
$\mathcal{L}\left(x,y,\dots ,\lambda \right)=f\left(x,y,\dots \right)-\lambda \left(g\left(x,y,\dots \right)-c\right)$
This function $\mathcal{L}$ is called the "Lagrangian", and the new variable $\lambda$ is referred to as a "Lagrange multiplier"
• Step 2: Set the gradient of $\mathcal{L}$ equal to the zero vector.
$\mathrm{\nabla }\mathcal{L}\left(x,y,\dots ,\lambda \right)=\mathbf{\text{0}}\phantom{\rule{1em}{0ex}}←\text{Zero vector}$
In other words, find the critical points of $\mathcal{L}$.
• Step 3: Consider each solution, which will look something like $\left({x}_{0},{y}_{0},\dots ,{\lambda }_{0}\right)$. Plug each one into $f$. Or rather, first remove the ${\lambda }_{0}$ component, then plug it into $f$, since $f$ does not have $\lambda$ as an input. Whichever one gives the greatest (or smallest) value is the maximum (or minimum) point you are seeking.

## Want to join the conversation?

• In the final "side note" example graph, the equation for the red diagonal line should be x - y = 0, not x + y = 0 • Why is that the maximum or minimum value for f lies at the point where the contour lines of f and g are tangent? How did you prove it more rigorously? • I won't prove this, but imagine them not being tangent, that would mean the function's contours cross the constraint at some point. If they cross, however, it means we can always shift the contour to a higher (or lower, depending of we're maximising or minimising) level and still be crossing the constraint. The time when you cannot shift it any higher is precisely at the level where they're only touching (tangent), not crossing. This is all assuming we have well-behaved functions/constraints
• “There's a slight twist to this story, best illustrated with an example.” is repeated. • what is the geometrical interpretation for lagrange multipliers involving two constraints?? • Use the method of Lagrange Multipliers to determine the maximum and minimum of f(x,y,z) = x + y + z subject to the two conditions g(x,y,z) = x2 + y2 − 2 = 0 and h(x, y, z) = x + z − 1 = 0 • We haven't learned about multiple constraints yet. The Lagrangian Function for multiple constraints is ℒ(x1,x2,...,xn,λ1,λ2,...,λM) = f(x1,x2,...,xn) - Σ λkgk(x1,x2,...,xn), where f(x) is the function to be maximized and g1, g2, ..., gM are the constraints (the Σ sum is from k = 1 to M). Note that here we use gk(x) = 0 instead of gk(x) = c.

We take the three gradients and get ∇f = i + j + k, ∇g = 2x i + 2y j, ∇h = i + k. ∇f = λ1∇g + λ2∇h, and g = h = 0, so we get: 1 = 2xλ1 + λ2, 1 = 2yλ1, 1 = λ2, x^2 + y^2 = 2, and x + z - 1 = 0. We see that λ2 = 1, so 2xλ1 = 0. because λ1 = 1/2y, x/y = 0, so x must be 0. Then y = +-√2, and λ1 = +-√2/4, and z = 1.

So we get two solutions: (0,√2,1) and (0,-√2,1). I think you can figure out which is the maximum.

If you can graph this, try it, and you'll see how stupid our computations were. After all this work the answers are on the y-z plane!
• Hi, thanks for the article. Question ... how does a parallel gradient tell us that the 2 contour lines are tangent to each other? Parallel gradients should be possible even if the 2 functions don't touch right? Thank you. • Actually, parallel gradients tell us that the contour lines are parallel. Contour lines are tangent when they are both touching and parallel at the place they're touching. Of course, we had to answer the question "Where are they parallel?" The direction of gradients and contour lines are different at every point.

Therefore, the gradients of the two functions need to be parallel at the same point. ∇f(0,1) being parallel to ∇g(1,0) wouldn't be much use. ∇f(0,1) must be parallel to ∇g(0,1).

Of course, this says nothing about where the functions f and g are, only what their gradients (slants or tilts) are. In fact, it doesn't matter whether f(0,1) = g(0,1). After all, we are restricting g to g(x,y) = c, and c may be arbitrary. If the restraint is x + y = 4, we can let g(x,y) = x + y and c = 4, or we can let g(x,y) = x + y - 4 and c = 0, or g(x,y) = x + y + 100 and c = 104. So how "high" g is doesn't matter at all, it's just its gradient (slant or tilt) that we need.

Of course, sometimes you might be required to let c = 0, but there's no difference. The "height" of g doesn't matter.
• What does it mean when you have more than one constraint (g and h), so you have 5 equations, 5 unknowns, but the answer you get for the Lagrange multipliers don't come out as a nice scalars? For example lambda ends up being something that depends on x and y. • Hello,

In the chapter explaining When the gradient comes into play, we insert the constraint in g(x, y) formula (we have x² + y² - 1 instead of x² + y²).

Why do we insert the constraint -1 when computing the gradient ? • At that phase in the lesson, I think there is no difference (constants do not affect the gradient there). The problem is that the latter phases of the lesson use that same g-function multiplied with a Lagrange multiplier, and since we want the partial derivative of λg with respect to λ to be g = function − c so that setting that partial derivative to zero returns the constraint function = c, the constant is a bit more useful in the later phases.
(1 vote)
• The contour graph for f(x,y) = 2x^2 + √(5y) doesn't seem right. For example, check the point (0,2). It should equal √10, yet in the contour graph, it has two values, √10 and -√10. And f(2,3) should be 8 + √15, about 12, but on the plot it seems it also has a value at about 4.

I think there is a bug in the program that displays this plot. Once you square everything and solve the equation, you get roots that weren't there before. For example, if you square both sides of x = √2, you get x^2 = 2, so x = +-√2. Though I'm not sure if that's what's wrong with the program.

I graphed the function with my calculator and there was only one value for (0,2) and (2,3). But then, f is a function, so the can only be one output for each pair of inputs. The contour plot above doesn't seem right.  