TExcelTAD
Home › Calculus in Depth › Areas Between Curves

Areas Between Curves

The first application of the definite integral generalises "area under a curve" to the area between two curves. The recipe is short: subtract, find where the curves cross, and integrate the absolute difference, sometimes with respect to yy instead of xx. The same calculation measures income inequality (the Gini coefficient) and classifier quality (the area under a ROC curve). For the basics see Area.

The formula

If ff and gg are continuous and f(x)≥g(x)f(x) \ge g(x) on [a,b][a, b], the area of the region between them is

A=∫ab[f(x)−g(x)] dxA = \int_a^b [f(x) - g(x)]\,dx

Why. Slice the region into vertical strips of width Δx\Delta x. A strip at xi∗x_i^* is approximately a rectangle of height f(xi∗)−g(xi∗)f(x_i^*) - g(x_i^*). Adding the strips gives a Riemann sum for f−gf - g, and its limit is the integral. The position of the axis does not matter: if both curves are shifted up by cc, the difference is unchanged. That is why no case analysis about signs of ff and gg themselves is needed.

If the curves cross, the "top" curve changes. In general

A=∫ab∣f(x)−g(x)∣ dxA = \int_a^b \lvert f(x) - g(x) \rvert\,dx

and to evaluate it by hand, split [a,b][a, b] at the crossing points.

Example: a parabola and a line

Region bounded by y=4−x2y = 4 - x^2 and y=x+2y = x + 2. They meet where 4−x2=x+24 - x^2 = x + 2, i.e. x2+x−2=(x+2)(x−1)=0x^2 + x - 2 = (x + 2)(x - 1) = 0, so at x=−2x = -2 and x=1x = 1. In between the parabola is on top (test x=0x = 0: 4>24 > 2).

A=∫−21[(4−x2)−(x+2)] dx=∫−21(2−x−x2) dx=[2x−x22−x33]−21=(2−12−13)−(−4−2+83)=76+103=92\begin{aligned} A &= \int_{-2}^{1} [(4 - x^2) - (x + 2)]\,dx = \int_{-2}^{1} (2 - x - x^2)\,dx \\ &= \Big[2x - \frac{x^2}{2} - \frac{x^3}{3}\Big]_{-2}^{1} = \Big(2 - \frac{1}{2} - \frac{1}{3}\Big) - \Big(-4 - 2 + \frac{8}{3}\Big) = \frac{7}{6} + \frac{10}{3} = \frac{9}{2} \end{aligned}
python · try it yourself
import sympy as sp

x = sp.symbols('x', real=True)
top, bottom = 4 - x**2, x + 2
a, b = sorted(sp.solve(sp.Eq(top, bottom), x))
print("intersections:", a, b)
print("area:", sp.integrate(top - bottom, (x, a, b)))
intersections: -2 1
area: 9/2

Curves that cross several times

sin⁡x\sin x and cos⁡x\cos x on [0,2π][0, 2\pi] cross at π/4\pi/4 and 5π/45\pi/4. Splitting there:

A=∫0π/4(cos⁡−sin⁡)+∫π/45π/4(sin⁡−cos⁡)+∫5π/42π(cos⁡−sin⁡)=(2−1)+22+(2+1)=42A = \int_0^{\pi/4} (\cos - \sin) + \int_{\pi/4}^{5\pi/4} (\sin - \cos) + \int_{5\pi/4}^{2\pi} (\cos - \sin) = (\sqrt{2} - 1) + 2\sqrt{2} + (\sqrt{2} + 1) = 4\sqrt{2}

Numerically you can integrate ∣f−g∣\lvert f - g \rvert directly. The kinks at the crossings are where adaptive quadrature works hardest, so quad accepts a points list of trouble spots; for this smooth case both calls agree to all printed digits, but with sharper kinks (or a fixed-step rule) the hint matters:

python · try it yourself
import numpy as np
from scipy.integrate import quad

h = lambda x: abs(np.sin(x) - np.cos(x))
plain, _ = quad(h, 0, 2 * np.pi)
split, _ = quad(h, 0, 2 * np.pi, points=[np.pi / 4, 5 * np.pi / 4])
print(f"quad |sin - cos|            = {plain:.12f}")
print(f"quad with crossing points   = {split:.12f}")
print(f"exact 4√2                   = {4 * np.sqrt(2):.12f}")
quad |sin - cos|            = 5.656854249492
quad with crossing points   = 5.656854249492
exact 4√2                   = 5.656854249492

Integrating with respect to y

Some regions are bounded by curves of the form x=f(y)x = f(y). Then slice horizontally: A=∫cd[xright(y)−xleft(y)] dyA = \int_c^d [x_{\text{right}}(y) - x_{\text{left}}(y)]\,dy.

Region between x=y2−1x = y^2 - 1 and x=y+1x = y + 1. They meet where y2−1=y+1y^2 - 1 = y + 1, i.e. y=−1y = -1 and y=2y = 2; the line is to the right in between.

A=∫−12[(y+1)−(y2−1)] dy=∫−12(y+2−y2) dy=[y22+2y−y33]−12=103+76=92A = \int_{-1}^{2} [(y + 1) - (y^2 - 1)]\,dy = \int_{-1}^{2} (y + 2 - y^2)\,dy = \Big[\frac{y^2}{2} + 2y - \frac{y^3}{3}\Big]_{-1}^{2} = \frac{10}{3} + \frac{7}{6} = \frac{9}{2}

