19  Second Derivative Test

In this unit, we will develop an analog of the second derivative test for identifying extrema for multi-variable functions. This will involve making sense of the second derivatives of \(f(x,y)\):

Remark 19.1 (Second Derivatives).

  • \(f_{xx}(x_0,y_0)\) measures the concavity (in the \(x\)-direction) of \(f\) at \((x_0,y_0)\).
  • \(f_{yy}(x_0,y_0)\) measures the concavity (in the \(y\)-direction) of \(f\) at \((x_0,y_0)\).
  • \(f_{xy}(x_0,y_0)\) measures the cross-concavity of \(f\) at \((x_0,y_0)\).

Recall Clairaut’s Theorem Section 15.4, which says that \(f_{xy} = f_{yx}\) for the functions we will study in this class.

19.1 The Test

Second Derivative Test (Stewart et al. 2020, 14.7.3)

Suppose that \((x_0,y_0)\) is a critical point of \(f(x,y),\) and set \[ D = f_{xx}(x_0,y_0)f_{yy}(x_0,y_0) - f_{xy}(x_0,y_0)^2. \] This value is called the Hessian discriminant, though we usually just call it the “discriminant.”

  • If \(D > 0,\) then \(f\) has a local extrema at \((x_0,y_0)\):
    • If \(f_{xx}(x_0,y_0) > 0,\) then \(f\) has a local minimum at \((x_0,y_0)\).
    • If \(f_{xx}(x_0,y_0) < 0,\) then \(f\) has a local maximum at \((x_0,y_0)\).
  • If \(D < 0,\) then \(f\) has a saddle point at \((x_0,y_0)\).
  • If \(D = 0,\) then the test is inconclusive.

Remark 19.2. If \(D\) is positive, i.e., if \(f\) has an extrema at \((x_0,y_0),\) then note that we must have

  • \(f_{xx}(x_0,y_0) > 0\) and \(f_{yy}(x_0,y_0) > 0\), or
  • \(f_{xx}(x_0,y_0) < 0\) and \(f_{yy}(x_0,y_0) < 0\).

This follows because the \(-f_{xy}^2\) term in \(D\) is always nonpositive, so \(f_{xx} f_{yy}\) must be positive in order to make \(D\) positive.

Spoiler alert

The \(D\) of the second derivative test is the determinant of a matrix (the Hessian): \[ D = \begin{vmatrix} f_{xx} & f_{xy}\\ f_{yx} & f_{yy} \end{vmatrix} = f_{xx}f_{yy} - f_{xy}^2, \] where we lean on Clairaut’s theorem (\(f_{xy} = f_{yx}\)). As will be discussed in future courses, this interpretation of \(D\) provides geometric data in the language of linear algebra. While we won’t use this fact in this class, it is a handy way to remember the formula for \(D\).

19.2 Classifying Critical Values

The second derivative test gives us a way to classify critical values of \(f(x,y)\).

  • The pure second derivatives ask how much a function looks like a paraboloid at the origin. \[ f(x,y) = \tfrac{1}{2} ( x^2 + y^2 ) \quad \implies \quad f_{xx}(0,0) = 1 = f_{yy}(0,0) \text{ and } f_{xy}(0,0) = 0. \]

  • The mixed second derivative asks how much a function looks like a saddle at the origin. \[ f(x,y) = xy \quad \implies \quad f_{xx}(0,0) = 0 = f_{yy}(0,0) \text{ and } f_{xy}(0,0) = 1. \]

The \(D\) of the second derivative test is measuring a tug-of-war between the pure second derivatives and the mixed second derivative. We are asking:

“Near this critical point, does the function look more like a bowl or a saddle?”

We can visualize this tug of war by considering the function \(f(x,y) = \frac{1}{2}(x^2 + y^2) - a xy,\) as the parameter \(a\) varies, using both contours and the \(z = f(x,y)\) surface plot.

