#! /usr/bin/env python # def line01_monomial_integral ( e ): #*****************************************************************************80 # ## LINE01_MONOMIAL_INTEGRAL: monomial integral over the unit line in 1D. # # Discussion: # # The integration region is # # 0 <= X <= 1. # # The monomial is F(X) = X^E. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 22 June 2015 # # Author: # # John Burkardt # # Reference: # # Philip Davis, Philip Rabinowitz, # Methods of Numerical Integration, # Second Edition, # Academic Press, 1984, page 263. # # Parameters: # # Input, integer E, the exponent. # E must not equal -1. # # Output, real INTEGRAL, the integral. # from sys import exit if ( e == -1 ): print ( '' ) print ( 'LINE01_MONOMIAL_INTEGRAL - Fatal error!' ) print ( ' Exponent E = -1 is not allowed!' ) exit ( 'LINE01_MONOMIAL_INTEGRAL - Fatal error!' ) integral = 1.0 / float ( e + 1 ) return integral def line01_monomial_integral_test ( ): #*****************************************************************************80 # ## LINE01_MONOMIAL_INTEGRAL_TEST tests LINE01_MONOMIAL_INTEGRAL. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 22 June 2015 # # Author: # # John Burkardt # import numpy as np import platform from line01_length import line01_length from line01_sample import line01_sample from monomial_value_1d import monomial_value_1d m = 1 n = 4192 test_num = 11 print ( '' ) print ( 'LINE01_MONOMIAL_INTEGRAL_TEST' ) print ( ' Python version: %s' % ( platform.python_version ( ) ) ) print ( ' LINE01_MONOMIAL_INTEGRAL computes integrals of monomials' ) print ( ' along the length of the unit line in 1D.' ) print ( ' Compare with a Monte Carlo estimate.' ) # # Get sample points. # seed = 123456789 x, seed = line01_sample ( n, seed ) print ( '' ) print ( ' Number of sample points used is %d' % ( n ) ) print ( '' ) print ( ' E MC-Estimate Exact Error' ) print ( '' ) for test in range ( 0, test_num ): e = test value = monomial_value_1d ( n, e, x ) result = line01_length ( ) * np.sum ( value ) / float ( n ) exact = line01_monomial_integral ( e ) error = abs ( result - exact ) print ( ' %2d %14.6g %14.6g %10.2g' % ( e, result, exact, error ) ) # # Terminate. # print ( '' ) print ( 'LINE01_MONOMIAL_INTEGRAL_TEST:' ) print ( ' Normal end of execution.' ) return if ( __name__ == '__main__' ): from timestamp import timestamp timestamp ( ) line01_monomial_integral_test ( ) timestamp ( )