topic:  constrained optimization and the method of Lagrange multipliers
description:  introduces root-finding algorithms in Mathematica.  Then, discusses the use of the method of Lagrange multipliers in an application specific to the Air Force Academy.
file name:  lagrange.nb
directions:  Read the text cells for background information.  Execute any existing input cells as you come to them.  Answer questions or work exercises as you come to them.  Modify the existing input cells as needed to work exercises requiring the use of Mathematica.

Computer Assignment #3
Lagrange Multipliers

Special thanks to Calculus&Mathematica (Addison-Wesley, 1994) for their support of these assignments.  Each is very much in the style of C&M, and some problems and commands are adaptations of C&M problems and commands.

Why should I have to do this computer assignment anyway?

This computer assignment contains the secret to graduating at the top of your class with the minimal change in your current habits!
	In using the method of Lagrange Multipliers, we always end up with a system of equations which we must solve in order to obtain a solution to our optimization problem.  In Part 1 of this assignment, you will learn how to use the Mathematica FindRoot[] command to approximate the solutions to such systems of equations.  In Part 2, you will apply the method of Lagrange Multipliers to determine how you can increase your Professional Selection Index (PSI).

Part 1 -- Solving Equations with Mathematica

This part is meant to introduce you to a Mathematica command which enables you to solve equations.  Knowing this command will help you in all of your technical courses.

The "FindRoot" Command

Whenever we want to solve some relatively simple equations, such as those involving polynomials, we can use Mathematica's "Solve" or "NSolve" command.  Mathematica will (usually) return ALL solutions to an equation when Solve or NSolve is used.  However, certain equations, such as those involving exponentials or trig functions, cannot be solved so nicely.

However, there IS still a way to find solutions for complicated equations involving exponentials or trig functions.  Let's see what Mathematica has to say about "FindRoot" (execute the help command below.)

?FindRoot