/bin/sh: tachyon: command not found
/var/tmp/sage-10.9-current/local/lib/python3.14/site-packages/sage/repl/rich_output/display_manager.py:587: RichReprWarning: Exception in _rich_repr_ while displaying object: [Errno 2] No such file or directory: 'tachyon'
  warnings.warn(
Animation with 82 frames

Indeed, when \(-1 < a < 1\), the surface traced out is a paraboloid; when \(a > 1\) and \(a < -1\), we have a hyperboloid; when \(a = \pm 1\), the shape degenerates into a cylinder.

19.3 The Recipe

Procedure: Second Derivative Test

Given \(f(x,y),\) we want to find and classify its critical points: maybe we want to find its maximum or minimum values, or are interested in some other phenomenon.

  1. Compute the gradient, \(\Gradient{f} = \Vector{f_x, f_y},\)
  2. Set \(\Gradient{f} = 0\) (a system of equations) and solve to find critical points,
  3. Compute the second derivatives (\(f_{xx}, f_{yy}, f_{xy}\)) and write \(D = f_{xx} f_{yy} - f_{xy}^2,\) and
  4. Classify the critical points via the second derivative test.

The algebra (not the calculus!) in Step 2 is the hardest part of this Procedure. You should practice solving these problems by hand!—but you should also use SageMath to check your work.

SageMath can…

  • Compute partial derivatives
var('x y')
f = x^4 + y^4 - 4*x*y

# Computes the first derivatives of f
fx = diff(f, x)
fy = diff(f, y)

# Computes the second derivatives of f (recall Clairaut's theorem)
fxx = diff(fx, x)
fyy = diff(fy, y)
fxy = diff(fx, y)

# Computes the discriminant of the 2nd derivative test
D = fxx*fyy - fxy^2

# Prints the results
print(f"The first partial derivatives are {fx} and {fy}.")
print(f"The second derivative discriminant is {D}.")
The first partial derivatives are 4*x^3 - 4*y and 4*y^3 - 4*x.
The second derivative discriminant is 144*x^2*y^2 - 16.
  • Solve systems of equations (and filter out complex solutions!)
# Solves for critical points
solutions = solve((fx==0, fy==0), (x, y))

# Filters out complex critical points
real_solutions = [
    sol for sol in solutions
    if all(expr.rhs().imag() == 0 for expr in sol)
]

# Prints the critical points
print("The real critical points are:", real_solutions)
The real critical points are: [[x == -1, y == -1], [x == 1, y == 1], [x == 0, y == 0]]
  • Evaluate functions at specific points
# Goes through each real critical point and substitutes the values into D
for sol in real_solutions:
  print(f"{sol} gives D = {D.subs(sol)}")
[x == -1, y == -1] gives D = 128
[x == 1, y == 1] gives D = 128
[x == 0, y == 0] gives D = -16

19.4 More examples

Example 19.1 (Critical points of Patrick’s pants) Let’s see how the algebra works out! Starting with \(f(x,y) = x^4 + y^4 - 4x\), we compute \[ \Gradient{f} = \Vector{x^3 - 4y, 4y^3 - 4x = 0}. \] This might look intimidating, but solving is still fairly straightforward:

flowchart LR
  A("4x³ - 4y = 0 <br> 4y³ - 4x = 0")
  A --> B("y = x³ <br> 4y³ - 4x = 0")
  B --> C("4x⁹ - 4x = 0")
  C --> D("4x (x⁸ - 1) = 0")
  D -->|"x=0"| E("(x,y) = (0,0)"):::solution
  D -->|"x⁸=1"| F("y = x³ <br> x = ±1")
  F --> G("(x,y) = (1,1), (-1,-1)"):::solution
%% GENERATED from _cells.scss and _palette.scss by mermaid-classes.py; do not edit.
%% Node classes: solution, contradiction, engine, source, auxiliary, log, final.
%%{init: {"themeCSS": ".node.solution :is(rect, circle, polygon, path, ellipse) { fill: color-mix(in srgb, var(--ctp-green, #40a02b) 15%, var(--ctp-base, #eff1f5)) !important; stroke: var(--ctp-green, #40a02b) !important; stroke-width: 2px !important; } .node.contradiction :is(rect, circle, polygon, path, ellipse) { fill: color-mix(in srgb, var(--ctp-red, #d20f39) 15%, var(--ctp-base, #eff1f5)) !important; stroke: var(--ctp-red, #d20f39) !important; stroke-width: 3px !important; } .node.engine :is(rect, circle, polygon, path, ellipse) { fill: color-mix(in srgb, var(--ctp-red, #d20f39) 15%, var(--ctp-base, #eff1f5)) !important; stroke: var(--ctp-red, #d20f39) !important; stroke-width: 2px !important; } .node.source :is(rect, circle, polygon, path, ellipse) { fill: color-mix(in srgb, var(--ctp-blue, #1e66f5) 15%, var(--ctp-base, #eff1f5)) !important; stroke: var(--ctp-blue, #1e66f5) !important; stroke-width: 2px !important; } .node.auxiliary :is(rect, circle, polygon, path, ellipse) { fill: color-mix(in srgb, var(--ctp-peach, #fe640b) 15%, var(--ctp-base, #eff1f5)) !important; stroke: var(--ctp-peach, #fe640b) !important; stroke-dasharray: 4 4 !important; stroke-width: 2px !important; } .node.log :is(rect, circle, polygon, path, ellipse) { fill: color-mix(in srgb, var(--ctp-mauve, #8839ef) 15%, var(--ctp-base, #eff1f5)) !important; stroke: var(--ctp-mauve, #8839ef) !important; stroke-dasharray: 4 4 !important; stroke-width: 2px !important; } .node.final :is(rect, circle, polygon, path, ellipse) { fill: color-mix(in srgb, var(--ctp-green, #40a02b) 20%, var(--ctp-base, #eff1f5)) !important; stroke: var(--ctp-green, #40a02b) !important; stroke-width: 2.5px !important; } .label, .nodeLabel, .cluster span { font-family: var(--mermaid-font-family, inherit); font-weight: var(--mermaid-font-weight, inherit); }"}}%%

Using the results from the previous block, we can see that

  • \((-1,-1)\) and \((1,1)\) are extrema of \(f(x,y)\)
  • \((0,0)\) is a saddle point.

To check what type of extrema we have, we evaluate either \(f_{xx}\) or \(f_{yy}\) at the point—their signs must be the same! For good measure, we will also evaluate the function at the points, just to see what we can see.

for sol in real_solutions:
  print(f"{sol} gives f = {f.subs(sol)} and fxx = {fxx.subs(sol)}")
[x == -1, y == -1] gives f = -2 and fxx = 12
[x == 1, y == 1] gives f = -2 and fxx = 12
[x == 0, y == 0] gives f = 0 and fxx = 0

In particular, \(f_{xx} > 0\) at \((x,y)=(-1,-1)\) and \((1,1),\) so these are local minima!

Example 19.2 (Critical points of \(f(x,y) = (x^2 + y^2) e^{-x}\)) Computing the gradient is a bit more work—we need to use the product rule: \[ \Gradient{f} = \Vector{ 2x e^{-x} - (x^2 + y^2) e^{-x}, 2y e^{-x} } = e^{-x} \Vector{ -x^2 + 2x - y^2, 2y }. \]

Since \(e^{-x} > 0\) for all \(x,\) the critical points are determined by the equation \[ \Vector{ -x^2 + 2x - y^2, 2y } = \vec{0}. \]

flowchart LR
  A("-x²+2x-y² = 0 <br> 2y = 0")
  A --> B("x(2-x) = 0 <br> y = 0")
  B -->|"x=0"| C("(x,y) = (0,0)"):::solution
  B -->|"x=2"| D("(x,y) = (2,0)"):::solution
%% GENERATED from _cells.scss and _palette.scss by mermaid-classes.py; do not edit.
%% Node classes: solution, contradiction, engine, source, auxiliary, log, final.
%%{init: {"themeCSS": ".node.solution :is(rect, circle, polygon, path, ellipse) { fill: color-mix(in srgb, var(--ctp-green, #40a02b) 15%, var(--ctp-base, #eff1f5)) !important; stroke: var(--ctp-green, #40a02b) !important; stroke-width: 2px !important; } .node.contradiction :is(rect, circle, polygon, path, ellipse) { fill: color-mix(in srgb, var(--ctp-red, #d20f39) 15%, var(--ctp-base, #eff1f5)) !important; stroke: var(--ctp-red, #d20f39) !important; stroke-width: 3px !important; } .node.engine :is(rect, circle, polygon, path, ellipse) { fill: color-mix(in srgb, var(--ctp-red, #d20f39) 15%, var(--ctp-base, #eff1f5)) !important; stroke: var(--ctp-red, #d20f39) !important; stroke-width: 2px !important; } .node.source :is(rect, circle, polygon, path, ellipse) { fill: color-mix(in srgb, var(--ctp-blue, #1e66f5) 15%, var(--ctp-base, #eff1f5)) !important; stroke: var(--ctp-blue, #1e66f5) !important; stroke-width: 2px !important; } .node.auxiliary :is(rect, circle, polygon, path, ellipse) { fill: color-mix(in srgb, var(--ctp-peach, #fe640b) 15%, var(--ctp-base, #eff1f5)) !important; stroke: var(--ctp-peach, #fe640b) !important; stroke-dasharray: 4 4 !important; stroke-width: 2px !important; } .node.log :is(rect, circle, polygon, path, ellipse) { fill: color-mix(in srgb, var(--ctp-mauve, #8839ef) 15%, var(--ctp-base, #eff1f5)) !important; stroke: var(--ctp-mauve, #8839ef) !important; stroke-dasharray: 4 4 !important; stroke-width: 2px !important; } .node.final :is(rect, circle, polygon, path, ellipse) { fill: color-mix(in srgb, var(--ctp-green, #40a02b) 20%, var(--ctp-base, #eff1f5)) !important; stroke: var(--ctp-green, #40a02b) !important; stroke-width: 2.5px !important; } .label, .nodeLabel, .cluster span { font-family: var(--mermaid-font-family, inherit); font-weight: var(--mermaid-font-weight, inherit); }"}}%%

We compute \(f_{xx} = (x^2+y^2-4x+2)e^{-x},\) \(f_{xy} = -2 e^{-x} y,\) and \(f_{yy} = 2 e^{-x},\) so \[ D = 2 (x^2-y^2-4x+2) e^{-2x}. \] Ultimately, we see that:

  • \((0,0)\) is a local minimum (\(D=4, f_{xx}=2\))
  • \((2,0)\) is a saddle (\(D=-\frac{4}{e^4}\)).

We can confirm all this with a good graphic:

var('x y z')
pts = point(
  [(0,0,0), (2,0,4/e^2)], 
  color='black', size=50
)
surface = implicit_plot3d(
  z == (x^2 + y^2)/e^x,
  (x, -1, 3), (y, -2, 2), (z, -1, 1),
  opacity = 0.7
)
show(surface + pts)

While we’re at it, let’s make another of our gradient vector plots, to remind ourselves how the second derivative tests confirms our many graphical ways of understanding functions of two variables:

var('x y')
f = (x^2 + y^2)/e^x
fx = diff(f,x)
fy = diff(f,y)

level_curves = contour_plot(
    f, (x, -1, 3), (y, -2, 2), 
    cmap='Blues', contours = (0, 1/e^2, 2/e^2, 3/e^2, 4/e^2), zorder=1
)
gradient_field = plot_vector_field(
    (fx/sqrt(fx^2+fy^2), fy/sqrt(fx^2+fy^2)), 
    (x, -1, 3), (y, -2, 2), 
    color='orange', headwidth=5, zorder=2
)
pts = point(
  [(0,0), (2,0)], 
  color='red', size=50, zorder=3
)
show(level_curves + gradient_field + pts)

How could we identify the critical points from the level curves or gradient vectors alone?

Example 19.3 (Critical points of \(f(x,y) = x y - x^2 y - x y^2\)) We have \(\Gradient{f} = \Vector{ y(1-2x-y), x(1-x-2y) }\). Thus we have:

flowchart LR
  A("y(1-2x-y) = 0 <br> x(1-x-2y) = 0")
  A -->|"y=0"| B("x(1-x)=0")
  A -->|"y=1-2x"| C("x(3x-1)=0")
  B -->|"x=0"| D("(x,y) = (0,0)"):::solution
  B -->|"x=1"| E("(x,y) = (1,0)"):::solution
  C -->|"x=0"| F("(x,y) = (0,1)"):::solution
  C -->|"x=1/3"| G("(x,y) = (1/3,1/3)"):::solution
%% GENERATED from _cells.scss and _palette.scss by mermaid-classes.py; do not edit.
%% Node classes: solution, contradiction, engine, source, auxiliary, log, final.
%%{init: {"themeCSS": ".node.solution :is(rect, circle, polygon, path, ellipse) { fill: color-mix(in srgb, var(--ctp-green, #40a02b) 15%, var(--ctp-base, #eff1f5)) !important; stroke: var(--ctp-green, #40a02b) !important; stroke-width: 2px !important; } .node.contradiction :is(rect, circle, polygon, path, ellipse) { fill: color-mix(in srgb, var(--ctp-red, #d20f39) 15%, var(--ctp-base, #eff1f5)) !important; stroke: var(--ctp-red, #d20f39) !important; stroke-width: 3px !important; } .node.engine :is(rect, circle, polygon, path, ellipse) { fill: color-mix(in srgb, var(--ctp-red, #d20f39) 15%, var(--ctp-base, #eff1f5)) !important; stroke: var(--ctp-red, #d20f39) !important; stroke-width: 2px !important; } .node.source :is(rect, circle, polygon, path, ellipse) { fill: color-mix(in srgb, var(--ctp-blue, #1e66f5) 15%, var(--ctp-base, #eff1f5)) !important; stroke: var(--ctp-blue, #1e66f5) !important; stroke-width: 2px !important; } .node.auxiliary :is(rect, circle, polygon, path, ellipse) { fill: color-mix(in srgb, var(--ctp-peach, #fe640b) 15%, var(--ctp-base, #eff1f5)) !important; stroke: var(--ctp-peach, #fe640b) !important; stroke-dasharray: 4 4 !important; stroke-width: 2px !important; } .node.log :is(rect, circle, polygon, path, ellipse) { fill: color-mix(in srgb, var(--ctp-mauve, #8839ef) 15%, var(--ctp-base, #eff1f5)) !important; stroke: var(--ctp-mauve, #8839ef) !important; stroke-dasharray: 4 4 !important; stroke-width: 2px !important; } .node.final :is(rect, circle, polygon, path, ellipse) { fill: color-mix(in srgb, var(--ctp-green, #40a02b) 20%, var(--ctp-base, #eff1f5)) !important; stroke: var(--ctp-green, #40a02b) !important; stroke-width: 2.5px !important; } .label, .nodeLabel, .cluster span { font-family: var(--mermaid-font-family, inherit); font-weight: var(--mermaid-font-weight, inherit); }"}}%%

So there are four critical points: \((0,0),\) \((1,0),\) \((0,1),\) and \((\tfrac{1}{3},\tfrac{1}{3})\). Since \(f_{xx} = -2y,\) \(f_{yy} = -2x,\) and \(f_{xy} = 1-2x-2y,\) we have \[ D(0,0) = D(1,0) = D(0,1) = -1 \quad \text{and} \quad D(\tfrac{1}{3},\tfrac{1}{3}) = \tfrac{1}{3}. \] Since \(f_{xx}(\tfrac{1}{3},\tfrac{1}{3}) = -\tfrac{2}{3},\) the first three of these are saddles and the last is a local maximum. We’ll visualize these with a contour plot:

var('x y')
level_curves = contour_plot(
  x*y - x^2*y - x*y^2,
  (x, -0.75, 1.25), (y, -0.75, 1.25),
  contours=12, 
  cmap='Blues', colorbar=True, zorder=1
)
pts = point(
  [(0,0), (1,0), (0,1), (1/3,1/3)], 
  color='yellow', size=50, zorder=2
)
show(level_curves + pts)