Sections18-04.mws

Unit 4: Vector Calculus

Chapter 18: The Gradient Vector

Sections18.4: Lagrange multipliers

Copyright

Copyright * 2001 by Addison Wesley Longman, Inc.

All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior written permission of the publisher. Printed in the United States of America.

Initializations

> restart;

> with(plots):
with(plottools):
with(linalg):
with(student):
read(`C:/Program Files/Maple 6.01/pvac.txt`):

Warning, the name changecoords has been redefined

Warning, the protected names norm and trace have been redefined and unprotected

>

Constrained Optimization

Elementary calculus considers constrained optimization problems of the form

Find the rectangular box of maximum area if the perimeter is fixed at 100.

Initially, these problems sre solved by eliminating one of the variables. For example, with the area of the box written as

> A := x*y;

A := x*y

>

and the perimeter constraint as

> P := 2*x + 2*y = 100;

P := 2*x+2*y = 100

>

the constraint can be solved, for y = 50-x , that is, for

> Y := solve(P,y);

Y := -x+50

>

so that

> Ax := subs(y=Y,A);

Ax := x*(-x+50)

>

The ordinary techniques of differentiation (or plotting) now lead to the maximum of 625 at x = y = 25, as we see from the graph

> plot(Ax,x=0..50);

[Maple Plot]

>

or by the analytical device of

> X[max] := solve(diff(Ax,x)=0,x);

X[max] := 25

>

The corresponding y -coordinate is

> Y[max] := subs(x=X[max],Y);

Y[max] := 25

>

The rectangle is a square of side 25, and area 25^2 = 625 .

>

The Lagrange Multiplier Method

The Lagrange multiplier technique is an alternate method for solving constrained optimization problems. By making an elegant application of the gradient vector, it avoids the algebra of using the constraint to eliminate one of the variables. The method of Lagrange multipliers is illustrated through the following five examples.

>

Example 18.7

In this first example, we show how the Lagrange-multiplier method selects points at which the level curves of the objective function f(x,y) are tangent to the constraint curve. To do this, we will find the extreme value of f(x,y) = x*y along the constraining ellipse g(x,y) = x^2+4*y^2-8 .

The objective function (what is maximized or minimized) is therefore

> f := x*y;

f := x*y

>

and the constraint curve is defined by the function

> g := x^2 + 4*y^2 - 8;

g := x^2+4*y^2-8

>

with the actual constraint equation being

> constraint_equation := g = 0;

constraint_equation := x^2+4*y^2-8 = 0

>

Figure 8.11 shows the constraint ellipse in the xy -plane, along with the surface z = f(x,y) .

> a := sqrt(8):
b := sqrt(2):
xt := a*cos(t):
yt := b*sin(t):
ft := subs({x=xt, y=yt},f):
p1 := spacecurve([xt,yt,.01], t=0..2*Pi, color = black, thickness = 3):
p2 := plot3d(f,x=-2..2,y=-2..2,style=hidden,color=black):
p3 := plot3d(0, x = -a..a, y = -2..2, color = cyan, style=wireframe):
display([p||(1..3)],scaling=constrained, tickmarks=[3,3,3], labels=[`x `,` y`,`z `], labelfont=[TIMES,ITALIC,12],axes=frame, orientation =[-25,70]);

[Maple Plot]

>

By way of interpretation, imagine being restricted to walking on this ellipse in the xy -plane. Overhead, the function f(x,y) determines the shape of the ceiling. You want to know where, on the elliptic path being walked, you will find the highest and lowest points of the ceiling.

The interSectionsof the cylinder g(x,y) = 0 and the surface z = f(x,y) is the space curve (thin black) shown in Figure 18.12, below. Included is a rendition of the ellipse (thick black) in the xy -plane. Clearly, there are two maxima and two minima.

> p1 := spacecurve([xt,yt,ft], t=0..2*Pi, color = black, thickness = 3):
p2 := spacecurve([xt,yt,0], t=0..2*Pi, color = black, thickness = 6):
p3 := plot3d(0, x = -a..a, y = -2..2, color = cyan, style=wireframe):
dots:=spacecurve({[[-3,-2,-2],[-3,2,-2],[3,2,-2]]}, color=black, linestyle=2):
display([p||(1..3),dots], axes = frame, orientation = [45,70], scaling = constrained, labels=[` x`,`y `,`z `], labelfont=[TIMES,ITALIC,12], tickmarks=[5,5,5], orientation=[-40,75]);

