# Lagrange Multipliers: Method, Intuition and Worked Examples

> Learn the method of Lagrange multipliers for constrained optimization, why the gradients must be parallel, and how to solve the equations in worked examples.

Source: https://practicecalculus.com/blog/lagrange-multipliers
Updated: 2026-09-18

## Key takeaways

- To optimize $f(x, y)$ subject to $g(x, y) = c$, solve $\nabla f = \lambda \nabla g$ together with $g(x, y) = c$.
- At a constrained max or min, the level curve of $f$ just touches the constraint curve, so their gradients are parallel.
- Evaluate $f$ at every solution you find. The largest value is the maximum and the smallest is the minimum, provided the constraint curve is closed and bounded.
- The multiplier $\lambda$ measures how fast the optimal value changes as the constraint constant $c$ changes.

Most real optimization problems come with strings attached. Maximize area, but the fence has a fixed length. Minimize cost, but you must produce a fixed amount. When the variables are tied together by a constraint, setting the partial derivatives to zero no longer works. Lagrange multipliers give you a clean system of equations that finds the answer anyway.

This guide explains the method, the geometry behind it, and three fully worked examples, including one in three variables.

## What is the method of Lagrange multipliers?

Suppose you want the maximum or minimum of $f(x, y)$, but only over points that satisfy a constraint $g(x, y) = c$. The **method of Lagrange multipliers** says that at any such extreme point (where $\nabla g \ne \mathbf{0}$), there is a number $\lambda$ with

$$\nabla f(x, y) = \lambda\, \nabla g(x, y), \qquad g(x, y) = c$$

Written out in components, that is three equations in the three unknowns $x$, $y$ and $\lambda$:

$$f_x = \lambda g_x, \qquad f_y = \lambda g_y, \qquad g(x, y) = c$$

The Greek letter $\lambda$ (lambda) is the **Lagrange multiplier**. For three variables, add $f_z = \lambda g_z$ and the constraint becomes $g(x, y, z) = c$.

## Why it works: the gradients are parallel

Picture the constraint $g(x, y) = c$ as a path drawn on a map, and the level curves of $f$ as contour lines. Walk along the path and watch the value of $f$.

- Where the path **crosses** a contour line, you can keep walking to a higher contour. That is not a maximum.
- At the highest point on the path, the path just **touches** a contour line without crossing it. The two curves are tangent.

Two tangent curves have the same tangent line at the point of contact. Since a [gradient](/blog/gradient-and-directional-derivatives) is perpendicular to its own level curve, $\nabla f$ and $\nabla g$ are both perpendicular to that shared tangent line. So they point along the same line, which means one is a multiple of the other: $\nabla f = \lambda \nabla g$.

## How to use Lagrange multipliers step by step

1. **Identify** the function to optimize, $f$, and write the constraint as $g = c$.
2. **Compute** $\nabla f$ and $\nabla g$.
3. **Set up** the system $\nabla f = \lambda \nabla g$, $g = c$.
4. **Solve** it. A reliable trick: solve each gradient equation for $\lambda$ and set the results equal, or divide one equation by another to eliminate $\lambda$. Watch for cases where a variable is $0$.
5. **Evaluate** $f$ at every solution. The largest value is the maximum and the smallest is the minimum.

> **Tip:** step 5 is not optional. The equations find candidates, and only comparing $f$ values tells you which is which.

## Worked example 1: a line meets a circle

Find the maximum and minimum of $f(x, y) = x + y$ on the circle $x^2 + y^2 = 2$.

**Step 1. Gradients.** $\nabla f = \langle 1, 1 \rangle$ and, with $g = x^2 + y^2$, $\nabla g = \langle 2x, 2y \rangle$.

**Step 2. The system.**

$$1 = 2\lambda x, \qquad 1 = 2\lambda y, \qquad x^2 + y^2 = 2$$

**Step 3. Eliminate $\lambda$.** Neither $\lambda$, $x$ nor $y$ can be $0$ (or $1 = 0$). Solve for $\lambda$: $\lambda = \frac{1}{2x} = \frac{1}{2y}$, so $x = y$.

**Step 4. Use the constraint.** $x^2 + x^2 = 2$ gives $x = \pm 1$. The candidates are $(1, 1)$ and $(-1, -1)$.

**Step 5. Evaluate.** $f(1, 1) = 2$ and $f(-1, -1) = -2$.

The maximum is $2$ at $(1, 1)$ and the minimum is $-2$ at $(-1, -1)$. In the graph, the level lines $x + y = 2$ and $x + y = -2$ just touch the circle, while any line in between, such as $x + y = 0$, cuts straight through it.

