Math 2248 Lab 6:
Introduction to Lagrange multipliers

© Copyright by T.I. Lakoba
Last modified  2025-10-31

Preamble

In this Lab you will learn a new approach to the problem of finding an extremum of a function subject to a given constraint. A typical example of such a problem is: 
"Find two numbers such that their product is a maximum and their sum equals 10."
In Calculus I, you approached this problem as follows. Denote these numbers as  x  and  y.  Since  x+y=10,  then  y=10-x.  Then seek the value of  x  such that  x(10-x)  is the maximum. 

Here you will look at this problem differently. Namely, you will want to maximize the function   f(x,y)=x*y   given that  g(x,y)=0,  where  g(x,y)=x+y-10.  Thus, problems considered in this Lab have two ingredients: 
*   the function  f(x,y)  to be maximized or minimized and 
** the constraint equation g(x,y)=0. 

To get the idea behind the method, let us begin by plotting the graphs of:
* several level curves   x*y=c  of the function f(x,y)=x*y, which you need to maximize in the above example, 
along with 
** equation  x+y-10=0, which is the constraint equation in this case.

Clear["`*"]

hyperbola[c_] := Plot[ c/x, {x,0.1, 10}] 
(* 
Note 1:
The above command defines a function "hyperbola(c)" that corresponds to the level curve  f(x,y)=c.
In this Preamble,we use Cartesian equations of the curves:  y = c/x  here and  y=10-x  in the next command cell. 
In the detailed Example to follow, we will use the parametric equation of one of the curves, because it will be one of the curves whose parametric equations you learned in Lab 1 and in Sec. 12.6.

Note 2:
 Note that above, the delayed evaluation symbol ":=", as opposed to just "=",suppresses immediate plotting. (You may try omitting the ":" to see what happens after you execute the next Input cell.)
So, this is one of those RARE CASES where you indeed need to use ":=" instead of just "=". Keep using "=" in all other cases where you are not explicitly told to use ":=".
 *)

constraint = Plot[ 10-x, {x,0.1,10},AspectRatio->Automatic, PlotStyle->RGBColor[1,0,0]] 
(*
 Setting  AspectRatio->Automatic  makes the scales of the x- and y-axes equal.
*)

p=Show[ constraint, hyperbola[1],hyperbola[5],hyperbola[10],hyperbola[15],hyperbola[20],hyperbola[25],hyperbola[30],PlotRange->{0,10}]

You can see that the maximum value of  c  for which the level curve (i.e., the hyperbola  y=c/x ) still has a common point with the curve of constraint  g(x,y)=0  (i.e., with the line  y=10-x ),  is  c=25.  At this value of  c,  the level curve is tangent to the curve of constraint.  Here is a closer view at the corresponding region :

Show[p,PlotRange->{{4,7},{4,6}}]
(*
Note the syntax for PlotRange, which allows you to specify onto which region you want to zero in.
*)

At the point where two curves are tangent to each other, their normal vectors (the level curve and the constraint curve) must be parallel.  This observation is behind the idea of the method of Lagrange multipliers.  Namely, instead of considering the equation 

				(Overscript[▽, ->])f(x,y) = (Overscript[0, ->])

which would have been true had we had to find an extremum of  f(x,y) = 0  if there had been no constraint, one now considers two equations that must hold simultaneously; see below.  

The first equation is

				(Overscript[▽, ->])f(x,y)  = λ (Overscript[▽, ->])g(x,y) .			(1)
				
It says that the gradients to the level curve f(x,y) = c  and to the constraint curve  g(x,y) = 0  at the point of tangency are proportional and, therefore, parallel. Equation (1) must be true because:  
   *  the normal vectors to these curves are parallel at the tangency point (we have noted this fact above in red bold font),  
and 
   *  the normal vector always points along the gradient.  
The second equation is the constraint equation, 
 
				g(x,y) = 0.				(2)
						
The new parameter λ  (the Greek letter “lambda”) introduced in Equation (1) above is called the Lagrange multiplier.

					
FYI only: This technique generalizes to the case of three or more variables in a straightforward manner. For example, if we need to maximize (or minimize) a function   w=f(x,y,z)  subject to a constraint  g(x,y,z)=0, we need to find the point where the level surface   f(x,y,z)=c  is tangent to the constraint surface  g(x,y,z)=0.  (This, however, if much harder to visualize using Mathematica.) The equations we need to solve are still the same as for functions of two variables:

			(Overscript[▽, ->])f(x,y,z)  = λ (Overscript[▽, ->])g(x,y,z) ,	
			
along with
                    	g(x,y,z) = 0.

