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.

Course: Multivariable calculus>Unit 3

Lesson 6: Constrained optimization (articles)

Lagrange multipliers, examples

Examples of the Lagrangian and Lagrange multiplier technique in action.

Lagrange multiplier technique, quick recap

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 your are seeking.

Example 1: Budgetary constraints

Problem

Suppose you are running a factory, producing some sort of widget that requires steel as a raw material. Your costs are predominantly human labor, which is $\mathrm{}20$ per hour for your workers, and the steel itself, which runs for $\mathrm{}170$ per ton. Suppose your revenue $R$ is loosely modeled by the following equation:
$R\left(h,s\right)=200{h}^{2/3}{s}^{1/3}$
• $h$ represents hours of labor
• $s$ represents tons of steel
If your budget is $\mathrm{}20,000$, what is the maximum possible revenue?

Solution

The $\mathrm{}20$ per hour labor costs and $\mathrm{}170$ per ton steel costs tell us that the total cost of production, in terms of $h$ and $s$, is
$\begin{array}{r}\phantom{\rule{1em}{0ex}}20h+170s\end{array}$
Therefore the budget of $\mathrm{}20,000$ can be translated to the constraint
$\begin{array}{r}\phantom{\rule{1em}{0ex}}20h+170s=20,000\end{array}$
Before we dive into the computation, you can get a feel for this problem using the following interactive diagram. You can see which values of $\left(h,s\right)$ yield a given revenue (blue curve) and which values satisfy the constraint (red line).
Since we need to maximize a function $R\left(h,s\right)$, subject to a constraint, $20h+170s=20,000$, we begin by writing the Lagrangian function for this setup:
$\mathcal{L}\left(h,s,\lambda \right)=200{h}^{2/3}{s}^{1/3}-\lambda \left(20h+170s-20,000\right)$
Next, set the gradient $\mathrm{\nabla }\mathcal{L}$ equal to the $\mathbf{\text{0}}$ vector. This is the same as setting each partial derivative equal to $0$. First, we handle the partial derivative with respect to $h$.
$\begin{array}{rl}\phantom{\rule{1em}{0ex}}0& =\frac{\partial \mathcal{L}}{\partial h}\\ \\ 0& =\frac{\partial }{\partial h}\left(200{h}^{2/3}{s}^{1/3}-\lambda \left(20h+170s-20,000\right)\right)\\ \\ 0& =200\cdot \frac{2}{3}{h}^{-1/3}{s}^{1/3}-20\lambda \end{array}$
Next, we handle the partial derivative with respect to $s$.
$\begin{array}{rl}\phantom{\rule{1em}{0ex}}0& =\frac{\partial \mathcal{L}}{\partial s}\\ \\ 0& =\frac{\partial }{\partial s}\left(200{h}^{2/3}{s}^{1/3}-\lambda \left(20h+170s-20,000\right)\right)\\ \\ 0& =200\cdot \frac{1}{3}{h}^{2/3}{s}^{-2/3}-170\lambda \end{array}$
Finally we set the partial derivative with respect to $\lambda$ equal to $0$, which as always is just the same thing as the constraint. In practice, you can of course just write the constraint itself, but I'll write out the partial derivative here just to make things clear.
$\begin{array}{rl}\phantom{\rule{1em}{0ex}}0& =\frac{\partial \mathcal{L}}{\partial \lambda }\\ \\ 0& =\frac{\partial }{\partial \lambda }\left(200{h}^{2/3}{s}^{1/3}-\lambda \left(20h+170s-20,000\right)\right)\\ \\ 0& =-20h-170s+20,000\\ \\ 20h& +170s=20,000\end{array}$
Putting it together, the system of equations we need to solve is
$\begin{array}{rl}\phantom{\rule{1em}{0ex}}0& =200\cdot \frac{2}{3}{h}^{-1/3}{s}^{1/3}-20\lambda \\ \\ 0& =200\cdot \frac{1}{3}{h}^{2/3}{s}^{-2/3}-170\lambda \\ \\ 20h& +170s=20,000\end{array}$
In practice, you should almost always use a computer once you get to a system of equations like this. Especially because the equation will likely be more complicated than these in real applications. Once you do, you'll find that the answer is
$\begin{array}{rl}\phantom{\rule{1em}{0ex}}h& =\frac{2,000}{3}\approx 666.667\\ \\ s& =\frac{2,000}{51}\approx 39.2157\\ \\ \lambda & =\sqrt[3]{\phantom{A}\frac{8,000}{459}}\approx 2.593\\ \end{array}$
This means you should employ about $667$ hours of labor, and purchase $39$ tons of steel, which will give a maximum revenue of
$\begin{array}{r}\phantom{\rule{1em}{0ex}}R\left(667,39\right)=200\left(667{\right)}^{2/3}\left(39{\right)}^{1/3}\approx \overline{)\mathrm{}51,777}\end{array}$
The interpretation of this constant $\lambda =2.593$ is left to the next article

Example 2: Maximizing dot product

