Lagrange Multipliers Calculator - Constrained Optimum

Maximise or minimise f subject to a constraint.

Enter the objective and the constraint as an equation. The calculator differentiates both, solves the Lagrange system ∇f = λ∇g together with the constraint, lists every solution with its multiplier, and classifies each one with the bordered Hessian test.

Use +, −, *, /, ^, parentheses, pi, e and sin, cos, tan, exp, ln, log, sqrt, abs. 2x, xy and 2(x + y) are read as products.

Write an equation with one “=”, for example x^2 + y^2 = 1 or 2xy + 2yz + 2xz = 12.

Starting points for Newton’s method fill this box. Solutions far outside it can be missed. R from 0.01 to 10000.

Problem

optimise f(x, y) = x + y

subject to x^2 + y^2 = 1

2 solutions of the Lagrange system found from 169 starting points.

Constrained maximum

f = 1.4142136

= √2 (closed form matched to 10 digits)

at (0.70710678, 0.70710678) = (√2/2, √2/2)

λ = 0.70710678 = √2/2

This candidate has the largest value of f among all candidates and the bordered Hessian test confirms a local maximum.

Constrained minimum

f = −1.4142136

= −√2 (closed form matched to 10 digits)

at (−0.70710678, −0.70710678) = (−√2/2, −√2/2)

λ = −0.70710678 = −√2/2

This candidate has the smallest value of f among all candidates and the bordered Hessian test confirms a local minimum.

Where the gradients line up

Grey curves are level curves of f; dashed emerald curves are the levels through the candidates; the thick emerald curve is the constraint. At each candidate the green arrow is ∇f and the blue arrow is ∇g.

(0.70710678, 0.70710678): f = 1.4142136, Local maximum on the constraint(−0.70710678, −0.70710678): f = −1.4142136, Local minimum on the constraintx = −1.5556x = 1.5556y = 1.5556y = −1.5556
Purple: local maximum. Blue: local minimum. Amber: saddle. Grey: inconclusive. Red ring: ∇g = 0. When λ is negative the two arrows point in opposite directions along the same line, which still counts as lining up.

All candidates, largest f first

#Point (x, y)λfSecond-order test
1
(0.70710678, 0.70710678)
= (√2/2, √2/2)
0.70710678 = √2/2
1.4142136
= √2
Local maximum on the constraint
Largest value among candidates
2
(−0.70710678, −0.70710678)
= (−√2/2, −√2/2)
−0.70710678 = −√2/2
−1.4142136
= −√2
Local minimum on the constraint
Smallest value among candidates
How this was solved, step by step
  1. 1. Partial derivatives.
    ∂f/∂x = 1∂f/∂y = 1g/∂x = 2xg/∂y = 2y

    Here g = x^2 + y^2 − 1 is the constraint moved to one side. The condition is ∇f = λ∇g, which comes from the Lagrangian L = f − λ(g).

  2. 2. The Lagrange system. 3 equations in 3 unknowns (x, y, λ):
    ∂f/∂x = 1 = λ·2x
    ∂f/∂y = 1 = λ·2y
    x^2 + y^2 − 1 = 0
  3. 3. Solving. The system is solved numerically: Newton’s method with step damping starts from 169 points spread over the search box, each with a least-squares first guess for λ. Runs that converge to the same point are merged, and a solution is kept only when every equation is satisfied to about 9 significant digits. Decimal answers that agree with a simple fraction, square root or multiple of π to 10 digits are also shown in that form; treat that as a recognition, not a proof.
  4. 4. Second-order test. Each solution is classified with the bordered Hessian, the Hessian of L with the constraint gradient as a border. For two variables and one constraint its determinant D is positive at a constrained local maximum and negative at a constrained local minimum.
    • (0.70710678, 0.70710678): D = 5.6568542local maximum on the constraint
    • (−0.70710678, −0.70710678): D = −5.6568542local minimum on the constraint
  5. 5. Compare the values. This candidate has the largest value of f among all candidates and the bordered Hessian test confirms a local maximum. This candidate has the smallest value of f among all candidates and the bordered Hessian test confirms a local minimum. Sampling f along the constraint inside the ±10 box gave values from −1.4 to 1.4, and the whole constraint lies inside the box.
  6. 6. What λ means. The multiplier is the rate at which the optimal value changes when the constraint’s right-hand side moves. Check: shift the right-hand side by ε and solve again.
    • (0.70710678, 0.70710678), λ = 0.70711: ε = 0.0002, f* goes 1.41421361.414355, Δf*/ε = 0.70707
    • (−0.70710678, −0.70710678), λ = −0.70711: ε = 0.0002, f* goes −1.4142136−1.414355, Δf*/ε = −0.70707