Above, the former equation says that the normal vectors to the surfaces    f(x,y,z)=c  and  g(x,y,z)=0  are parallel.




Having explained the idea of the method, we will now illustrate its steps with an Example. You will need to follow exactly these steps in Exercise 1 except for its part (c) and in the entire Exercise 2.

Example

Find the point on the curve   y = 1 / (Sqrt[x])   that is closest to the origin.
Find the distance from that point to the origin (which thus equals the minimum distance from the curve to the origin). 


Discussion:
The problem says that the function that we need to minimize is the distance, measured from the origin to some point (x,y) on the given curve. 
This distance is given by the formula (see Sec. 12.1): (Sqrt[(x^2)+(y^2)]). 
However, if we take our  f(x,y)  as this square root expression, we may have a harder time when taking its partial derivatives. To avoid this hardship, we notice that  (Sqrt[(x^2)+(y^2)]) and (x^2)+(y^2) attain their respective minimum values at the same  point (x,y). Therefore, we will seek to minimize  f(x,y)=(x^2)+(y^2)  -- the squared distance of point  (x,y)  to the origin.

We will now list Steps that you will need to follow in your own work on the Exercises.

Step 1:	 

Function to minimize:  f(x,y) = (x^2)+(y^2) 
Constraint equation:  y - 1 / (Sqrt[x])=0
(This comes from the curve  y = 1 / (Sqrt[x]), which we rewrote in the form  g(x,y) =0  to stay consistent with the notation for the constraint equation as introduced in the Preamble.) 

From there, you can find (by hand is fine) the expressions for  (Overscript[▽, ->])f(x,y)  and   (Overscript[▽, ->])g(x,y).     


Step 2:  

Set up the equations

		(Overscript[▽, ->])f(x,y)  = λ (Overscript[▽, ->])g(x,y),		g(x,y)=0.
			
The equation for the gradients yields: 

		<2x,  2y>  = λ  <1/(2 (Sqrt[(x^3)]) ) ,  1>, 	
		
i.e.     2x = λ /(2 (Sqrt[(x^3)]) )    and     2y  = λ  .   

The constraint equation stays the same as in Step 1.

Note that these are 3 equations for 3 unknowns:  x,  y, and  λ. 
Even though one can solve them “by pen and paper,” it is not a good idea to do so. The main reason is that this method is error-prone; another reason is that in some cases they may be too complicated to be attempted “by pen and paper.” Therefore, we will employ Mathematica to solve these equations for us. After all, solving equations is one of the tasks that Mathematica is designed for, and it does it much better than most people. The command to solve the equations in question is:

Solve[{2x==lambda/(2*Sqrt[x^3]),y*2==lambda, y-1/Sqrt[x]==0}]
(*
Note 1: 
Note that the double equal sign, == , and not the "usual" single equal sign, = , has to be used in each equation inside the Solve command. 
If, by accident, you entered a single "=", Mathematica will not produce a solution. Moreover, if you correct the syntax and re-execute the command with "==", Mathematica may still refuse to cooperate! In such a case, save your work, execute the  Clear["`*"]  command at the beginning of this notebook, re-define your functions, and attempt the Solve command again (with the correct syntax).

Note 2:   
Even with the correct syntax, the Solve command may sometimes act stubbornly "for its own reasons." In such a case, try using Reduce instead. The syntax of Reduce is the same as the syntax of Solve used above.
If nothing else helps, save your work, do Clear["`*"], and retype your Solve command from scratch.
*)

Let us note that the two of the three triplets {λ, x, y} that contain either the 6th root of a negative number or the symbol “i” are not “real-valued” solutions. Rather, they are “complex-valued.” You do not need to understand what exactly this means. What is important is that such solutions should be ignored when considering real curves. So, the only answer that is of interest to us is that with real values of  x,  y,  and λ.

Thus, the answer to this problem is:

Step 3:  

You can verify this answer by plotting together the graphs of
   y = 1 / (Sqrt[x])   and the level curve corresponding to the minimum distance that we have found above:   
   
                           (x^2)+(y^2)  =  (((1/ (2^(1/( 3)))))^2) + ((((2^(1/6))))^2).

(*
The plotting commands below are analogous to the commands used to obtain the plot named "p" at the beginning of this Lab. So, if you are not sure what the commands below do, review the process of creating plot "p".
*)

q1=Plot[1/Sqrt[x],{x,0.25,2},PlotStyle->RGBColor[1,0,0]];
(* 
This is the curve of the constraint.
*)

