Trapezoidal Rule Program in C and C++

Trapezoidal Rule also referred to as Trapezoid Rule or Trapezium Rule is a method to find an approximate value of the integral of a given polynomial or function in a given interval. In simple terms, it is a method to evaluate a definite integral.  

There are several rules that have to applied to solve a definite integral. This is not easy, especially in scientific experiments in which the functions are too complex or too large to apply rules. For such cases there are many numerical methods created to evaluate a definite integral. One such method is the Trapezoidal RuleThere are many generalized forms of Trapezoidal Rule like the Simpson’s 1/3rd Rule. 

Trapezoidal rule works by finding the area under the curve of a function f(x) by approximating the region under the curve as a trapezoid. The interval is partitioned into multiple parts approximated as small trapezoids instead of rectangles.  

Trapezoidal Rule graph
Image Source – Wikipedia

The value obtained by the Trapezoidal Rule is only approximate but not accurate. The result approximated from the Trapezoidal rule formula gets more accurate as the number of intervals / partitions is increased.  

The formula for the Trapezoidal Rule program in C is:  

Trapezoidal Rule formula

where,

trapezoidal rule formula

Algorithm for Trapezoidal Rule Program in C :

1. Start.
2. Define an equation for f(x).
3. Define a method by the name of trapezoidalRule().
4. Take the values of lower and upper limits of integration as well as the number of sub-intervals as inputs from the user.
5. Initialize a variable ifx with 0.
6. Find the value of h. [h = (b-a)/n]
7. Add the values of f(a) and f(b) to ifx.
8. Set the value of i = a+h.
9. Multiply the value of f(i) by 2 and add the result to ifx.
10. Add h to the existing value of i.
11. Repeat steps 8, 9 and 10 till the value of i is less than b.
12. Multiply the final value of ifx with h and divide the result by 2 and store it back in ifx.
13. Print the value of integration ifx.
14. Stop.

Pseudo Code for Trapezoidal Rule Program in C: 

h = fabs(b - a) / n; 
ifx = ifx + f(a) + f(b); 
for (i = a+h; i < b;){ 
    ifx = ifx + (2 * f(i)); 
    i = i + h; 
} 
ifx = ifx * h / 2; 

Sample Input: 

f(x)=x+1/x

Lower limit of integration (a) = 2 

Upper limit of integration (b) = 6 

Number of Sub-Intervals (n) = 8 

Sample Output:  

The value of integration of f(x) under a and b is 17.1032 

Trapezoidal Rule in C Program

/* ******************************
Author: Exploring Bits
****************************** */

#include <stdio.h>
#include <conio.h>
#include <math.h>
#define f(x) (x + (1/x))

void trapezoidalRule (){
    double a, b, h, ifx = 0.0, i;
    int n;
    printf("Enter the lower and upper limits of the integral: ");
    scanf("%lf", &a);
    scanf("%lf", &b);
    printf("Enter the number of subintervals you want: ");
    scanf("%d", &n);
    h = fabs(b - a) / n;
    ifx = ifx + f(a) + f(b);
    for (i = a+h; i < b;){
        ifx = ifx + (2 * f(i));
        i = i + h;
    }
    ifx = ifx * h / 2;
    printf("\nThe integral of the equation using Trapezoidal Rule is %lf\n", ifx);
}

int main (){
    trapezoidalRule();
}

Trapezoidal Rule in C ++ Program

/* ******************************
Author: Exploring Bits
****************************** */
#include <iostream>
#include <math.h>
#define f(x) (x + (1/x))

using namespace std;

void trapezoidalRule (){
    double a, b;
    int n;
    cout << "Enter the lower and upper limits of the integral: ";
    cin >> a >> b;
    cout << "Enter the number of subintervals you want: ";
    cin >> n;
    double h = abs(b - a) / n;
    double ifx = 0;
    ifx = ifx + f(a) + f(b);
    for (double i = a+h; i < b;){
        ifx = ifx + (2 * f(i));
        i = i + h;
    }
    ifx = ifx * h / 2;
    cout << "\nThe integral of the equation using Trapezoidal Rule is " << ifx << endl;
}

int main (){
    trapezoidalRule();
}

 

Output of Trapezoidal Rule in C and C++

trapezoidal program in C output

 

In the above program, the trapezoidalRule() is used to apply the Trapezoidal Rule formula to the function f(x) = x + (1 / x). If you want to change this function, then simply replace #define f(x) x * x – 3  with #define f(x) your_own_equation. 

The above method takes the values of the lower and upper limits from the user. It also requires the number of intervals we want to make for use in the formula. A point to be noted here is that the greater number of intervals we make, the more accurate the result will be but then the working and execution of the program will take longer (just like in real life), although that will still be fast enough for the user to notice any difference. And after a certain number of intervals, the accuracy will also stop increasing indicating that a sufficiently accurate value has been found already. So, it is advised that a reasonable value for the number of intervals be provided.  

The above formula is then applied to the equation using the values inputted and the required answer is printed to the output console. 

Leave a Comment