22M:28 Spring 05 J. Simon. Examples of constrained optimization using
Lagrange multipliers. Example 1. Find the point on the line 3x+4y = 5 that is
closest to ...
> 22M:28 Spring 05 J. Simon Examples of constrained optimization using Lagrange multipliers Example 1. Find the point on the line 3x+4y = 5 that is closest to the origin. First we have to agree on what is the constraint set (the line 2x+5y=6) and what is the function f(x,y) to be optimized. When working with "distance to the origin", we may as well square the distance and find maxima and minima for that. So we'll use f(x,y) = x^2 + y^2. Here is a picture of the constraint set (blue line) and several level sets of the function x^2+y^2. > with(plots):with(linalg): Warning, the name changecoords has been redefined Warning, the protected names norm and trace have been redefined and unprotected > f:=x^2+y^2; 2
2
f := x + y > g:=2*x+5*y;
g := 2 x + 5 y > ConstraintSet:=implicitplot(g=6, x=-2..2, y=-2..2, scaling=constrained, color=blue, thickness=2): > LevelSetsOfF:=contourplot(f, x=-2..2, y=-2..2, scaling=constrained, color=red): > display({ConstraintSet, LevelSetsOfF}, scaling=constrained);
> We want to find point(s) on the constraint set where the constraint line is tangent to a level curve of f. To do this, we find where grad(f) is a scalar multiple of grad(g). > VectorEquation:=grad(f,[x,y])=scalarmul(grad(g,[x,y]),lambda); VectorEquation := [2 x, 2 y] = [2 l , 5 l ] > SystemOfEquationsToSolve:={2*x=2*lambda, 2*y=5*lambda, g=6}; SystemOfEquationsToSolve := {2 y = 5 l , 2 x = 2 l , 2 x + 5 y = 6} > Usually, it is helpful to consider the possibility lambda=0 as a separate case (since sometimes the subsequent algebra involves dividing by lambda). Here, from the first two equations, lambda=0 implies y=0 and x=0; but then the third equation would be 0=6, impossible. So there are no solutions with lambda=0. The text suggests a general approach to solving these systems of equations: eliminate lambda and reduce the problem to solving a system of equations just involving x, y (, z if we are in 3-dim). Just to offer you another approach, let's keep lambda and, in fact, write everything else in terms of lambda. Equation 1 says y=(5/2)lambda. Equation 2 says x=lambda. So Equation 3 becomes > LambdaEquation:=subs(x=lambda, y=(5/2)*lambda, g=6);
LambdaEquation :=
29 l=6 2
> Lambda:=solve(LambdaEquation,lambda); 12 L := 29 > X:=Lambda; Y:=(5/2)*Lambda; X :=
12 29
Y :=
30 29
We have found that the point on the line 2x+5y=6 closest to the origin is the point (12/29, 30/29). Let's see the picture. > MinimumSquareDist:=subs(x=X,y=Y,f); MinimumSquareDist :=
36 29
> Radius:=sqrt(MinimumSquareDist); Radius :=
6 29
29
[I want to draw a circle; there is a package called "plottools" that has a built-in "circle" command. In addition to drawing big circles, we also can use this function, with a tiny radius, to draw a big "point".] > with(plottools): Warning, the name arrow has been redefined > LevelSetOfFTangentToConstraintSet:=circle([0,0], Radius): > WinningPoint:=circle([X,Y], .05, color=red, thickness=2): > display({ConstraintSet, LevelSetOfFTangentToConstraintSet, WinningPoint});
########END OF EXAMPLE 1 ############ Example 2. (Sec. 4.3 problem 5) Find max and min of for points on the plane
f(x,y,z)=xyz 2x+3y+z=6.
> f:=x*y*z; f := x y z > g:=2*x+3*y+z; g := 2 x + 3 y + z > ConstraintSet:=implicitplot3d(g=6, x=-3..3, y=-3..3, z=-3..3, style=wireframe, color=black): > LevelSet1:=implicitplot3d(f=2, x=-3..3, y=-3..3, z=-3..3, style=patchnogrid): > display({ConstraintSet, LevelSet1}, scaling=constrained, orientation=[-64,81], axes=normal);
> Each level set of f is a system of hyperboloid surfaces; except f=0 gives the set of 3 coordinate planes. We proceed using Lagrange multipliers, as before. The only difference is that the algebra is going to be more complicated. > VectorEquation:=grad(f,[x,y,z])=scalarmul(grad(g,[x,y,z]),lambda); VectorEquation := [y z, x z, x y] = [2 l , 3 l , l ] > SystemOfEquationsToSolve:={y*z=2*lambda, x*z=3*lambda, x*y=lambda, g=6}; SystemOfEquationsToSolve := {2 x + 3 y + z = 6, y z = 2 l , x z = 3 l , x y = l } > We went through this algebra in class. If lambda = 0 then two of x,y,z have to be 0, and the last equation tells what the third variable must be. If lambda is not 0, then none of x,y,z is 0. In that case, use solve the first three equations to say (yz)/2 = (xz)/3 = (xy); simplify, plug in to 3rd eq. to get... > > solve(SystemOfEquationsToSolve, {x,y,z,lambda});
{x = 0, z = 6, l = 0, y = 0}, {x = 3, l = 0, z = 0, y = 0}, {x = 0, y = 2, l = 0, z = 0}, ì 2 2ü í z = 2, x = 1, y = , l = ý 3 3þ î > The three points where lambda=0 are points where grad(f) = , critical points of f. Any critical point of f that happens to lie on the constraint set are candidates for optima on the constraint set. Here f (x,y,z) = 0 at each of the three points (0,0,6), (3,0,0), (0,2,0). So let's just remember to include value f(x,y,z)=0 as a candidate for max/min of f on the plane g=6. At the remaining point, (1, 2/3, 2), we have f(x,y,z) = > subs(x=1, y=2/3, z=2, f); 4 3 > We conclude that for points on the plane 2x+3y+z=6, the function f(x,y,z) = xyz has maximum value (4/3) attained at the point (1, 2/3, 2), and minimum value 0 attained at the three points where the plane cuts the coordinate axes. > ConstraintSet:=implicitplot3d(g=6, x=0..7, y=0..7, z=0..7, style=patchnogrid): > BigPoint:=sphere([1,2/3,2],0.2, color=green): > > LevelSet1:=implicitplot3d(f=4/3, style=wireframe, color=black):
x=0..4, y=0..4, z=0..4,
> display({ConstraintSet, LevelSet1, BigPoint}, scaling=constrained, orientation=[25,56], axes=normal);
> ######## END OF EXAMPLE 2 ######## ########END OF HANDOUT ##########