circle[c_]:=ParametricPlot[{Sqrt[c]*Cos[t],Sqrt[c]*Sin[t]},{t,0,2*Pi}];
(*
This is the level curve  f(x,y)=c.
Note that you must use parametric equations to plot curves whose equations (in this case,  (x^2)+(y^2)=c )  you learned in Lab 1 and Sec. 12.6.
*)

Show[q1,circle[ ((1/2)^(1/3))^2 + (2^(1/6))^2 ],AspectRatio->Automatic]

As you see, the curves are indeed tangent to each other at the point where the constrained curve is closest to the origin. The approximate coordinates of this point and the distance to it from the origin are:

decimalx = N[1/2^(1/3),2]
 decimaly = N[2^(1/6),3]
decimalmindist = N[ Sqrt[ ((1/2)^(1/3))^2 + (2^(1/6))^2  ],3]

Note again that the level and constraint curves are tangent to each other at the point of interest. Indeed, this fact was our motivation  for stating the key formula

			(Overscript[▽, ->])f(x,y)  = λ (Overscript[▽, ->])g(x,y) 

So, when you are asked to plot the level and constraint curves in the exercises below, make sure that they, too, are tangent at certain points.


		---  End  of  Example  ---

Note two important issues before proceeding to the Exercises.
You will not be able to successfully do the Exercises without that.

   1. Make sure that you understand where the number  (((1/ (2^(1/( 3)))))^2) + ((((2^(1/6))))^2)  in the above Example came from, and why we use it to plot the level curve. If you are not sure, review the Preamble before the Example (mostly its plots and their explanations).
   2. Note that we have used parametric equations to plot the level curve. In the Exercises, some of the curves must also be plotted using parametric equations of ellipses or hyperbolas, which we have studied in this course. 
    Your score will be harshly reduced if you use Cartesian equations to plot  *ANY*  ellipses (circles) or hyperbolas in this Lab. 
You should review Part 1 of Lab 1 and Notes for Sec. 12.6 in order to obtain correct parametric equations.

Exercise 1

Follow closely the lines of the above Example to find the minimum distance from the origin to the inverted parabola 
					y = 1- (x^2).


Clarification: Refer to the Example, not the Preamble. 


(a)   Set up the corresponding equations for the function that you need to minimize and for the constraint.

Record these equations in the box below.

Compute the vector components of the gradients of the function  f  to be minimized and of the function  g  in the constraint equation.

Record these gradients in the box below.

(b)   Set up the vector equation for the gradients  (this is the equation that involves the Lagrange multiplier).

Solve these equations along with the constraint equation. Use a Mathematica command to do so, as illustrated in the Example.

(c)   If you have done everything correctly, you should have obtained more than one solution (i.e., more than one pair of coordinates (x,y)). So you need to determine: 
	(i)   which of these solutions correspond(s) to the minimum distance from the origin, and 
	(ii)  what the other solution(s) correspond(s) to.
Note that this part of the Exercise does not have a counterpart in the Example.

To answer these questions, first define the distance from the origin to the point  (x,y)  on the parabola as a function of  x  only.
(If you are not sure what function I am asking you to define, review Example 2 in the posted notes for Sec. 14.7 and/or Example 5 in the textbook for that section. Note that the functions that one ends up minimizing there are of two variables, (x,y). Here, you need to use a similar procedure, but this will lead you to a function of only one variable, x.)

Next, go ahead and plot this function, distance(x),  using a “good” range for  x. (A range is “good” if it makes essential details of the curve well visible in your plot.) Experiment to find what range is “good” in the aforementioned sense.

Now answer the questions (i) and (ii) stated above.

(d)  Now we return to the guidelines of the Example.

Make a plot that shows the constraint curve (plotted in red) together with the appropriate level curves, as at the end of the Example. This plot must illustrate your answers to both questions (i) and (ii) in the Answer box above. 
Present an explicit calculation of the  c  value of these appropriate level curves, as shown in the Example. 

Here are some checkpoints that you should use to assess the correctness of your plot:
   *  Do you have as many level curves as has been asked for? (The number of the level curves has not been specified explicitly, but it should follow from your answers in the Answer box above.) 
   *  Have you followed the Note highlighted in red before this Exercise? 
   *  What is the key feature of the level curves relative to the constraint curve (when finding an extremum subject to a constraint)? If you are not sure, re-read the Introduction, including the Example. This key feature is conspicuously mentioned there several times. 
   *  Is your constraint curve plotted in red and the level curves, in a different color?
   *  Have you made it clear how the  c values in your level curves have been found?

Finally, record the minimum distance from the origin to the parabola:

Exercise 2

Find the highest and lowest points on the curve of intersection of the circular cylinder   (x^2)+(y^2) = 1  and the elliptical paraboloid    z = (x^2) + 4(y^2) . 

 
To visualize the problem, execute the next Input cell to view the corresponding graph.

