A tool that visualizes the Lagrange multiplier method for optimizing an objective function under an equality constraint. Change the objective coefficients, the constraint and minimize/maximize to see, in real time, the optimum where a contour of f touches the constraint curve, the multiplier λ, and the sensitivity (shadow price).
Parameters
Objective coefficient a (f = a·x² + b·y²)
Weight in the x² direction. Larger means a steeper bowl along x
Objective coefficient b (f = a·x² + b·y²)
Weight in the y² direction. Larger means a steeper bowl along y
Constraint
The equality constraint curve g(x,y)=0 the optimum must lie on
Constraint constant c
Sets the intercept sum of the line or the radius of the circle
Optimization
Whether to minimize or maximize the objective function
Results
—
Optimum x*
—
Optimum y*
—
Optimal value f*
—
Lagrange multiplier λ
—
Constraint residual g(x*,y*)
—
Sensitivity df*/dc (= λ check)
—
Contours and constraint — tangency and gradient vectors
Concentric ellipses are contours of the objective f; the bold curve is the constraint g=0. At the optimum a contour is tangent to the constraint and ∇f (yellow) and ∇g (blue) become parallel.
Objective f along the constraint
Optimal value f* vs constraint constant c (slope = λ)
Theory & Key Formulas
$$\nabla f=\lambda\,\nabla g,\qquad g(x,y)=0$$
The constrained-optimization condition. At the optimum the gradient ∇f of the objective is parallel to the gradient ∇g of the constraint, the proportionality factor being the Lagrange multiplier λ. Solve this together with g=0.
$$\lambda=\frac{d f^\star}{dc}\quad(\text{sensitivity of relaxing the constraint — shadow price})$$
The multiplier λ is the rate of change of the optimal value f* as the constraint constant c is relaxed by one unit — the "shadow price" of one extra unit of resource.
The objective and the optimum for the line constraint x+y=c. Here f* = abc²/(a+b) and λ = 2abc/(a+b); at the optimum a contour of f is tangent to the constraint curve.
What is the Lagrange Multiplier Method?
🙋
I've heard the name "Lagrange multipliers", but what is it actually a tool for?
🎓
Roughly speaking, it is a tool for finding the maximum or minimum of a function "with strings attached". In ordinary optimization you just find where the derivative is zero. But real problems usually come with an equality constraint — "the budget is exactly this", "the total amount of material is fixed". With a constraint, the point where the derivative is zero (the unconstrained optimum) almost never satisfies the constraint. That is where this method comes in.
🙋
Why doesn't the ordinary approach work once there's a constraint? In the chart on the left the centre of the ellipses and the line are clearly apart.
🎓
Exactly that. The ellipses are the "contours" of the objective f, and the centre is the unconstrained minimum. But if you have to stay on the line x+y=c, you can't reach the centre. So which point on the line is best? The trick is to find "the point where a contour is exactly tangent to the constraint line". At a non-tangent point you could slide along the line onto a smaller contour. The moment it becomes tangent, you can no longer improve — that is the optimum.
🙋
I see! How does "tangent" connect to the equation ∇f = λ∇g?
🎓
The gradient vector ∇f is always perpendicular to a contour, and ∇g is perpendicular to the constraint curve. If the contour and the constraint curve are tangent, they share the same tangent line at that point. Their perpendicular directions then automatically lie along the same line. So ∇f and ∇g are parallel. Being parallel means we can write ∇f = λ∇g for some number λ. That λ is the "Lagrange multiplier". After that you just solve this equation together with g=0. Watch the yellow and blue arrows overlap in the chart on the left.
🙋
I thought λ was just some mysterious letter that pops up mid-calculation. Does it actually mean something?
🎓
That's the most interesting part. λ represents "how much the optimal value gains if you relax the constraint a little". In symbols, λ = df*/dc. In economics it is called the "shadow price". For example, if you could use one more kilogram of material, how much would profit increase? That answer is exactly λ. So a constraint with a large λ is a design hint: "relaxing this one helps the most". You can confirm in the lower-right chart that the slope of f* equals λ.
🙋
One last thing. The answer changes when I make the constraint a "circle" or switch to "maximize". What should I watch out for?
🎓
Good question. A line constraint extends to infinity, so the bowl-shaped f has a minimum on the line but no maximum. That is why choosing "line + maximize" makes f* infinite — and that is the correct behaviour. A circle constraint, on the other hand, is a closed bounded curve, so it has both a maximum and a minimum. With a circle you get two candidate pairs, (±c,0) and (0,±c); for minimize you pick the smaller and for maximize the larger. The point is that the "shape" of the constraint changes how many answers you get.
Frequently Asked Questions
It is a technique for maximizing or minimizing an objective function f(x,y) subject to an equality constraint g(x,y)=0. With a constraint you cannot simply set the gradient to zero, because the unconstrained optimum usually violates the constraint. The Lagrange multiplier method uses the geometric fact that at the constrained optimum a contour of f is tangent to the constraint curve, so the two gradients point along the same line. Introducing an unknown scalar λ (the Lagrange multiplier) and solving ∇f = λ∇g together with g=0 locates the optimum.
λ is not just a bookkeeping variable; it carries a clear physical and economic meaning. λ equals the sensitivity of the optimal value to relaxing the constraint, that is df*/dc. This is called the shadow price: it tells you how much the objective improves per unit of extra resource. For the line constraint x+y=c in this tool, dfStar/dc = 2abc/(a+b) is exactly equal to λ, and the result cards let you verify that match.
Imagine moving along the constraint curve g=0 while optimizing f. If at some point the gradient ∇f has any component along the tangent of the constraint curve, you can move that way and improve f further. You can no longer improve only when ∇f is orthogonal to the tangent, that is when it points along the normal of the constraint curve (the direction of ∇g). So at the optimum ∇f and ∇g are collinear and we can write ∇f = λ∇g. Geometrically this appears as a contour of f tangent to the constraint curve.
The objective f = a·x² + b·y² is a convex bowl, so along the line x+y=c it has only one stationary point, the minimum. Because the line extends to infinity, pushing x large (or small) along the line makes f arbitrarily large, so there is no finite maximum. This tool reports f* as infinity (∞) for a line constraint with maximize and flags that the maximum is unbounded. A circle, by contrast, is a closed bounded curve, so both a maximum and a minimum exist.
Real-World Applications
Structural optimization and design: Design problems such as "maximize stiffness while keeping weight fixed" or "minimize cross-section under an allowable-stress constraint" are exactly constrained optimization. In topology and shape optimization of trusses, an objective such as compliance or mass is minimized under a volume or stress constraint. The Lagrange multiplier of each constraint then tells you quantitatively how much the design would improve if that constraint were relaxed slightly.
The finite element method and governing equations: The variational principles that underpin FEM are themselves constrained optimization. For incompressible flow or contact problems, an "incompressibility condition" or a "non-penetration condition" is imposed as an equality or inequality constraint, and its multiplier appears as a physical quantity (pressure or contact force). The pressure field in mixed finite element methods is precisely the Lagrange multiplier of a volume-conservation constraint. Knowing that the multiplier has physical meaning makes such formulations far more intuitive.
Economics and operations research: The consumer problem of maximizing utility under a budget constraint and the production plan of maximizing profit under resource constraints are classic constrained optimization. Here the multiplier is the "shadow price": it shows how much the objective rises if you obtain one more unit of that resource. The dual variables of linear programming follow the same idea and directly inform the decision of which constraint to relax.
Machine learning and statistics: A support vector machine formulates margin maximization as constrained optimization, and the multipliers of its dual problem identify the support vectors. Regularization (such as ridge regression) is equivalent to a constraint that keeps the parameter norm below a level, with the regularization coefficient playing the role of a multiplier. Constrained maximum-likelihood estimation uses the same framework, making the Lagrange multiplier method a common language across applied mathematics.
Common Misconceptions and Pitfalls
The biggest pitfall is assuming that any point obtained by solving ∇f = λ∇g must be the optimum. This condition is a necessary condition that the optimum satisfies, not a sufficient one. A point satisfying it (a stationary point) may be a minimum, a maximum or a saddle point. For the circle constraint in this tool two candidates appear, and which is the minimum and which the maximum is decided only by comparing the values of f. In practice you must check the second-order condition (the sign of the bordered Hessian) or evaluate the objective at every stationary point and compare.
Next, ignoring the sign and magnitude of λ. Since λ is the sensitivity df*/dc itself, its sign carries meaning. If λ is positive, relaxing the constraint increases the optimal value; if negative, it decreases it. In a minimization problem a constraint with small λ "barely gains anything when relaxed", so it can be left as design freedom for later, while a constraint with large λ signals "this is the bottleneck". For inequality constraints, the multiplier of an inactive (non-binding) constraint is zero — this is the complementarity in the KKT conditions. Discarding λ as a mere by-product throws away the most valuable design information.
Finally, not checking the regularity of the constraint (that its gradient does not vanish). The Lagrange multiplier method implicitly assumes ∇g is non-zero at the optimum. Where ∇g = 0, the equation ∇f = λ∇g cannot hold and no multiplier can be defined. At points where several constraints meet, or where the constraint curve has a corner, the constraint qualification fails and the standard method does not apply directly. In real optimization, avoiding such singular points by rescaling the constraints or revising the formulation is a key technique for getting a stable solution.
How to Use
Enter coefficients for your objective function f(x,y) = ax² + by² + cxy using the sliders for aNum, bNum, cNum
Define the equality constraint g(x,y) = 0 by adjusting aRange, bRange, cRange to form a linear or nonlinear boundary
Click Optimize to solve the Lagrangian ∇f = λ∇g and display the critical point (x*, y*), optimal value f*, multiplier λ, and constraint residual
Worked Example
Minimize production cost f(x,y) = 2x² + 3y² + xy subject to material constraint x + 2y = 10. Setting aNum=2, bNum=3, cNum=1, and constraint coefficients aRange=1, bRange=2, cRange=−10 yields optimum x*=2.4, y*=3.8, with optimal cost f*=42.16 and Lagrange multiplier λ=−8.64 indicating that relaxing the constraint by one unit reduces cost by 8.64 currency units.
Practical Notes
Verify constraint residual g(x*,y*) ≈ 0; values exceeding 1e−6 indicate numerical instability in the solver
Sensitivity df*/dc equals λ: use this check to confirm the multiplier correctly quantifies shadow price in resource allocation problems like portfolio optimization or structural load distribution
For highly nonconvex objectives (large negative cNum), multiple local optima exist; simulator returns the solution nearest the initialization point