TExcelTAD
Home › Calculus Essentials › Maxima, Minima and Curve Shape

Maxima, Minima and Curve Shape

The first and second derivatives tell you the shape of a graph without plotting it: where it rises and falls, where it peaks and bottoms out, and which way it bends. Finding the lowest point of a function is also exactly what training a machine-learning model means.

Increasing, decreasing and critical points

Sign of f′f' on an interval Graph
f′>0f' > 0 increasing
f′<0f' < 0 decreasing

A critical point is an xx where f′(x)=0f'(x) = 0 or f′(x)f'(x) does not exist. Local maxima and minima (the peaks and valleys) can only happen at critical points (Fermat's theorem): at a smooth peak the tangent must be horizontal.

Note: The converse fails. f(x)=x3f(x) = x^3 has f′(0)=0f'(0) = 0, but 0 is neither a maximum nor a minimum: the graph flattens briefly and keeps rising.

Two tests

First derivative test. Look at the sign of f′f' on each side of the critical point cc: +→−+ \to - gives a local maximum, −→+- \to + a local minimum, no sign change means neither.

Second derivative test. If f′(c)=0f'(c) = 0: f′′(c)<0f''(c) < 0 → local maximum (concave down), f′′(c)>0f''(c) > 0 → local minimum (concave up), f′′(c)=0f''(c) = 0 → the test is inconclusive.

Inflection points are where the concavity changes, i.e. where f′′f'' changes sign.

A full analysis

Take f(x)=x3−6x2+9x+1f(x) = x^3 - 6x^2 + 9x + 1:

f′(x)=3x2−12x+9=3(x−1)(x−3)critical points x=1, x=3f′′(x)=6x−12f′′=0 at x=2f′′(1)=−6<0→local max f(1)=5f′′(3)=+6>0→local min f(3)=1f′′ changes sign at x=2→inflection point (2,3)\begin{aligned} f'(x) &= 3x^2 - 12x + 9 = 3(x - 1)(x - 3) && \text{critical points } x = 1,\ x = 3 \\ f''(x) &= 6x - 12 && f'' = 0 \text{ at } x = 2 \\[1ex] f''(1) &= -6 < 0 \to \text{local max } f(1) = 5 \\ f''(3) &= +6 > 0 \to \text{local min } f(3) = 1 \\ &f'' \text{ changes sign at } x = 2 \to \text{inflection point } (2, 3) \end{aligned}
python · try it yourself
import sympy as sp

x = sp.symbols('x')
f = x**3 - 6 * x**2 + 9 * x + 1
f1, f2 = sp.diff(f, x), sp.diff(f, x, 2)
for c in sp.solve(f1, x):
    kind = "max" if f2.subs(x, c) < 0 else "min" if f2.subs(x, c) > 0 else "?"
    print(f"critical x = {c}: f = {f.subs(x, c)}, f'' = {f2.subs(x, c)} -> local {kind}")
print("inflection at x =", sp.solve(f2, x))
critical x = 1: f = 5, f'' = -6 -> local max
critical x = 3: f = 1, f'' = 6 -> local min
inflection at x = [2]

And a picture with the critical points and inflection point marked:

python · try it yourself
import numpy as np
import matplotlib.pyplot as plt

xs = np.linspace(-0.5, 4.5, 300)
f = lambda x: x**3 - 6 * x**2 + 9 * x + 1
plt.figure(figsize=(6, 4))
plt.plot(xs, f(xs), label="f(x) = x³ − 6x² + 9x + 1")
plt.plot([1, 3], [f(1), f(3)], "o", label="local max / min (f′ = 0)")
plt.plot([2], [f(2)], "s", label="inflection (f″ = 0)")
plt.axhline(0, color="gray", lw=0.5)
plt.legend(); plt.grid(alpha=0.3); plt.title("Shape from derivatives")
print("f(1), f(2), f(3) =", f(1), f(2), f(3))
f(1), f(2), f(3) = 5 3 1

Absolute extrema on a closed interval

A continuous function on [a,b][a, b] always has a largest and smallest value there (the Extreme Value Theorem). To find them, compare ff at the critical points inside and at both endpoints. For our ff on [0,5][0, 5]:

f(0)=1,f(1)=5,f(3)=1,f(5)=21absolute max 21 at x=5 (an endpoint!), absolute min 1 at x=0 and x=3\begin{aligned} &f(0) = 1, \quad f(1) = 5, \quad f(3) = 1, \quad f(5) = 21 \\ &\text{absolute max 21 at } x = 5 \text{ (an endpoint!), absolute min 1 at } x = 0 \text{ and } x = 3 \end{aligned}

Endpoints are easy to forget, and they often win.

Numeric evidence: scanning a grid

When the derivative is messy, a fine grid search gives a quick check of the analytic answer:

python · try it yourself
import numpy as np

xs = np.linspace(0, 5, 500_001)
ys = xs**3 - 6 * xs**2 + 9 * xs + 1
interior = (xs > 0.2) & (xs < 2)
i = np.argmax(np.where(interior, ys, -np.inf))
print(f"local max near x = {xs[i]:.4f}, f = {ys[i]:.6f}")
print(f"absolute max on [0, 5]: x = {xs[np.argmax(ys)]}, f = {ys.max():.1f}")
print(f"absolute min on [0, 5]: f = {ys.min():.6f}")
local max near x = 1.0000, f = 5.000000
absolute max on [0, 5]: x = 5.0, f = 21.0
absolute min on [0, 5]: f = 1.000000

In software: minimising a loss

Training a model means choosing parameters ww that minimise a loss L(w)L(w), the error on the training data. With one parameter the recipe is the one above: solve L′(w)=0L'(w) = 0 and check L′′>0L'' > 0. Fitting y≈w⋅xy \approx w \cdot x to points by least squares:

L(w)=∑(wxi−yi)2L′(w)=∑2xi(wxi−yi)=0  ⟹  w=∑xiyi∑xi2L′′(w)=2∑xi2>0  ⟹  a minimum\begin{aligned} L(w) &= \sum (w x_i - y_i)^2 \\ L'(w) &= \sum 2x_i(w x_i - y_i) = 0 \implies w = \frac{\sum x_i y_i}{\sum x_i^2} \\ L''(w) &= 2 \sum x_i^2 > 0 \implies \text{a minimum} \end{aligned}

Real models have millions of parameters and no closed-form solution, so instead of solving L′=0L' = 0 they repeatedly step downhill, w←w−η⋅L′(w)w \leftarrow w - \eta \cdot L'(w): that is gradient descent. Concepts from this lesson survive in many dimensions: critical points, local versus global minima, and saddle points (the multidimensional cousin of the flat spot of x3x^3), which are common in neural-network losses.

Exercises

Exercise 1: Find and classify

Find and classify the critical points of f(x)=x4−8x2f(x) = x^4 - 8x^2.

Answer

Local max at x=0x = 0 (f=0f = 0); local minima at x=±2x = \pm 2 (f=−16f = -16).

Solution
f′=4x3−16x=4x(x2−4)=0  ⟹  x=0,±2f′′=12x2−16f′′(0)=−16<0→local max, f(0)=0f′′(±2)=32>0→local min, f(±2)=16−32=−16\begin{aligned} f' &= 4x^3 - 16x = 4x(x^2 - 4) = 0 \implies x = 0, \pm 2 \\ f'' &= 12x^2 - 16 \\ f''(0) &= -16 < 0 \to \text{local max}, \ f(0) = 0 \\ f''(\pm 2) &= 32 > 0 \to \text{local min}, \ f(\pm 2) = 16 - 32 = -16 \end{aligned}

Exercise 2: Closed interval

Find the absolute maximum and minimum of g(x)=2x3−3x2−12xg(x) = 2x^3 - 3x^2 - 12x on [−2,3][-2, 3].

Answer

Max 7 at x=−1x = -1; min −20-20 at x=2x = 2.

Solution
g′=6x2−6x−12=6(x−2)(x+1)critical x=−1,2g(−2)=−16−12+24=−4g(−1)=−2−3+12=7g(2)=16−12−24=−20g(3)=54−27−36=−9\begin{aligned} g' &= 6x^2 - 6x - 12 = 6(x - 2)(x + 1) && \text{critical } x = -1, 2 \\ g(-2) &= -16 - 12 + 24 = -4 \\ g(-1) &= -2 - 3 + 12 = 7 \\ g(2) &= 16 - 12 - 24 = -20 \\ g(3) &= 54 - 27 - 36 = -9 \end{aligned}

Exercise 3: Inflection points

Find the inflection points of h(x)=xe−xh(x) = x e^{-x}.

Answer

One inflection point at x=2x = 2, h(2)=2/e2≈0.271h(2) = 2/e^2 \approx 0.271.

Solution
h′=e−x−xe−x=(1−x)e−xh′′=−e−x−(1−x)e−x=(x−2)e−xh′′ changes sign at x=2(e−x>0 always)\begin{aligned} h' &= e^{-x} - x e^{-x} = (1 - x) e^{-x} \\ h'' &= -e^{-x} - (1 - x) e^{-x} = (x - 2) e^{-x} \\ &h'' \text{ changes sign at } x = 2 \quad (e^{-x} > 0 \text{ always}) \end{aligned}

Exercise 4: Best slope through the origin

Find the least-squares ww for y≈w⋅xy \approx w \cdot x with the points (1,2.1),(2,3.9),(3,6.2),(4,7.8)(1, 2.1), (2, 3.9), (3, 6.2), (4, 7.8), using w=∑xy/∑x2w = \sum xy / \sum x^2. Confirm L′(w)=0L'(w) = 0 numerically.

Answer

w=59.7/30=1.99w = 59.7 / 30 = 1.99

Solution
python · try it yourself
xs = [1, 2, 3, 4]
ys = [2.1, 3.9, 6.2, 7.8]
w = sum(x * y for x, y in zip(xs, ys)) / sum(x * x for x in xs)
L = lambda w: sum((w * x - y) ** 2 for x, y in zip(xs, ys))
h = 1e-6
print(f"w = {w:.4f}")
print(f"L'(w) ≈ {(L(w + h) - L(w - h)) / (2 * h):.6f}")
w = 1.9900
L'(w) ≈ -0.000000

Test yourself

f′(c)=0f'(c) = 0 and f′′(c)=5f''(c) = 5. What is at x=cx = c?

  1. A local maximum
  2. An inflection point
  3. Nothing can be said
  4. A local minimum

Why must you check the endpoints when finding the absolute maximum on [a,b][a, b]?

  1. Derivatives are always zero there
  2. The largest value may occur at an endpoint where f′f' is not zero
  3. Endpoints are inflection points
  4. You don't need to

f(x)=x3f(x) = x^3 at x=0x = 0 has f′(0)=0f'(0) = 0. What is x=0x = 0?

  1. A local minimum
  2. A local maximum
  3. Neither: ff keeps increasing (an inflection point)
  4. A discontinuity