Problem: Let the three-dimensional vector $\stackrel{\to }{\mathbf{\text{v}}}$ be defined as follows.
$\begin{array}{r}\phantom{\rule{1em}{0ex}}\stackrel{\to }{\mathbf{\text{v}}}=\left[\begin{array}{c}2\\ 3\\ 1\end{array}\right]\end{array}$
Consider every possible unit vector $\stackrel{^}{\mathbf{\text{u}}}$ in three-dimensional space. For which one is the dot product $\stackrel{^}{\mathbf{\text{u}}}\cdot \stackrel{\to }{\mathbf{\text{v}}}$ the greatest?
The diagram below is two-dimensional, but not much changes in the intuition as we move to three dimensions.
If you are fluent with dot products, you may already know the answer. It's one of those mathematical facts worth remembering. If you don't know the answer, all the better! Because we will now find and prove the result using the Lagrange multiplier method.
Solution:
First, we need to spell out how exactly this is a constrained optimization problem. Write the coordinates of our unit vectors as $x$, $y$ and $z$:
$\begin{array}{r}\phantom{\rule{1em}{0ex}}\stackrel{^}{\mathbf{\text{u}}}=\left[\begin{array}{c}x\\ y\\ z\end{array}\right]\end{array}$
The fact that $\stackrel{^}{\mathbf{\text{u}}}$ is a unit vector means its magnitude is $1$:
$\begin{array}{rl}\phantom{\rule{1em}{0ex}}||\stackrel{^}{\mathbf{\text{u}}}||=\sqrt{{x}^{2}+{y}^{2}+{z}^{2}}& =1\\ & ⇓\\ {x}^{2}+{y}^{2}+{z}^{2}& =1\end{array}$
This is our constraint.
Maximizing $\stackrel{^}{\mathbf{\text{u}}}\cdot \stackrel{\to }{\mathbf{\text{v}}}$ means maximizing the following quantity:
$\left[\begin{array}{c}x\\ y\\ z\end{array}\right]\cdot \left[\begin{array}{c}2\\ 3\\ 1\end{array}\right]=2x+3y+z$
The Lagrangian, with respect to this function and the constraint above, is
$\mathcal{L}\left(x,y,z,\lambda \right)=2x+3y+z-\lambda \left({x}^{2}+{y}^{2}+{z}^{2}-1\right).$
We now solve for $\mathrm{\nabla }\mathcal{L}=\mathbf{\text{0}}$ by setting each partial derivative of this expression equal to $0$.
$\begin{array}{rl}\frac{\partial }{\partial x}\left(2x+3y+z-\lambda \left({x}^{2}+{y}^{2}+{z}^{2}-1\right)\right)& =2-\lambda 2x=0\\ \frac{\partial }{\partial y}\left(2x+3y+z-\lambda \left({x}^{2}+{y}^{2}+{z}^{2}-1\right)\right)& =3-\lambda 2y=0\\ \frac{\partial }{\partial z}\left(2x+3y+z-\lambda \left({x}^{2}+{y}^{2}+{z}^{2}-1\right)\right)& =1-\lambda 2z=0\end{array}$
Remember, setting the partial derivative with respect to $\lambda$ equal to $0$ just restates the constraint.
$\begin{array}{rl}\frac{\partial }{\partial \lambda }\left(2x+3y+z-\lambda \left({x}^{2}+{y}^{2}+{z}^{2}-1\right)\right)& =-{x}^{2}-{y}^{2}-{z}^{2}+1=0\end{array}$
Solving for $x$, $y$ and $z$ in the first three equations above, we get
$\begin{array}{rl}\phantom{\rule{1em}{0ex}}x& =2\cdot \frac{1}{2\lambda }\\ y& =3\cdot \frac{1}{2\lambda }\\ z& =1\cdot \frac{1}{2\lambda }\end{array}$
Ah, what beautiful symmetry. Each of these expressions has the same $\frac{1}{2\lambda }$ factor, and the coefficients $2$, $3$ and $1$ match up with the coordinates of $\stackrel{\to }{\mathbf{\text{v}}}$. Being good math students as we are, we won't let good symmetry go to waste. In this case, combining the three equations above into a single vector equation, we can relate $\stackrel{^}{\mathbf{\text{u}}}$ and $\stackrel{\to }{\mathbf{\text{v}}}$ as follows:
$\begin{array}{r}\phantom{\rule{1em}{0ex}}\stackrel{^}{\mathbf{\text{u}}}=\left[\begin{array}{c}x\\ y\\ z\end{array}\right]=\frac{1}{2\lambda }\left[\begin{array}{c}2\\ 3\\ 1\end{array}\right]=\frac{1}{2\lambda }\stackrel{\to }{\mathbf{\text{v}}}\end{array}$
Therefore $\stackrel{^}{\mathbf{\text{u}}}$ is proportional to $\stackrel{\to }{\mathbf{\text{v}}}$! Geometrically, this means $\stackrel{^}{\mathbf{\text{u}}}$ points in the same direction as $\stackrel{\to }{\mathbf{\text{v}}}$. There are two unit vectors proportional $\stackrel{\to }{\mathbf{\text{v}}}$,
• One which points in the same direction, this is the vector that $\text{maximizes}$ $\stackrel{^}{\mathbf{\text{u}}}\cdot \stackrel{\to }{\mathbf{\text{v}}}$.
• One which points in the opposite direction. This one $\text{minimizes}$ $\stackrel{^}{\mathbf{\text{u}}}\cdot \stackrel{\to }{\mathbf{\text{v}}}$.
We can write these two unit vectors by normalizing $\stackrel{\to }{\mathbf{\text{v}}}$, which just means dividing $\stackrel{\to }{\mathbf{\text{v}}}$ by its magnitude:
$\begin{array}{rl}\phantom{\rule{1em}{0ex}}{\stackrel{^}{\mathbf{\text{u}}}}_{\text{max}}& =\frac{\stackrel{\to }{\mathbf{\text{v}}}}{||\stackrel{\to }{\mathbf{\text{v}}}||}\\ \\ {\stackrel{^}{\mathbf{\text{u}}}}_{\text{min}}& =-\frac{\stackrel{\to }{\mathbf{\text{v}}}}{||\stackrel{\to }{\mathbf{\text{v}}}||}\end{array}$
The magnitude $||\stackrel{\to }{\mathbf{\text{v}}}||$ is $\sqrt{{2}^{2}+{3}^{2}+{1}^{2}}=\sqrt{14}$, so we can write the maximizing unit vector ${\stackrel{^}{\mathbf{\text{u}}}}_{\text{max}}$ explicitly as like this:
${\stackrel{^}{\mathbf{\text{u}}}}_{\text{max}}=\left[\begin{array}{c}2/\sqrt{14}\\ 3/\sqrt{14}\\ 1/\sqrt{14}\end{array}\right]$

