# Program for finding the Integral of a given function using Boole’s Rule

Given a function, f(x), tabulated at points equally spaced by such that

• • • …..and so on

The upper and lower limits a, b correspond to which the integral needs to be found, the task is to find the integral value of the given equation f(x).

Examples:

Input: a = 0, b = 4, Output: 1.6178
Explanation:
Integral of (1 / (1 + x)) is 4ln(|1 + x|)0 + c.
On substituting the limits, ln(|5|) + ln(|1|) = 1.6178.

Input: a = 0.2, b = 0.6, Output: 0.3430

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: In this article, Boole’s rule is discussed to compute the approximate integral value of the given function f(x).

Boole’s rule is a numerical integration technique to find the approximate value of the integral. It is named after a largely self-taught mathematician, philosopher and logician George Boole. The idea of the Boole’s technique is to approximate the integral using the values of ‘fk‘ at some equally spaced values(h in the given image). The following illustration shows how various fk’s are considered: The integral value of the Boole’s rule is given by the formula: • In the above formula, an error term which comes when integration is of the order 6. The error term is .
• are the values of f(x) at their respective intervals of x.
• Therefore, the following steps can be followed to compute the integral of some function f(x) in the interval (a, b):
1. The value of n=6, which is the number of parts the interval is divided into.
2. Calculate the width, h = (b – a)/4.
3. Calculate the values of x1 to x5 as Consider y = f(x). Now find the values of for the corresponding values.
4. Substitute all the values in the Boole’s rule to calculate the integral value.

Below is the implementation of the above approach:

## C++

 // C++ program to implement Boole's Rule  // on the given function  #include  using namespace std;     // In order to represent the implementation,  // a function f(x) = 1/(1 + x) is considered  // in this program     // Function to return the value of f(x)  // for the given value of x  float y(float x)  {      return (1 / (1 + x));  }     // Function to computes the integrand of y  // at the given intervals of x with  // step size h and the initial limit a  // and final limit b  float BooleRule(float a, float b)  {         // Number of intervals      int n = 4;      int h;         // Computing the step size      h = ((b - a) / n);      float sum = 0;         // Substituing a = 0, b = 4 and h = 1      float bl = ((7 * y(a) +                   32 * y(a + h) +                  12 * y(a + 2 * h) +                   32 * y(a + 3 * h) +                    7 * y(a + 4 * h)) *                   2 * h / 45);         sum = sum + bl;      return sum;  }     // Driver code  int main()  {      float lowlimit = 0;      float upplimit = 4;             cout << fixed << setprecision(4) <<           "f(x) = " << BooleRule(0, 4);                 return 0;  }     // This code is contributed by shivanisinghss2110

## C

 // C program to implement Boole's Rule  // on the given function     #include  #include     // In order to represent the implementation,  // a function f(x) = 1/(1 + x) is considered  // in this program     // Function to return the value of f(x)  // for the given value of x  float y(float x)  {      return (1 / (1 + x));  }     // Function to computes the integrand of y  // at the given intervals of x with  // step size h and the initial limit a  // and final limit b  float BooleRule(float a, float b)  {      // Number of intervals         int n = 4;      int h;         // Computing the step size      h = ((b - a) / n);      float sum = 0;         // Substituing a = 0, b = 4 and h = 1      float bl = (7 * y(a) + 32 * y(a + h)                  + 12 * y(a + 2 * h)                  + 32 * y(a + 3 * h)                  + 7 * y(a + 4 * h))                 * 2 * h / 45;         sum = sum + bl;      return sum;  }     // Driver code  int main()  {      float lowlimit = 0;      float upplimit = 4;      printf("f(x) = %.4f",             BooleRule(0, 4));      return 0;  }

## Java

 // Java program to implement Boole's Rule  // on the given function  class GFG{         // In order to represent the implementation,  // a function f(x) = 1/(1 + x) is considered  // in this program      // Function to return the value of f(x)  // for the given value of x  static float y(float x)  {      return (1 / (1 + x));  }      // Function to computes the integrand of y  // at the given intervals of x with  // step size h and the initial limit a  // and final limit b  static float BooleRule(float a, float b)  {      // Number of intervals          int n = 4;      int h;          // Computing the step size      h = (int) ((b - a) / n);      float sum = 0;          // Substituing a = 0, b = 4 and h = 1      float bl = (7 * y(a) + 32 * y(a + h)                  + 12 * y(a + 2 * h)                  + 32 * y(a + 3 * h)                  + 7 * y(a + 4 * h))                 * 2 * h / 45;          sum = sum + bl;      return sum;  }      // Driver code  public static void main(String[] args)  {      System.out.printf(("f(x) = %.4f"),             BooleRule(0, 4));  }  }     // This code is contributed by 29AjayKumar

## Python3

 # Python3 program to implement Boole's Rule  # on the given function     # In order to represent the implementation,  # a function f(x) = 1/(1 + x) is considered  # in this program     # Function to return the value of f(x)  # for the given value of x  def y(x):      return (1 / (1 + x))     # Function to computes the integrand of y  # at the given intervals of x with  # step size h and the initial limit a  # and final limit b  def BooleRule(a, b):             # Number of intervals      n = 4        # Computing the step size      h = ((b - a) / n)      sum = 0        # Substituing a = 0, b = 4 and h = 1      bl = (7 * y(a) + 32 * y(a + h) + 12 *          y(a + 2 * h)+32 * y(a + 3 * h)+7 *          y(a + 4 * h))* 2 * h / 45        sum = sum + bl      return sum    # Driver code  if __name__ == '__main__':      lowlimit = 0     upplimit = 4     print("f(x) =",round(BooleRule(0, 4),4))     # This code is contributed by Surendra_Gangwar

Output:

f(x) = 1.6178


Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.