Lagrange Multipliers Calculator: Where the Gradients Line Up
About the Author

A constrained optimisation problem asks for the largest or smallest value of a function f while the variables are tied together by an equation g = c. The method of Lagrange multipliers turns that into a system of equations: at any constrained extremum the gradient of f is a multiple of the gradient of g, ∇f = λ∇g, and the point still satisfies g = c. This Lagrange multipliers calculator differentiates f and g for you, solves that system for two or three variables and one or two constraints, lists every solution with its multiplier λ, tells you which solutions are maxima, minima or neither, and draws the level curves of f against the constraint so you can see the tangency that the equations describe.
What the Lagrange Multipliers Calculator Solves
The tool handles equality constraints only. Type an objective such as x^2y and a constraint written as an equation such as x^2 + y^2 = 3; the calculator moves everything to one side, forms g = x² + y² − 3, and solves ∇f = λ∇g together with g = 0. With three variables you can add a second constraint h = d, in which case it solves ∇f = λ∇g + μ∇h. Every partial derivative is computed symbolically, so the equations you see are exact; the system itself is solved numerically with Newton’s method from a grid of starting points, and each converged solution is verified to about nine significant digits. Decimal answers that match a simple fraction, a square root or a multiple of π to ten digits are also shown in that form. If you only need the derivatives, the partial derivative calculator and the gradient calculator do that on their own; if your problem has a single variable or a closed interval, the optimization calculator and the extrema calculator are the right tools.
Why the Gradients Must Line Up at a Constrained Extremum
Picture the level curves of f, the curves along which f keeps one value, and the constraint curve g = c drawn on the same plane. Walking along the constraint, f rises and falls as you cross level curves. At the point where f is largest along the constraint you cannot cross to a higher level curve without leaving the constraint, so the constraint must be touching a level curve there rather than cutting through it: the two curves are tangent.
The gradient of a function is perpendicular to its level curves, so at a point of tangency ∇f and ∇g are perpendicular to the same line and therefore parallel. That is the whole content of ∇f = λ∇g; the number λ just records how much longer one arrow is than the other, and its sign records whether they point the same way. The calculator’s plot draws both arrows at every solution so you can see them lining up, and its directional derivative calculator companion gives the same idea in numbers: along the constraint’s tangent direction the directional derivative of f is zero at the extremum.
Setting Up the Lagrange Equations: xy With x + y = 10
Take the classic question of which two numbers with sum 10 have the largest product. Here f = xy and g = x + y − 10. The partial derivatives are ∂f/∂x = y, ∂f/∂y = x, ∂g/∂x = 1 and ∂g/∂y = 1, so the Lagrange system is:
y = λ · 1
x = λ · 1
x + y − 10 = 0
The first two equations force x = y = λ, and the third gives 2λ = 10, so λ = 5 and the point is (5, 5) with f = 25. The multiplier is not a spare unknown: it is exactly what makes three equations in three unknowns, and once the system has one you can solve it like any other, by hand or with the system of equations calculator. Note what the calculator says about the minimum: there is none. The line x + y = 10 runs off to infinity in both directions, and along it f = x(10 − x) is a downward parabola, so f can be made as negative as you like (x = 100, y = −90 gives −9,000). A single solution of the Lagrange equations is a candidate, never automatically an answer.
A Circle Constraint With Two Answers: Maximum and Minimum of x + y
Now maximise and minimise f = x + y on the unit circle x² + y² = 1. The system is 1 = 2λx, 1 = 2λy and x² + y² = 1. The first two equations give x = y = 1/(2λ), and substituting into the circle gives 2/(4λ²) = 1, so λ = ±1/√2. There are two solutions:
| Point | λ | f = x + y | Bordered Hessian D | Verdict |
|---|---|---|---|---|
| (√2/2, √2/2) ≈ (0.7071, 0.7071) | √2/2 ≈ 0.7071 | √2 ≈ 1.4142 | 8λ = 4√2 > 0 | maximum |
| (−√2/2, −√2/2) | −√2/2 | −√2 | −4√2 < 0 | minimum |
Because the circle is closed and bounded, f must attain both a largest and a smallest value on it, and with only two candidates the larger is the maximum and the smaller the minimum. The bordered Hessian column is the second-order test the calculator runs at every solution. For two variables and one constraint it is the determinant of the 3×3 matrix whose first row and column hold 0, ∂g/∂x, ∂g/∂y and whose remaining block is the Hessian of L = f − λg. For this problem L has second derivatives −2λ on the diagonal and 0 off it, so D = −(2x)²(−2λ) − (2y)²(−2λ) = 8λ(x² + y²) = 8λ. A positive D means a constrained local maximum and a negative D a constrained local minimum, which agrees with the comparison of values. The Hessian calculator evaluates the unbordered matrix at a point if you want to see the pieces separately.
What λ Means: The Multiplier as a Rate of Change
The multiplier has a meaning of its own. If V(c) is the optimal value of f when the constraint reads g = c, then dV/dc = λ: the multiplier tells you how fast the best achievable value changes when the constraint is loosened or tightened. Economists call it the shadow price of the constraint. On the circle above, the maximum of x + y on x² + y² = c is V(c) = √(2c), whose derivative at c = 1 is 1/√2, exactly the λ of the maximum. The calculator checks this for you: it shifts the right-hand side by a small ε, solves again from the same point and reports Δf*/ε next to λ; for the circle it prints 0.7071 against 0.7071.
A three-variable example makes the interpretation concrete. Maximise the volume V = xyz of a box whose surface area is 2xy + 2yz + 2xz = 12. The Lagrange system yz = λ(2y + 2z), xz = λ(2x + 2z), xy = λ(2x + 2y) forces x = y = z, so the box is a cube of side √2 ≈ 1.4142 with volume 2√2 ≈ 2.8284 and λ = √2/4 ≈ 0.3536. Solving the family of problems exactly gives V(S) = (S/6)^(3/2), and V′(12) = √2/4, the same number. So one extra unit of surface area is worth about 0.354 extra units of volume at the margin; going all the way from S = 12 to S = 13 actually adds 0.361, the small difference being the curvature of V that a first-order rate ignores. The calculator finds the mirror cube (−√2, −√2, −√2) too, with λ = −√2/4 and volume −2√2, and labels it the minimum; whether negative side lengths make sense is a modelling question the mathematics cannot answer for you.
Six Candidates, Two Winners: Why the Second-Order Test Matters
Optimise f = x²y on the circle x² + y² = 3. The x-equation is 2xy = 2λx, and the tempting move is to divide by x and conclude y = λ. That loses a whole family of solutions. Keeping x = 0 as a separate case, the calculator finds six points:
- (√2, 1) and (−√2, 1): λ = 1, f = 2, both constrained local maxima, and the global maximum.
- (√2, −1) and (−√2, −1): λ = −1, f = −2, both local minima, and the global minimum.
- (0, √3): λ = 0, f = 0, a local minimum (near it y > 0, so f = x²y ≥ 0).
- (0, −√3): λ = 0, f = 0, a local maximum (near it y < 0, so f ≤ 0).
Comparing values alone would rank the two points with f = 0 as “middle”, which is true globally but hides that each is a genuine local extremum of the constrained function. The bordered Hessian sorts them out: at (√2, 1) the determinant is 48, at (0, √3) it is −8(√3)³ ≈ −41.6, and the sign pattern is what the table reports. When a determinant comes out zero the test is inconclusive and the calculator says so instead of guessing; f = x³ + y on the line y = 0 is a case in point, with a single candidate at the origin that is neither a maximum nor a minimum.
Three Variables and Two Constraints
With two constraints the extremum sits on the curve where two surfaces meet, and the gradient of f must lie in the plane spanned by the two constraint gradients: ∇f = λ∇g + μ∇h. Maximise f = x + 2y + 3z on the intersection of the plane x − y + z = 1 with the cylinder x² + y² = 1. Componentwise the condition reads 1 = λ + 2μx, 2 = −λ + 2μy and 3 = λ. So λ = 3, then μx = −1 and μy = 5/2; the cylinder equation turns those into μ² = 29/4. The two solutions are μ = √29/2 at (−2/√29, 5/√29, 1 + 7/√29) ≈ (−0.371, 0.928, 2.300) with f = 3 + √29 ≈ 8.385, and μ = −√29/2 at the opposite point with f = 3 − √29 ≈ −2.385. The intersection curve is a closed ellipse, so these are the maximum and the minimum, and the calculator reports two multipliers per point along with a 5×5 bordered determinant whose sign confirms each one.
When the Lagrange Multiplier Method Fails
The condition ∇f = λ∇g is necessary only where ∇g ≠ 0; textbooks call this the constraint qualification. Where the constraint gradient vanishes, the constraint curve can have a corner or a cusp with no tangent line for a level curve to match, and the equations simply have no solution there even if an extremum exists. Minimise f = x on the curve y² = x³. The gradient of g = y² − x³ is (−3x², 2y), which is zero only at the origin, and the origin lies on the curve. Away from the origin the system 1 = −3λx², 0 = 2λy has no solution at all, yet the curve only exists for x ≥ 0, so x = 0 at the cusp is the minimum. The calculator searches separately for points on the constraint where ∇g = 0 (or, with two constraints, where ∇g and ∇h are parallel), lists them with a red ring on the plot, and refuses to classify them because the second-order test does not apply there either.
Three other things can go wrong, and the calculator reports each one differently. The constraint set may be empty, as with x² + y² = −1: then no candidates exist and the search finds no point of the constraint inside the box. The Lagrange system may have no solution because f has no extremum on an unbounded constraint, as with f = x² − y² on xy = 1, where f runs from −∞ to ∞ along the hyperbola. Or the equations may hold at every point of the constraint, as with f = x + y on x + y = 2, because f is constant there; the calculator recognises that the solutions form a whole curve and says so instead of listing points forever. Finally, the search is numerical: starting points fill the box from −R to R in each variable, so a candidate at x = 50 needs a search range of at least about 50, and the candidate table marks any solution that landed outside the box.
Common Mistakes in Constrained Optimization
- Dividing by a variable that can be zero. In x²y on the circle of radius √3, dividing 2xy = 2λx by x discards the two candidates (0, ±√3). Both are local extrema of the constrained function, and in a problem where one of them were the global answer the whole solution would be wrong.
- Treating the only candidate as the answer. f = xy on x + y = 10 has exactly one solution, (5, 5), and it is the maximum, but there is no minimum: f = −9,000 at (100, −90) on the same line. Always ask whether the constraint set is bounded before reading a lone candidate as both the maximum and the minimum.
- Reading the sign of λ as meaningful on its own. Writing the constraint as 1 = x² + y² instead of x² + y² = 1 flips λ from √2/2 to −√2/2 while the point (√2/2, √2/2) and the value √2 do not change, and the convention ∇f + λ∇g = 0 used by some texts flips it again. Only the magnitude, together with a stated convention, carries the sensitivity meaning.
- Applying the method to an inequality. Minimising f = x² + y² on the disk x² + y² ≤ 1 has its answer at the interior point (0, 0), where ∇f = 0 and no multiplier is involved. Lagrange multipliers handle the boundary x² + y² = 1 only, where they find the value 1; the interior needs the ordinary critical point check ∇f = 0, and the true minimum is the smaller of the two.
References: the statement of the method, the constraint qualification ∇g ≠ 0 and the interpretation of λ as the rate of change of the optimal value follow Wikipedia’s article on Lagrange multipliers; the sign rules for the bordered Hessian with m constraints and n variables follow the bordered Hessian section of Wikipedia’s article on the Hessian matrix; Wolfram MathWorld’s Lagrange multiplier entry states the method with the opposite sign convention, ∇f + λ∇g = 0, which is why λ can differ in sign between sources. This page uses ∇f = λ∇g throughout.