Just skip the Lagrangian

If you read the last article, you'll recall that the whole point of the Lagrangian $\mathcal{L}$ is that setting $\mathrm{\nabla }\mathcal{L}=0$ encodes the two properties a constrained maximum must satisfy:
• Gradient alignment between the target function and the constraint function,
$\begin{array}{rl}\phantom{\rule{1em}{0ex}}\mathrm{\nabla }f\left(x,y\right)& =\lambda \mathrm{\nabla }g\left(x,y\right)\end{array}$
• The constraint itself,
$\begin{array}{r}\phantom{\rule{1em}{0ex}}g\left(x,y\right)=c\end{array}$
When working through examples, you might wonder why we bother writing out the Lagrangian at all. Wouldn't it be easier to just start with these two equations rather than re-establishing them from $\mathrm{\nabla }\mathcal{L}=0$ every time? The short answer is yes, it would be easier. If you find yourself solving a constrained optimization problem by hand, and you remember the idea of gradient alignment, feel free to go for it without worrying about the Lagrangian.
In practice, it's often a computer solving these problems, not a human. Given that there are many highly optimized programs for finding when the gradient of a given function is $0$, it's both clean and useful to encapsulate our problem into the equation $\mathrm{\nabla }\mathcal{L}=0$.
Furthermore, the Lagrangian itself, as well as several functions deriving from it, arise frequently in the theoretical study of optimization. In this light, reasoning about the single object $\mathcal{L}$ rather than multiple conditions makes it easier to see the connection between high-level​ ideas. Not to mention, it's quicker to write down on a blackboard.
In either case, whatever your future relationship with constrained optimization might be, it is good to be able to think about the Lagrangian itself and what it does. The examples above illustrate how it works, and hopefully help to drive home the point that $\mathrm{\nabla }\mathcal{L}=0$ encapsulates both $\mathrm{\nabla }f=\lambda \mathrm{\nabla }g$ and $g\left(x,y\right)=c$ in a single equation.

Want to join the conversation?

• In example 2, why do we put a hat on u? Is it because it is a unit vector, or because it is the vector that we are looking for?
• It is because it is a unit vector. Unit vectors will typically have a hat on them.
• I have seen some questions where the constraint is added in the Lagrangian, unlike here where it is subtracted. e.g. Lagrangian = f(x) + λg(x)
How does one decide whether to add or subtract?
Thanks!
• Hello, I have been thinking about this and can't really understand what is happening. So suppose I want to maximize
the function:

f(x,y,z) = 5xy + 8xz + 3yz, with the constraint 2xyz = 1920.

After doing the multiplier method, I only get one solution. Does that mean that the function does not have a maximum or a minimum? Or how can I tell if the solution I get represents or not a maximum?
• the determinant of hessian evaluated at a point indicates the concavity of f at that point
(1 vote)
• Hi everyone, I hope you all are well. When Grant writes that "therefore u-hat is proportional to vector v!" in example two, is the exclamation point representing a factorial symbol or just something for "wow" exclamation? Thanks for your help.
• Just an exclamation. I do not know how factorial would work for vectors.
• Instead of constraining optimization to a curve on x-y plane, is there which a method to constrain the optimization to a region/area on the x-y plane. Like the region
x^2+y^2<=2 which r all the points in the unit circle including the boundary.
• Hello and really thank you for your amazing site. Can you please explain me why we dont use the whole Lagrange but only the first part? Why we dont use the 2nd derivatives