calculus25 min

Area Between Curves

Computing the area enclosed between two functions using definite integrals and Riemann sums

0/9Not Started

Why This Matters

In the real world, you often need to measure the difference between two quantities, not just the size of one. How much extra revenue does strategy A generate over strategy B? How much more fuel does one car burn than another over a trip? These questions ask for the area between curves -- the integral of the difference between two functions.

The key step is determining which function is the upper function and which is the lower function on each subinterval. The area between them is the integral of (upper minus lower). If the curves cross, you need to split the integral at the intersection points so that you always subtract the lower from the upper -- otherwise positive and negative regions cancel and you get the wrong answer.

Finding intersection points is essential because they define the boundaries of the enclosed region. At these points, the two functions have equal values, and the roles of "upper" and "lower" may switch. Setting f(x) = g(x) and solving for x gives you the intersection points, which become the limits of integration.

Define Terms

Visual Model

Two Functions f and gDefined on same interval
Find IntersectionsSolve f(x) = g(x)
Determine Upper/LowerWhich is larger on each subinterval
Integrate |f - g|Upper minus lower on each piece
Split at CrossingsSeparate regions if curves cross
Total AreaSum of all region areas

The full process at a glance. Click Start tour to walk through each step.

Area between curves: find intersections, determine upper/lower, and integrate the difference. Split at crossings to avoid cancellation.

Code Example

Code
// Area between curves using numerical integration
function trapezoidalRule(f, a, b, n = 10000) {
  const dx = (b - a) / n;
  let sum = f(a) + f(b);
  for (let i = 1; i < n; i++) {
    sum += 2 * f(a + i * dx);
  }
  return (dx / 2) * sum;
}

// Find intersection points by scanning for sign changes in f - g
function findIntersections(f, g, a, b, steps = 10000) {
  const points = [];
  const dx = (b - a) / steps;
  for (let i = 0; i < steps; i++) {
    const x1 = a + i * dx;
    const x2 = x1 + dx;
    const d1 = f(x1) - g(x1);
    const d2 = f(x2) - g(x2);
    if (d1 * d2 < 0) {
      let lo = x1, hi = x2;
      for (let j = 0; j < 50; j++) {
        const mid = (lo + hi) / 2;
        if ((f(mid) - g(mid)) * (f(lo) - g(lo)) <= 0) hi = mid;
        else lo = mid;
      }
      points.push(parseFloat(((lo + hi) / 2).toFixed(6)));
    }
  }
  return points;
}

// Area between f(x) = x and g(x) = x^2 on [0, 1]
const f = x => x;
const g = x => x * x;

const intersections = findIntersections(f, g, -1, 2);
console.log("f(x) = x, g(x) = x^2");
console.log("Intersections:", intersections);

// Area = integral of |f - g|
const area = trapezoidalRule(x => Math.abs(f(x) - g(x)), 0, 1);
console.log(`Area between curves: ${area.toFixed(6)}`);
console.log(`Exact: 1/6 = ${(1/6).toFixed(6)}\n`);

// Example 2: sin(x) and cos(x) from 0 to pi
const f2 = Math.sin;
const g2 = Math.cos;
const ints2 = findIntersections(f2, g2, 0, Math.PI);
console.log("sin(x) vs cos(x) on [0, pi]");
console.log("Intersection at x =", ints2[0].toFixed(4), "(pi/4)");
const area2 = trapezoidalRule(x => Math.abs(f2(x) - g2(x)), 0, Math.PI);
console.log(`Area: ${area2.toFixed(6)}`);

// Riemann sum visualization
console.log("\nRiemann sum convergence for area between x and x^2:");
for (const n of [10, 100, 1000, 10000]) {
  const dx = 1 / n;
  let rsum = 0;
  for (let i = 0; i < n; i++) {
    const x = (i + 0.5) * dx;
    rsum += Math.abs(f(x) - g(x)) * dx;
  }
  console.log(`  n=${String(n).padStart(5)}: area = ${rsum.toFixed(8)}`);  
}

Interactive Experiment

Try these exercises:

  • Find the area between y = x^3 and y = x on [-1, 1]. Be careful: the curves cross at x = -1, 0, and 1.
  • Compute the area between y = sin(x) and y = 0 on [0, 2pi]. Why is this different from the integral of sin(x) on [0, 2pi]?
  • Find the area enclosed between y = x^2 and y = 4 (the region below the horizontal line and above the parabola).
  • Modify the code to handle vertical slicing (integrating with respect to x) vs horizontal slicing (integrating with respect to y). When is each approach easier?
  • Find the area between e^x and 1 from x = 0 to x = 1. The exact answer is e - 2.

Quick Quiz

Coding Challenge

Approximate Area Between Two Functions Using Riemann Sums

Write a function called `areaBetween` that takes two functions f and g, and interval endpoints a and b. Compute the area between the curves using a midpoint Riemann sum with n = 10000 rectangles. Each rectangle has height |f(x_mid) - g(x_mid)| and width dx. Round the result to 4 decimal places.

Loading editor...

Real-World Usage

Area between curves has many practical applications:

  • Economics: Consumer surplus is the area between the demand curve and the price line. Producer surplus is the area between the price line and the supply curve.
  • Medicine: Bioequivalence testing compares drug concentration curves. The area between the reference and test drug curves indicates how different their absorption profiles are.
  • Climate science: The area between actual and target emissions curves measures cumulative excess emissions -- a key metric in climate policy.
  • Statistics: The area between two distribution curves quantifies how different two populations are. The Kolmogorov-Smirnov test uses the maximum vertical distance between CDFs.
  • Engineering: The area between the stress curve and the yield strength line indicates how much a material has been overstressed, predicting fatigue failure.

Connections