#! /usr/bin/env python # def trapezoid_log_test ( ): #*****************************************************************************80 # ## TRAPEZOID_LOG_TEST tests the trapezoid rule on the log-singular integrand. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 06 December 2015 # # Author: # # John Burkardt # import numpy as np from test_functions import integral_log from test_functions import integrand_log from r8vec_uniform_01 import r8vec_uniform_01 print ( '' ) print ( 'TRAPEZOID_LOG_TEST' ) print ( ' Test the trapezoidal rule on the log-singular integrand.' ) print ( '' ) print ( ' N Estimate Error' ) print ( '' ) n = 17 v2 = integral_log ( ) for nlog in range ( 5, 13 ): n = ( n - 1 ) * 2 + 1 h = 1.0 / float ( n - 1 ) x = np.linspace ( 0.0, 1.0, n ) x[0] = 0.5 * ( x[0] + x[1] ) f = integrand_log ( n, x ) v1 = h * ( np.sum ( f ) - 0.5 * ( f[1] + f[n-1] ) ) print ( ' %7d %14.6g %14.6g' % ( n, v1, abs ( v1 - v2 ) ) ) print ( '' ) print ( ' Exact: %14.6g' % ( v2 ) ) # # Terminate. # print ( '' ) print ( 'TRAPEZOID_LOG_TEST' ) print ( ' Normal end of execution.' ) return def trapezoid_power_test ( ): #*****************************************************************************80 # ## TRAPEZOID_POWER_TEST tests the trapezoid rule on the power-singular integrand. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 06 December 2015 # # Author: # # John Burkardt # import numpy as np from test_functions import integral_power from test_functions import integrand_power from r8vec_uniform_01 import r8vec_uniform_01 print ( '' ) print ( 'TRAPEZOID_POWER_TEST' ) print ( ' Test the trapezoidal rule on the power-singular integrand.' ) print ( '' ) print ( ' N Estimate Error' ) print ( '' ) n = 17 v2 = integral_power ( ) for nlog in range ( 5, 13 ): n = ( n - 1 ) * 2 + 1 h = 1.0 / float ( n - 1 ) x = np.linspace ( 0.0, 1.0, n ) x[0] = 0.5 * ( x[0] + x[1] ) f = integrand_power ( n, x ) v1 = h * ( np.sum ( f ) - 0.5 * ( f[1] + f[n-1] ) ) print ( ' %7d %14.6g %14.6g' % ( n, v1, abs ( v1 - v2 ) ) ) print ( '' ) print ( ' Exact: %14.6g' % ( v2 ) ) # # Terminate. # print ( '' ) print ( 'TRAPEZOID_POWER_TEST' ) print ( ' Normal end of execution.' ) return def trapezoid_regular_test ( ): #*****************************************************************************80 # ## TRAPEZOID_REGULAR_TEST tests the trapezoid rule on the regular integrand. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 06 December 2015 # # Author: # # John Burkardt # import numpy as np from test_functions import integral_regular from test_functions import integrand_regular from r8vec_uniform_01 import r8vec_uniform_01 print ( '' ) print ( 'TRAPEZOID_REGULAR_TEST' ) print ( ' Test the trapezoidal rule on the regular integrand.' ) print ( '' ) print ( ' N Estimate Error' ) print ( '' ) n = 17 v2 = integral_regular ( ) for nlog in range ( 5, 13 ): n = ( n - 1 ) * 2 + 1 h = 1.0 / float ( n - 1 ) x = np.linspace ( 0.0, 1.0, n ) f = integrand_regular ( n, x ) v1 = h * ( np.sum ( f ) - 0.5 * ( f[0] + f[n-1] ) ) print ( ' %7d %14.6g %14.6g' % ( n, v1, abs ( v1 - v2 ) ) ) print ( '' ) print ( ' Exact: %14.6g' % ( v2 ) ) # # Terminate. # print ( '' ) print ( 'TRAPEZOID_REGULAR_TEST' ) print ( ' Normal end of execution.' ) return if ( __name__ == '__main__' ): from timestamp import timestamp timestamp ( ) trapezoid_log_test ( ) trapezoid_power_test ( ) trapezoid_regular_test ( ) timestamp ( )