Slicing vertically would need two integrals, because the lower boundary switches from the left half of the parabola to the line at x=0x = 0. Choosing the right direction saves work.

Application: the Gini coefficient

The Lorenz curve L(p)L(p) of an income distribution is the share of total income earned by the poorest fraction pp of people. Perfect equality is the line L(p)=pL(p) = p. The Gini coefficient is

G=area between p and L(p)area under p=2∫01[p−L(p)] dpG = \frac{\text{area between } p \text{ and } L(p)}{\text{area under } p} = 2\int_0^1 [p - L(p)]\,dp

from 0 (equal) to 1 (one person has everything). With data, the Lorenz curve is piecewise linear and the integral is a trapezoid sum:

python · try it yourself
import numpy as np

incomes = np.sort(np.array([12, 18, 22, 25, 30, 35, 41, 55, 80, 182], float))   # thousands
p = np.linspace(0, 1, len(incomes) + 1)
L = np.concatenate(([0], np.cumsum(incomes) / incomes.sum()))
area_under_L = float(np.sum((L[:-1] + L[1:]) / 2 * np.diff(p)))   # trapezoid rule
gini = 2 * (0.5 - area_under_L)
print("Lorenz curve:", np.round(L, 3))
print(f"Gini coefficient = {gini:.4f}")
Lorenz curve: [0.    0.024 0.06  0.104 0.154 0.214 0.284 0.366 0.476 0.636 1.   ]
Gini coefficient = 0.4364

In software

  • ROC AUC. A classifier's ROC curve plots true-positive rate against false-positive rate; the area under it (AUC) is the probability that a random positive scores above a random negative. sklearn.metrics.auc is a trapezoid rule. The area between the ROC curve and the diagonal measures skill above chance, and 2⋅AUC−12 \cdot \text{AUC} - 1 is the Gini coefficient of the classifier.
  • Diffing curves. The integral of ∣f−g∣\lvert f - g \rvert (the L1L^1 distance) compares a model's prediction with measurements, two audio envelopes, or two CDFs (the 1-D Wasserstein distance used in ML is exactly ∫∣F1−F2∣\int \lvert F_1 - F_2 \rvert).
  • Polygon area. The shoelace formula for polygon area is the "area between curves" idea applied to straight edges: signed trapezoids under each edge, added up.

Exercises

Exercise 1: Two parabolas

Find the area between y=x2y = x^2 and y=8−x2y = 8 - x^2.

Answer

643\frac{64}{3}.

Solution
x2=8−x2⇒x=±2A=∫−22(8−2x2) dx=2∫02(8−2x2) dx=2(16−163)=643\begin{aligned} &x^2 = 8 - x^2 \Rightarrow x = \pm 2 \\ &A = \int_{-2}^{2} (8 - 2x^2)\,dx = 2\int_0^2 (8 - 2x^2)\,dx = 2\Big(16 - \frac{16}{3}\Big) = \frac{64}{3} \end{aligned}

Exercise 2: Crossing curves

Find the total area between y=x3y = x^3 and y=xy = x on [−1,1][-1, 1].

Answer

12\frac{1}{2}.

Solution

They cross at −1,0,1-1, 0, 1. On [0,1][0, 1], x≥x3x \ge x^3 and ∫01(x−x3) dx=12−14=14\int_0^1 (x - x^3)\,dx = \frac{1}{2} - \frac{1}{4} = \frac{1}{4}. By odd symmetry the area on [−1,0][-1, 0] is also 14\frac{1}{4}. Total 12\frac{1}{2}. (The signed integral ∫−11(x−x3) dx\int_{-1}^{1} (x - x^3)\,dx is 0.)

python · try it yourself
import sympy as sp
x = sp.symbols('x', real=True)
print(sp.integrate(sp.Abs(x - x**3), (x, -1, 1)))
1/2

Exercise 3: Slice horizontally

Find the area bounded by x=4−y2x = 4 - y^2 and the yy-axis.

Answer

323\frac{32}{3}.

Solution

The curve meets x=0x = 0 at y=±2y = \pm 2: A=∫−22(4−y2) dy=16−163=323A = \int_{-2}^{2} (4 - y^2)\,dy = 16 - \frac{16}{3} = \frac{32}{3}.

Exercise 4: Gini of a formula

The Lorenz curve of a model economy is L(p)=p3L(p) = p^3. Compute its Gini coefficient.

Answer

G=12G = \frac{1}{2}.

Solution

G=2∫01(p−p3) dp=2(12−14)=12G = 2\int_0^1 (p - p^3)\,dp = 2\Big(\frac{1}{2} - \frac{1}{4}\Big) = \frac{1}{2}.

Test yourself

Two curves cross inside [a,b][a, b]. What does ∫ab(f−g) dx\int_a^b (f - g)\,dx compute?

  1. The total area between them
  2. The net (signed) area, with parts where g>fg > f subtracted
  3. Zero
  4. The area under ff

When is integrating with respect to yy the better choice?

  1. Never
  2. Only for circles
  3. When the curves are polynomials
  4. When the region's left and right boundaries are single curves x=f(y)x = f(y), but the top or bottom boundary changes

What does a Gini coefficient of 0 mean?

  1. One person has all income
  2. No data
  3. Perfect equality: the Lorenz curve is the line L(p)=pL(p) = p
  4. Half the population has no income