# 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 subject to the constraint that another multivariable function equals a constant, , follow these steps:

**Step 1**: Introduce a new variable , and define a new function $\mathcal{L}$ as follows:This function $\mathcal{L}$ is called the "Lagrangian", and the new variable is referred to as a "Lagrange multiplier"**Step 2**: Set the gradient of $\mathcal{L}$ equal to the zero vector.In other words, find the**critical points**of $\mathcal{L}$.**Step 3**: Consider each solution, which will look something like . Plug each one into . Or rather, first remove the component, then plug it into , since does not have 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 per hour for your workers, and the steel itself, which runs for per ton. Suppose your revenue is loosely modeled by the following equation:

- represents hours of labor
- represents tons of steel

If your budget is , what is the maximum possible revenue?

#### Solution

The per hour labor costs and per ton steel costs tell us that the total cost of production, in terms of and , is

Therefore the budget of can be translated to the constraint

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 yield a given revenue (blue curve) and which values satisfy the constraint (red line).

Since we need to maximize a function , subject to a constraint, , we begin by writing the Lagrangian function for this setup:

Next, set the gradient $\nabla \mathcal{L}$ equal to the vector. This is the same as setting each partial derivative equal to . First, we handle the partial derivative with respect to .

Next, we handle the partial derivative with respect to .

Finally we set the partial derivative with respect to equal to , 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.

Putting it together, the system of equations we need to solve is

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

This means you should employ about hours of labor, and purchase tons of steel, which will give a maximum revenue of

The interpretation of this constant is left to the next article

## Example 2: Maximizing dot product

**Problem**: Let the three-dimensional vector be defined as follows.

Consider every possible unit vector $\hat{\textbf{u}}$ in three-dimensional space. For which one is the dot product $\hat{\textbf{u}} \cdot \vec{\textbf{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 , and :

The fact that $\hat{\textbf{u}}$ is a

**unit vector**means its magnitude is :This is our constraint.

Maximizing $\hat{\textbf{u}} \cdot \vec{\textbf{v}}$ means maximizing the following quantity:

The Lagrangian, with respect to this function and the constraint above, is

We now solve for $\nabla \mathcal{L} = \textbf{0}$ by setting each partial derivative of this expression equal to .

Remember, setting the partial derivative with respect to equal to just restates the constraint.

Solving for , and in the first three equations above, we get

Ah, what beautiful symmetry. Each of these expressions has the same factor, and the coefficients , and match up with the coordinates of . 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 $\hat{\textbf{u}}$ and as follows:

Therefore $\hat{\textbf{u}}$ is proportional to ! Geometrically, this means $\hat{\textbf{u}}$ points in the same direction as . There are two unit vectors proportional ,

- One which points in the same direction, this is the vector that $\hat{\textbf{u}} \cdot \vec{\textbf{v}}$.
- One which points in the opposite direction. This one $\hat{\textbf{u}} \cdot \vec{\textbf{v}}$.

We can write these two unit vectors by normalizing , which just means dividing by its magnitude:

The magnitude is , so we can write the maximizing unit vector $\greenE{\hat{\textbf{u}}_{\text{max}}}$ explicitly as like this:

## 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 $\nabla \mathcal{L} = 0$ encodes the two properties a constrained maximum must satisfy:

- Gradient alignment between the target function and the constraint function,
- The constraint itself,

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 $\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 , it's both clean and useful to encapsulate our problem into the equation $\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 $\nabla \mathcal{L} = 0$ encapsulates both and in a single equation.