(* 
Sketching the surfaces: 
*)

p1=ParametricPlot3D[{Cos[t],Sin[t],z},{t,0,2*Pi},{z,0,5}];
(* The cylinder *)

p2=ParametricPlot3D[{r*Cos[t],r*Sin[t],r^2*(Cos[t]^2+4*Sin[t]^2)},{t,0,2*Pi},{r,0,3}];
(* The paraboloid *)

Show[p1,p2,PlotRange->{{-3,3},{-1.5,1.5},{0,5}},BoxRatios->Automatic,AxesLabel->{"X","Y"," "}]
(* The two combined *)

(a)  Follow directions of Exercise 1(a).

Hint for helping you decide what you should take as f(x,y).  (Recall that this is the function whose extrema you are to find.) 
   - Re-read the statement of the problem (i.e., the 1st sentence of this Exercise).
   - Now, which coordinate -- x, y, or z -- does one tacitly imply when saying “highest” and “lowest”?

(b)  Follow directions of Exercise 1(b).

(c)  Follow directions of Exercise 1(d). (That is, you should not worry about defining a counterpart of the distance function in Exercise 1(c). This is because you can determine the maxima and minima of the height by inspection of the 3D plot made in part (a).)

     Note 0:  Your plots must be in 2D (not 3D!), just as they are in Exercise 1(d). Reason: You are finding conditional extrema of a function of two variables, not three. If somehow you still get three variables at this point, go back to part (a) and re-assess what you did there (make sure to use the Hint). 
     
     Note 1:  Refer to the checkpoints listed in Exercise 1(d) to assess the correctness of your plots of level curves and the constraint curve. 

     Note 2:  Review the “two important issues” listed before Exercise 1 and immediately after the Example. 
     
     Note 3:  You will need to review the posted Notes for Sec. 12.6 to make sure that your plots are correct. 
     
     Note 4:  You are also required to indicate how you have arrived at the specific values of the constant  c  in your level curves. For example, suppose that your level curve is y-x=5, where the “5” has been found as (8-3), with (x0,y0)=(3,8) being a solution of the Lagrange equations. Then I want you to present your work as  ParametricPlot[{t,(8-3)+t},...]  rather than as  ParametricPlot[{t,5+t},...].  
     For convenience of my grading, you must present your work of finding the  c-values  either  before you make the plots  or  as an explicit calculation in the argument of your level curves. (The last clause may sound too convoluted. For an illustration of what it means, see the argument of the circle[...] command at the end of the Example.)  Any such work presented after the plots will receive  no  credit, because it will confuse and slow down my grading.

Bonus  (25%; credit will be given only for the mostly correct work)

Follow the hints listed below to arrive at the equation which replaces  
                               (Overscript[▽, ->])f(x,y,z)  = λ (Overscript[▽, ->])g(x,y,z) 
                               
when one needs to find a local extremum of  f(x,y,z)  subject to two constraints   g(x,y,z)=0  and  h(x,y,z)=0.  

(i)  Geometrically, what is the common solution to the two constraints  g(x,y,z)=0    and   h(x,y,z)=0 ? For future reference, I will say that this solution represents some geometrical Object X. You need to explicitly say what this object is and use that name (i.e., point, line, curve, surface, etc.) in your subsequent answers. 
(ii) How are  (Overscript[▽, ->])g(x,y,z) , and  (Overscript[▽, ->])h(x,y,z)  related to Object X? (Recall that the gradient is perpendicular to the surface, which means that it is perpendicular to any curve lying in that surface.)
(iii) How should the graph of the level surface   f(x,y,z) =c  be positioned relative to Object X? (Use the analogy with the case of functions of two variables; the statement you need to look for there is typed in boldface red font in the Introduction to this Lab.)
(iv)  From the answer to (iii), how should   (Overscript[▽, ->])f(x,y,z)  be oriented relative to Object X?
(v) (This is the hardest of the five parts, and the key to the correct answer.) What does the answers to (ii) and (iv) imply in regards to the relation among (Overscript[▽, ->])f(x,y,z),  (Overscript[▽, ->])g(x,y,z) , and  (Overscript[▽, ->])h(x,y,z) ? To answer this question, you may want to review the topics in the notes for Sec. 12.1&12.2 and for Sec. 12.4, posted on the course webpage, about three vectors lying in the same plane. 

To check if your answer makes sense, verify that the number of equations that you need to solve equals the number of unknowns. 

Credit will be given only if your answers are coherent and provide succinct, but sufficient details. 

No credit will be given for an answer not following the above hints.