#! /usr/bin/env python3 # def p00_f ( prob, n, x ): #*****************************************************************************80 # ## p00_f() evaluates the function for any problem. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 27 June 2015 # # Author: # # John Burkardt # # Input: # # integer PROB, the number of the desired test problem. # # integer N, the number of evaluation points. # # real X(N), the evaluation points. # # Output: # # real VALUE(N), the function values. # if ( prob == 1 ): value = p01_f ( n, x ) elif ( prob == 2 ): value = p02_f ( n, x ) elif ( prob == 3 ): value = p03_f ( n, x ) elif ( prob == 4 ): value = p04_f ( n, x ) elif ( prob == 5 ): value = p05_f ( n, x ) elif ( prob == 6 ): value = p06_f ( n, x ) elif ( prob == 7 ): value = p07_f ( n, x ) elif ( prob == 8 ): value = p08_f ( n, x ) else: print ( '' ) print ( 'p00_f - Fatal error!' ) print ( ' Illegal problem number = %d' % ( prob ) ) raise Exception ( 'p00_f - Fatal error!' ) return value def p01_f ( n, x ): #*****************************************************************************80 # ## p01_f evaluates the function for problem p01. # # Discussion: # # Step function. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 27 June 2015 # # Author: # # John Burkardt # # Input: # # integer N, the number of evaluation points. # # real X(N), the evaluation points. # # Output: # # real VALUE(N), the function values. # import numpy as np value = 2.0 * np.ones ( n ) i = ( x <= 1.0 / 3.0 ) j = ( 4.0 / 5.0 <= x ) value[i] = -1.0 value[j] = +1.0 return value def p02_f ( n, x ): #*****************************************************************************80 # ## p02_f evaluates the function for problem p02. # # Discussion: # # Nondifferentiable function. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 27 June 2015 # # Author: # # John Burkardt # # Input: # # integer N, the number of evaluation points. # # real X(N), the evaluation points. # # Output: # # real VALUE(N), the function values. # import numpy as np value = np.zeros ( n ) i = ( x <= 1.0 / 3.0 ) j = ( 1.0 / 3.0 < x ) value[i] = 1.0 - 3.0 * x[i] value[j] = 6.0 * x[j] - 2.0 return value def p03_f ( n, x ): #*****************************************************************************80 # ## p03_f evaluates the function for problem p03. # # Discussion: # # Step function. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 27 June 2015 # # Author: # # John Burkardt # # Input: # # integer N, the number of evaluation points. # # real X(N), the evaluation points. # # Output: # # real VALUE(N), the function values. # import numpy as np value = x * ( 10.0 * x - 1.0 ) * ( 5.0 * x - 2.0 ) * ( 5.0 * x - 2.0 ) \ * ( 4 * x - 3.4 ) * ( x - 1.0 ) return value def p04_f ( n, x ): #*****************************************************************************80 # ## p04_f evaluates the function for problem p04. # # Discussion: # # Step function. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 27 June 2015 # # Author: # # John Burkardt # # Input: # # integer N, the number of evaluation points. # # real X(N), the evaluation points. # # Output: # # real VALUE(N), the function values. # import numpy as np value = np.arctan ( 40.0 * x - 15.0 ) return value def p05_f ( n, x ): #*****************************************************************************80 # ## p05_f evaluates the function for problem p05. # # Discussion: # # Step function. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 27 June 2015 # # Author: # # John Burkardt # # Input: # # integer N, the number of evaluation points. # # real X(N), the evaluation points. # # Output: # # real VALUE(N), the function values. # import numpy as np value = np.cos ( 7.0 * x ) \ + 5.0 * np.cos ( 11.2 * x ) \ - 2.0 * np.cos ( 14.0 * x ) \ + 5.0 * np.cos ( 31.5 * x ) \ + 7.0 * np.cos ( 63.0 * x ) return value def p06_f ( n, x ): #*****************************************************************************80 # ## p06_f evaluates the function for problem p06. # # Discussion: # # f(x) = exp ( - (4 * x - 1)^2 ) # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 27 June 2015 # # Author: # # John Burkardt # # Reference: # # Alan Genz, # A Package for Testing Multiple Integration Subroutines, # in Numerical Integration: Recent Developments, Software # and Applications, # edited by Patrick Keast and Graeme Fairweather, # D Reidel, 1987, pages 337-340, # LC: QA299.3.N38. # # Input: # # integer N, the number of points. # # real X(N), the evaluation points. # # Output: # # real VALUE(N), the function values. # import numpy as np value = np.exp ( - ( 4.0 * x - 1.0 ) ** 2 ) return value def p07_f ( n, x ): #*****************************************************************************80 # ## p07_f evaluates the function for problem p07. # # Discussion: # # f(x) = exp ( 4 * x ) if x <= 1/2 # 0 otherwise # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 27 June 2015 # # Author: # # John Burkardt # # Reference: # # Alan Genz, # A Package for Testing Multiple Integration Subroutines, # in Numerical Integration: Recent Developments, Software # and Applications, # edited by Patrick Keast and Graeme Fairweather, # D Reidel, 1987, pages 337-340, # LC: QA299.3.N38. # # Input: # # integer N, the number of points. # # real X(N), the evaluation points. # # Output: # # real VALUE(N), the function values. # import numpy as np value = np.zeros ( n ) i = ( x < 0.5 ) value[i] = np.exp ( 4.0 * x[i] ) return value def p08_f ( n, x ): #*****************************************************************************80 # ## p08_f evaluates the function for problem p08. # # Discussion: # # This is a famous example, due to Runge. If equally spaced # abscissas are used, the sequence of interpolating polynomials Pn(X) # diverges, in the sense that the max norm of the difference # between Pn(X) and F(X) becomes arbitrarily large as N increases. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 27 June 2015 # # Author: # # John Burkardt # # Input: # # integer N, the number of evaluation points. # # real X(N), the evaluation points. # # Output: # # real VALUE(N), the function values. # import numpy as np value = 1.0 / ( ( 10.0 * ( x - 0.5 ) ) ** 2 + 1.0 ) return value def p00_f_test ( ): #*****************************************************************************80 # ## p00_f_test tests p00_f. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 27 June 2015 # # Author: # # John Burkardt # import numpy as np import platform print ( '' ) print ( 'p00_f_test' ) print ( ' Python version: %s' % ( platform.python_version ( ) ) ) print ( ' p00_f evaluates any function at N points X.' ) prob_num = p00_prob_num ( ) n = 11 a = 0.0 b = 1.0 x = np.linspace ( a, b, n ) print ( '' ) for prob in range ( 1, prob_num + 1 ): y = p00_f ( prob, n, x ) title = 'X, Y for problem ' + str ( prob ) r8vec2_print ( n, x, y, title ) # # Terminate. # print ( '' ) print ( 'p00_f_test:' ) print ( ' Normal end of execution.' ) return def p00_plot ( prob ): #*****************************************************************************80 # ## p00_plot plots the data for any of the tests. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 27 June 2015 # # Author: # # John Burkardt # # Input: # # integer PROB, the problem index. # # Output: # # string FILENAME, the plot filename. # import matplotlib.pyplot as plt import numpy as np prob_num = p00_prob_num ( ) if ( prob < 1 or prob_num < prob ): print ( '' ) print ( 'p00_plot - Fatal error!' ) print ( ' Values of PROB must be between 1 and %d.' % ( prob_num ) ) raise Exception ( 'p00_plot - Fatal error!' ) xmin = 0.0 xmax = 1.0 n = 501 x = np.linspace ( xmin, xmax, n ) y = p00_f ( prob, n, x ) t = p00_title ( prob ) plt.plot ( x, y, linewidth = 2.0 ) plt.title ( t ) plt.grid ( True ) plt.xlabel ( '<---X--->' ) plt.ylabel ( '<---Y--->' ) filename = 'p0' + str ( prob ) + '_plot.png' plt.savefig ( filename ) plt.show ( block = False ) plt.close ( ) return filename def p00_plot_test ( ): #*****************************************************************************80 # ## p00_plot_test tests p00_plot. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 27 June 2015 # # Author: # # John Burkardt # import platform print ( '' ) print ( 'p00_plot_test' ) print ( ' Python version: %s' % ( platform.python_version ( ) ) ) print ( ' p00_plot plots any test problem.' ) num = p00_prob_num ( ) print ( '' ) print ( ' test_interp_1d includes %d test problems.' % ( num ) ) print ( '' ) for prob in range ( 1, num + 1 ): filename = p00_plot ( prob ) print ( ' # %d "%s"' % ( prob, filename ) ) # # Terminate. # print ( '' ) print ( 'p00_plot_test:' ) print ( ' Normal end of execution.' ) return def p00_prob_num ( ): #*****************************************************************************80 # ## p00_prob_num returns the number of problems. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 27 June 2015 # # Author: # # John Burkardt # # Output: # # integer VALUE, the number of problems. # value = 8 return value def p00_prob_num_test ( ): #*****************************************************************************80 # ## p00_prob_num_test tests p00_prob_num. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 27 June 2015 # # Author: # # John Burkardt # import platform print ( '' ) print ( 'p00_prob_num_test' ) print ( ' Python version: %s' % ( platform.python_version ( ) ) ) print ( ' p00_prob_num returns the number of test problems.' ) num = p00_prob_num ( ) print ( '' ) print ( ' test_interp_1d includes %d test problems.' % ( num ) ) # # Terminate. # print ( '' ) print ( 'p00_prob_num_test:' ) print ( ' Normal end of execution.' ) return def p00_title ( prob ): #*****************************************************************************80 # ## p00_title returns the title of any problem. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 27 June 2015 # # Author: # # John Burkardt # # Input: # # integer PROB, the number of the desired test problem. # # Output: # # string TITLE, the title of the problem. # if ( prob == 1 ): title = p01_title ( ) elif ( prob == 2 ): title = p02_title ( ) elif ( prob == 3 ): title = p03_title ( ) elif ( prob == 4 ): title = p04_title ( ) elif ( prob == 5 ): title = p05_title ( ) elif ( prob == 6 ): title = p06_title ( ) elif ( prob == 7 ): title = p07_title ( ) elif ( prob == 8 ): title = p08_title ( ) else: print ( '' ) print ( 'p00_title - Fatal error!' ) print ( ' Illegal problem number = %d' % ( prob ) ) raise Exception ( 'p00_title - Fatal error!' ) return title def p01_title ( ): #*****************************************************************************80 # ## p01_title returns the title of problem p01. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 27 June 2015 # # Author: # # John Burkardt # # Output: # # string TITLE, the title of the problem. # title = 'f(x) = steps -1/2/1 at [0,1/3], [1/3,4/5], [4/5,1].' return title def p02_title ( ): #*****************************************************************************80 # ## p02_title returns the title of problem p02. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 27 June 2015 # # Author: # # John Burkardt # # Output: # # string TITLE, the title of the problem. # title = 'f(x) = (1-3x), x < 1/3 (6x-2) if 1/3 < x' return title def p03_title ( ): #*****************************************************************************80 # ## p03_title returns the title of problem p03. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 27 June 2015 # # Author: # # John Burkardt # # Output: # # string TITLE, the title of the problem. # title = 'f(x) = x (10*x-1) (5x-2) (5x-2) (4x-3.4) (x-1)' return title def p04_title ( ): #*****************************************************************************80 # ## p04_title returns the title of problem p04. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 27 June 2015 # # Author: # # John Burkardt # # Output: # # string TITLE, the title of the problem. # title = 'f(x) = atan ( 40 * x - 15 )' return title def p05_title ( ): #*****************************************************************************80 # ## p05_title returns the title of problem p05. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 27 June 2015 # # Author: # # John Burkardt # # Output: # # string TITLE, the title of the problem. # title = 'f(x) = cos(7*x)+5*cos(11.2*x)-2*cos(14*x)+5*cos(31.5*x)+7*cos(63*x).' return title def p06_title ( ): #*****************************************************************************80 # ## p06_title returns the title of problem p06. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 27 June 2015 # # Author: # # John Burkardt # # Output: # # string TITLE, the title of the problem. # title = 'f(x) = exp ( - ( 4*x-1 )^2 )' return title def p07_title ( ): #*****************************************************************************80 # ## p07_title returns the title of problem p07. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 27 June 2015 # # Author: # # John Burkardt # # Output: # # string TITLE, the title of the problem. # title = 'f(x) = exp ( 2 x ) if x < 0.5, 0 otherwise' return title def p08_title ( ): #*****************************************************************************80 # ## p08_title returns the title of problem p08. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 27 June 2015 # # Author: # # John Burkardt # # Output: # # string TITLE, the title of the problem. # title = 'f(x) = 1 / ( 1 + ( 10 * (x-1/2) )^2 )' return title def p00_title_test ( ): #*****************************************************************************80 # ## p00_title_test tests p00_title. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 27 June 2015 # # Author: # # John Burkardt # import platform print ( '' ) print ( 'p00_title_test' ) print ( ' Python version: %s' % ( platform.python_version ( ) ) ) print ( ' p00_title returns the title of any test problems.' ) num = p00_prob_num ( ) print ( '' ) print ( ' test_interp_1d includes %d test problems.' % ( num ) ) print ( '' ) for prob in range ( 1, num + 1 ): title = p00_title ( prob ) print ( ' # %d "%s"' % ( prob, title ) ) # # Terminate. # print ( '' ) print ( 'p00_title_test:' ) print ( ' Normal end of execution.' ) return def r8vec2_print ( n, a1, a2, title ): #*****************************************************************************80 # ## r8vec2_print prints an R8VEC2. # # Discussion: # # An R8VEC2 is a dataset consisting of N pairs of real values, stored # as two separate vectors A1 and A2. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 27 June 2015 # # Author: # # John Burkardt # # Input: # # integer N, the number of components of the vector. # # real A1(N), A2(N), the vectors to be printed. # # string TITLE, a title. # print ( '' ) print ( title ) print ( '' ) for i in range ( 0, n ): print ( ' %6d: %12g %12g' % ( i, a1[i], a2[i] ) ) return def r8vec2_print_test ( ): #*****************************************************************************80 # ## r8vec2_print_test tests r8vec2_print. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 27 June 2015 # # Author: # # John Burkardt # import numpy as np import platform print ( '' ) print ( 'r8vec2_print_test' ) print ( ' Python version: %s' % ( platform.python_version ( ) ) ) print ( ' r8vec2_print prints a pair of R8VEC\'s.' ) n = 6 v = np.array ( [ 0.0, 0.20, 0.40, 0.60, 0.80, 1.0 ], dtype = np.float64 ) w = np.array ( [ 0.0, 0.04, 0.16, 0.36, 0.64, 1.0 ], dtype = np.float64 ) r8vec2_print ( n, v, w, ' Print a pair of R8VEC\'s:' ) # # Terminate. # print ( '' ) print ( 'r8vec2_print_test:' ) print ( ' Normal end of execution.' ) return def test_interp_1d_test ( ): #*****************************************************************************80 # ## test_interp_1d_test() tests test_interp_1d(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 27 June 2015 # # Author: # # John Burkardt # import platform print ( '' ) print ( 'test_interp_1d_test():' ) print ( ' Python version: %s' % ( platform.python_version ( ) ) ) print ( ' Test test_interp_1d().' ) p00_prob_num_test ( ) p00_title_test ( ) p00_f_test ( ) p00_plot_test ( ) # # Terminate. # print ( '' ) print ( 'test_interp_1d_test():' ) print ( ' Normal end of execution.' ) return def timestamp ( ): #*****************************************************************************80 # ## timestamp() prints the date as a timestamp. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 06 April 2013 # # Author: # # John Burkardt # import time t = time.time ( ) print ( time.ctime ( t ) ) return None if ( __name__ == '__main__' ): timestamp ( ) test_interp_1d_test ( ) timestamp ( )