Mathwords logoMathwords

Convex Function — Definition, Formula & Examples

A convex function is a function whose graph curves upward, meaning the line segment connecting any two points on the graph never dips below the graph itself. Informally, a convex function "holds water" like a bowl.

A function f:IRf: I \to \mathbb{R} defined on a convex set II is convex if for all x,yIx, y \in I and all λ[0,1]\lambda \in [0, 1], the inequality f(λx+(1λ)y)λf(x)+(1λ)f(y)f(\lambda x + (1-\lambda)y) \leq \lambda f(x) + (1-\lambda) f(y) holds. If ff is twice differentiable, this is equivalent to f(x)0f''(x) \geq 0 for all xx in the domain.

Key Formula

f(λx+(1λ)y)λf(x)+(1λ)f(y)f(\lambda x + (1 - \lambda)y) \leq \lambda\, f(x) + (1 - \lambda)\, f(y)
Where:
  • ff = The function being tested for convexity
  • x,yx, y = Any two points in the domain
  • λ\lambda = A parameter between 0 and 1 that weights the two points

How It Works

To check whether a twice-differentiable function is convex, compute its second derivative and verify that f(x)0f''(x) \geq 0 throughout the domain. When f(x)>0f''(x) > 0 everywhere, the function is strictly convex, meaning the chord between any two distinct points lies strictly above the graph. Convexity guarantees that any local minimum is also a global minimum, which is why convex functions are central to optimization. If you cannot compute a second derivative, you can fall back on the definition: pick any two points and check whether the secant line stays on or above the curve.

Worked Example

Problem: Show that f(x)=x2f(x) = x^2 is convex using the second-derivative test.
Step 1: Compute the first derivative.
f(x)=2xf'(x) = 2x
Step 2: Compute the second derivative.
f(x)=2f''(x) = 2
Step 3: Check the sign of the second derivative. Since f(x)=2>0f''(x) = 2 > 0 for all xx, the condition f(x)0f''(x) \geq 0 is satisfied everywhere.
Answer: Because f(x)=2>0f''(x) = 2 > 0 for all xx, f(x)=x2f(x) = x^2 is strictly convex on R\mathbb{R}.

Why It Matters

In machine learning and operations research, convex functions ensure that gradient descent and similar algorithms converge to a global optimum rather than getting stuck at a local one. Many loss functions—such as mean squared error and cross-entropy—are designed to be convex for exactly this reason.

Common Mistakes

Mistake: Confusing convex functions with concave functions based on everyday language. Students sometimes think "convex" means it bulges outward (downward).
Correction: A convex function curves upward (like a cup), meaning f0f'' \geq 0. A concave function curves downward (like a cap), meaning f0f'' \leq 0. The chord test settles any confusion: if the chord is above the curve, the function is convex.