Hello! Welcome to your next lesson in the "Calculus for Optimization" module.
In our previous lesson, we explored gradient descent, a powerful iterative algorithm for finding the minimum of a function when there are no constraints on the input variables. We imagined walking down a foggy hill, always taking a step in the steepest downward direction. This is the workhorse of optimization in machine learning.
However, many real-world problems aren't so "free." What if you want to maximize your company's profit, but you have a fixed budget? What if you need to find the most efficient flight path, but you have to stay at a certain altitude? These are constrained optimization problems.
Today, we'll learn a classic and elegant analytical technique to solve them: the method of Lagrange multipliers. This is a fundamental topic for the GATE exam, combining your knowledge of multivariable calculus and problem-solving.
Our learning outcome is to solve constrained optimization problems using the method of Lagrange multipliers.
1. The Challenge: Optimization with a Catch
Let's start with a visual. Imagine the surface below represents a function we want to maximize or minimize (e.g., profit). The "level curves" on the left are like contours on a map, each one representing a constant value of the function.
Now, imagine we are not free to roam anywhere on this surface. We are restricted to a specific path, the "constraint" curve. Our goal is to find the highest and lowest points on the surface while staying on the path.

How can we find these optimal points?
2. The Geometric Insight: Tangency and Parallel Gradients
The key insight is geometric. As you move along the constraint path, the value of the function changes. The maximum (or minimum) value will occur at a point where the constraint curve just barely "kisses" or is tangent to one of the level curves of .
Why? If the constraint curve crossed a level curve, it would mean you are moving from a region of lower function value to higher (or vice-versa). Therefore, the point of crossing can't be a maximum or a minimum. The extreme value must occur at the point of tangency.
Recall from our previous lessons on partial derivatives that the gradient vector () is always perpendicular (normal) to the level curve at any given point.
- If the constraint curve
g = cand the level curvef = kare tangent at a point(x₀, y₀). - And
∇gis normal to the constraint curve. - And
∇fis normal to the level curve. - Then at that point of tangency, the two gradient vectors
∇fand∇gmust be pointing in the same (or exactly opposite) directions. That is, they must be parallel.
Two vectors are parallel if one is a scalar multiple of the other. This gives us the central equation of the Lagrange multiplier method:
Here, (the Greek letter lambda) is a scalar constant called the Lagrange multiplier.
This video provides an excellent visual and geometric explanation of this core idea.
Lagrange Multipliers | Geometric Meaning & Full Example
Watch the first part of this video, 'Lagrange Multipliers | Geometric Meaning & Full Example' by Dr. Trefor Bazett, to solidify this geometric intuition. It beautifully illustrates the relationship between level curves, the constraint, and the alignment of gradient vectors.
Watch from the beginning to 07:24. Focus on: 00:00 - 01:30: The setup of a constrained optimization problem. 01:30 - 06:40: The key insight: how the optimal point occurs where the constraint curve is tangent to a level curve, which implies their gradients are parallel. 06:40 - 07:24: How this geometric idea gives us our system of equations.
3. The Method: A Step-by-Step Strategy
The geometric insight gives us a concrete method for solving constrained optimization problems. We simply need to find the point(s) (x, y) that satisfy both our constraint and the gradient condition.
For a problem where we want to optimize subject to the constraint , we can use the following strategy.
The textbook 'Calculus (OpenStax)' provides a formal summary of the method. Please read the problem-solving strategy outlined here.
In the resource, find and read the 'Problem-Solving Strategy: Steps for Using Lagrange Multipliers'. It's located just after the formal statement of Theorem 14.8.1.
To summarize and combine with our notation, the strategy is:
- Identify Functions: Determine your objective function to be maximized/minimized and your constraint function .
- Calculate Gradients: Find the gradient of the objective function, , and the gradient of the constraint function, .
- Set Up the System of Equations: Create a system of three equations with three unknowns (, , and ):
- (from the x-component of )
- (from the y-component of )
- (the original constraint)
- Solve the System: Solve the system of equations for and . These are your candidate points for the maximum or minimum.
- Evaluate: Plug the candidate point(s) back into the original function to determine which gives the maximum and which gives the minimum value.
4. A Worked Example
Now, let's see this strategy in action. The same video we watched for the intuition also includes a complete, step-by-step algebraic solution to a problem.
Lagrange Multipliers | Geometric Meaning & Full Example
Let's continue with Dr. Bazett's video to see how to apply the 4-step strategy to solve a problem algebraically.
Watch from 07:24 to 11:58. The problem is to optimize f(x,y) = xy + 1 subject to x² + y² = 1. Observe how he: Calculates the gradients of f and g. Sets up the three equations for x, y, and λ. Solves the system to find four candidate points. Concludes by explaining that you would plug these points into f to find the actual max/min values.
Test your understanding!
You want to find the point on the line that is closest to the origin .
- What is your objective function that you want to minimize? (Hint: Minimizing distance is the same as minimizing the square of the distance.)
- What is your constraint function ?
- Set up the system of three equations you would need to solve. (You don't need to solve it yet).
Show answer
-
The squared distance from the origin to a point is . So, our objective function is .
-
The constraint is simply the equation of the line: .
-
We need the gradients:
The system of equations is:
(The solution to this system is ).
5. Extension to More Variables and Constraints
The power of Lagrange multipliers is that the logic extends easily.
-
Three Variables, One Constraint: To optimize subject to , the system simply gets one more equation from the z-component of the gradient:
You would solve this along with the constraint .
-
Two Constraints: Some problems, especially in engineering and physics, have multiple constraints. To optimize subject to both and , you introduce a second Lagrange multiplier, . The gradient condition becomes:
This will give you a system of 5 equations (3 from the gradient components, 2 from the constraints) with 5 unknowns (). While more complex to solve, the principle is the same. The OpenStax resource LINK covers this case in the section "Problems with Two Constraints" if you'd like to see a full example.
6. Practical Application: Solving with Code
Since you have an interest in coding, it's great to see how this mathematical tool connects to programming. While solving complex systems of non-linear equations can be tricky, many problems result in a system of linear equations. We can use Python's NumPy library to solve these instantly, bypassing tedious manual algebra.
This is a very practical skill for an engineer. Let's see how it's done.
Lagrange multipliers and constrained optimization
This resource, a Jupyter notebook from Duke University, shows how to solve the system of equations from a Lagrange multiplier problem using NumPy.
Read through 'Example 1' and 'Example 2' in this resource. You don't need to run the code yourself, but focus on understanding the process: Take the partial derivatives to get the system of equations. Arrange this linear system into the matrix form Ax = b. Use a library function (like NumPy's solve) to find the solution vector x.
This approach powerfully combines your calculus knowledge with your coding skills to solve complex problems efficiently.
Conclusion
You've done an excellent job today! We have added a powerful new tool to your optimization toolkit. Where gradient descent helps us find optima in unconstrained spaces, Lagrange multipliers provide an elegant, analytical way to handle optimization with strict equality constraints.
Key Takeaways:
- Constrained optimization seeks to find the maximum or minimum of a function subject to certain conditions or constraints.
- The geometric intuition is that at an optimal point, the constraint curve is tangent to a level curve of the objective function.
- This tangency implies that the gradient vectors are parallel: .
- This single vector equation, combined with the constraint equation, creates a system of equations that we can solve to find candidate points for the optima.
- The method extends to more variables and multiple constraints.
- For problems that result in a linear system, we can use tools like NumPy to find the solution computationally.
Preview of the Next Lesson:
We are now at the end of the "Calculus for Optimization" module. You have learned about derivatives, partial derivatives, finding optima, and methods for both unconstrained (gradient descent) and constrained (Lagrange multipliers) problems.
In our next lesson, we will change gears and begin the next major topic in calculus: integration. We will start with the Fundamental Theorem of Calculus, which establishes the profound link between differentiation and integration.