def kronrod_test02 ( ): #*****************************************************************************80 # ## KRONROD_TEST02 tests the code for the even case N = 4. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 30 April 2013 # # Author: # # John Burkardt # import numpy from kronrod import kronrod n = 4 print '' print 'KRONROD_TEST02' print ' Request KRONROD to compute the Gauss rule' print ' of order 4, and the Kronrod extension of' print ' order 4+5=9.' tol = 0.000001 [ x, w1, w2 ] = kronrod ( n, tol ) print '' print ' KRONROD returns 3 vectors of length %d' % ( n + 1 ) print '' print ' I X WK WG' print '' for i in range ( 1, n + 2 ): print ' %4d %14f %14f %14f' % ( i, x[i-1], w1[i-1], w2[i-1] ) return
def kronrod_test02(): #*****************************************************************************80 # ## KRONROD_TEST02 tests the code for the even case N = 4. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 30 April 2013 # # Author: # # John Burkardt # import numpy from kronrod import kronrod n = 4 print '' print 'KRONROD_TEST02' print ' Request KRONROD to compute the Gauss rule' print ' of order 4, and the Kronrod extension of' print ' order 4+5=9.' tol = 0.000001 [x, w1, w2] = kronrod(n, tol) print '' print ' KRONROD returns 3 vectors of length %d' % (n + 1) print '' print ' I X WK WG' print '' for i in range(1, n + 2): print ' %4d %14f %14f %14f' % (i, x[i - 1], w1[i - 1], w2[i - 1]) return
def kronrod_test01 ( ): #*****************************************************************************80 # ## KRONROD_TEST01 tests the code for the odd case N = 3. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 30 April 2013 # # Author: # # John Burkardt # import numpy from kronrod import kronrod n = 3 wg = numpy.array ( [ \ 0.555555555555555555556, \ 0.888888888888888888889, \ 0.555555555555555555556 ] ) wk = numpy.array ( [ \ 0.104656226026467265194, \ 0.268488089868333440729, \ 0.401397414775962222905, \ 0.450916538658474142345, \ 0.401397414775962222905, \ 0.268488089868333440729, \ 0.104656226026467265194 ] ) xg = numpy.array ( [ \ -0.77459666924148337704, \ 0.0, \ 0.77459666924148337704 ] ) xk = numpy.array ( [ \ -0.96049126870802028342, \ -0.77459666924148337704, \ -0.43424374934680255800, \ 0.0, \ 0.43424374934680255800, \ 0.77459666924148337704, \ 0.96049126870802028342 ] ) print '' print 'KRONROD_TEST01' print ' Request KRONROD to compute the Gauss rule' print ' of order 3, and the Kronrod extension of' print ' order 3+4=7.' print '' print ' Compare to exact data.' tol = 0.000001 [ x, w1, w2 ] = kronrod ( n, tol ) print '' print ' KRONROD returns 3 vectors of length %d' % ( n + 1 ) print '' print ' I X WK WG' print '' for i in range ( 1, n + 2 ): print ' %4d %14f %14f %14f' % ( i, x[i-1], w1[i-1], w2[i-1] ) print '' print ' Gauss Abscissas' print ' Exact Computed' print '' for i in range ( 1, n + 1 ): if ( 2 * i <= n + 1 ): i2 = 2 * i s = -1.0 else: i2 = 2 * ( n + 1 ) - 2 * i s = +1.0 print ' %4d %14f %14f' % ( i, xg[i-1], s * x[i2-1] ) print '' print ' Gauss Weights' print ' Exact Computed' print '' for i in range ( 1, n + 1 ): if ( 2 * i <= n + 1 ): i2 = 2 * i else: i2 = 2 * ( n + 1 ) - 2 * i print ' %4d %14f %14f' % ( i, wg[i-1], w2[i2-1] ) print '' print ' Gauss Kronrod Abscissas' print ' Exact Computed' print '' for i in range ( 1, 2 * n + 2 ): if ( i <= n + 1 ): i2 = i s = -1.0 else: i2 = 2 * ( n + 1 ) - i s = +1.0 print ' %4d %14f %14f' % ( i, xk[i-1], s * x[i2-1] ) print '' print ' Gauss Kronrod Weights' print ' Exact Computed' print '' for i in range ( 1, 2 * n + 2 ): if ( i <= n + 1 ): i2 = i else: i2 = 2 * ( n + 1 ) - i print ' %4d %14f %14f' % ( i, wk[i-1], w1[i2-1] ) return
def kronrod_test03 ( ): #*****************************************************************************80 # ## KRONROD_TEST03 uses the program to estimate an integral. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 30 April 2013 # # Author: # # John Burkardt # from f import f from kronrod import kronrod exact = 1.5643964440690497731 print '' print 'KRONROD_TEST03' print ' Call Kronrod to estimate the integral of a function.' print ' Keep trying until the error is small.' # # TOL just tells KRONROD how carefully it must compute X, W1 and W2. # It is NOT a statement about the accuracy of your integral estimate! # tol = 0.000001 # # Start the process with a 1 point rule. # n = 1 while ( 1 ): [ x, w1, w2 ] = kronrod ( n, tol ) # # Compute the estimates. # There are two complications here: # # 1) Both rules use all the points. However, the lower order rule uses # a zero weight for the points it doesn't need. # # 2) The points X are all positive, and are listed in descending order. # this means that 0 is always in the list, and always occurs as the # last member. Therefore, the integral estimates should use the # function value at 0 once, and the function values at the other # X values "twice", that is, once at X and once at -X. # i1 = w1[n+1-1] * f ( x[n+1-1] ) i2 = w2[n+1-1] * f ( x[n+1-1] ) for i in range ( 1, n + 1 ): i1 = i1 + w1[i-1] * ( f ( - x[i-1] ) + f ( x[i-1] ) ) i2 = i2 + w2[i-1] * ( f ( - x[i-1] ) + f ( x[i-1] ) ) if ( abs ( i1 - i2 ) < 0.0001 ): print '' print ' Error tolerance satisfied with N = %d' % ( n ) print ' Coarse integral estimate = %14.6g' % ( i1 ) print ' Fine integral estimate = %14.6g' % ( i2 ) print ' Error estimate = %g' % ( abs ( i2 - i1 ) ) print ' Actual error = %g' % ( abs ( exact - i2 ) ) break if ( 25 < n ): print '' print ' Error tolerance failed even for n = %d' % ( n ) print ' Canceling iteration, and accepting bad estimates!' print ' Coarse integral estimate = %14.6g' % ( i1 ) print ' Fine integral estimate = %14.6g' % ( i2 ) print ' Error estimate = %g' % ( abs ( i2 - i1 ) ) print ' Actual error = %g' % ( abs ( exact - i2 ) ) break n = 2 * n + 1 return
def F(x): return np.exp(-x**2) n = 1 # Setting the Order tol = 1e-6 # Tolerance Err = 1 # Error of the difference of Kronrod and Gauss while Err > tol: abscissa = [] # an array for abscissa WK = [] # an array for Kronrod Weights WG = [] # an array for Gauss Weights # Calling Kronrod to get abscissa [0,1] and weights abscissa, WK, WG = kronrod(n, tol) abscissa_rev = abscissa[::-1] # make a reverse of abscissa # making the abscissa to [-1,1] for i in range(n): abscissa = np.append(abscissa, 2 * abscissa_rev[0] - abscissa_rev[i + 1]) WG_ = [] WK_ = [] for i in range(n): WG_ = np.append(WG_, WG[i]) WK_ = np.append(WK_, WK[i]) WG_rev = WG_[::-1] WK_rev = WK_[::-1] for i in range(n): WK = np.append(WK, WK_rev[i]) WG = np.append(WG, WG_rev[i])