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 on an interval | Graph |
|---|---|
| increasing | |
| decreasing |
A critical point is an where or 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. has , 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 on each side of the critical point : gives a local maximum, a local minimum, no sign change means neither.
Second derivative test. If : → local maximum (concave down), → local minimum (concave up), → the test is inconclusive.
Inflection points are where the concavity changes, i.e. where changes sign.
A full analysis
Take :
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:
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 always has a largest and smallest value there (the Extreme Value Theorem). To find them, compare at the critical points inside and at both endpoints. For our on :
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:
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 that minimise a loss , the error on the training data. With one parameter the recipe is the one above: solve and check . Fitting to points by least squares:
Real models have millions of parameters and no closed-form solution, so instead of solving they repeatedly step downhill, : 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 ), which are common in neural-network losses.
Exercises
Exercise 1: Find and classify
Find and classify the critical points of .
Answer
Local max at (); local minima at ().
Solution
Exercise 2: Closed interval
Find the absolute maximum and minimum of on .
Answer
Max 7 at ; min at .
Solution
Exercise 3: Inflection points
Find the inflection points of .
Answer
One inflection point at , .
Solution
Exercise 4: Best slope through the origin
Find the least-squares for with the points , using . Confirm numerically.
Answer
Solution
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
and . What is at ?
- A local maximum
- An inflection point
- Nothing can be said
- A local minimum
Why must you check the endpoints when finding the absolute maximum on ?
- Derivatives are always zero there
- The largest value may occur at an endpoint where is not zero
- Endpoints are inflection points
- You don't need to
at has . What is ?
- A local minimum
- A local maximum
- Neither: keeps increasing (an inflection point)
- A discontinuity