Trapezoidal Rule Calculator

Approximate a definite integral using the composite trapezoidal rule. Enter a function f(x), lower and upper limits a and b, and number of subintervals n.

Use x as the variable. Supported: + - * / ^ ( ), sin, cos, tan, log, exp, sqrt, abs, pi, e.

Equation

Composite trapezoidal rule with step h = (b - a) / n: $$\int_a^b f(x)\,dx \approx \frac{h}{2} \big[f(a) + 2\sum_{i=1}^{n-1} f(a + i\,h) + f(b)\big].$$

Result

This is the trapezoidal approximation to the definite integral of f(x) from a to b.

Exact Formula (with values)

Calculation Steps

    What is a Trapezoidal Rule Calculator?

    A Trapezoidal Rule Calculator numerically approximates definite integrals by replacing a curve with straight line segments and summing the areas of the resulting trapezoids. For a continuous function \(f(x)\) on \([a,b]\), the simplest (single-panel) trapezoid estimate is $$\int_a^b f(x)\,dx \approx \frac{b-a}{2}\,\big[f(a)+f(b)\big].$$ To improve accuracy, the interval is partitioned into \(n\) subintervals of equal width \(h=(b-a)/n\). The composite trapezoidal rule then averages the end values and doubles interior values: $$\int_a^b f(x)\,dx \approx \frac{h}{2}\Big[f(x_0)+2\sum_{i=1}^{n-1}f(x_i)+f(x_n)\Big],\quad x_i=a+ih.$$ When sample points are not evenly spaced (tabulated data), the calculator applies segment-wise trapezoids $$\int_a^b f(x)\,dx \approx \sum_{i=0}^{m-1} \frac{(x_{i+1}-x_i)}{2}\,\big[y_i+y_{i+1}\big],$$ where \(y_i=f(x_i)\). The trapezoidal rule is favored for its simplicity, transparency, and stable performance on smooth functions.

    About the Trapezoidal Rule Calculator

    This calculator supports three common workflows: (1) function input with limits \([a,b]\) and chosen \(n\); (2) evenly spaced samples; (3) arbitrary tabulated \((x_i,y_i)\) data. It reports \(h\), the composite sum, and (optionally) an error estimate. For sufficiently smooth \(f\), the global error for the composite rule obeys $$\bigg|\int_a^b f(x)\,dx - T_n\bigg| \le \frac{(b-a)}{12}\,h^2\,\max_{\xi\in[a,b]}|f''(\xi)|.$$ Display formulas responsively with MathJax, and use math.js to evaluate function samples, sums, and step sizes while preserving these same formulas on screen.

    How to Use this Trapezoidal Rule Calculator

    1. Function mode: enter \(f(x)\), limits \(a,b\), and panels \(n\). The tool computes \(h=(b-a)/n\) and applies $$T_n=\frac{h}{2}\Big[f(x_0)+2\sum_{i=1}^{n-1}f(x_i)+f(x_n)\Big].$$
    2. Even samples: paste \(y_0,\dots,y_n\) with known uniform spacing \(h\); the same composite formula is used.
    3. Tabulated data: paste pairs \((x_i,y_i)\) (not necessarily uniform). The tool sums per-segment trapezoids $$\sum \frac{(x_{i+1}-x_i)}{2}\,(y_i+y_{i+1}).$$
    4. Optionally view an error bound using a supplied or estimated \(\max|f''|\).

    Examples (using the same formulas)

    Example 1 (composite, uniform): \(f(x)=x^2,\ a=0,\ b=1,\ n=4\Rightarrow h=0.25.\)
    $$T_4=\frac{0.25}{2}\Big[0+2(0.0625+0.25+0.5625)+1\Big]=0.34375.$$ Exact \(\int_0^1 x^2 dx=1/3\approx 0.33333\).

    Example 2 (composite, uniform): \(f(x)=1/x,\ a=1,\ b=3,\ n=4,\ h=0.5.\)
    Points \(x=\{1,1.5,2,2.5,3\}\), \(y=\{1,\tfrac{2}{3},\tfrac{1}{2},0.4,\tfrac{1}{3}\}.\)
    $$T_4=\frac{0.5}{2}\Big[1+2\Big(\tfrac{2}{3}+\tfrac{1}{2}+0.4\Big)+\tfrac{1}{3}\Big]\approx 1.1167.$$ Exact \(\int_1^3 \tfrac{1}{x}dx=\ln 3\approx 1.0986\).

    Example 3 (tabulated, nonuniform): \((0,0),(2,1.5),(5,4)\).
    $$\text{Area}= \underbrace{(2-0)\frac{0+1.5}{2}}_{=1.5}+\underbrace{(5-2)\frac{1.5+4}{2}}_{=8.25}=9.75.$$

    FAQs

    Q1: Is the trapezoidal rule exact for linear functions?
    Yes. Since the method uses straight-line segments, it integrates linear \(f(x)\) exactly over any partition.

    Q2: How many panels \(n\) should I choose?
    Increase \(n\) until results stabilize within your tolerance; smoother functions require fewer panels.

    Q3: How does trapezoidal compare with Simpson’s rule?
    Simpson’s rule is often more accurate on smooth functions but requires even \(n\) and evaluates additional midpoints.

    Q4: Can I integrate noisy experimental data?
    Yes. The per-segment trapezoid sum works directly on tabulated \((x_i,y_i)\) and is robust to modest noise.

    Q5: What if the spacing is irregular?
    Use the nonuniform formula \(\sum \tfrac{(x_{i+1}-x_i)}{2}(y_i+y_{i+1})\); no resampling is required.

    More Math & Algebra Calculators