```graph
title: 'The circle x² + y² = 2 with the level lines x + y = 2 and x + y = −2 tangent at (1, 1) and (−1, −1)'
caption: 'The level curves of $f = x + y$ are lines. The highest one that still touches the circle is $x + y = 2$, at $(1, 1)$, and the lowest is $x + y = -2$, at $(-1, -1)$.'
x: [-3.5, 3.5]
y: [-2.1, 2.1]
functions:
    - expr: sqrt(2 - x^2)
      label: constraint x² + y² = 2
    - expr: -sqrt(2 - x^2)
      color: '#5b4fe0'
    - expr: 2 - x
      label: x + y = 2 (maximum)
      dashed: true
    - expr: -2 - x
      label: x + y = −2 (minimum)
      dashed: true
points:
    - [1, 1, '(1, 1)']
    - [-1, -1, '(−1, −1)']
```

## Worked example 2: a product on an ellipse

Find the maximum and minimum of $f(x, y) = xy$ on the ellipse $x^2 + 4y^2 = 8$.

**Step 1. Gradients.** $\nabla f = \langle y, x \rangle$ and $\nabla g = \langle 2x, 8y \rangle$.

**Step 2. The system.**

$$y = 2\lambda x, \qquad x = 8\lambda y, \qquad x^2 + 4y^2 = 8$$

**Step 3. Substitute.** Put the second equation into the first: $y = 2\lambda(8\lambda y) = 16\lambda^2 y$, so $y(1 - 16\lambda^2) = 0$.

- **Case $y = 0$.** Then $x = 8\lambda \cdot 0 = 0$, but $(0, 0)$ is not on the ellipse. No solution.
- **Case $\lambda^2 = \frac{1}{16}$.** Then $\lambda = \pm\frac{1}{4}$, and $y = 2\lambda x = \pm\frac{x}{2}$.

**Step 4. Use the constraint.** With $y = \pm\frac{x}{2}$, $4y^2 = x^2$, so $x^2 + x^2 = 8$ and $x = \pm 2$. That gives four candidates: $(2, 1)$, $(-2, -1)$, $(2, -1)$ and $(-2, 1)$.

**Step 5. Evaluate.**

| Point | $f = xy$ |
| --- | --- |
| $(2, 1)$ | $2$ |
| $(-2, -1)$ | $2$ |
| $(2, -1)$ | $-2$ |
| $(-2, 1)$ | $-2$ |

The maximum is $2$, at $(2, 1)$ and $(-2, -1)$. The minimum is $-2$, at $(2, -1)$ and $(-2, 1)$. Since the ellipse is closed and bounded, both are guaranteed to exist.

The level curves of $xy$ are hyperbolas. The hyperbola $xy = 2$ is tangent to the ellipse at the two maximum points, exactly as the parallel gradient condition predicts.

```graph
title: 'The ellipse x² + 4y² = 8 with the hyperbola xy = 2 tangent to it at (2, 1) and (−2, −1)'
caption: 'The level curve $xy = 2$ touches the ellipse at the two maximum points. At $(2, 1)$ both curves have slope $-\frac{1}{2}$, so their gradients are parallel.'
x: [-5, 5]
y: [-3, 3]
functions:
    - expr: sqrt(8 - x^2)/2
      label: constraint x² + 4y² = 8
    - expr: -sqrt(8 - x^2)/2
      color: '#5b4fe0'
    - expr: 2/sqrt(x)^2
      label: level curve xy = 2
      dashed: true
    - expr: -2/sqrt(-x)^2
      color: '#16a34a'
      dashed: true
points:
    - [2, 1, '(2, 1)']
    - [-2, -1, '(−2, −1)']
```

## Worked example 3: closest point on a plane

Find the point on the plane $x + 2y + 2z = 9$ closest to the origin.

**Step 1. Choose $f$.** Distance is $\sqrt{x^2 + y^2 + z^2}$. Minimizing the square root is the same as minimizing what is inside, so use $f = x^2 + y^2 + z^2$. The constraint is $g = x + 2y + 2z = 9$.

**Step 2. Gradients.** $\nabla f = \langle 2x, 2y, 2z \rangle$ and $\nabla g = \langle 1, 2, 2 \rangle$.

**Step 3. The system.**

$$2x = \lambda, \qquad 2y = 2\lambda, \qquad 2z = 2\lambda, \qquad x + 2y + 2z = 9$$

