In the field of numerical analysis, Trapezoidal rule is used to find the approximation of a definite integral. The basic idea in Trapezoidal rule is to assume the region under the graph of the given function to be a trapezoid and calculate its area.
It follows that:
![Rendered by QuickLaTeX.com {\displaystyle \int _{a}^{b}f(x)\,dx\approx (b-a)\left[{\frac {f(a)+f(b)}{2}}\right]}](https://www.geeksforgeeks.org/wp-content/ql-cache/quicklatex.com-b7df47f04551ee6bdcea9030d7ce2b65_l3.png)
For more accurate results the domain of the graph is divided into n segments of equal size as shown below:

Grid spacing or segment size h = (b-a) / n.
Therefore, approximate value of the integral can be given by:
![Rendered by QuickLaTeX.com \int_{a}^{b}f(x)dx=\frac{b-a}{2n}\left [ f(a)+2\left \{ \sum_{i=1}^{n-1}f(a+ih) \right \}+f(b) \right ]](https://www.geeksforgeeks.org/wp-content/ql-cache/quicklatex.com-152221d79e2f501da9f3bdc481362d5c_l3.png)
C++
#include<stdio.h>
float y( float x)
{
return 1/(1+x*x);
}
float trapezoidal( float a, float b, float n)
{
float h = (b-a)/n;
float s = y(a)+y(b);
for ( int i = 1; i < n; i++)
s += 2*y(a+i*h);
return (h/2)*s;
}
int main()
{
float x0 = 0;
float xn = 1;
int n = 6;
printf ( "Value of integral is %6.4f\n" ,
trapezoidal(x0, xn, n));
return 0;
}
|
Java
class GFG
{
static float y( float x)
{
return 1 / ( 1 + x * x);
}
static float trapezoidal( float a, float b, float n)
{
float h = (b - a) / n;
float s = y(a) + y(b);
for ( int i = 1 ; i < n; i++)
s += 2 * y( a + i * h);
return (h / 2 ) * s;
}
public static void main (String[] args)
{
float x0 = 0 ;
float xn = 1 ;
int n = 6 ;
System.out.println( "Value of integral is " +
Math.round(trapezoidal(x0, xn, n)
* 10000.0 ) / 10000.0 );
}
}
|
Python3
def y( x ):
return ( 1 / ( 1 + x * x))
def trapezoidal (a, b, n):
h = (b - a) / n
s = (y(a) + y(b))
i = 1
while i < n:
s + = 2 * y(a + i * h)
i + = 1
return ((h / 2 ) * s)
x0 = 0
xn = 1
n = 6
print ( "Value of integral is " ,
"%.4f" % trapezoidal(x0, xn, n))
|
C#
using System;
class GFG {
static float y( float x)
{
return 1 / (1 + x * x);
}
static float trapezoidal( float a,
float b, float n)
{
float h = (b - a) / n;
float s = y(a) + y(b);
for ( int i = 1; i < n; i++)
s += 2 * y( a + i * h);
return (h / 2) * s;
}
public static void Main ()
{
float x0 = 0;
float xn = 1;
int n = 6;
Console.Write( "Value of integral is "
+ Math.Round(trapezoidal(x0, xn, n)
* 10000.0) / 10000.0);
}
}
|
PHP
<?php
function y( $x )
{
return 1 / (1 + $x * $x );
}
function trapezoidal( $a , $b , $n )
{
$h = ( $b - $a ) / $n ;
$s = y( $a ) + y( $b );
for ( $i = 1; $i < $n ; $i ++)
$s += 2 * Y( $a + $i * $h );
return ( $h / 2) * $s ;
}
$x0 = 0;
$xn = 1;
$n = 6;
echo ( "Value of integral is " );
echo (trapezoidal( $x0 , $xn , $n ));
?>
|
Javascript
<script>
function y(x)
{
return 1 / (1 + x * x);
}
function trapezoidal(a, b, n)
{
let h = (b - a) / n;
let s = y(a) + y(b);
for (let i = 1; i < n; i++)
s += 2 * y(a + i * h);
return (h / 2) * s;
}
let x0 = 0;
let xn = 1;
let n = 6;
document.write( "Value of integral is " +
Math.round(trapezoidal(x0, xn, n) *
10000.0) / 10000.0);
</script>
|
Output:
Value of integral is 0.7842
References:
https://en.wikipedia.org/wiki/Trapezoidal_rule
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
11 Sep, 2023
Like Article
Save Article