This command will give you ONE approximate solution to an equation, and it will do so in a relatively short amount of time.  It uses a numerical root-finding algorithm known as "Newton's method."  Newton's method requires a first guess at the solution in order to work.  USUALLY, the solution which Newton's method gives you is the one which is closest to your initial guess.  (You can learn more about Newton's method in Math 342.)  

So how do you get that first guess?  Well, normally one looks at a plot to eyeball it.

a.  Plot the two functions y = E^x and y = x^2 on the same graph.  Plot over an interval on which the graphs intersect.  (Can't remember the syntax to plot two graphs on the same axes?  Use ?Plot)

<<ANSWER QUESTION IN YOUR COPY OF THE FILE COMP3ANS.MA>>

b.  Using this plot, give an eyeball estimate of the solution to E^x == x^2.

<<ANSWER QUESTION IN YOUR COPY OF THE FILE COMP3ANS.MA>>

c.  Now use the "FindRoot" command, including your first guess, to get Mathematica's approximation of the root.  (Hint:  Note Mathematica's use of the double equal signs in the equation.)

<<ANSWER QUESTION IN YOUR COPY OF THE FILE COMP3ANS.MA>>

You can also use the "FindRoot" command to find approximate solutions to a system of equations.  However, you will need a guess for EACH variable.  For example, the following code will find an approximate solution to the set of equations:
                   Cos[x]-xy=0   and   3Cos[x] = Log[x]

FindRoot[{Cos[x] - x y == 0,3Cos[x] == Log[x]},
		{x,1.},{y,1.}]

A SMALL WARNING:  Again, if there is more than one solution to an equation or a system of equations (and there often are!), then "FindRoot" will usually find the solution closest to the first guess which you provide.  If you are searching for a particular solution, you will have to keep this in mind when choosing your first guess.

Solving Equations Involving Derivatives Using the FindRoot Command

In using the method of Lagrange Multipliers, we need to solve a system of equations involving partial derivatives.  We'll need to be a little careful with the FindRoot command, in this case.  For example, if you want to solve the equations:
                             D[x^2 + y^2 - 3, x] = D[3x^3 - y^2, x]
                             D[x^2 + y^2 - 3, y] = D[3x^3 - y^2, y]
for x and y, you might want to try:

FindRoot[{D[x^2 + y^2 - 3,x] == D[3x^3 - y^2,x],
    	  D[x^2 + y^2 - 3,y] == D[3x^3 - y^2,y]},
		  {x,.2},{y,.1}]

Unfortunately, Mathematica doesn't like this!  To get around the problem, name the equations (execute the cells below):

equations = {D[x^2 + y^2 - 3,x] == D[3x^3 - y^2,x],
    	     D[x^2 + y^2 - 3,y] == D[3x^3 - y^2,y]}

And then try the FindRoot command:

FindRoot[equations,{x,.2},{y,.1}]

IT WORKS!

Part 2 -- A Really Cool "Real-Life" Optimization Problem

C3C Cal Q. Lus is a cadet at the U.S. Air Force Academy, and he wants to raise his Professional Selection Index (PSI) in order to assure himself his choice of assignment after graduation (does this  sound like a "real-life" problem yet???)  Cal discovers that his PSI is based on his grade point average (GPA) with his PE grades removed, military performance average (MPA), and his physical education average (PEA) as follows:
                        PSI = GPA^.6 MPA^.3 PEA^.1

Cal's averages are
GPA = 3.28
MPA = 2.95
PEA = 2.80,
and these numbers yield an overall average of:

Clear[PSI,GPA,MPA,PEA];
PSI[GPA_,MPA_,PEA_] = GPA^(.6) MPA^(.3) PEA^(.1);
PSI[3.28,2.95,2.80]

Interesting, eh?! Try your own numbers if you wish...

Now, Cal really wants to up his PSI.  In fact, after talking to a couple firsties, C1C Lynn E. Arity and C1C Paul E. Nomial, he discovers that a PSI of 3.30  would really increase his chances for a great assignment, and he sets 3.30 as his target PSI.  Next, Cal sets to work figuring out the best way to reach his target.  After some thought, he decides that he needs to find his target GPA, MPA, and PEA (which he decides to call tGPA, tMPA, and tPEA, respectively) so that the following three-variable function is minimized:

Clear[f,tGPA,tMPA,tPEA];
f[tGPA_,tMPA_,tPEA_] = 
	(tGPA-3.28)^2 + (tMPA-2.95)^2 + (tPEA-2.80)^2

Cal also realizes that he must include the constraint g[tGPA,tMPA,tPEA] = 0, where:

Clear[g,tGPA,tMPA,tPEA];
g[tGPA_,tMPA_,tPEA_] = 
	tGPA^(.6) tMPA^(.3) tPEA^(.1) - 3.30

a.  What is the function f based on and why does Cal wish to minimize it?

<<ANSWER QUESTION IN YOUR COPY OF THE FILE COMP3ANS.MA>>

b.  Determine what Cal's tGPA, tMPA, and tPEA should be so that f is minimized subject to the constraint.  OR, if you prefer to choose YOUR own target PSI and YOUR current GPA, MPA, and PEA instead, feel free to do so.  You may be interested in the following data when determining your target PSI:
These percentages were taken from the Order of Merit list dated 17 Jan 96:

RECOGNITION           CRITERIA             1996       1997        1998         1999

Dist. Grad                       COPA - 7%           3.462       3.472       3.402        3.286
Acad. Distinction            CAPA - 10%         3.508       3.524       3.407        3.355
Mil.  Distinction              CMPA - 10%       3.293       3.265       3.315        3.250
Pref. for Grad Ed            COPA - 15%        3.298       3.282       3.217        3.138

HINT:  When setting up the equations resulting from Lagrange's Method, it's probably  best to define them component-wise; for example, the equation resulting from the x component has the form
                                    D[f[x,y,z],x] == s D[g[x,y,z],x]
Also, if you find that Mathematica is cranking away for minutes with no answer in sight, you may want to abort the calculation (by choosing "interrupt" in the "Action" Menu, and then clicking on "Abort") and try another method.

<<ANSWER QUESTION IN YOUR COPY OF THE FILE COMP3ANS.MA>>

c.  Which of the three performance averages must increase the most in order to achieve the desired PSI?  Which must change the least?  What is it about the definition of the PSI function which makes this so?

<<ANSWER QUESTION IN YOUR COPY OF THE FILE COMP3ANS.MA>>

d.  If Cal were to shoot for a PSI of 3.90, what additional constraint would we have to consider in using the method of Lagrange Multipliers?

<<ANSWER QUESTION IN YOUR COPY OF THE FILE COMP3ANS.MA>>