#! /usr/bin/env python # def clenshaw_curtis_compute ( n ): #*****************************************************************************80 # ## CLENSHAW_CURTIS_COMPUTE computes a Clenshaw Curtis quadrature rule. # # Discussion: # # This method uses a direct approach. The paper by Waldvogel # exhibits a more efficient approach using Fourier transforms. # # The integral: # # integral ( -1 <= x <= 1 ) f(x) dx # # The quadrature rule: # # sum ( 1 <= i <= n ) w(i) * f ( x(i) ) # # The abscissas for the rule of order N can be regarded # as the cosines of equally spaced angles between 180 and 0 degrees: # # X(I) = cos ( ( N - I ) * PI / ( N - 1 ) ) # # except for the basic case N = 1, when # # X(1) = 0. # # A Clenshaw-Curtis rule that uses N points will integrate # exactly all polynomials of degrees 0 through N-1. If N # is odd, then by symmetry the polynomial of degree N will # also be integrated exactly. # # If the value of N is increased in a sensible way, then # the new set of abscissas will include the old ones. One such # sequence would be N(K) = 2*K+1 for K = 0, 1, 2, ... # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 02 April 2015 # # Author: # # John Burkardt # # Reference: # # Charles Clenshaw, Alan Curtis, # A Method for Numerical Integration on an Automatic Computer, # Numerische Mathematik, # Volume 2, Number 1, December 1960, pages 197-205. # # Philip Davis, Philip Rabinowitz, # Methods of Numerical Integration, # Second Edition, # Dover, 2007, # ISBN: 0486453391, # LC: QA299.3.D28. # # Joerg Waldvogel, # Fast Construction of the Fejer and Clenshaw-Curtis Quadrature Rules, # BIT Numerical Mathematics, # Volume 43, Number 1, 2003, pages 1-18. # # Parameters: # # Input, integer N, the order. # # Output, real X(N), the abscissas. # # Output, real W(N), the weights. # import numpy as np if ( n == 1 ): x = np.zeros ( n ) w = np.zeros ( n ) w[0] = 2.0 else: theta = np.zeros ( n ) for i in range ( 0, n ): theta[i] = float ( n - 1 - i ) * np.pi / float ( n - 1 ) x = np.cos ( theta ) w = np.zeros ( n ) for i in range ( 0, n ): w[i] = 1.0 jhi = ( ( n - 1 ) // 2 ) for j in range ( 0, jhi ): if ( 2 * ( j + 1 ) == ( n - 1 ) ): b = 1.0 else: b = 2.0 w[i] = w[i] - b * np.cos ( 2.0 * float ( j + 1 ) * theta[i] ) \ / float ( 4 * j * ( j + 2 ) + 3 ) w[0] = w[0] / float ( n - 1 ) for i in range ( 1, n - 1 ): w[i] = 2.0 * w[i] / float ( n - 1 ) w[n-1] = w[n-1] / float ( n - 1 ) return x, w def clenshaw_curtis_compute_test ( ): #*****************************************************************************80 # ## CLENSHAW_CURTIS_COMPUTE_TEST tests CLENSHAW_CURTIS_COMPUTE # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 02 April 2015 # # Author: # # John Burkardt # import platform print ( '' ) print ( 'CLENSHAW_CURTIS_COMPUTE_TEST' ) print ( ' Python version: %s' % ( platform.python_version ( ) ) ) print ( ' CLENSHAW_CURTIS_COMPUTE computes' ) print ( ' a Clenshaw-Curtis quadrature rule over [-1,1].' ) print ( '' ) print ( ' Index X W' ) for n in range ( 1, 11 ): [ x, w ] = clenshaw_curtis_compute ( n ) print ( '' ) for i in range ( 0, n ): print ( ' %2d %24.16g %24.16g' % ( i, x[i], w[i] ) ) # # Terminate. # print ( '' ) print ( 'CLENSHAW_CURTIS_COMPUTE_TEST:' ) print ( ' Normal end of execution.' ) return if ( __name__ == '__main__' ): from timestamp import timestamp timestamp ( ) clenshaw_curtis_compute_test ( ) timestamp ( )