acalculator

What do Lagrange multipliers give?

Type the function f and the constraint, such as x + 2y = 7. The page solves ∇f = λ∇g with the constraint and says which points are maxima and which are minima.

Your numbers

Extrema
minimum 27 at (5, 1)

For x^2 + 4y^2 - 2x + 8y with x + 2y = 7: minimum 27 at (5, 1).

All candidate points
(5, 1): f = 27, local minimum

Extrema: minimum 27 at (5, 1). For x^2 + 4y^2 - 2x + 8y with x + 2y = 7: minimum 27 at (5, 1).

How to calculate

Finds where f is largest or smallest on a constraint g = c, by Lagrange multipliers.

Example with the default inputs (Objective f x^2 + 4y^2 - 2x + 8y, Constraint x + 2y = 7): For x^2 + 4y^2 - 2x + 8y with x + 2y = 7: minimum 27 at (5, 1).

Method: Newton’s method on ∇f = λ∇g from a grid of starts; a second-order test on the constraint.

  • Decimals to 10 significant figures; points where ∇g = 0 are not candidates.

Machine-readable copies: Markdown, JSON.

Worked examples

Each example is checked against the calculator on every build.

  1. Objective f x^2 + 4y^2 - 2x + 8y, Constraint x + 2y = 7 gives Extrema minimum 27 at (5, 1).Source: OpenStax Calculus Vol. 3 (Strang and Herman, 2016), 4.8, Ex. 4.42
  2. Objective f 48x + 96y - x^2 - 2x y - 9y^2, Constraint 20x + 4y = 216 gives Extrema maximum 540 at (10, 4).

How it works

You type the objective f and one constraint as an equation, left = right. The page writes the constraint as g = left − right = 0 and solves the n + 1 equations

∂f/∂xᵢ − λ ∂g/∂xᵢ = 0 (for each of the n variables), g = 0,

for the n variables (2 or 3 letters, found in f and the constraint) and λ.

When f is constant on the constraint. First the page moves six points onto the constraint by 12 Newton steps along ∇g: (0.37, 0.37), (−1.3, −0.3), (2.9, 4.9), (−4.1, −1.1), (5.3, 9.3) and (−0.61, 4.39) (for three variables each gets a third coordinate: 0.37, 0.7, 6.9, 1.9, 13.3 and 9.39). There it computes λ = ∇f·∇g/|∇g|². If at all six |g| is at most 10⁻⁹ and |∇f − λ∇g| is at most 10⁻⁹ × |∇f| + 10⁻¹², f is constant or nearly constant on the constraint (as x + y on x + y = 4, or x² + y² on x² + y² = 1). The candidates may then fill a curve of the constraint, so the page shows no points and says "No verified answer: ∇f is parallel to ∇g (to 10⁻⁹) at six points of the constraint, so f is constant or nearly so on it, and the candidates may fill a curve (every point a candidate)."

Solving. The partial derivatives are numeric: a five-point central difference with step h = 0.001 × max(1, |xᵢ|). Newton’s method starts from every point of the grid whose values in each variable are −9.3, −2.1, −0.7, 0.6, 1.9 and 9.7 (36 starts for 2 variables, 216 for 3), with the first λ from the least-squares fit of ∇f ≈ λ∇g there. Each Newton step uses a numeric Jacobian (central differences with step 10⁻⁶ × max(1, |value|)), and halves the step (up to 9 times) until the size of the equations falls. It stops after 100 steps, when no tried step makes the size fall, or when the size is under 10⁻⁹ and the last step lowered it by less than 10% (the size has reached the rounding level). A result x is a candidate when |∇f − λ∇g| is at most 10⁻⁸ × (|∇f| + |λ| |∇g|) + 10⁻¹³, |g| is at most 10⁻¹⁰ × max(1, largest |coordinate| × |∇g|), and |∇g| is at least 10⁻⁹ (points where ∇g = 0 are not candidates). Far out along a line where ∇f is never parallel to ∇g (x + y on x + 2y = 7), Newton’s method drifts without meeting the first test, so no candidate is found; the page then says that no point where ∇f = λ∇g was found on the constraint. Two candidates within 10⁻⁶ × max(1, largest |coordinate|) of each other in every coordinate are the same point.

Placing each point. At each candidate the page checks that its digits are fixed. Let s = max(1, largest |coordinate|) and ε = 4 × 10⁻¹³ × (1 + |f| + |λ| × (1 + s|∇g|)) / s, the size of the rounding in the difference quotients. The page takes the next Newton step, and for each equation the Newton step that an error ε in that equation alone would cause. If any of these moves the point by more than 10⁻¹¹ × s, the point cannot be placed to 10 significant figures: it is repeated (the equations only touch 0 there, as for x³ on x − y² = 0 at (0, 0), or x⁴ on y = 0), it is on a curve of candidates (x + y on the line part of (x + y − 4)(x² + y² − 100) = 0), or the equations are nearly flat there (x + y + 0.00001x² on x + y = 4). The page then shows no list and says "No verified answer: a point where ∇f = λ∇g could not be placed to 10 significant figures (a repeated point, a curve of candidates, or nearly flat equations)."

