A minimum with a constraint
Lesson 4.4.1 found the lowest point of a function where its gradient is . Often the point may not move freely. It must satisfy an equation , called a constraint: is a function and a fixed number.
Take the bowl and the constraint , so and . The bowl's lowest point, the origin, is not on the line. The question is which point of the line gives the smallest .
On a line you can substitute. Put , and becomes a function of one variable, . Its slope is 0 at (lesson 2.4.1), so the best point is and the smallest value is 6.
| 0 | 1 | 1.5 | 2 | 2.5 | 3 | |
|---|---|---|---|---|---|---|
| 3 | 2 | 1.5 | 1 | 0.5 | 0 | |
| 18 | 9 | 6.75 | 6 | 6.75 | 9 |
Substitution needs the constraint solved for one variable. On a circle that brings in square roots, and with many variables it gets long. Lagrange's method handles both, and it starts from a picture.
The level curve touches the constraint
The level curves of are the ellipses (lesson 4.1.1). Walk along the line from : the ellipses you cross get smaller, down to at , then grow again.
At the line does not cross its level curve: it touches it. A crossing would mean smaller ellipses on one side, and so lower values further along the line.
On the line, the smallest level curve that still meets it, , touches it at : there and are parallel. On the circle two level lines touch, at the largest and the smallest value.
Gradients say the same thing. Lesson 4.2.1 showed that is at right angles to the level curve of . The line is a level curve of , so is at right angles to the line.
Where the two curves touch, both gradients are at right angles to the same line, so they are parallel. Elsewhere has a part along the line, and a step along the line changes :
- At , . Its slope along the unit direction is , so stepping that way lowers .
- At , . Its slope along the line is 0.
- At , and the slope is : falls the other way.
Rule: Lagrange's condition
At a best point on the constraint, the gradient of is a multiple of the gradient of . The number (lambda) is the Lagrange multiplier. The second line keeps the point on the constraint.
Try it
The loop prints the table above. The second loop prints at three points of the line and its slope along the line, with (lesson 4.2.1).
Only at is that slope 0.
The explorer keeps the point on the line . Set to 3 and slide the point until and are parallel: near , with close to 4. Then move and find the best point again: shrinks to 0 at and turns negative below it.
Explore
The point can move only along the line x + y = c. Slide it and watch the two gradients: at the lowest point on the line they point the same way, and the level curve just touches the line.
- Point
- (1, 1), f = 3
- Gradient ∇f (dark arrow)
- (2, 4), length 4.472
- ∇g (green arrow)
- (1, 1), at right angles to the line
- So
- ∇f has a part along the line, so moving along the line still changes f.
The Lagrangian
Lagrange's condition in components is three equations for three unknowns: , and . One function collects them, the Lagrangian.
Rule: The Lagrangian
Set all three partial derivatives of to 0. The ones in and give ; the one in gives back . With variables and one constraint there are equations.
For the bowl, , and the equations are
The first two give and . Then , so and the point is , as substitution found.
Try it
SymPy builds , takes its three partial derivatives and solves them together. It writes as lambda.
Each equation is printed as an expression that must equal 0.
A maximum and a minimum on a circle
Now find the largest and smallest values of on the circle . Substitution would need . Lagrange's method does not.
Here and . The equations are , and . The first shows , so dividing the second by the first is safe and gives .
Then , so or . There are two candidates:
- , with and : the maximum.
- , with and : the minimum.
The condition picks out every point where a level line touches the circle, the top and the bottom alike. Comparing the values of tells which is which. In the figure, and point the same way at the maximum and opposite ways at the minimum.
Try it
SymPy solves the three equations and finds both candidates. Then a scan of 3,601 points round the circle, one every tenth of a degree, confirms the largest and smallest values.
Three variables: a linear system
The method works the same way in more variables. Minimise subject to . The gradient condition gives , and , and the constraint is a fourth equation.
All four are linear in , , and , so they form a system (lesson 3.3.1):
Its solution is , , and , and the smallest value is . Notice that , and all equal the same number, .
A quadratic function with linear constraints always gives a linear system like this. Case study 4.5.5 solves one for a portfolio.
Try it
np.linalg.solve solves the system (lesson 3.3.1). The second line checks that , and are all equal to , and the last compares the smallest value with .
What measures
Write for the best value on the constraint . Then is the rate at which changes as changes. Its units are units of per unit of .
Rule: The multiplier as a rate
Move the constraint from to and the best value changes by about . Economists call a shadow price: what one more unit of is worth.
For the bowl on , the same steps as before give the best point , so and .
| best point | |||
|---|---|---|---|
| 1.5 | |||
| 0 | 0 | 0 | |
| 2 | 2.6667 | 2.6667 | |
| 3 | 6 | 4 |
From to , rises by 0.4067, close to . The central difference gives 4: for a parabola the central difference is exact (lesson 2.8.1).
The sign has a meaning too:
- Positive: raising moves the line away from the origin, and the best value rises.
- Negative: at , raising moves the line towards the origin, and the best value falls.
- Zero: at the line passes through the bowl's own lowest point. The constraint costs nothing, and there.
On the circle the largest value of is , by the same steps. At its slope is , the multiplier at the maximum. At the largest value is 5.0498, a rise of 0.4975 per unit of .
Try it
best(c) returns the best point, and for the bowl. The loop prints the table.
The next line prints the change from to 3.1 and the central difference. The last prints at and its rate of change from .
Where the method needs care
It finds candidates, not answers. On the circle only their values told the maximum from the minimum. On , the product has one candidate, , and it is the largest value, not the smallest.
A best point may not exist. The bowl has no largest value on : and . Lagrange's method still returns its one candidate, , which is the smallest value.
The constraint needs . Write the same line as . Now , which is at every point of the line. At the condition would read , which no satisfies, so the method misses the answer.
Try it
The first line evaluates the bowl far along the line: it grows without limit.
Then for the badly written constraint, next to at .
Lesson 4.5.2 allows inequalities, such as , and lesson 4.5.3 says when a single candidate is sure to be the answer.
Common mistakes
- Keep the constraint as an equation. alone has too many unknowns; is the last of the equations.
- Compare the candidates. The condition holds at a maximum as well as at a minimum: on the circle, gives 5 and gives .
- Check that a best value exists. The bowl has a smallest value on a line but no largest one.
- Do not divide by something that may be 0. On the circle, showed before dividing.
- Write the constraint so that . is the right line with a useless gradient.
- Mind the sign convention. Some books write , which flips the sign of . Here , so .
Lagrange multipliers
0 of 12 correct
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
How to type answers
- Powers with
^:x^7,2^(1/3),a^-2. - Multiply with
*or by writing side by side:3x^2,2(x+1). ln(x)is the natural log,log(x, 2)is log base 2,eis Euler's number,sqrt(x)is a square root.sin(x),cos(x)andtan(x)take radians; writepifor π andsin(x)^2for sin²x.- Your first answer to each question is saved: it counts towards your score and brings the question back for review.
- Exact answers like
ln(20)/ln(3)are best. Decimals need about 4 significant figures.
Summary card
- To find the best value of on a constraint : at the best point and . The level curve of touches the constraint there.
- The Lagrangian : set every partial derivative to 0. With variables and one constraint that is equations; for a quadratic and a linear constraint they form a linear system.
- The solutions are candidates. Compare their values, and check that a best value exists.
- : the rate at which the best value changes with , in units of per unit of . when the constraint costs nothing.
- The method needs at the best point.