#! /usr/bin/env python # def chebyshev_even2 ( n, f ): #*****************************************************************************80 # ## CHEBYSHEV_EVEN2 returns the even Chebyshev coefficients of F. # # Discussion: # # The coefficients are calculated using the zeros of Tn(x). # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 08 January 2016 # # Author: # # John Burkardt # # Reference: # # D B Hunter, H V Smith, # A quadrature formula of Clenshaw-Curtis type for the Gegenbauer weight function, # Journal of Computational and Applied Mathematics, # Volume 177, 2005, pages 389-400. # # Parameters: # # Input, integer N, the number of points to use. # 1 <= N. # # Input, real F(x), the handle for the function to be integrated with the # Gegenbauer weight. # # Output, real B2(1+N/2), the even Chebyshev coefficients of F. # import numpy as np s = ( n // 2 ) sigma = ( n % 2 ) b2 = np.zeros ( s + 1 ) for r in range ( 0, 2 * s + 1, 2 ): total = 0.0 for j in range ( 0, n + 1 ): total = total \ + f ( np.cos ( float ( 2 * j + 1 ) * np.pi / 2.0 / float ( n + 1 ) ) ) \ * np.cos ( r * float ( 2 * j + 1 ) * np.pi / 2.0 / float ( n + 1 ) ) rh = ( r // 2 ) b2[rh] = ( 2.0 / ( n + 1 ) ) * total return b2 def chebyshev_even2_test ( ): #*****************************************************************************80 # ## CHEBYSHEV_EVEN2_TEST tests CHEBYSHEV_EVEN2. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 14 January 2016 # # Author: # # John Burkardt # import platform from r8vec_print import r8vec_print print ( '' ) print ( 'CHEBYSHEV_EVEN2_TEST:' ) print ( ' Python version: %s' % ( platform.python_version ( ) ) ) print ( ' CHEBYSHEV_EVEN2 computes the even Chebyshev coefficients' ) print ( ' of a function F, using the zeros of Tn(x).' ) lam = 0.75 a = 2.0 n = 6 b2 = chebyshev_even2 ( n, f ) s = ( n // 2 ) r8vec_print ( s + 1, b2, ' Computed Coefficients:' ) # # Terminate. # print ( '' ) print ( 'CHEBYSHEV_EVEN2_TEST' ) print ( ' Normal end of execution.' ) return def f ( x ): #*****************************************************************************80 # ## F defines the function whose Fourier coefficients are desired. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 14 January 2016 # # Author: # # John Burkardt # # Parameters: # # Input, real X, the argument. # # Output, real VALUE, the value. # import numpy as np a = 2.0 value = np.cos ( a * x ) return value if ( __name__ == '__main__' ): from timestamp import timestamp timestamp ( ) chebyshev_even2_test ( ) timestamp ( )