Lesson 14 of 18

Area Between Curves

Area Between Two Curves

The area between two curves f(x)f(x) and g(x)g(x) on [a,b][a, b] is:

A=abf(x)g(x)dxA = \int_a^b |f(x) - g(x)|\, dx

The absolute value handles the case where the curves cross (one is above the other at different points).

When fgf \geq g Everywhere

If f(x)g(x)f(x) \geq g(x) throughout [a,b][a, b], the area simplifies to:

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

Finding Intersection Points

To split the interval where the curves cross, find xx where f(x)=g(x)f(x) = g(x) — this is a root-finding problem.

Classic Example

Area between y=xy = x and y=x2y = x^2 on [0,1][0, 1]:

  • xx2x \geq x^2 on [0,1][0, 1] since xx2=x(1x)0x - x^2 = x(1-x) \geq 0
  • Area =01(xx2)dx=[x22x33]01=1213=160.1667= \int_0^1 (x - x^2)\, dx = \left[\frac{x^2}{2} - \frac{x^3}{3}\right]_0^1 = \frac{1}{2} - \frac{1}{3} = \frac{1}{6} \approx 0.1667

Numerically

Use the midpoint rule on f(x)g(x)|f(x) - g(x)|:

for (int i = 0; i < n; i++) {
    double x = a + (i + 0.5) * h;
    double diff = f(x) - g(x);
    sum += (diff < 0 ? -diff : diff);  /* abs */
}

Your Task

Implement double area_between(double (*f)(double), double (*g)(double), double a, double b, int n) that returns abf(x)g(x)dx\int_a^b |f(x) - g(x)|\, dx.

TCC compiler loading...
Loading...
Click "Run" to execute your code.