Type of each point. Let δ = 0.001 × max(1, largest |coordinate|) and let e₁ be the unit vector along (g_y, −g_x) for two variables, or along the first nonzero of (g_y, −g_x, 0), (g_z, 0, −g_x), (0, g_z, −g_y) for three; for three variables e₂ is the unit vector along ∇g × e₁. For a unit direction d, q(d) is the mean of f(y) − f(x) over the two points y = x ± δd, each moved back onto the constraint by 12 Newton steps along ∇g. With two variables, q(e₁) above 10⁻¹² × max(1, |f(x)|) makes x a local minimum, below minus that a local maximum, and otherwise neither. With three, Q₁₁ = q(e₁), Q₂₂ = q(e₂) and Q₁₂ = q((e₁ + e₂)/√2) − (Q₁₁ + Q₂₂)/2; when Q₁₁Q₂₂ − Q₁₂² is above (10⁻¹² × max(1, |f(x)|))², x is a local minimum if Q₁₁ > 0 and a local maximum if Q₁₁ < 0; otherwise neither.

Extrema. The candidates are put in increasing order of f; values of f within 10⁻⁹ × max(1, |f|) of each other count as equal, and then the points go in increasing order of their coordinates (first coordinate first; coordinates within 10⁻⁹ × max(1, largest |coordinate|) of each other count as equal). The headline gives the last local maximum and the first local minimum in that order, with their points; if there is neither, it says so.

What you can type

  • f: a function of 2 or 3 letters (any lowercase letters except e, which is Euler’s number).
  • Constraint: one equation with one =, such as x + 2y = 7 or x^2 + y^2 = 1. It may use the same letters.
  • Operations: + − * / and ^; brackets group; numbers and letters side by side multiply. Functions: sqrt, ln, exp, sin, cos, tan and the others the calculators on this site read. Angles are in radians.

How answers are written

  • Numbers are decimals to 10 significant figures; a value within 10⁻¹² of 0 is 0.
  • A point is (x, y) or (x, y, z), with the letters in alphabetical order.
  • Extrema: "maximum V at P; minimum W at Q", either part left out when there is none.
  • All candidate points: each point, f there, and its type (local maximum, local minimum or neither), in the order above, separated by semicolons.

Assumptions

  • f and g are smooth near the candidates. Candidates outside the reach of the starting grid can be missed, and so can points where ∇g = 0.
  • A point that cannot be placed to 10 significant figures, such as a repeated solution, gets no answer (see above).

Worked examples by hand

f(x, y) = x² + 4y² − 2x + 8y on x + 2y = 7 (OpenStax Calculus Volume 3, section 4.8, Example 4.42). ∇f = (2x − 2, 8y + 8) = λ(1, 2) gives 2x − 2 = λ and 8y + 8 = 2λ, so 8y + 8 = 4x − 4, that is x = 2y + 3. With x + 2y = 7: 4y + 3 = 7, y = 1 and x = 5. f(5, 1) = 25 + 4 − 10 + 8 = 27, a minimum (f grows along the line in both directions).

f(x, y) = 48x + 96y − x² − 2xy − 9y² on 20x + 4y = 216 (Example 4.43). ∇f = (48 − 2x − 2y, 96 − 2x − 18y) = λ(20, 4). Then 48 − 2x − 2y = 5(96 − 2x − 18y), so 8x + 88y = 432, x + 11y = 54. With 5x + y = 54: y = 4, x = 10, and f(10, 4) = 480 + 384 − 100 − 80 − 144 = 540, a maximum.

Other questions people ask

What is the method of Lagrange multipliers?

To find the largest or smallest value of f subject to a constraint g = c, look for points where the gradients are parallel: ∇f = λ∇g for some number λ, the Lagrange multiplier, together with g = c. With two variables that is three equations in x, y and λ. Every constrained maximum or minimum at a point where ∇g is not 0 is among these candidate points.

How does the page tell a maximum from a minimum?

It uses the second-order test along the constraint. At each candidate point it steps a short distance along the constraint in each direction and compares f there with f at the point: with two variables along the one tangent direction, with three along two perpendicular tangent directions and the diagonal between them, which fixes the curvature of f on the surface. Curving up in every direction is a local minimum, down in every direction a local maximum, and anything else (a saddle, or flat) neither.

Is the largest value a global maximum?

When the constraint is a closed, bounded curve or surface, such as a circle or a sphere, f has a largest and a smallest value on it, and they are among the candidates, so the largest local maximum is the maximum. On an unbounded constraint such as a line, f may grow without limit, so a local minimum can be the only extreme value: for f = x² + 4y² − 2x + 8y on x + 2y = 7 the minimum is 27 at (5, 1) and there is no maximum.

Why are the answers decimals?

The equations ∇f = λ∇g, g = c are solved numerically, by Newton’s method, so every coordinate and value is a decimal to 10 significant figures. A point such as (5, 1) shows as 5 and 1 because its decimals round to those numbers.

What does λ mean?

The multiplier λ tells how fast the best value of f changes when the constraint constant c changes: d(max f)/dc = λ. In economics, when f is output and g = c is a budget, λ is the extra output per extra dollar of budget.

Can I use two constraints?

No. This page takes one constraint g = c in 2 or 3 variables. With two constraints the method uses two multipliers, ∇f = λ∇g + μ∇h.