# Maximizing a multivariate function under a constraint

1. Jun 17, 2012

### Bipolarity

1. The problem statement, all variables and given/known data

Consider $f(x,y) = \frac{1}{x} - \frac{1}{y}$

You need to maximize $f(x,y)$ given the constraint:
$x + y = 11$

2. Relevant equations
I have never solved a problem like this before. In fact I made up a problem a few minutes ago.

3. The attempt at a solution
I can only imagine graphing f(x,y) on the x,y plane and seeing the maximum point of intersection with the plane of constraint. Actually I am not even sure if that is right.

BiP
1. The problem statement, all variables and given/known data

2. Relevant equations

3. The attempt at a solution

2. Jun 17, 2012

### SammyS

Staff Emeritus
Solve for y: $x + y = 11$ .

Plug that into $\displaystyle f(x,y) = \frac{1}{x} - \frac{1}{y}$, so that you have a function of x only.

3. Jun 17, 2012

### Bipolarity

I see. The single-variate function of x that is obtained once I do that: what is its visual relationship to the graphs of f(x,y) and x+y=11 ?

Is it their intersection? It would be helpful to have a visual reference for each step so I can clearly understand what's going on.

BiP

4. Jun 17, 2012

### Ray Vickson

Say you have a problem of the form max f(x,y), subject to g(x,y) = 0, with both f and g at least once continuously differentiable. In your case
$$f(x,y) = \frac{1}{x} - \frac{1}{y} \text{ and } g(x,y) = x+y-11$$
but the basic idea is more general. The constraint g=0 gives a curve C in the (x,y)-plane from which we want to select the point (x,y) giving the largest f. What conditions must hold at the optimal solution p* = (x*,y*) (assuming there IS a finite solution)? Basically, the gradient vectors $Df =\nabla f$ and $Dg = \nabla g$ must be parallel or antiparallel at p*. The reason is this: if $Df$ and $Dg$ point along different lines, there is a nonzero component of $Df$ that is perpendicular to $Dg$, so is tangent to C at the point p*; and that means that by going away from p* (but staying in C), we can make f go up a bit. That would mean that f(p*) is not the maximum. Therefore, to prevent this from happening we need the two gradient vectors to point along the same line (but maybe in opposite directions).

If you imagine a plot of "level curves" of f and g (like the constant-altitude contours in a hiker's area map), then at the point p* the level curves of f and g are parallel---that is, they have the same tangent line.

The geometric property above can be expressed by saying that at p* there is a scalar λ such that $\nabla f = \lambda \nabla g$. The scalar is usually called the Lagrange Multiplier, and the optimization rule amounts to saying that the so-called Lagrangian
$L = f - \lambda g$ must be stationary at p*; that is, we need $\nabla L = 0$ at p* = (x*,y*). Note that λ is not a function of x or y.

So, let's do this for your problem:
$$L = \frac{1}{x}-\frac{1}{y} - \lambda(x+y-11).$$
We have
$$\frac{\partial L}{\partial x} = 0 \longrightarrow -\frac{1}{x^2} - \lambda = 0,\\ \frac{\partial L}{\partial y} = 0 \longrightarrow +\frac{1}{y^2} - \lambda = 0.$$
From this, we see that $x^2 = -y^2$ at $x = x^*,\, y = y^*,$ and that can only happen if $x^* = y^* = 0.$ But f(0,0) is not defined, so there is no optimal solution. You can also see that this is the case by substituting y = 11-x into f and looking at a plot of the result.

A nicer example would be to take $$f = \frac{1}{x} + \frac{2}{y}$$ and keeping the same g as before. Now you would get $$L = \frac{1}{x} + \frac{2}{y} - \lambda (x+y-11),$$ and so
$$\frac{\partial L}{\partial x} = -\frac{1}{x^2} - \lambda = 0,\\ \frac{\partial L}{\partial y} = -\frac{2}{y^2} - \lambda = 0,\\ x+y=11,$$
so either (1)
$$x = 11\sqrt{2}-11, \; y = 22 - 11\sqrt{2}, \; \lambda = -\frac{3+2\sqrt{2}}{121},$$
or (2)
$$x = -11 - 11 \sqrt{2}, \; y = 22 + 11\sqrt{2}, \; \lambda = -\frac{3 - 2\sqrt{2}}{121}.$$

It turns out that point (1) is a maximum while point (2) is a minimum. In general we would need to perform second-order tests to check whether a point is a max or a min (or neither), because just looking at gradients equal to zero does not tell us whether the point is a max or a min. However, in this case we can just evaluate f at the two points to see which is which.

Note: by max and min above, we mean local max and min, not global. In this case there are no global max or min values, because we can have $f \rightarrow + \infty$ by letting x go to zero from above (keeping y = 11-x), or letting x go to 11 from below (so y goes to zero from above). Similarly, we can have $f \rightarrow -\infty$ by letting x go to zero from below or go to 11 from above.

RGV

Last edited: Jun 18, 2012