The trapezoidal rule of integration

In a previous article I discussed the situation where you have a sequence of (x,y) points and you want to find the area under the curve that is defined by those points. I pointed out that usually you need to use statistical modeling before it makes sense to compute the area.

However, there is a numerical technique that is very useful for a wide range of numerical integration scenarios, and that is the trapezoidal rule.

The following graph illustrates the trapezoidal rule. Given a set of points (x1, y1), (x2, y2), ..., (xn, yn), with x1x2 ≤ ... ≤ xn, the trapezoidal rule computes the area of the piecewise linear curve that passes through the points. You can compute the area under the piecewise linear segments by summing the area of the trapezoids A1, A2, A3, and A4. (Sometimes a trapezoid is degenerate and is actually a rectangle or a triangle.)

Implementing the Trapezoidal Rule in SAS/IML Software

It is easy to use SAS/IML software (or the SAS DATA step) to implement the trapezoidal rule. The area of a trapezoid defined by (xi, yi) and (xi+1, yi+1) is

( xi+1 – xi ) ( yi + yi+1 ) / 2
The first term is just the width of the subinterval [xi, xi+1] and the second term is the average of the heights at each end of the subinterval.

The following user-defined function computes the area under the piecewise linear segments that connect the points. The function does not check that the x values are sorted in nondecreasing order.

proc iml;
/**Given two vectors x,y where y=f(x), this module
   approximates the definite integral int_a^b f(x) dx 
   by the trapezoid rule. 
   The vector x is assumed to be in numerically increasing
   order so that a=x[1] and b=x[nrow(x)].
   The module does not assume equally spaced intervals.
   The formula is 
   Integral = Sum(  (x[i+1] - x[i]) * (y[i] + y[i+1])/2 )   
start TrapIntegral(x,y);
   N = nrow(x);
   dx    =   x[2:N] - x[1:N-1];
   meanY = ( y[2:N] + y[1:N-1] )/2;
   return( dx` * meanY );
/** test it **/
x = {0.0, 0.2, 0.4, 0.8, 1.0};
y = {0.5, 0.8, 0.9, 1.0, 1.0};
area = TrapIntegral(x,y);
print area;

Notice that the implementation does not require equally spaced points. The width of all subintervals are computed in a single statement and assigned to the vector dx. Similarly, the average of the heights are computed in a single statement and assigned to the vector meanY. The summation of all the areas is then computed by using a dot product of vectors. (Equivalently, the module could also return the quantity sum(dx # meanY), but the dot product is the more efficient computation.)

The simplicity of the trapezoidal rule makes it an ideal for many numerical integration tasks. Also, the trapezoidal rule is exact for piecewise linear curves such as an ROC curve. Also, as John D. Cook points out, there are other situations in which the trapezoidal rule performs more accurately than other, fancier, integration techniques.

The trapezoidal rule is not as accurate as Simpson's Rule when the underlying function is smooth, because Simpson's rule uses quadratic approximations instead of linear approximations. The formula is usually given in the case of an odd number of equally spaced points. Leave a comment to discuss the relative advantages and disadvantages of Simpson's rule as compared to the trapezoidal rule.

In a future blog post, I will use the TrapIntegral function to integrate some functions that arise in statistical data analysis.

tags: Numerical Analysis


  1. Charlie Huang
    Posted June 2, 2011 at 12:52 pm | Permalink

    Fantastic work! I applied it in my daily routine immediately. Thanks a lot.

  2. Posted April 28, 2014 at 8:51 am | Permalink

    If you could state the strengths and limitations of both simpson's rule and trapezodial rule that would be greatly appreciated. However, the work already posted is great.

    • Posted April 28, 2014 at 9:56 am | Permalink

      The strength of the trapezoidal rule is that it is fast and it is exact for piecewise linear functions. The strength of Simpson's rule is that it is usually more accurate: it has a smaller error when integrating smooth functions. The limitations for both algorithms is that they are designed for finite intervals. They are also require that you choose the number of subdivisions in advance, which is why I use the QUAD subroutine in SAS/IML software for serious work. The QUAD subroutine implements an adaptive Romberg-type algorithm that is is more accurate than Simposon's Rule and can work on infinite domains.

  3. willard rupia
    Posted January 29, 2015 at 9:43 am | Permalink

    I need some help on the application of trapezoidal rule or simpson rule in mining metallurgy, where exactly can we apply these two rules?

    • Posted February 2, 2015 at 10:55 am | Permalink

      These rules enable you to numerically approximate an integral. Integrals appear in many areas of material science, including loads, shears, stresses, torsion, etc. See any engineering or calculus textbook.

3 Trackbacks

  1. [...] there is a numerical technique that is very useful for a wide range of numerical integration scenarios, and that is the trapezoidal rule [...]

  2. [...] a numerical integration technique. For this example, you can use the TrapIntegral module from my blog post on the trapezoidal rule of integration. The following statements define the TrapIntegral module and verify that the KDE curve is [...]

  3. […] to add together the areas of many slices of the density. For example, it is simple to modify the trapezoidal rule of integration to return a cumulative sum of the trapezoidal areas that approximate the area under a curve, as […]

Post a Comment

Your email is never published nor shared. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>