**Step 4. Express everything in terms of $\lambda$.** $x = \frac{\lambda}{2}$, $y = \lambda$, $z = \lambda$.

**Step 5. Use the constraint.**

$$\frac{\lambda}{2} + 2\lambda + 2\lambda = \frac{9\lambda}{2} = 9 \quad\Longrightarrow\quad \lambda = 2$$

So the point is $(1, 2, 2)$, and $f = 1 + 4 + 4 = 9$, giving a distance of $\sqrt{9} = 3$.

**Step 6. Is it a min or a max?** There is only one candidate, and a plane has points as far from the origin as you like, so there is no maximum. The single candidate must be the minimum. As a check, the point-to-plane distance formula gives $\frac{|9|}{\sqrt{1 + 4 + 4}} = 3$.

## What does lambda mean?

The multiplier is not just a helper variable. **$\lambda$ is the rate at which the optimal value changes as the constraint constant $c$ changes.**

Check it on example 1 with a general circle $x^2 + y^2 = c$. The same steps give a maximum at $x = y = \sqrt{c/2}$, with value $f^* = 2\sqrt{c/2} = \sqrt{2c}$. Then

$$\frac{df^*}{dc} = \frac{1}{\sqrt{2c}} = \frac{1}{2} \text{ at } c = 2$$

And at $(1, 1)$, the equation $1 = 2\lambda x$ gave $\lambda = \frac{1}{2}$. They match. In economics this is the **shadow price**: how much more profit one extra unit of a limited resource is worth.

## Common mistakes with Lagrange multipliers

1. **Dividing by a variable that could be zero.** In example 2, dividing $y = 16\lambda^2 y$ by $y$ would hide the case $y = 0$. Factor instead, and handle each case.
2. **Forgetting the constraint equation.** $\nabla f = \lambda\nabla g$ alone has too many solutions. You need $g = c$ to pin them down.
3. **Stopping before evaluating $f$.** The system gives candidates, not answers.
4. **Missing sign cases.** Square roots give $\pm$. Example 2 has four candidates, not one.
5. **Claiming a max that does not exist.** On an unbounded constraint like a line or a plane, one of the extremes may not exist. Argue which one you found.
6. **Optimizing distance with the square root.** It works, but the algebra is much cleaner with distance squared.

## Quick reference: Lagrange multipliers

| Item | Two variables | Three variables |
| --- | --- | --- |
| Goal | optimize $f(x, y)$ with $g(x, y) = c$ | optimize $f(x, y, z)$ with $g(x, y, z) = c$ |
| Equations | $f_x = \lambda g_x$, $f_y = \lambda g_y$, $g = c$ | add $f_z = \lambda g_z$ |
| Unknowns | $x, y, \lambda$ | $x, y, z, \lambda$ |
| Geometry | level curve of $f$ tangent to the constraint | level surface of $f$ tangent to the constraint |
| Deciding max or min | compare $f$ at every candidate | compare $f$ at every candidate |

## Practice the algebra, master the method

The calculus in a Lagrange problem takes one line. The real work is solving the system without losing a case, and that only gets reliable with practice. The interactive lesson below gives you constrained problems to solve step by step, checks your system and each candidate point instantly, and shows you any case you missed.

## Frequently asked questions

### What are Lagrange multipliers used for?

They find the maximum and minimum of a function when the variables must satisfy a constraint, such as maximizing area with a fixed perimeter or minimizing cost with a fixed output. Instead of solving the constraint for one variable and substituting, you solve the system $\nabla f = \lambda \nabla g$, $g = c$.

### Why are the gradients parallel at the optimum?

At a constrained maximum or minimum, the level curve of $f$ touches the constraint curve without crossing it. Two curves that touch share a tangent line, and the gradient of each function is perpendicular to its own level curve, so the two gradients point along the same line.

### What does lambda mean in Lagrange multipliers?

The number $\lambda$ is the rate of change of the optimal value of $f$ with respect to the constraint constant $c$. If $\lambda = 2$, loosening the constraint from $c$ to $c + 0.1$ raises the best value of $f$ by about $0.2$. In economics it is called a shadow price.

### How do you know if a point is a maximum or a minimum?

Plug every candidate point into $f$ and compare. If the constraint is a closed, bounded curve or surface (like a circle or an ellipse), the extreme value theorem guarantees both a max and a min exist, so the largest value is the max and the smallest is the min.

### Can lambda be zero?

Yes. A solution with $\lambda = 0$ means $\nabla f = \mathbf{0}$, so the point is also a critical point of $f$ without the constraint. You still include it as a candidate and evaluate $f$ there.
