Simpson’s 1/3 Rule in C and C++ Program

Simpson’s 1/3rd Rule also referred to as Simpson’s 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 Simpson’s 1/3rd Rule 

The values from Simpson’s Rule are more accurate compared to other numerical methods like Trapezoidal or Mid-Point Rule. Simpson’s Rule is an extension of the Trapezoidal rule aimed at making the result of the Trapezoidal Rule more accurate.  

Value from Simpson’s rule can be derived by approximating the integrand f(x) by a quadratic interpolant. 

Simpson's 1/3 Rule
Image Source – Wikipedia

In the above graph, f(x) is the integrand and g(x) is a quadratic interpolant. 

The formula used in Simpson’s Rule is:

where,

 

Algorithm for Simpson’s 1/3 Rule Program in C :

1. Start. 
2. Define an equation for f(x). 
3. Define a method by the name of simpsonsRule(). 
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 4 and add the result to ifx. 
10. Add (2*h) to the existing value of i. 
11. Repeat steps 8, 9 and 10 till the value of i is less than b. 
12. Set the value of j = a+(2*h). 
13. Multiply the value of f(j) by 2 and add the result to ifx. 
14. Add (2*h) to the existing value of j. 
15. Repeat steps 12, 13 and 14 till the value of j is less than b. 
16. Multiply the final value of ifx with h and divide the result by 3 and store it back in ifx. 
17. Print the value of integration ifx. 
18. Stop.

Pseudo Code for Simpson’s 1/3 Rule Program in C: 

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

Sample Input: 

f(x)=log10x  Lower limit of integration (a) = 1 

Upper limit of integration (b) = 5 

Number of Sub-Intervals (n) = 8 

Sample Output:  

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

Simpson’s 1/3 Rule in C Program

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

#include <stdio.h>
#include <conio.h>
#include <math.h>
#define f(x) log10(x)

void simpsonsRule (){
    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 + (4 * f(i));
        i = i + (2*h);
    }
    for (i = a+(2*h); i < b;){
        ifx = ifx + (2 * f(i));
        i = i + (2*h);
    }
    ifx = ifx * h / 3;
    printf("\nThe integral of the equation using Simpson's 1/3rd Rule is %lf\n", ifx);
}

int main (){
    simpsonsRule();
}

 

Simpson’s 1/3 Rule in C++ Program

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

#include <iostream>
#include <math.h>
#define f(x) log10(x)

using namespace std;

void simpsonsRule (){
    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 + (4 * f(i));
        i = i + (2*h);
    }
    for (double i = a+(2*h); i < b;){
        ifx = ifx + (2 * f(i));
        i = i + (2*h);
    }
    ifx = ifx * h / 3;
    cout << "\nThe integral of the equation using Simpson's 1/3rd Rule is " << ifx << endl;
}

int main (){
    simpsonsRule();
}

 

Output of Simpson’s 1/3 Rule in C and C++

In the above program, the simpsonsRule () is used to apply the Simpson’s formula to the function f(x) = log10(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