[Maple Plot]

>

Maple Graphics - Generating Figure 8.12

Write the constraint ellipse in standard form by dividing through by 8 so that the form

x^2/(a^2)+y^2/(b^2) = 1

is obtained.

> constraint_equation/8;

1/8*x^2+1/2*y^2-1 = 0

>

In this form, we realize that

> a := sqrt(8);
b := sqrt(2);

a := 2*sqrt(2)

b := sqrt(2)

>

and a convenient parametric form of the ellipse is then

> xt := a*cos(t);
yt := b*sin(t);

xt := 2*sqrt(2)*cos(t)

yt := sqrt(2)*sin(t)

>

The function f(x,y) , evaluated along this ellipse, is then

> ft := subs({x=xt, y=yt},f);

ft := 4*cos(t)*sin(t)

>

Plot the heights of f(x,y) above the ellipse as a space curve. Include the ellipse itself in the figure.

> p1 := spacecurve([xt,yt,ft], t=0..2*Pi, color = black, thickness = 3):
p2 := spacecurve([xt,yt,0], t=0..2*Pi, color = green, thickness = 6):
p3 := plot3d(0, x = -a..a, y = -2..2, color = red, style=wireframe):
display([p||(1..3)], axes = boxed, orientation = [45,75], scaling = constrained);

[Maple Plot]

>

The green path is the constraint ellipse along which we walk. The black curve is the contour of the ceiling directly above the constraint ellipse.

>

The contour map in Figure 8.13 is yet another way of looking at this problem. The level curves of f(x,y) (black) and the constraint ellipse (red) are all drawn in the xy -plane.

> gx := implicitplot(g = 0, x = -3..3, y = -2..2, color=red):
p1 := contourplot(f, x =-3..3, y = -3..3, contours = [1,2,3,-1,-2,-3], color = black):
p2 := textplot({[-2.1,2.5,`f(x,y) < 0`],[2.2,2.5,`f(x,y) > 0`],
[-2.1,-2.5,`f(x,y) > 0`], [2.2,-2.5,`f(x,y) < 0`], [0,1,`g(x,y) = 0`]}, font=[TIMES,ROMAN,12]):
p3 := display([p1,p2,gx], axes = boxed, scaling = constrained, xtickmarks=7, ytickmarks=[-1,0,1], labels=[x,`y `], labelfont=[TIMES,ITALIC,12]):
p3;

[Maple Plot]

>

Where a level curve of f(x,y) cuts through the graph of g = 0, there can't be a stationary value.

Where the level curve of f(x,y) is tangent to the graph of g = 0, the value of f(x,y) becomes stationary because it "pauses" momentarily.

The method of Lagrange multipliers seeks points were the level curves of f are tangent to the constraint curve g = 0 .

It does this by looking for points where the gradient vectors of f and g are colinear. This happens where

grad(f) = MATRIX([[y], [x]])

is a multiple of

grad(g) = MATRIX([[2*x], [8*y]])

where we obtain the gradients in Maple as

> gf := grad(f, [x,y]);
gg := grad(g, [x,y]);

gf := MATRIX([[y], [x]])

gg := MATRIX([[2*x], [8*y]])

>

Figure 18.14 (below) shows the gradient vectors at the four points where they are colinear.

> gft := subs({x=xt,y=yt},op(gf)):
ggt := subs({x=xt,y=yt},op(gg)):
f1 := z -> arrow(subs(t=z,[xt,yt]), subs(t=z,op(gft)),.2,.4,.3, color=black):
f2 := z -> arrow(subs(t=z,[xt,yt]), subs(t=z,op(ggt)),.2,.4,.3, color=cyan):
f3 := z -> display([f1(z),f2(z)]):
p4 := display([f3(Pi/4),f3(3*Pi/4),f3(5*Pi/4),f3(7*Pi/4)]):
display([p4,gx,p1], scaling=constrained, axes=box, xtickmarks=7, ytickmarks=[-1,0,1], labels=[x,`y `], labelfont=[TIMES,ITALIC,12]);

[Maple Plot]

>

