Convex Function Explained with Worked Examples

A function is convex if for : the chord lies on or above the graph. For differentiable this means .

Write 2x or 2*x to multiply, ^ for powers, and ; between simultaneous equations. You can also type factor, expand, derivative of, integrate … from a to b, limit … as x->0 or 15% of 80.

Try an example

Before you start

This page is easier if you are comfortable with:

What Convex function means

Test with , , : . The function has , so it is convex (the second derivative, concavity and inflection). But is not convex on all of : with , , , is larger than .

The key property: for a convex function every local minimum is a global minimum, and the set of minimisers is convex. That is why convex problems can be solved reliably (gradient descent, Newton's method, the KKT conditions, LP duality). Sums and maxima of convex functions are convex.

Convexity gives Jensen's inequality (Jensen's inequality) and hence AM–GM. It appears in probability (variance via ), in the KL divergence (KL divergence) and in the existence of minimisers on compact sets (Heine–Borel and compactness).

What is it used for?

A convex function curves upwards so that any chord lies above the graph.

How to use convex function

  1. Test the definition with sample points, or compute .
  2. If everywhere, is convex.
  3. One violation of the chord inequality shows is not convex.
  4. Use convexity to conclude that local minima are global.

Graph

f(x) = x² and the chord from (0, 0) to (4, 16)f(2) = 4chord at 2: 800.511.522.533.540246810121416f(x) = x² and the chord from (0, 0) to (4, 16)xyf(x) = x²chordGraph with x from 0 to 4 and y from 0 to 17. Shows the curve f(x) = x²; the curve chord; point f(2) = 4 at (2, 4); point chord at 2: 8 at (2, 8).

The chord is above the curve between the two points.

Worked examples

These examples are worked by hand, step by step, so you can compare your own working.

Example 1: x² chord test

Test convexity of at , , : compare and the chord value.

Worked example
Answer
4≤84\le8
  1. Left
  2. Right

Example 2: x⁴

Find the sign of for at .

Worked example
Answer
48≥0 (convex)48\ge0\ (\text{convex})
  1. f″

Example 3: x³ fails

Test at , , .

Worked example
Answer
−1>−4 (not convex)-1>-4\ (\text{not convex})
  1. f(−1)
  2. Chord

Special cases

A few situations come up again and again. Knowing them saves time and helps you spot answers that cannot be right.

x² is convex

f(2) = 4 ≤ (f(1) + f(3))/2 = 5

Midpoint below the chord.

eˣ is convex

f(1) = 2.718 ≤ (f(0) + f(2))/2 = 4.19

Chord above the curve.

√x is concave

f(2) = 1.414 ≥ (f(1) + f(3))/2 = 1.366

Inequality reversed.

Second-derivative test

f″ ≥ 0 everywhere ⇒ convex

x²: f″ = 2.

Word problems

f is convex if f(λx + (1 − λ)y) ≤ λf(x) + (1 − λ)f(y) for λ ∈ [0, 1]. For twice-differentiable f this holds iff f″ ≥ 0 (or the Hessian is positive semi-definite).

x² at the midpoint

Check convexity of x² with points 1 and 3.

  1. f(2)4
  2. Average of f(1), f(3)5
  3. Check: 4 ≤ 5 ✓Convex.

eˣ

Check eˣ with points 0 and 2 at x = 1.

  1. f(1)2.718
  2. Average4.19
  3. Check: 2.718 ≤ 4.19 ✓Convex.

√x Higher

Is √x convex? Test points 1 and 3.

  1. f(2)1.414
  2. Average1.366
  3. Check: 1.414 > 1.366, concave ✓Concave.

Hessian Higher

Is f = xy convex? Find the Hessian determinant.

  1. Hessian(0 1; 1 0)
  2. det−1
  3. Check: negative determinant, not convex ✓Not convex.

Common mistakes

Reversing the inequality

f(mid) ≥ chord average ✗

f(mid) ≤ chord average for convex ✓

Convex curves lie below chords.

Using one test pair

one pair proves convexity ✗

must hold for all pairs ✓

Use f″ ≥ 0.

f″ ≥ 0 at a point only

convex if f″(0) ≥ 0 ✗

f″ ≥ 0 everywhere ✓

Global condition.

Confusing convex and concave

x² is concave ✗

x² is convex ✓

Convex ∪-shaped.

Exam tips

  1. f″ ≥ 0 ⇒ convex.
  2. Chord above the curve.
  3. Hessian PSD in several variables.
  4. Convex: local min = global min.
  5. Concave: −f is convex.

Practice questions

Try each one on paper, then open the answer. Use the solver above to see the full working.

1. f″ of x² Foundation

2

2. f″ of x⁴ at 1 Foundation

12

3. Average of f(0), f(4) for f = x² Foundation

8

4. f(2) for f = x² vs average 8 Higher

4

5. Hessian determinant of x² + y² Higher

4

6. Hessian determinant of xy Higher

-1

Practice generator

Unlimited fresh questions with instant marking. Choose a level, work it out on paper, then type your answer.

Solve: …

Key terms

Convex function
Lies below its chords.
Concave function
Lies above its chords.
Hessian
Matrix of second partials.
Positive semi-definite
Eigenvalues ≥ 0.
Jensen’s inequality
f(E X) ≤ E f(X).
Global minimum
Lowest value overall.

Frequently asked questions

What is a convex function?

One whose chords lie above its graph.

How do I test it?

Check f″ ≥ 0 (Hessian PSD).

Is x² convex?

Yes.

Why does convexity matter?

Any local minimum is global.

Which page covers KKT?

KKT conditions.

Which page covers gradient descent?

gradient descent.

More in Numerical methods and optimisation and the formula sheets.