Trapezoidal Rule for Approximate Value of Definite Integral | A sample function whose definite integral 's approximate value is computed using Trapezoidal rule ; Declaring the function f ( x ) = 1 / ( 1 + x * x ) ; Function to evaluate the value of integral ; Grid spacing ; Computing sum of first and last terms in above formula ; Adding middle terms in above formula ; h / 2 indicates ( b - a ) / 2 n . Multiplying h / 2 with s . ; Range of definite integral ; Number of grids . Higher value means more accuracy
< ? 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 ) ) ; ? >