We can do better with the live medium of a computer screen. We can simulate a trip around the ellipse, showing the two gradient vectors encountered at each point. Thus, we evaluate grad(f) and grad(g) on the ellipse via

> gft := subs({x=xt,y=yt},op(gf));
ggt := subs({x=xt,y=yt},op(gg));

gft := MATRIX([[sqrt(2)*sin(t)], [2*sqrt(2)*cos(t)]...

ggt := MATRIX([[4*sqrt(2)*cos(t)], [8*sqrt(2)*sin(t...

>

then generate the following animation. The constraint ellipse is drawn in red, but grad( g ) is drawn in cyan. The level curves of f are drawn in black, as is grad( f ).

> f1 := z -> arrow(subs(t=z,[xt,yt]), subs(t=z,op(gft)),.2,.4,.3, color=black):
f2 := z -> arrow(subs(t=z,[xt,yt]), subs(t=z,op(ggt)),.2,.4,.3, color=cyan):
f3 := z -> display([f1(z),f2(z)]):
p3 := display([seq(f3(.05*Pi*k),k=0..39)],insequence=true):
display([gx,p1,p3], scaling=constrained);

[Maple Plot]

>

This example shows two things. First, grad(f) (black) never equals grad(g) (cyan) which is considerably longer than grad(f) . Second, at two points of tangency, grad(f) points in the same direction as grad(g) , but at the other two points of tangency, the two gradients point in exactly opposite directions.

The analytic condition which expresses the colinearity of the gradients is

grad(f) = lambda*grad(g)

which stands for the pair of component equations

f[x] = lambda*g[x]

f[y] = lambda*g[y]

The factor of proportionality, lambda , is called the Lagrange multiplier .

There are three unknowns, namely, the coordinates x and y , and the Lagrange multiplier lambda . These are determined by the two equations in

grad(f) = lambda*grad(g)

and the constraint equation

g(x,y) = 0

Table 18.1 lists the equations, the solutions, and the value of f(x,y) at each extreme point.

Equations lambda ``(x,y) f(x,y)

=========== === ====== =====

x = 8*lambda*y 1/4 ``(2,1) 2

y = 2*lambda*x 1/4 ``(-2,-1) 2

x^2+4*y^2 = 8 -1/4 ``(2,-1) -2

-1/4 ``(-2,1) -2

__________________________________________________

Table 18.1

The solutions in Table 18.1 are obtained in Maple as follows.

The simplest way to equate coefficients of the two gradient vectors is with the equate command from the student package. Note how we use the notationally simpler Lagrange multiplier m instead of lambda .

> q := equate(gf, m*gg);

q := {x = 8*m*y, y = 2*m*x}

>

So far, we have two equations in the three unknowns x , y , and the Lagrange multiplier m .

The third equation is the constraint equation g = 0. Consequently, we solve three equations in three unknowns. (Note that the first two equations are already in a set called q , so to insert the remaining equation into that set, use the operator for the union of sets, thereby generating a set of three equations.)

> q1 := solve(q union {g = 0}, {x, y, m});

q1 := {m = 1/4, y = 1, x = 2}, {m = 1/4, y = -1, x ...

>

There are 4 possible extreme values corresponding to the critical points (2, 1), (-2, -1), -2, 1), and (2, -1)

> for k from 1 to 4 do
P||k := subs(q1[k],[x,y]);
od;

P1 := [2, 1]

P2 := [-2, -1]

P3 := [2, -1]

P4 := [-2, 1]

>

The values of f(x,y) at the critical points are

> for k from 1 to 4 do
subs(q1[k],[[x,y],f]);
od;

[[2, 1], 2]

[[-2, -1], 2]

[[2, -1], -2]

[[-2, 1], -2]

>

Hence, there are two places on the constraint curve g(x,y) = 0 where f(x,y) attains a maximum and two places on the constraint curve where f(x,y) attains a minimum.

f(2,1) = 2

f(-2,-1) = 2

f(-2,1) = -2

f(2, -1) = -2

>

Example 18.8

In this second example, we will find the Lagrange multiplier is zero, so an extreme point occurs where the constraint is not operative. The extreme point is on the constraint, but it is also a critical point for the unconstrained objective function. To illustrate this, we find the extreme values of

> f := x^2*y;

f := x^2*y

>

subject to the constraint g(x,y) = 0 , where g(x,y) is given by

> g := x + y -3;

g := x+y-3

>

Figure 18.15 (below) shows the surface z = f(x,y) , the xy -plane, constraint (line) g(x,y) = 0 , and the interSectionsof the plane

x+y = 3

with the surface z = f(x,y) .

> F := subs(y = 3-x,f):
p4 := spacecurve([x,3-x,F, x = -2..4], color = black, thickness=3):
p5 := spacecurve([x,3-x,0,x=-2..4], color = red, thickness=3):
p6 := plot3d(0,x=-2..4,y=-1..5,color=cyan):
p7 := plot3d(f,x = -2..4,y=-1..5,color=black,style=wireframe):
display([p||(4..7)],axes=frame, labels=[x,y,z], view=[-2..4,-1..5,-2..20], orientation=[-60,60], tickmarks=[7,7,5], labels=[`x `,` y`,`z `], labelfont=[TIMES,ITALIC,12]);

[Maple Plot]

>

The space curve in which the plane x+y = 3 intersects the surface, namely,

> Z := subs(y = 3-x,f);

Z := x^2*(3-x)

>

is shown in Figure 18.16.

> plot(Z, x = -2..4, color=black, xtickmarks=7, ytickmarks=8, labels=[x,`z `], labelfont=[TIMES,ITALIC,12]);

[Maple Plot]

>

It suggests f(x,y) has extreme values at x = 0 and x = 2 along the constraint line.

Figure 18.17 shows a contour plot of the surface z = f(x,y) and in red, a graph of the constraint line g(x,y) = 0 .

> p8 := contourplot(f, x = -2..4, y = -1..5, contours = [0,.1,1,2,3,4,5,6,7,8], color = black):
p9 := implicitplot(g,x=-2..4,y=-1..5,color=red):
display([p8,p9], axes=boxed, xtickmarks=7, ytickmarks=7, labels=[x,`y `], labelfont=[TIMES,ITALIC,12],scaling=constrained);

[Maple Plot]

>

This view suggests we will find extreme values of f(x,y) at x = 0 and x = 2.

This view is significant since it shows but a single point of tangency at about (2,1).

Table 18.2 lists the equations grad(f) = lambda*grad(g), g(x,y) = 0 , and the solutions for lambda, x , and y , and the values of f(x,y) at the extreme points.

Equations lambda ``(x,y) f(x,y)

======== === ===== ======

2*x*y = lambda 4 ``(2,1) 4

x^2 = lambda 0 ``(0,3) 0

x+y = 3

_____________________________________________

Table 18.2

When lambda = 0 , the constraint does not apply. The corresponding extreme point is a critical point for the unconstrained optimization problem, and would have been found without the constraint. That is why Figure 18.17 shows but one point of tangency between the constraint line and the level curves of the objective function f(x,y) .

Carrying out the computations in Maple, we have the gradient vectors of both f(x,y) and g(x,y) given by

> gf := grad(f, [x,y]);
gg := grad(g, [x,y]);

gf := MATRIX([[2*x*y], [x^2]])

gg := MATRIX([[1], [1]])

>

I f we then equate the components of grad(f) with a multiple of grad(g) , we obtain

> q := equate(gf, m*gg);

q := {2*x*y = m, x^2 = m}

>

Solving three equations in three unknowns, we obtain

> q1 := solve(q union {g = 0}, {x, y, m});

q1 := {m = 0, x = 0, y = 3}, {y = 1, x = 2, m = 4}

>

There are two critical points:

(2, 1) with m = 4

(0, 3) with m = 0

with corresponding function values

> subs(q1[1],[[x,y],f]);
subs(q1[2],[[x,y],f]);

[[0, 3], 0]

[[2, 1], 4]

>

Example 18.9

In this third example, the objective function must be constructed from a verbal description provided by the problem statement, namely, the requirement to find the (shortest) distance from the origin to the plane 2*x+y-z = 5 .

Thus, the quantity to be minimized is the distance from the origin to a point ( x, y, z ) on the plane. It's generally easier, however, to miminize the square of the distance.

The constraint g(x,y,z) = 0 is the equation of the plane. We herefore have

> f := x^2 + y^2 + z^2;

f := x^2+y^2+z^2

>

and

> g := 2*x + y - z - 5;

g := 2*x+y-z-5

>

The gradients of f(x,y,z) and of g(x,y,z) are, respectively,

> gf := grad(f, [x,y,z]);
gg := grad(g, [x,y,z]);

gf := MATRIX([[2*x], [2*y], [2*z]])

gg := MATRIX([[2], [1], [-1]])

>

There are three equations in grad(f) = lambda*grad(g) , and four unknowns, namely, x, y, z , and lambda . Table 18.3 lists the four equations and their single solution.

Equations lambda ``(x,y,z) sqrt(f(x,y,z))

============ === ========== ===========

2*x = 2*lambda 5/3 ``(5/3,5/6,-5/6) 5/sqrt(6)

2*y = lambda

2*z = -lambda

2*x+y-z = 5

________________________________________________________

Table 18.3

To obtain the equations grad(f) = lambda*grad(g) in Maple, equate the components of grad(f) with a multiple of grad(g) , using the more convenient letter m instead of lambda . We obtain

> q := equate(gf, m*gg);

q := {2*x = 2*m, 2*y = m, 2*z = -m}

>

Solve four equations in the four unknowns x, y, z, m , again using the union command to add the constraint equation to the set of three equations obtained from the gradient condition. We find

> q1 := solve(q union {g = 0}, {x,y,z,m});

q1 := {y = 5/6, m = 5/3, z = -5/6, x = 5/3}

>

Evaluate the distance sqrt(f(x,y,z)) at the critical point, obtaining

> simplify(subs(q1,[[x,y,z],sqrt(f)]));

[[5/3, 5/6, -5/6], 5/6*sqrt(6)]

>

Example 18.10

A pentagon is formed from a rectangle surmounted by an isosceles triangle. What dimensions give the pentagon least perimeter if the area is fixed at the value a ? (See the following figure, Figure 18.18.)

> a:='a':
p10 := plot([[0,0],[6,0],[6,3],[3,5],[0,3],[0,0]], color=black):
p11 := plot({[[0,3],[6,3]],[[3,5],[3,3]]}, linestyle = 3, color=black):
p12 := textplot({[1.5,2.7,`x`], [4.5,2.7,`x`], [3.2,4,`z`], [5.4,4.5,`sqrt(z^2 + x^2)`], [6.2,1.5,`y`], [3,-.5,`2x`]}, font=[TIMES,ROMAN,12]):
display([p||(10..12)],axes=none,scaling=constrained);

[Maple Plot]

>

In this fourth example, the constraint contains a symbolic parameter, and the algebra becomes significantly more complicated. The point of the example is to show how to navigate through such seemingly complexity.

>

From Figure 18.18 the area of the triangle, the area of the rectangle, the total area, and the perimeter are given by

Area of Triangle: 2 [ 1/2 x z ]

Area of Rectangle: ``(2*x)*y

Total Area: g(x,y,z) = 2*x*y+x*z = a

Perimeter: f(x,y,z) = 2*sqrt(x^2+z^2)+2*x+y+y

The objective function is the perimeter, so we define

> f := 2*sqrt(z^2 + x^2) + 2*x + 2*y;

f := 2*sqrt(z^2+x^2)+2*x+2*y

>

The constraint is the fixed area, so we define

> g := 2*x*y + x*z - a;

g := 2*x*y+x*z-a

>

The gradient vectors grad( f ) and grad( g ) are then

> gf := grad(f, [x,y,z]);
gg := grad(g, [x,y,z]);

gf := MATRIX([[2*x/(sqrt(z^2+x^2))+2], [2], [2*z/(s...

gg := MATRIX([[2*y+z], [2*x], [x]])

>

Table 18.4 lists the four equations arising from grad(f) = lambda*grad(g) and the constraint g(x,y,z) = a . There are four possible solutions for x, y, z , and lambda ; computed exactly in Maple (below), they are listed in floating-point form in table 18.4. Only one solution gives all three dimensions as positive.

Equations lambda ``(x,y,z) f(x,y,z)

====================== === ====================== ========

2*x/sqrt(x^2+z^2)+2 = lambda*(2*y+z) .52/sqrt(a) ``(1.9*sqrt(a),.82*sqrt(a),-1.1*sqrt(a))

2 = 2*lambda*x -.52/sqrt(a) ``(-1.9*sqrt(a),-.82*sqrt(a),1.1*sqrt(a))

2*z/sqrt(x^2+z^2) = lambda*x 1.9/sqrt(a) ``(.52*sqrt(a),.82*sqrt(a),.30*sqrt(a)) 3.86*sqrt(a)

2*x*y+x*z = a -1.9/sqrt(a) ``(-.52*sqrt(a),-.82*sqrt(a),-.30*sqrt(a))

______________________________________________________________________________

Table 18.4

The third solution is the physically meaningful one, and can be given exactly as

``(beta,(1+1/sqrt(3))*beta,beta/sqrt(3))

where

beta = sqrt(a*(2-sqrt(3)))

The minimum value of the perimeter can also be given exactly as

(1+sqrt(3))*sqrt(2*a)

It is surprising how much the presence of the symbolic parameter a complicates the algebra. But the reader should not let the additional complexity in the algebra obscure the underlying simplicity of the basic technique of Lagrange multipliers.

To execute these calculations in Maple, equate the coefficients of grad(f) with a multiple of grad(g) , obtaining (after again replacing lambda with the more convenient letter m ),

> q := equate(gf, m*gg);

q := {2*z/(sqrt(z^2+x^2)) = m*x, 2 = 2*m*x, 2*x/(sq...

>

Solve four equations in the four unknowns x, y, z, m , obtaining

> q1 := solve(q union {g = 0}, {x,y,z,m});

q1 := {z = -RootOf(_Z^4-4*_Z^2*a+a^2)^3/(2*RootOf(_...
q1 := {z = -RootOf(_Z^4-4*_Z^2*a+a^2)^3/(2*RootOf(_...
q1 := {z = -RootOf(_Z^4-4*_Z^2*a+a^2)^3/(2*RootOf(_...

>

where again, the union command adds the constraint equation to the set of three equations obtained from the gradient condition.

The cure for the RootOf structure is allvalues .

> q2 := allvalues(q1);

q2 := {m = -sqrt(2*a+a*sqrt(3))*(-2*a+a*sqrt(3))/(a...
q2 := {m = -sqrt(2*a+a*sqrt(3))*(-2*a+a*sqrt(3))/(a...
q2 := {m = -sqrt(2*a+a*sqrt(3))*(-2*a+a*sqrt(3))/(a...
q2 := {m = -sqrt(2*a+a*sqrt(3))*(-2*a+a*sqrt(3))/(a...

>

There are 4 possible critical points and we will have to investigate each one. Execute the following Maple instruction to isolate and name each critical point.

> for k from 1 to 4 do
s||k := q2[k];
od;

s1 := {m = -sqrt(2*a+a*sqrt(3))*(-2*a+a*sqrt(3))/(a...

s2 := {y = -sqrt(2*a+a*sqrt(3))*(a+a*sqrt(3))/(3*a+...

s3 := {m = -sqrt(2*a-a*sqrt(3))*(-2*a-a*sqrt(3))/(a...

s4 := {m = sqrt(2*a-a*sqrt(3))*(-2*a-a*sqrt(3))/(a^...

>

A useful view of the critical points is afforded by a conversion to floating-point form. Thus, for the first critical point, we have

> evalf(s1);

{m = .5176380895*1/(sqrt(a)), z = -1.115355072*sqrt...

>

Reject this solution since z (a length) is negative.

The next critical point to examine is

> evalf(s2);

{y = -.8164965812*sqrt(a), m = -.5176380895*1/(sqrt...

>

Again, reject this solution since at least one variable (a length) is negative.

The situation is the same for the last critical point, as we see from

> evalf(s4);

{m = -1.931851651*1/(sqrt(a)), y = -.8164965793*sqr...

>

where both x and y (which are lengths) are negative.

The remaining critical point evaluates to

> evalf(s3);

{m = 1.931851651*1/(sqrt(a)), y = .8164965793*sqrt(...

>

and we see this is viable since all three variables (lengths) are positive. The function value at this candidate is

> f3 := simplify(subs(s3,f),symbolic);

f3 := -sqrt(a)*sqrt(2)*(sqrt(3)-3)/(-3+2*sqrt(3))

>

Thus, there is just the one solution, s3, for which the point ( x, y, z ) is

> P := subs(s3,[x,y,z]);

P := [sqrt(2*a-a*sqrt(3)), sqrt(2*a-a*sqrt(3))*(a-a...

>

The function value at this point is

> f3;

-sqrt(a)*sqrt(2)*(sqrt(3)-3)/(-3+2*sqrt(3))

>

An alternate view of this value is given by

> evalf(f3);

3.863703295*sqrt(a)

>

Maple can be coerced into simplifying the optimal value.

> factor(expand(rationalize(f3)));

sqrt(a)*sqrt(2)*(sqrt(3)+1)

>

Maple can also be coerced into simplifying (slightly) the representation of the extreme point.

> P1 := factor(expand(rationalize(P)));

P1 := [sqrt(-(-2+sqrt(3))*a), 1/3*sqrt(-(-2+sqrt(3)...

>

Maple resists compressing this solution to the form

``(beta,(1+1/sqrt(3))*beta,beta/sqrt(3))

where

beta = sqrt(a*(2-sqrt(3)))

However, Maple does help with enough of the algebra so that with just a bit of intervention, this form can be achieved.

Let us begin by defining the quantity

> u := a*(2-sqrt(3));

u := (2-sqrt(3))*a

>

whose square root is beta . (Thus, beta = sqrt(u) .)

In as many places as Maple will allow, insert U where this quantity appears. (Since we have just assigned to the letter u , we can't substitute this letter into the expression, so we substitute the alternate letter, U .)

We obtain

> PP1 := algsubs(u=U,P1);

PP1 := [sqrt(U), 1/3*sqrt(U)*(sqrt(3)+3), 1/3*U^(3/...

>

Next, replace sqrt(U) with beta .

> PP2 := subs(sqrt(U)=beta,PP1);

PP2 := [beta, 1/3*beta*(sqrt(3)+3), 1/3*U^(3/2)*(3+...

>

But U^`3/2` = U*sqrt(U) = u*beta which we can inject via

> radnormal(subs(U^(3/2)=u*beta,PP2));

[beta, 1/3*beta*(sqrt(3)+3), 1/3*sqrt(3)*beta]

>

Except for Maple's tendency to rationalize 1/sqrt(3) to sqrt(3)/3 , we have achieved the desired form.

>

Example 18.11

In this fifth example we extend the method to the case of two constraints by finding the shortest distance from the origin to the interSectionsof the two planes

g[1] = `` 2*x-3*y+5*z = 9

g[2] = `` 6*x+y-7*z = 12

that is,

> g1 := 2*x - 3*y + 5*z - 9;
g2 := 6*x + y - 7*z - 12;

g1 := 2*x-3*y+5*z-9

g2 := 6*x+y-7*z-12

>

We give two solutions, the first with the use of Lagrange multipliers, and the second, without. For both solutions, the objective function is

f(x,y,z) = x^2+y^2+z^2

that is,

> f := x^2 + y^2 + z^2;

f := x^2+y^2+z^2

>

the square of the distance from the origin to the point ``(x,y,z) .

As explained below, the Lagrange-multiplier method generalizes to

f f = lambda[1] f g[1] + lambda[2] f g[2]

that is, to

grad(f) = lambda[1]*grad(g[1])+lambda[2]*grad(g[2])...

so now, there are five equations in the five unknowns

x, y, z, lambda[1], lambda[2]

Alternatively, the function

F(x,y,z) = f(x,y,z)-`` sum(lambda[k]*g[k](x,y,z),k = 1 .. 2)

could be defined, and the same set of equations obtained from

f F = MATRIX([[F[x]], [F[y]], [F[z]]]) = MATRIX([[0], [0]... = 0

The equations, and their single solution, are listed in Table 18.5. The point closest to the origin is designated by P .

Equations ``(lambda[1],lambda[2]) ``(x,y,z) sqrt(f(x,y,z))

============== ========== =============== =========

2*x = 2*lambda[1]+6*lambda[2] ``(181/216,115/216) ``(263/108,-197/108,25/108) sqrt(1003)/12

2*y = -3*lambda[1]+lambda[2]

2*z = 5*lambda[1]-7*lambda[2]

2*x-3*y+5*z = 9

6*x+y-7*z = 12

_____________________________________________________________________

Table 18.5

Implementing this first solution in Maple begins with the computation of the gradients of the objective function and the constraints, which we designate as

> grad_f := grad(f,[x,y,z]);
grad_g1 := grad(g1,[x,y,z]);
grad_g2 := grad(g2,[x,y,z]);

grad_f := MATRIX([[2*x], [2*y], [2*z]])

grad_g1 := MATRIX([[2], [-3], [5]])

grad_g2 := MATRIX([[6], [1], [-7]])

>

The generalization of the Lagrange-multiplier technique relevant here is

grad(f) = Sum(lambda[k]*grad(g[k]),k = 1 .. 2) = lambda[1]*gr...

This form of the Lagrange-multiplier method conforms well to the available Maple syntax, and is implemented with

> q := equate(grad_f,m1*grad_g1 + m2*grad_g2);

q := {2*x = 2*m1+6*m2, 2*z = 5*m1-7*m2, 2*y = -3*m1...

>

which forms three equations in the five unknowns x, y, z, m[1], m[2] . The additional two equations needed to make a determinate system are the constraints themselves. The five relevant equations are then solved in Maple with the syntax

> q1 := solve(q union {g1,g2}, {x,y,z,m1,m2});

q1 := {x = 263/108, z = 25/108, y = -107/108, m1 = ...

>

The point on the interSectionsof the two planes which is closest to the origin is then

> P:=subs(q1,[x,y,z]);

P := [263/108, -107/108, 25/108]

>

and the distance of this point from the origin is

> f1 := simplify(subs(q1,sqrt(f)));

f1 := 1/12*sqrt(1003)

>

or

> evalf(f1);

2.639181270

>

as a floating-point number.

At the point P = ``(263/108,-107/108,25/108) , we find grad(f) to be the vector

> F := subs(q1,op(grad_f));

F := MATRIX([[263/54], [-107/54], [25/54]])

>

Of course, at P , this vector will agree with lambda[1]*grad(g[1])+lambda[2]*grad(g[2]) , as we see by the following calculation.

> subs(q1,evalm(m1*grad_g1 + m2*grad_g2));

MATRIX([[263/54], [-107/54], [25/54]])

>

A second solution to this problem can be framed within the confines of simple multivariable calculus operations. First, a vector along the line of interSectionsof the two constraint planes is given by the cross product of normals to the two planes. Thus, we compute the vector

V = grad(g[1]) x grad(g[2])

that is,

> V := crossprod(grad_g1,grad_g2);

V := MATRIX([[16], [44], [20]])

>

which lies along the line of interSectionsof the constraint planes. This vector is perpendicular to F = grad(f) at the point P , as we can see from

> dotprod(F,V);

0

>

Hence, the line of interSectionsof the constraint planes is tangent to the level surface determined by the objective function, f(x,y,z) . This is the analog of the geometry which applies in the case of the objective function f(x,y) constrained by a single constraint curve g(x,y) = 0 .

To find a point on the line of interSectionsof the two constraint planes, set z = 0 in both planes to find where the line passes through the xy -plane. The solution of the equations

> eq1 := subs(z=0,g1);
eq2 := subs(z=0,g2);

eq1 := 2*x-3*y-9

eq2 := 6*x+y-12

>

is found to be

> q2 := solve({eq1,eq2},{x,y});

q2 := {x = 9/4, y = -3/2}

>

so the line of interSectionscan be given parametrically by the vector

> R := evalm(subs(q2,[x,y,0]) + t*V);

R := MATRIX([[9/4+16*t], [-3/2+44*t], [20*t]])

>

If we evaluate f(x,y,z) along this line, we obtain f(t) = f(x(t),y(t),z(t)) , a function of a single variable. This function is

> ft := simplify(subs(x=R[1],y=R[2],z=R[3],f));

ft := 117/16-60*t+2592*t^2

>

and the techniques of elementary differential calculus, setting the derivative to zero and solving, gives the critical value of t as

> T := solve(diff(ft,t),t);

T := 5/432

>

Evaluating R ``(t) at this value of t gives

> subs(t=T,op(R));

MATRIX([[263/108], [-107/108], [25/108]])

>

which is the vector form of the point P found by the Lagrange-multiplier method.

Finally, we construct Figure 18.19, a sketch of the sphere

> f = f1^2;