Skip to main content

The second derivative and convexity

The second derivative measures how fast the slope changes: acceleration for a moving cart, bending for a graph. Use it to tell peaks from valleys in one step, find inflection points, and prove that the flat point of a convex function, such as a squared-error loss, is its lowest point.
M04.4.2L1 Core25 minIn review

Before this lesson

M04.4.1 Critical points and the first-derivative test

After this lesson you can

  • Compute a second derivative and read it as the rate of change of the slope: acceleration for the running trip, bending for a graph.
  • Find where a function is concave up or concave down, and locate its inflection points.
  • Classify critical points with the second-derivative test, and recognise when the test says nothing.
  • Prove that a flat point of a function whose second derivative is never negative is its lowest point, and apply this to the running loss.

In review. The maths and code on this page are checked by automated tests on every change, but an independent reader has not signed it off yet. Spot a mistake or a confusing step? Tell us.

The question

The running loss \({L(w)} = {7.5w^2 - 23.5w + 18.5}\) has one flat point, at \({w = 47/30}\). The double well \({x^4 - 2x^2}\) has three, and one of them is a peak. A training method that stops where the slope is zero needs to know which kind of curve it is on. What property of a function guarantees that a flat point is the lowest point of all, with nothing deeper anywhere else?

From the last lesson

1. State Fermat’s theorem, with both of its conditions on \(c\).

Show answer

If \(f\) has a local maximum or minimum at \(c\), \(c\) is not an end of the domain, and \({f'(c)}\) exists, then \({f'(c) = 0}\).

2. Why is 0 a critical point of \({x^3}\) but not an extreme?

Show answer

Its derivative \({3x^2}\) is zero at 0, but positive on both sides, so \({x^3}\) rises before 0 and after it.

Intuition

The first derivative is the slope. The second derivative is the slope of the slope: it says how fast the tilt of the tangent changes as you move right.

Walk along \({x^2}\). At \({x = -1}\) the tangent slopes down with slope \({-2}\), at 0 it is flat, and at 1 it slopes up with slope 2. The slopes rise, the tangent turns anticlockwise, and the curve bends up like a cup. Along \({-x^2}\) the slopes fall, from 2 through 0 to \({-2}\), the tangent turns clockwise, and the curve bends down like a cap.

Two panels without axes. Left, titled Slopes rise: concave up: a blue U-shaped curve, x squared, with three green tangent arrows pointing right: at the left one points steeply down, labelled slope minus 2; at the bottom one is flat, labelled slope 0; at the right one points steeply up, labelled slope plus 2. Inside the cup: x squared, f double prime equals 2, greater than 0. Right, titled Slopes fall: concave down: a blue upside-down U, minus x squared, with tangent arrows labelled slope plus 2 on the left, slope 0 at the top and slope minus 2 on the right. Under the cap: minus x squared, f double prime equals minus 2, less than 0.
M04.4.2-F1

Read the slopes from left to right. Rising slopes make a cup, falling slopes make a cap. The second derivative is the rate at which they rise or fall.

So the sign of the second derivative says which way a curve bends, and that settles the flat points: where the tangent is flat and the curve bends up, the point is a valley; where it bends down, a peak.

Build it up

Step 1. Differentiate twice. The second derivative of \(f\), written \({f''}\), is the derivative of \({f'}\). For the running cubic \({g'(x) = 3x^2 - 3}\), so \({g''(x) = 6x}\).

Step 2. Acceleration. For a moving object, position differentiates to velocity and velocity to acceleration. The running trip has position \({s(t) = 2t^2 - t^3/3}\) metres, so

