Skip to content
FoundationSource checked

Trapezoidal Rule

Approximates an integral by summing trapezoid areas under sampled function values.

Quick Facts

Compact metadata for implementation and discovery.

Category
Numerical Methods
Difficulty
Beginner
Family
Numerical Integration
Tier
Foundation
Status
Verified
Review
Source Checked
Sources
1
Formulas
1
Relationships
4
Code example
Ready
Visualization
Ready

Overview

The Trapezoidal Rule replaces the curve between neighboring sample points with a straight line and adds the resulting trapezoid areas.

Core Concepts

numerical integrationsample spacingtrapezoid areacomposite rule

Quick facts

Complexity

Runtime and memory behavior at a glance.

Best

O(n)

Average

O(n)

Worst

O(n)

Space

O(1)

When to Use

  • Use it for simple numerical integration of sampled or easy-to-evaluate functions.
  • Use it when moderate accuracy is enough and the function is reasonably smooth.
  • Use composite trapezoids over many intervals for better accuracy.

When Not to Use

  • Avoid it when the function has sharp curvature or discontinuities and higher accuracy is required.
  • Avoid it when adaptive integration is needed to handle difficult regions.
  • Avoid assuming error is small without checking interval width.

What It Does

  • Approximates an integral by summing trapezoids under sampled function values
  • Uses sample spacing and trapezoid area to guide the work.
  • Produces approximate numeric result.

Use Cases

  • Solving equations
  • Approximating integrals
  • Working with matrices and differential equations

Inputs

  • Numeric function, matrix, or sampled data
  • Initial estimate or discretization
  • Tolerance

Outputs

  • Approximate numeric result
  • Error estimate or residual

Key Formulas (1)

Trapezoidal estimate

integralab f(x) dx ≈ h · (f(a)/2 + sum f(xi) + f(b)/2)
integral_a^b f(x) dx ~= h * (f(a)/2 + sum f(x_i) + f(b)/2)

Approximates an integral by summing trapezoid areas.

Pseudocode

  1. 1.Prepare the inputs and state needed for Trapezoidal Rule.
  2. 2.Apply the sample spacing step while the stopping condition has not been met.
  3. 3.Update intermediate state and discard invalid candidates.
  4. 4.Return approximate numeric result.

Canonical Example

Input
f(x) = x on [0, 2] with one interval
Output
area = 2
Explanation
The trapezoid has parallel sides 0 and 2 with width 2, so its area is (0 + 2) / 2 * 2.

Implementation Notes

  • For uniform spacing, weight interior samples twice and endpoints once.
  • For nonuniform spacing, sum each trapezoid using its own width.
  • Compare results at smaller step sizes to estimate convergence.

Common Pitfalls

  • Using too few intervals for a curved function.
  • Forgetting endpoint weights in the composite formula.
  • Mixing sample spacing when the formula assumes uniform spacing.

Interactive Mechanism Walkthrough

Iterative Convergence & State Trajectory

Show the numeric approximation improving against the target function, matrix, or state.

Trapezoidal Rule mechanism walkthrough step 1 of 4x₀x₁x₂x*
Step 1 of 4: Prepare the inputs and state needed for Trapezoidal Rule.
numerical-methods-mechanism

Notes

The trapezoidal rule is exact for linear functions over each interval.

Sources (1)

What each reference supports for this entry.

Numerical Recipes

Book

Reference text and implementation-oriented treatment for root finding, interpolation, integration, and ODE methods.

Open source

Explore Next

Good next jumps from the relationship graph, ranked for useful discovery.

Prerequisite ofRomberg Integration

Romberg integration refines trapezoidal estimates through extrapolation.

Alternative toSimpson's Rule

Both estimate definite integrals with different polynomial assumptions.

Tags

Numerical MethodsNumerical IntegrationQuadratureInterpolation