Why This Matters
The definite integral from a to b is the net signed area between the function and the x-axis. "Signed" means area above the axis is positive and area below the axis is negative. This distinction is crucial: the integral of sin(x) over a full period is zero because the positive and negative regions cancel exactly.
The integration bounds a (lower) and b (upper) specify the interval. Unlike indefinite integrals which produce a family of functions, definite integrals produce a single number. This number represents accumulated change: the total distance traveled, total work done, total probability in a range, or total revenue over a period.
The trapezoidal rule improves on basic Riemann sums by using trapezoids instead of rectangles. Each trapezoid matches the function at both endpoints of a subinterval, giving a better approximation with the same number of subintervals. This simple upgrade is the basis for many practical numerical integration methods.
Define Terms
Visual Model
The full process at a glance. Click Start tour to walk through each step.
The definite integral is signed area: positive above the axis, negative below. The trapezoidal rule provides an accurate numerical approximation.
Code Example
// Trapezoidal rule for definite integrals
function trapezoidalRule(f, a, b, n) {
const dx = (b - a) / n;
let sum = f(a) + f(b); // First and last terms
for (let i = 1; i < n; i++) {
sum += 2 * f(a + i * dx); // Middle terms counted twice
}
return (dx / 2) * sum;
}
// Test: integral of x^2 from 0 to 3 (exact = 9)
console.log("Integral of x^2 from 0 to 3 (exact = 9):");
for (const n of [4, 10, 100, 1000]) {
const result = trapezoidalRule(x => x * x, 0, 3, n);
const error = Math.abs(result - 9);
console.log(`n=${String(n).padStart(4)}: result=${result.toFixed(8)}, error=${error.toExponential(2)}`);
}
// Signed area: integral of sin(x) from 0 to 2*pi (exact = 0)
console.log("\nIntegral of sin(x) from 0 to 2*pi (exact = 0):");
const sinResult = trapezoidalRule(Math.sin, 0, 2 * Math.PI, 1000);
console.log(`Result: ${sinResult.toFixed(10)} (positive and negative cancel)`);
// Integral of sin(x) from 0 to pi (exact = 2)
console.log("\nIntegral of sin(x) from 0 to pi (exact = 2):");
const sinHalf = trapezoidalRule(Math.sin, 0, Math.PI, 1000);
console.log(`Result: ${sinHalf.toFixed(8)}`);
// Total vs signed area
console.log("\nSigned area of sin(x) on [0, 2pi]:", sinResult.toFixed(6));
const totalArea = trapezoidalRule(x => Math.abs(Math.sin(x)), 0, 2 * Math.PI, 1000);
console.log("Total (unsigned) area:", totalArea.toFixed(6)); // = 4
// Properties of definite integrals
const int_0_2 = trapezoidalRule(x => x * x, 0, 2, 1000);
const int_2_3 = trapezoidalRule(x => x * x, 2, 3, 1000);
const int_0_3 = trapezoidalRule(x => x * x, 0, 3, 1000);
console.log(`\nAdditivity: int(0,2) + int(2,3) = ${(int_0_2 + int_2_3).toFixed(4)}, int(0,3) = ${int_0_3.toFixed(4)}`);Interactive Experiment
Try these exercises:
- Compute the integral of cos(x) from 0 to pi/2 using the trapezoidal rule. The exact answer is 1.
- Compute the integral of e^x from 0 to 1. The exact answer is e - 1 (about 1.71828).
- Try integrating 1/x from 1 to e. The exact answer is 1 (this is ln(e)).
- Compare the trapezoidal rule with n = 10 to the midpoint Riemann sum with n = 10 for sin(x) on [0, pi]. Which is more accurate?
- Compute the integral of x from -2 to 2. Why is the signed area 0 even though the function is not zero?
Quick Quiz
Coding Challenge
Write a function called `trapIntegral` that takes a function f, lower bound a, upper bound b, and number of trapezoids n. Implement the trapezoidal rule: T = (dx/2) * [f(x0) + 2*f(x1) + 2*f(x2) + ... + 2*f(x_{n-1}) + f(xn)]. Round the result to 4 decimal places.
Real-World Usage
Definite integrals measure accumulated quantities throughout science:
- Physics: Work is the integral of force over distance. Impulse is the integral of force over time. Energy is the integral of power over time.
- Probability: The probability that a continuous random variable falls in [a, b] is the integral of its PDF from a to b. Every probability calculation is a definite integral.
- Economics: Consumer surplus is the integral of the demand curve minus price. Total revenue over a period is the integral of the revenue rate.
- Biology: Total drug exposure (AUC -- area under the concentration curve) is computed as a definite integral and is critical for dosing decisions.
- Data science: The area under the ROC curve (AUC-ROC) is a definite integral used to evaluate classifier performance.