\[ \begin{aligned} s'(t) &= 4t - t^2 = v(t), \\ s''(t) &= 4 - 2t = a(t) . \end{aligned} \]

The units divide by time twice: metres per second per second, m/s². At \({t = 1}\) the acceleration is 2 m/s² and the cart is speeding up; at \({t = 3}\) it is \({-2}\) m/s² and the cart is slowing down. At \({t = 2}\) it is 0, and the speed is at its top, 4 m/s.

Two panels sharing a time axis from 0 to 4 seconds. Top, titled Velocity v of t equals 4 t minus t squared, in metres per second: a blue arch from 0 up to 4 at t equals 2 and back to 0 at t equals 4, with a ring at the top. Green tangent arrows at t equals 1, 2 and 3 are labelled slope 2, slope 0: top speed, and slope minus 2. Bottom, titled Acceleration a of t equals v prime of t equals 4 minus 2 t, in metres per second squared: a sky-blue line falling from 4 to minus 4, with dots at a of 1 equals 2, at zero when t equals 2, and at a of 3 equals minus 2. Above the axis on the left: speeding up, a greater than 0. Below it on the right: slowing down, a less than 0.
M04.4.2-F2

The running trip. The acceleration (bottom) is the slope of the velocity (top) at each moment: positive while the cart speeds up, zero at top speed, negative while it slows down.

Step 3. Bending. On a graph the same number measures bending. If \({f'' > 0}\) on an interval, then \({f'}\) is increasing there: this is the rising theorem of M04.4.1, applied to \({f'}\), whose derivative is \({f''}\). The slopes rise from left to right, and the curve bends up.

Step 4. Flat and bending up. Now take a critical point \(c\) with \({f'(c) = 0}\) and \({f''(c) > 0}\). The slope is increasing through \(c\), so it is negative just before \(c\) and positive just after. By the first-derivative test, \(c\) is a valley. One number did the work of a sign chart.

The formal statement

NoteDefinitions: concavity and inflection

Let \(f\) be differentiable on an open interval \(I\). \(f\) is concave up on \(I\) if \({f'}\) is increasing on \(I\), and concave down on \(I\) if \({f'}\) is decreasing on \(I\). A point where \(f\) is continuous and its concavity changes is an inflection point.

Test for concavity (Step 3). If \({f''(x) > 0}\) for every \(x\) in \(I\), \(f\) is concave up on \(I\); if \({f''(x) < 0}\) for every \(x\) in \(I\), it is concave down.

NoteTheorem: the second-derivative test

Suppose \({f'(c) = 0}\) and \({f''(c)}\) exists.

  • If \({f''(c) > 0}\), \(f\) has a local minimum at \(c\).
  • If \({f''(c) < 0}\), \(f\) has a local maximum at \(c\).
  • If \({f''(c) = 0}\), the test says nothing.

Proof. Take \({f''(c) > 0}\). The second derivative is the derivative of \({f'}\), and \({f'(c) = 0}\), so

\[ \begin{aligned} f''(c) &= \lim_{h \to 0} \frac{f'(c + h) - f'(c)}{h} \\ &= \lim_{h \to 0} \frac{f'(c + h)}{h} . \end{aligned} \]

A positive limit forces the quotient to be positive for every \(h\) close enough to 0 (the argument of M04.4.1, with tolerance half the limit). So for small \(h\), \({f'(c + h)}\) has the sign of \(h\): negative just left of \(c\), positive just right. The first-derivative test gives a local minimum. If \({f''(c) < 0}\) the signs swap, and \(c\) is a local maximum.

The third case is silent for a reason. \({x^4}\), \({-x^4}\) and \({x^3}\) all have \({f'(0) = 0}\) and \({f''(0) = 0}\), yet 0 is a valley of the first, a peak of the second and neither for the third. When \({f''(c) = 0}\), go back to the first-derivative test.

Three small panels, each a blue curve through the origin with a flat green tangent there and the note f prime of 0 equals 0, f double prime of 0 equals 0. First, titled x to the fourth: a valley: a flat-bottomed U with a black dot at the bottom. Second, titled minus x to the fourth: a peak: a flat-topped cap with a white ring at the top. Third, titled x cubed: neither: a curve that rises, flattens at a grey dot and rises again.
M04.4.2-F4

Three functions with the same first and second derivative at 0, and three different answers.

Now the result that machine learning cares about most.

NoteDefinition: convex

A function with a second derivative is convex on an interval if \({f''(x) \geq 0}\) at every point of the interval.

This is the working definition of this course. The general one, which needs no derivatives, comes in M08.2 and agrees with this one for such functions. Convex asks a little less than concave up: the slope must never fall, but it may stay level, so even a straight line counts as convex.

NoteTheorem: the flat point of a convex function is its lowest point

If \(f\) is convex on an interval \(I\) and \({f'(c) = 0}\) at a point \(c\) inside \(I\), then \({f(x) \geq f(c)}\) for every \(x\) in \(I\).

Proof. First, \({f'}\) never decreases. For \({u < v}\) in \(I\), the mean value theorem applied to \({f'}\) gives a \(\xi\) between them with

\[ \begin{aligned} f'(v) - f'(u) &= f''(\xi)\,(v - u) \\ &\geq 0 , \end{aligned} \]

because \({f''(\xi) \geq 0}\) and \({v - u > 0}\).

To the right of \(c\). Take \(x\) in \(I\) with \({x > c}\). The mean value theorem applied to \(f\) on \({[c, x]}\) gives a \(\xi\) with \({c < \xi < x}\) and

\[ f(x) - f(c) = f'(\xi)\,(x - c) . \]

Since \({\xi > c}\) and \({f'}\) never decreases, \({f'(\xi) \geq f'(c) = 0}\), and \({x - c > 0}\). So the product is at least 0, and \({f(x) \geq f(c)}\).

To the left. For \({x < c}\) the same steps give \({x < \xi < c}\), so \({f'(\xi) \leq f'(c) = 0}\) while \({x - c < 0}\). A product of two numbers that are not positive is at least 0, so again \({f(x) \geq f(c)}\).

The running loss is convex. \({L'(w) = 15w - 23.5}\) (M04.3.1), so \({L''(w) = 15 > 0}\) for every \(w\). \({L'}\) is zero only at \({w = 23.5/15 = 47/30}\), so \({L(w)} \geq {L(47/30)} \approx {0.092}\) for every \(w\). No search, however long, can find a lower loss.

The double well is not. Its \({f''(x) = 12x^2 - 4}\) is negative when \({x^2 < 1/3}\), that is between about \({-0.577}\) and 0.577. The theorem does not apply, and rightly so: its flat point at 0 is a peak.

Worked example

Classify the critical points of the running cubic with the second derivative, and find its inflection point.

Step 1. From M04.4.1, \({g'(x) = 3x^2 - 3}\), zero at \({x = -1}\) and \({x = 1}\). Differentiating again gives \({g''(x) = 6x}\).

Step 2: the test. \({g''(-1) = -6 < 0}\), so \({x = -1}\) is a local maximum, with \({g(-1) = 2}\). \({g''(1) = 6 > 0}\), so \({x = 1}\) is a local minimum, with \({g(1) = -2}\). The same answers as the sign chart, from one number each.

Step 3: concavity. \({6x}\) is negative for \({x < 0}\) and positive for \({x > 0}\). So \(g\) is concave down on the left half of the line and concave up on the right half.

Step 4: inflection. The concavity changes at 0 and \(g\) is continuous there, so \({(0, g(0))}\), which is \({(0, 0)}\), is an inflection point. There the slope \({g'(0) = -3}\) is at its most negative: it stops falling and starts rising.

Two panels sharing an x-axis from minus 2 to 2, with dashed lines at x equals minus 1, 0 and 1. Top, titled The running cubic g of x equals x cubed minus 3 x: the blue curve with a ring at the peak, labelled peak 2, a black dot at the valley, labelled valley minus 2, and a black diamond at the origin labelled inflection. The left part is labelled concave down and the right part concave up. Bottom, titled Its second derivative g double prime of x equals 6 x: a sky-blue straight line through the origin, with dots at g double prime of minus 1 equals minus 6, less than 0, and g double prime of 1 equals 6, greater than 0. The region left of 0 is labelled g double prime less than 0 and the region right of 0 g double prime greater than 0.
M04.4.2-F3

The worked example. The sign of \({g''}\) at each critical point names it, and the place where \({g''}\) changes sign is the inflection point.

Step 5: convex? No: \({g'' < 0}\) for \({x < 0}\). That is why the valley at 1 is not the lowest point of \(g\).

Code

From scratch, the second derivative found by hand is checked with the second central difference. Estimate the slope at \({x + h/2}\) by \({(f(x + h) - f(x))/h}\) and the slope at \({x - h/2}\) by \({(f(x) - f(x - h))/h}\). Their difference divided by \(h\) estimates \({f''(x)}\):

\[ \begin{aligned} f''(x) \approx \frac{1}{h^2}\big(&f(x + h) - 2f(x) \\ &+ f(x - h)\big) . \end{aligned} \]

def second_difference(f, x, h=1e-4):
    return (f(x + h) - 2 * f(x) + f(x - h)) / h**2

def g(x):
    return x**3 - 3 * x

def well(x):
    return x**4 - 2 * x**2

def loss(w):
    return 7.5 * w**2 - 23.5 * w + 18.5

for x in [-1.0, 0.0, 1.0]:
    print(f"x = {x:4.1f}:  g'' by hand {6 * x:5.1f},  numerically {second_difference(g, x):8.4f}")
print("L'' numerically at w = 0, 1, 2:", [round(second_difference(loss, w), 3) for w in (0, 1, 2)])
x = -1.0:  g'' by hand  -6.0,  numerically  -6.0000
x =  0.0:  g'' by hand   0.0,  numerically   0.0000
x =  1.0:  g'' by hand   6.0,  numerically   6.0000
L'' numerically at w = 0, 1, 2: [15.0, 15.0, 15.0]

The test itself, with the curvature measured the same way:

def second_derivative_test(f, c, h=1e-4):
    curvature = second_difference(f, c, h)
    if curvature > 1e-6:
        return "local minimum"
    if curvature < -1e-6:
        return "local maximum"
    return "the test is silent"

for c in (-1, 0, 1):
    print(f"double well at x = {c:2d}: {second_derivative_test(well, c)}")
print("x**4 at x = 0:", second_derivative_test(lambda x: x**4, 0))
double well at x = -1: local minimum
double well at x =  0: local maximum
double well at x =  1: local minimum
x**4 at x = 0: the test is silent

The library version differentiates exact polynomials twice and finds where the double well stops being convex:

import numpy as np
from numpy.polynomial import Polynomial

well_poly = Polynomial([0, 0, -2, 0, 1])          # x**4 - 2 x**2
curvature = well_poly.deriv(2)                      # 12 x**2 - 4
edges = np.sort(curvature.roots())
print("coefficients of f'':", curvature.coef)
print("f'' < 0 between", edges.round(3))
print("coefficients of L'':", Polynomial([18.5, -23.5, 7.5]).deriv(2).coef)
coefficients of f'': [-4.  0. 12.]
f'' < 0 between [-0.577  0.577]
coefficients of L'': [15.]

Explore

The descent explorer in lesson M04.4.4 marks the sign of the second derivative under the ball. Choose the double well and drag the ball across the middle: \({f''}\) turns negative between about \({-0.58}\) and 0.58.

Pitfalls

Two panels. Left, marked with a cross and titled Not convex: x to the fourth minus 2 x squared: the blue double well, with flat green tangents at three points: stars at the two valleys at x equals minus 1 and 1, height minus 1, and a white ring at x equals 0, height 0, labelled flat, but a peak. The middle band between about minus 0.58 and 0.58 is shaded sky blue and labelled f double prime less than 0 here. Right, marked with a tick and titled Convex: the loss L of w: the purple running loss, a U-shaped curve for w from about 0.4 to 2.7, with L double prime equals 15, greater than 0, written above it. Two green tangent lines cross the whole panel, each touching the curve at one green dot and staying below it, labelled tangents stay below. A flat green tangent and a star mark the lowest point near w equals 1.57.
M04.4.2-F5

Wrong beside right. On the double well a flat point can be a peak; on the convex running loss the one flat point is the lowest point, and every tangent line stays below the curve.

“The slope is zero here, so this is the minimum.”

Only on a convex function. The double well is flat at 0 and has a peak there. The running loss is convex, so its one flat point is the bottom. The squared-error loss of a linear model is convex; the loss of a neural network usually is not, so there a flat point may be a valley, a peak or neither (M08.11).

“\({f''(c) = 0}\), so \(c\) is an inflection point.”

\({x^4}\) has \({f''(0) = 0}\), yet \({f''(x) = 12x^2}\) is positive on both sides: concave up throughout, no inflection, and 0 is its lowest point. An inflection needs \({f''}\) to change sign, as \({6x}\) does for the running cubic.

“A convex function always has a lowest point.”

\({e^x}\) is convex, since its second derivative \({e^x}\) is positive, but its slope \({e^x}\) is never zero and it has no minimum: it sinks towards 0 as \(x\) moves left and never gets there. The theorem says a flat point of a convex function is the lowest point. It does not promise a flat point.

Check yourself

Recall

1. In words, what does \({f''}\) measure?

Show answer

How fast the slope \({f'}\) changes: acceleration for a moving object, bending for a graph.

2. What is the running trip’s acceleration at \({t = 3}\), and what does it mean?

Show answer

\({a(3) = 4 - 2 \cdot 3 = -2}\) m/s². It is negative, so the cart is slowing down.

3. State the second-derivative test.

Show answer

If \({f'(c) = 0}\) and \({f''(c) > 0}\), \(c\) is a local minimum; if \({f''(c) < 0}\), a local maximum; if \({f''(c) = 0}\), the test says nothing.

4. What does the test say about \({x^4}\) at 0, and what is the truth?

Show answer

Nothing, because \({f''(0) = 0}\). In fact 0 is the lowest point, since \({x^4 \geq 0}\) everywhere.

5. Why is \({w = 47/30}\) the lowest point of the running loss, not just a local minimum?

Show answer

\({L''(w) = 15 > 0}\) for every \(w\), so \(L\) is convex, and a flat point of a convex function is its lowest point.

Exercises

1. ★ For \({f(x) = x^3 - 6x^2 + 9x + 1}\), classify the critical points with the second-derivative test and find the inflection point.

Show solution

\({f'(x)} = {3x^2 - 12x + 9} = {3(x - 1)(x - 3)}\), zero at 1 and 3, and \({f''(x) = 6x - 12}\). At 1, \({f''(1) = -6 < 0}\): a local maximum, \({f(1) = 1 - 6 + 9 + 1 = 5}\). At 3, \({f''(3) = 6 > 0}\): a local minimum, \({f(3)} = {27 - 54 + 27 + 1} = {1}\). \({f''}\) changes sign at \({x = 2}\), so the inflection point is \({(2, f(2)) = (2, 3)}\).

2. ★★ Show that softplus, \({s(z) = \ln(1 + e^z)}\), is convex on the whole line but has no lowest point.

Show solution

By the chain rule (M04.3.4), with outer function \({\ln u}\) and inner \({u = 1 + e^z}\), \({s'(z) = e^z/(1 + e^z)}\). Dividing top and bottom by \({e^z}\) gives \({1/(e^{-z} + 1)}\), the sigmoid \({\sigma(z)}\). So \({s''(z)} = {\sigma'(z)} = {\sigma(z)(1 - \sigma(z))}\) (M04.3.3). Both factors lie between 0 and 1, so \({s'' > 0}\): \(s\) is convex. But \({s'(z) = \sigma(z) > 0}\) for every \(z\), so there is no critical point, and \(s\) is increasing: every value is beaten by the values to its left. As \(z\) falls, \({s(z)}\) sinks towards \({\ln 1 = 0}\) without reaching it.

3. ★★★ Prove that a convex function lies above each of its tangent lines: if \({f'' \geq 0}\) on an interval \(I\) and \(a\) is inside \(I\), then for every \(x\) in \(I\),

\[ f(x) \geq f(a) + f'(a)\,(x - a) . \]

Show solution

Let \({\varphi(x)} = {f(x) - f'(a)(x - a)}\). Then \({\varphi'(x)} = {f'(x) - f'(a)}\) and \({\varphi''(x) = f''(x) \geq 0}\), so \(\varphi\) is convex on \(I\), and \({\varphi'(a) = 0}\). By the theorem, \({\varphi(x) \geq \varphi(a) = f(a)}\) for every \(x\) in \(I\), which rearranges to the inequality. So on a convex loss the tangent line, the linear model behind a gradient step (M04.4.4), never lies above the loss: the true loss is always at least what the line predicts.

Summary card

The second derivative is the slope of the slope: it says which way a curve bends.

The anchor figure again: a blue cup, x squared, whose green tangent slopes rise from minus 2 through 0 to plus 2, beside a blue cap, minus x squared, whose tangent slopes fall from plus 2 through 0 to minus 2.
M04.4.2-F1

Rising slopes, a cup; falling slopes, a cap.

  • \({f'' > 0}\): concave up; \({f'' < 0}\): concave down. An inflection is where the sign changes.
  • Second-derivative test: \({f'(c) = 0}\) with \({f''(c) > 0}\) is a valley, with \({f''(c) < 0}\) a peak; \({f''(c) = 0}\) says nothing.
  • Convex, \({f'' \geq 0}\) on an interval: a flat point is the lowest point.
  • The running loss has \({L'' = 15}\), so \({w = 47/30}\) is its lowest point.

Sources and next steps

Sources. Each was opened and checked against this lesson on 12 November 2026.

  • Gilbert Strang and Edwin Herman, Calculus Volume 1 (OpenStax), Section 4.5: concave up and down defined by \({f'}\) increasing or decreasing, the test for concavity (Theorem 4.10), inflection points, and the second derivative test (Theorem 4.11), inconclusive when \({f''(c) = 0}\), shown with \({x^3}\), \({x^4}\) and \({-x^4}\).
  • Matthew Boelkins, Active Calculus, Section 1.6: the second derivative (Definition 1.6.4) as the rate of change of the first, read as acceleration, and concavity defined by an increasing derivative (Definition 1.6.8).
  • Stephen Boyd and Lieven Vandenberghe, Convex Optimization, free online. Section 3.1.4: a function on an interval is convex exactly when \({f'' \geq 0}\), which means its derivative never decreases; \({x^4}\) has zero second derivative at 0. Section 3.1.3: a convex function lies above its tangent lines, so a point with zero derivative is a global minimiser.
  • Marc Peter Deisenroth, A. Aldo Faisal and Cheng Soon Ong, Mathematics for Machine Learning, Section 7.3: the general definition of a convex function (Definition 7.3) and the second-order condition for twice-differentiable functions.

Next. M04.4.3 Optimisation problems solved exactly turns these tests into a recipe and derives two results that data science uses daily: the mean and the maximum likelihood estimate.

This lesson unlocks

M04.4.3 Optimisation problems solved exactly

Your progress is saved in this browser only. No account, no tracking.
Back to top