# Maxs and Mins Subject to a Constraint

Consider the following problem:

A cardboard box without a lid is to have a volume of 12,000 cm3. Find the dimensions that minimize the ammount of cardboard used.

If we denote by x,y,z the legth, width and hight of the box we see that the volume V and the area A of the box are given by:

> V := x*y*z; A := 2*x*z + 2*y*z + x*y;

```                                   V := x y z

A := 2 x z + 2 y z + x y```
 The problem becomes that of minimizing A (the ammount of cardboard used) subject to V=k (the volume being fixed at k=12,000cm3). Using the constraint V=k we can solve for z and substitute back this value of z into the expression for the area A. In this way the problem gets reduced to the unconstrained minimization of the area A but now in only two variables x and y. The value of z can later be determined by using the optimal values for x and y. Maple can handle substitutions very easily. We can do it this way:

> k := 12000; z := k/(x*y); New_A := A;

```                                   k := 12000

12000
z := -----
x y

24000   24000
New_A := ----- + ----- + x y
y       x```
 Let's now solve for the partials equal zero, with:

> solve({diff(New_A,x), diff(New_A,y)},{x,y});

```                             3                        3
{y = RootOf(_Z  - 24000), x = RootOf(_Z  - 24000)}```
 Recall that to see all the solutions (when maple answers with the cryptic RootOf function) you do:

> All := allvalues(");

```                      1/3           1/3                     1/3
All := {x = 24000   , y = 24000   }, {x = %2, y = 24000   },

1/3             1/3
{x = %1, y = 24000   }, {x = 24000   , y = %2}, {x = %2, y = %2},

1/3
{x = %1, y = %2}, {x = 24000   , y = %1}, {x = %2, y = %1},

{x = %1, y = %1}

1/3          1/2      1/3
%1 :=               - 1/2 24000    - 1/2 I 3    24000

1/3          1/2      1/3
%2 :=               - 1/2 24000    + 1/2 I 3    24000```
 Don't PANIC!!! Maple (as usual) tries to be clever and gives you ALL the solutions, including the complex solutions (do you see the I?). Notice that only real solution is the first element of the list of set of solutions, i.e.,

> sol := All[1];

```                                       1/3           1/3
sol := {x = 24000   , y = 24000   }```
 which evalf to,

> evalf(sol,3);

`                              {x = 28.8, y = 28.8}`

## Solution with Lagrange Multipliers

When we need to find the max (or min) of a function of several variables subject to a constraint of the form V=k, there is an alternative technique to plain substitution. This method is known as the method of Lagrange multipliers.

It is easier to visualize the reasoning behind this technique if we first consider a function of two variables f(x,y), say, that needs to be maximized subject to a constraint of the form g(x,y)=k.

as the picture shows to maximize the function f we need to move from one level curve of f to another of higher level until we touch the constraint (g(x,y)=k) for the last time. In the figure this happens when f is at level 8. As one can see from the picture, the solution (x0,y0) is characterized by the fact that at that point the level curve of f at level 8 and the level curve of g at level k (i.e. the constraint) are tangents to each other. In other words at this point the gradients of f and g are parallel to each other. Don't forget that the gradient is always perpendicular to the level curves.

Algebraically this is,

```           @             @                    @             @
[ ---- f(x, y), ---- f(x, y) ] = t [ ---- g(x, y), ---- g(x, y) ]
@x            @y                   @x            @y```
 where t is a scalar. This gives 2 equations and three unknowns, x,y and the number t. The fact that the point must satisfy the constraint i.e. g(x,y)=k gives the other equation. The scalar t above is known as a lagrange multiplier for the constraint g(x,y)=k, and the method that finds the maximum (or min) of f subject to the constraint g=k by finding when the grad(f) is parallel to the grad(g) is called the method of Lagrange multipliers. Let us apply this method to the cardboard problem above. We had:

> z := 'z': A := A; V := V;

```                            A := 2 x z + 2 y z + x y

V := x y z```
 and we want to minimize A subject to V=k=12000. We need to solve the 4 equations eq1,eq2,eq3,eq4 given by:

> eq1 := diff(A,x) = t*diff(V,x); eq2 := diff(A,y) = t*diff(V,y);
> eq3 := diff(A,z) = t*diff(V,z); eq4 := V = k;

```                             eq1 := 2 z + y = t y z

eq2 := 2 z + x = t x z

eq3 := 2 x + 2 y = t x y

eq4 := x y z = 12000```
 solving these set of equations in the 4 unknowns x,y,z and t,

> solve({eq1,eq2,eq3,eq4},{x,y,z,t});

```                                2
{t = 1/1500 %1 , x = 2 %1, y = 2 %1, z = %1}

3
%1 :=                        RootOf(_Z  - 3000)```
> S := ": aS := allvalues(S);
```                1/3            1/3            1/3
aS := {z = 3000   , y = 2 3000   , x = 2 3000   , %5},

.... plus many, many other solutions with complex numbers...

2/3
%5 :=                        t = 1/1500 3000```
 and as you can see we get the same answer as before.

Link to the commands in this file
Carlos Rodriguez <carlos@math.albany.edu>