A brief introduction to the Trapezoidal rule and a uniform interval Composite Trapezoidal Rule implementation.

Trapezoidal Rule

The Trapezoidal Rule is used to evaluate a definite integral \intop_{a}^{b}f(x)\, dx within some the limits a and b . Trapezoidal rule approximates f(x) with a polynomial of degree one ie. a line segment between the two points of the given function at with endpoints f(a) and f(b), and then finds the integral of that line segment which is used to approximate the definite integral \intop_{a}^{b}f(x)\, dx. The integral of
the line approximated function is simply the area of the trapezium formed by the straight line, with base width h=(b-a) and bounded by the lines f(a) and f(b) in the positive side of the X axis. The approximate evaluation of the integral is found by finding the integral of the straight line with endpoints (a,f(a)) and (b,f(b)) can be. found with the below iterative formula:

I_{T1}=\frac{h}{2}(y_{0}+y_{1})\mbox{ where }y_{i}=f(x_{i})

Trapezoidal Rule. (one interval)
Trapezoidal Rule. (one interval)

The above iterative formula approximates f(x) with only one straight line. To represent the function more accurately with the straight lines using the Trapezoidal Rule, the function is divided in many intervals n of the same uniformed width say h, such that h=\frac{(b-a)}{n}, and a line is fit within each interval to approximate the function and each such interval is evaluated with the above process and at last added to get the final integral. This is gives better results as the function f(x) is better approximated with the line segments within the small intervals, and the missed sections of a curves are better covered. This is known as the Trapezoidal Composite Rule. The iterative method for the Trapezoidal Composite rule could be derived from the above relation and is found to be:

I_{T}=\frac{h}{2}[(y_{0}+y_{n})+2\sum_{i=1}^{n-1}y_{i}]\mbox{ where }y_{i}=f(x_{i})

Trapezoidal Rule. (uniform multiple intervals)
Trapezoidal Rule. (uniform multiple intervals)

The Process

The Trapezoidal Composite Rule can be implemented directly from the iterative formula presented above. The first and the last ordinate would be calculated separately and the other ordinates would be summed by a loop and then is multiplied by 2. Next these two parts are added together to get the integral. For demonstration purpose some sample functions are used.

  1. f(x)=\frac{1}{x}+5+10x^{2}
  2. f(x)=\frac{sin(2x)}{(1+x)^{5}}
FUNCTION trapezoidal(f, a, b, n)
  Find width of each interval h = (b-a)/n 
  Find y0= f(a), yn= f(b)
  Initialize x = a+h, i = 1, sum = 0
/*  Calculate the sum of the ordinates from (x + h) to (x + (n-1)h) */
  WHILE (i < n)
      sum = sum + f (x)
      x = x + h
/*  Apply Trapezoidal Composite Formula to find the integral */
  Iz = (h/2) [(y0 + yn) + 2 * sum]


#include <stdio.h>
#include <math.h>

double f1 (double x);
double f2 (double x);

double trapezoidal (double (*f) (double x), double a, double b, int n);

main (void)
  double a, b, n;
  double Iz;

  printf ("\nEnter a,b,n: ");
  scanf ("%lf %lf %lf", &a, &b, &n);

  printf ("\nf(x) = sin (2x) / (1+x)^5");
  /* Show integral computed with trapezoidal rule */
  Iz = trapezoidal (f1, a, b, n);
  printf ("\n\tI_Trapezoidal (f(x), %g, %g, %g) = %g", a, b, n,Iz);

  printf ("\n");

  printf ("\nf(x) = (1/x) + 5 + 10x^2;");
  /* Show integral computed with trapezoidal rule */
  Iz = trapezoidal (f2, a, b, n);
  printf ("\n\tI_Trapezoidal (f(x), %g, %g, %g) = %g", a, b, n,Iz);
  printf ("\n");

  return 0;

/* Takes a function pointer which should represent the single variable
 * function of which the integral is to be found. The function should 
 * receive a double and return a double. The limits a and b and the 
 * number of intervals n is provided. The value returned is the integral
 * of the passed function is the argument
trapezoidal (double (*f) (double x), double a, double b, int n)
  double h;
  double y = 0, x, sum = 0, y0, yn;
  int i;

  /* Avoid calling NULL */  
  if (f == NULL)
    return 0;

  h = (b - a) / n;

  y0 = f (a);
  yn = f (b);

  for (i = 1, x = a + h; i < n; x = x + h, i++)
    sum = sum + f (x);

  y = (h / 2) * ((y0 + yn) + 2 * sum);

  return y;

