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=∫ab∣f(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 f≥gf \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) dx−∫abg(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]:

  • x≥x2x \geq x^2 on [0,1][0, 1] since x−x2=x(1−x)≥0x - x^2 = x(1-x) \geq 0
  • Area =∫01(x−x2) dx=[x22−x33]01=12−13=16≈0.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 ∫ab∣f(x)−g(x)∣ dx\int_a^b |f(x) - g(x)|\, dx.

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