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
[Tex]f_2=f(x_2)[/Tex]
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
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 Boole’s rule is given by the formula:
- In the above formula, an error term which comes when integration is of order 6. The error term is
[Tex]f_1, f_2, f_3, f_4, f_5, [/Tex]
- 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):
- The value of n=6, which is the number of parts the interval is divided into.
- Calculate the width, h = (b – a)/4.
- Calculate the values of x1 to x5 as
- Consider y = f(x). Now find the values
- values.
- Substitute all the values in Boole’s rule to calculate the integral value.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
float y( float x)
{
return (1 / (1 + x));
}
float BooleRule( float a, float b)
{
int n = 4;
int h;
h = ((b - a) / n);
float sum = 0;
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;
}
int main()
{
float lowlimit = 0;
float upplimit = 4;
cout << fixed << setprecision(4) <<
"f(x) = " << BooleRule(0, 4);
return 0;
}
|
C
#include <math.h>
#include <stdio.h>
float y( float x)
{
return (1 / (1 + x));
}
float BooleRule( float a, float b)
{
int n = 4;
int h;
h = ((b - a) / n);
float sum = 0;
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;
}
int main()
{
float lowlimit = 0;
float upplimit = 4;
printf ( "f(x) = %.4f" ,
BooleRule(0, 4));
return 0;
}
|
Java
class GFG{
static float y( float x)
{
return ( 1 / ( 1 + x));
}
static float BooleRule( float a, float b)
{
int n = 4 ;
int h;
h = ( int ) ((b - a) / n);
float sum = 0 ;
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;
}
public static void main(String[] args)
{
System.out.printf(( "f(x) = %.4f" ),
BooleRule( 0 , 4 ));
}
}
|
Python3
def y(x):
return ( 1 / ( 1 + x))
def BooleRule(a, b):
n = 4
h = ((b - a) / n)
sum = 0
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
if __name__ = = '__main__' :
lowlimit = 0
upplimit = 4
print ( "f(x) =" , round (BooleRule( 0 , 4 ), 4 ))
|
C#
using System;
class GFG{
static float y( float x)
{
return (1 / (1 + x));
}
static float BooleRule( float a,
float b)
{
int n = 4;
int h;
h = ( int )((b - a) / n);
float sum = 0;
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;
}
public static void Main( string [] args)
{
Console.Write(( "f(x) = " +
System.Math.Round(
BooleRule(0, 4), 4)));
}
}
|
Javascript
<script>
function y(x)
{
return (1 / (1 + x));
}
function BooleRule(a, b)
{
let n = 4;
let h;
h = ((b - a) / n);
let sum = 0;
let 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;
}
document.write( "f(x) = " +
BooleRule(0, 4).toFixed(4));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Last Updated :
18 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...