Lagrange Multipliers with Maple

The interactive version of this worksheet is in MSB 203 (xserve) in the Server1 volume.

The method of Lagrange multipliers applies to constrained optimization problems, in which the object is find the maximum and minimum of a function f : ® on a level surface g ( x , y , z ) = k in . Maple's built-in routine for solving systems of equations can be very helpful with such problems, because Lagrange's method requires finding the solutions of a system of equations

(*)
Ñ f ( x , y , z ) = Ñ g ( x , y , z )
g ( x , y , z ) = k

in the variables
x , y , z , and . Since both gradient vectors have three coordinates, this is a system of four equations in four unknowns. Hand solution of such systems usually requires some insight in eliminating variables, and the complexity of the associated algebra can be daunting. While Maple's solve command can't solve every such system, it can usually handle exercises that arise in multivariable calculus texts, because their authors designed them to be solvable by hand.

Example 1 . As of June, 2001, FedEx Corporation's size limitations on rectangular boxes are maximum dimension (called the length of the box) at most 119 inches, and girth (length plus the perimeter of the ends) at most 165 inches. What is the box of maximum volume that FedEx will accept for shipment?

Solution. Let z denote the maximum dimension of the box in inches, and let x and y denote the other two dimensions in inches. Then the problem is to maximize V = f ( x , y , z ) = xyz subject to 0 ² z ² 119, 0 ² x ² z , 0 ² y ² z , and 2 x + 2 y + z ² 165. Note that to get the most out of the constrained variables x , y and z , replace the last inequality by the corresponding equation. That defines the level surface g ( x , y , z ) = 165 for the function g , where

.

This problem is feasible for hand solution, but Maple provides a handy way to check the solution x = y = 27.5, z = 55 from hand implementation of the Lagrange-multiplier method. The next routine uses Maple's partial differentiation commands diff(f(x, y), x) , diff(f(x, y), y) and diff(f(x, y), z) to produce the system of equations (*) above. (This step would prove valuable in case of a discrepancy with a hand-computed answer: it checks all the partial differentiation that arises in the hand solution.) Note that since this routine doesn't put the partial derivatives together into the gradient vector, invoking the linalg package is not necessary.

> f := (x, y, z) -> x*y*z;
g := (x, y, z) -> 2*x + 2*y + z;
f_x := (x, y, z) -> diff(f(x, y, z), x):
f_y := (x, y, z) -> diff(f(x, y, z), y):
f_z := (x, y, z) -> diff(f(x, y, z), z):
evalf(f_x(x, y, z));
evalf(f_y(x, y, z));
evalf(f_z(x, y, z));
g_x := (x, y, z) -> diff(g(x, y, z), x):
g_y := (x, y, z) -> diff(g(x, y, z), y):
g_z := (x, y, z) -> diff(g(x, y, z), z):
evalf(g_x(x, y, z));
evalf(g_y(x, y, z));
evalf(g_z(x, y, z));

The next routine asks Maple to solve the system of equations (*) for the
f and g in this problem.

> solve( { evalf(f_x(x, y, z))= lambda*evalf(g_x(x, y, z)),
evalf(f_y(x, y, z))= lambda*evalf(g_y(x, y, z)),
evalf(f_z(x, y, z))= lambda*evalf(g_z(x, y, z)),
g(x, y, z) = 165});

Maple finds all solutions to the system, even those for which a variable is 0, which of course are not relevant in designing an actual box. Only the last solution has all positive dimensions, and it does agree with the solution obtained by hand calculation. The following example checks the hand solution to Exercise 16, Section 15.8, of
Calculus, 4th Edition, by James Stewart (ITP Brooks/Cole, 1999).

Example 2 . Find the maximum and minimum of f ( x , y , z ) = subject to the two constraints and .

Solution. Proceeding as before, re-code the formulas for f and g , add a the second constraint function h ( x , y , z ) = to the routine above, and then execute the edited routine:

> f := (x, y, z) -> 3*x - y - 3*z;
g := (x, y, z) -> x + y - z;
h := (x, y, z) -> x^2 + 2*z^2;
f_x := (x, y, z) -> diff(f(x, y, z), x):
f_y := (x, y, z) -> diff(f(x, y, z), y):
f_z := (x, y, z) -> diff(f(x, y, z), z):
evalf(f_x(x, y, z));
evalf(f_y(x, y, z));
evalf(f_z(x, y, z));
g_x := (x, y, z) -> diff(g(x, y, z), x):
g_y := (x, y, z) -> diff(g(x, y, z), y):
g_z := (x, y, z) -> diff(g(x, y, z), z):
evalf(g_x(x, y, z));
evalf(g_y(x, y, z));
evalf(g_z(x, y, z));
h_x := (x, y, z) -> diff(h(x, y, z), x):
h_y := (x, y, z) -> diff(h(x, y, z), y):
h_z := (x, y, z) -> diff(h(x, y, z), z):
evalf(h_x(x, y, z));
evalf(h_y(x, y, z));
evalf(h_z(x, y, z));

Next, have Maple solve the system of equations

Ñ f ( x , y , z ) = Ñ g ( x , y , z ) + Ñ g ( x , y , z )
, = 1.

> (ans1, ans2) := solve( {
evalf(f_x(x, y, z))= lambda*evalf(g_x(x, y, z)) + mu*evalf(h_x(x, y, z)),
evalf(f_y(x, y, z))= lambda*evalf(g_y(x, y, z)) + mu*evalf(h_y(x, y, z)),
evalf(f_z(x, y, z))= lambda*evalf(g_z(x, y, z)) + mu*evalf(h_z(x, y, z)), g(x, y, z) = 0, h(x, y, z) = 1
} );

> subs(ans1, f(x, y, z));

> subs(ans2, f(x, y, z));

From this, Maple's answers for extreme-value points and the corresponding maximum and minimum of
f subject to the two constraints are clear. How do they compare to the values found by hand? Hand computation gives minimum at x = , y = , z = and maximum at the point with the negative of those three values. Maple converts those numbers to floating-point numbers that agree with the output from its solve command.

> evalf([-2/sqrt(6), 3/sqrt(6), 1/sqrt(6), -2*sqrt(6)]);

Maple TM is a registered trademark of Waterloo Maple Inc.
Math rendered by WebEQ