#! /usr/bin/env python # def newton_coef_1d ( nd, xd, yd ): #*****************************************************************************80 # ## NEWTON_COEF_1D computes coefficients of a Newton 1D interpolant. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 10 July 2015 # # Author: # # John Burkardt # # Reference: # # Carl deBoor, # A Practical Guide to Splines, # Springer, 2001, # ISBN: 0387953663, # LC: QA1.A647.v27. # # Parameters: # # Input, integer ND, the number of data values. # # Input, real XD(ND), the X values at which data was taken. # # Input, real YD(ND), the corresponding Y values. # # Output, real CD(ND), the divided difference # coefficients corresponding to the input (XTAB,YTAB). # import numpy as np cd = np.zeros ( nd, dtype = np.float64 ) for i in range ( 0, nd ): cd[i] = yd[i] for i in range ( 1, nd ): for j in range ( nd - 1, i - 1, -1 ): cd[j] = ( cd[j] - cd[j-1] ) / ( xd[j] - xd[j-i] ) return cd def newton_coef_1d_test ( ): #*****************************************************************************80 # ## NEWTON_COEF_1D_TEST tests NEWTON_COEF_1D. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 10 July 2015 # # Author: # # John Burkardt # import numpy as np from r8vec_print import r8vec_print from r8vec2_print import r8vec2_print nd = 5 xd = np.array ( [ 0.0, 1.0, 2.0, 3.0, 4.0 ] ) yd = np.array ( [ 24.0, 0.0, 0.0, 0.0, 0.0 ] ) print ( '' ) print ( 'NEWTON_COEF_1D_TEST' ) print ( ' NEWTON_COEF_1D sets the coefficients for a 1D Newton interpolation.' ) r8vec2_print ( nd, xd, yd, ' Interpolation data:' ) cd = newton_coef_1d ( nd, xd, yd ) r8vec_print ( nd, cd, ' Newton interpolant coefficients:' ) # # Terminate. # print ( '' ) print ( 'NEWTON_COEF_1D_TEST:' ) print ( ' Normal end of execution.' ) return if ( __name__ == '__main__' ): from timestamp import timestamp timestamp ( ) newton_coef_1d_test ( ) timestamp ( )