/* Sample function 1 */
f1 (double x)
  return sin (2 * x) / pow ((1 + x), 5);

/* Sample function 2 */
f2 (double x)
  return (1 / x) + 5 + 10 * x * x;


The two given function defined in the previous section is implemented with the functions f1 () and f2 (). These two functions accept a double type data, and returns double.

  • double f1 (double x) : This function implements the equation sin (2 * x) / pow ((1 + x), 5) and returns the evaluated value.

  • double f2 (double x) : This function implements the equation (1 / x) + 5 + 10 * x * x and returns the evaluated value.

  • double trapezoidal (double (*f) (double x), double a, double b, int n) : This function evaluates a definite integral integral of a function with Trapezoidal method of a function f within limits a, and b with n equal intervals. This takes a pointer to a function which accepts one double type data and returns a double type data. The next argument is double a, which is interpreted as the lower limit of the integral, the next argument double b is considered as the upper limit of the integral. int n is the number of intervals to be taken. After finding the width of each interval, this first calculates the first and the last ordinate y0 = f(a) and y1 = f(b). Then it enters the for loop and computes the (n-2) values of the passed
    function starting from (x+h) up to (x+(n-1)h). Then it uses the composite formula to find the integral with Trapezoidal rule, and returns the value: (h / 2) * ((y0 + yn) + 2 * sum);

  • int main (void) : The main function prompts the user to enter the upper and the lower limits and the number of intervals to be taken and calls the functions with the proper parameters. The trapezoidal() is called as Iz = trapezoidal (f1, a, b, n); which computes the integral of the function f1 within upper and lower limit a and b with the Trapezoidal Rule.


When f'' is continuous and M is the upper bound for the values of
\left|f''\right| on [a,b], then the error induced by the
Trapezoidal rule is found to be: \left|E_{T}\right|\leq\frac{(b-a)}{12}h^{2}M

Although the theory tells us there will always be a smallest safe value of M, in practice we can hardly ever find it. Instead, we find the best value we can and go on from there to estimate \left|E_{T}\right|. To make \left|E_{T}\right| small we make h small. It also can be graphically seen that a line fits less accurately for a certain interval in a higher degree function than a lower degree function. So increasing the interval h will result in better results, or implementing better fitting curve than a line needs to be selected.


The Trapezoidal evaluation function is implemented to be independent of the function of which the integral is to be found, by allowing passing of the function to be integrated as a parameter. This implementation could be incorporated into an algebraic equation parser, which can be used to evaluate some user entered functions to be evaluated like for example:
ans = trapezoidal (parse_expression (user_expression, x), a, b, n) ; the ‘user_expression’ is being considered a single variable function with the variable replaced with the value of ‘x’ .


Some sample outputs of the above code is shown below

Run 1: input = lower limit a = 1, upper limit b = 2, intervals
n = 10

Enter a,b,n: 1 2 10

f(x) = sin (2x) / (1+x)^5
	I_Trapezoidal (f(x), 1, 2, 10) = 0.00513881

f(x) = (1/x) + 5 + 10x^2;
	I_Trapezoidal (f(x), 1, 2, 10) = 29.0438

Run 2: input = lower limit a = 5.3, upper limit b = 10.23,
intervals n = 100

Enter a,b,n: 5.3 10.23 100

f(x) = sin (2x) / (1+x)^5
	I_Trapezoidal (f(x), 5.3, 10.23, 100) = -3.20357e-05

f(x) = (1/x) + 5 + 10x^2;
	I_Trapezoidal (f(x), 5.3, 10.23, 100) = 3097.73


  1. Check for more: http://en.wikipedia.org/wiki/Trapezoidal_rule
  2. Simpson’s 1/3rd rule: https://phoxis.org/2011/03/11/simpson-13-rd-rule/


  1. Calculus and Analytic Geometry : Thomas Finney
  2. Images from : Wikipedia



2 thoughts on “Trapezoidal Rule

  1. Pingback: Phoxis

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s