How to Use This Calculator

  1. Choose Two variables for f(x, y) or Three variables for f(x, y, z). With three variables a second constraint can be added.
  2. Type the objective, for example x^2y or xyz, and the constraint as an equation such as x^2 + y^2 = 3. Products can be written as 2x, xy or 2(x + y); powers use ^; use pi and e for the constants.
  3. Set the search range if the answer may lie far from the origin. Newton’s method starts from a grid inside ±R, so a candidate at x = 50 needs R of at least about 50.
  4. Read the constrained maximum and minimum cards, then the table of every candidate with its λ, its value of f and its second-order classification. Closed forms such as √2/2 are shown when the decimal matches one.
  5. Open How this was solved for the partial derivatives, the Lagrange equations, the bordered Hessian values and the λ sensitivity check, or load a preset to see the layout with known answers.
average • 0 ratings
Your rating
Tap a star to rate

Your rating helps improve Lagrange Multipliers Calculator - Constrained Optimum. We store only an anonymized vote (no personal data).

Share this calculator

Help others solve their calculations

Found this calculator helpful? Share it with your friends, students, or colleagues who might need it!

Lagrange Multipliers Calculator: Where the Gradients Line Up

About the Author

Marko Šinko - Co-Founder & Lead Developer

Marko Šinko

Co-Founder & Lead Developer, AI Math Calculator

Lepoglava, Croatia
Advanced Algorithm Expert

Croatian developer with a Computer Science degree from University of Zagreb and expertise in advanced algorithms. Co-founder of award-winning projects, ensuring precise mathematical computations and reliable calculator tools.

📅 Published:
Lagrange Multipliers Calculator illustration: contour rings with a constraint curve tangent to one ring and two aligned gradient arrows

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 + yBordered Hessian DVerdict
(√2/2, √2/2) ≈ (0.7071, 0.7071)√2/2 ≈ 0.7071√2 ≈ 1.41428λ = 4√2 > 0maximum
(−√2/2, −√2/2)−√2/2−√2−4√2 < 0minimum

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.

Frequently Asked Questions

What kind of problem does the Lagrange multipliers calculator solve?

Equality-constrained problems: find the largest or smallest value of f(x, y) or f(x, y, z) while the variables satisfy an equation g = c, or two equations g = c and h = d with three variables. It solves ∇f = λ∇g (or ∇f = λ∇g + μ∇h) together with the constraint, lists every solution with its multiplier, and classifies each one. It does not handle inequality constraints such as x² + y² ≤ 1.

Why do the gradients have to be parallel at a constrained maximum or minimum?

Moving along the constraint changes f unless the constraint curve is tangent to a level curve of f at that point. Gradients are perpendicular to level curves, so at a point of tangency ∇f and ∇g are perpendicular to the same line and therefore parallel: ∇f = λ∇g. If the gradients were not parallel, a small step along the constraint would still raise or lower f, so the point could not be an extremum.

What does the value of λ tell you?

λ is the rate at which the optimal value changes when the constraint constant changes: if V(c) is the best value of f subject to g = c, then dV/dc = λ. For the largest box volume with surface area 12, λ = √2/4 ≈ 0.354, so allowing one more unit of surface area raises the best volume by about 0.354 at the margin. Economists call λ the shadow price of the constraint.

How do I know whether a Lagrange candidate is a maximum or a minimum?

Two checks. First, compare the values of f at all the candidates: on a closed, bounded constraint such as a circle the largest is the maximum and the smallest the minimum. Second, the bordered Hessian test classifies each point locally. For x + y on the unit circle the bordered determinant is 8λ: +4√2 at (√2/2, √2/2), a maximum, and −4√2 at (−√2/2, −√2/2), a minimum. The calculator runs both checks and reports when the determinant is zero and the test is inconclusive.

What if the Lagrange equations have no solution?

Three different things can cause it. The constraint set may be empty, like x² + y² = −1. The function may have no extremum on an unbounded constraint, like x² − y² on xy = 1, which runs from −∞ to ∞. Or the extremum may sit where ∇g = 0, where the method does not apply: f = x on the cusp curve y² = x³ has its minimum at the origin, where the constraint gradient vanishes and the equations 1 = −3λx², 0 = 2λy have no solution. The calculator searches for such points separately and lists them.

Does the sign of λ matter?

Only together with a stated convention. This calculator uses ∇f = λ∇g with the Lagrangian L = f − λ(g − c). Writing the constraint as 1 = x² + y² instead of x² + y² = 1 flips the sign of λ without changing the point or the value, and texts that use ∇f + λ∇g = 0 flip it again. The magnitude of λ, and its meaning as a rate of change with respect to the right-hand side as you wrote it, are what carry information.

Why does the calculator show both 0.70710678 and √2/2?

The system is solved numerically with Newton's method, so the answer arrives as a decimal. The calculator then checks whether that decimal agrees to ten digits with a simple fraction, a square root of a fraction, or a rational multiple of π, and shows the matching form when it finds one. That is a recognition, not an algebraic proof; for a proof, substitute the closed form back into the Lagrange equations.

Can I use Lagrange multipliers with an inequality constraint?

Not directly. For a region such as x² + y² ≤ 1, find the interior candidates where ∇f = 0, use Lagrange multipliers on the boundary x² + y² = 1, and compare all the values. Minimising x² + y² on that disk, for example, gives the interior point (0, 0) with value 0, which the boundary method alone would never find; the boundary candidates all have value 1.