#! /usr/bin/env python # def pwl_interp_1d_test ( ): #*****************************************************************************80 # ## PWL_INTERP_1D_TEST tests the PWL_INTERP_1D library. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 02 July 2015 # # Author: # # John Burkardt # import platform from pwl_basis_1d import pwl_basis_1d_test from pwl_value_1d import pwl_value_1d_test from p00_data import p00_data_test from p00_data_num import p00_data_num_test from p00_dim_num import p00_dim_num_test from p00_prob_num import p00_prob_num from p00_prob_num import p00_prob_num_test from r8mat_print import r8mat_print_test from r8mat_print_some import r8mat_print_some_test from r8mat_transpose_print import r8mat_transpose_print_test from r8mat_transpose_print_some import r8mat_transpose_print_some_test from r8vec_norm import r8vec_norm_test from r8vec_norm_affine import r8vec_norm_affine_test from r8vec_print import r8vec_print_test from r8vec_uniform_01 import r8vec_uniform_01_test from r8vec_uniform_ab import r8vec_uniform_ab_test from r8vec2_print import r8vec2_print_test print ( '' ) print ( 'PWL_INTERP_1D_TEST' ) print ( ' Python version: %s' % ( platform.python_version ( ) ) ) print ( ' Test the PWL_INTERP_1D library.' ) # # Utility functions. # p00_data_test ( ) p00_data_num_test ( ) p00_dim_num_test ( ) p00_prob_num_test ( ) r8mat_print_test ( ) r8mat_print_some_test ( ) r8mat_transpose_print_test ( ) r8mat_transpose_print_some_test ( ) r8vec_norm_test ( ) r8vec_norm_affine_test ( ) r8vec_print_test ( ) r8vec_uniform_01_test ( ) r8vec_uniform_ab_test ( ) r8vec2_print_test ( ) # # Library functions. # pwl_basis_1d_test ( ) pwl_value_1d_test ( ) prob_num = p00_prob_num ( ); for prob in range ( 1, prob_num + 1 ): for nd in ( [ 4, 8, 16, 32, 64 ] ): pwl_interp_1d_test01 ( prob, nd ) prob_num = p00_prob_num ( ); for prob in range ( 1, prob_num + 1 ): for nd in ( [ 4, 8, 16, 32, 64 ] ): pwl_interp_1d_test02 ( prob, nd ) # # Terminate. # print ( '' ) print ( 'PWL_INTERP_1D_TEST:' ) print ( ' Normal end of execution.' ) return def pwl_interp_1d_test01 ( prob, nd ): #*****************************************************************************80 # ## PWL_INTERP_1D_TEST01 tests PWL_VALUE_1D with evenly spaced data # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 01 July 2015 # # Author: # # John Burkardt # # Parameters: # # Input, integer PROB, the problem index. # # Input, integer ND, the number of data points to use. # import matplotlib.pyplot as plt import numpy as np import platform from pwl_value_1d import pwl_value_1d from p00_data import p00_data from p00_data_num import p00_data_num from r8vec_norm_affine import r8vec_norm_affine from r8vec2_print import r8vec2_print print ( '' ) print ( 'PWL_INTERP_1D_TEST01:' ) print ( ' Python version: %s' % ( platform.python_version ( ) ) ) print ( ' Interpolate data from TEST_INTERP problem #%d.' % ( prob ) ) print ( ' Use even spacing for data points.' ) print ( ' Number of data points = %d' % ( nd ) ) nd = p00_data_num ( prob ) print ( ' Number of data points = %d' % ( nd ) ) xy = p00_data ( prob, 2, nd ) xd = np.zeros ( nd ) yd = np.zeros ( nd ) for i in range ( 0, nd ): xd[i] = xy[0,i] yd[i] = xy[1,i] if ( nd < 10 ): r8vec2_print ( nd, xd, yd, ' Data array:' ) # # #1: Does interpolant match function at interpolation points? # ni = nd xi = xd yi = pwl_value_1d ( nd, xd, yd, ni, xi ) int_error = r8vec_norm_affine ( ni, yi, yd ) / float ( ni ) print ( '' ) print ( ' L2 interpolation error averaged per interpolant node = %g' % ( int_error ) ) # # #2: Plot the piecewise linear interpolant. # ni = 501 xmin = np.min ( xd ) xmax = np.max ( xd ) xi = np.linspace ( xmin, xmax, ni ) yi = pwl_value_1d ( nd, xd, yd, ni, xi ) plt.plot ( xd, yd, 'b-', linewidth = 3.0 ) plt.plot ( xi, yi, 'r-', linewidth = 4.0 ) plt.plot ( xd, yd, 'k.', markersize = 10 ) t = 'p0' + str ( prob ) + ' Lagrange/Even Polynomial Interpolant for ' + str ( nd ) + ' nodes.' plt.title ( t ) plt.grid ( True ) plt.xlabel ( '<---X--->' ) plt.ylabel ( '<---Y--->' ) filename = 'p0' + str ( prob ) + '_pwl_' + str ( nd ) + '.png' plt.savefig ( filename ) plt.clf ( ) print ( ' Created plot file "%s".' % ( filename ) ) # # Terminate. # print ( '' ) print ( 'PWL_INTERP_1D_TEST01:' ) print ( ' Normal end of execution.' ) return def pwl_interp_1d_test02 ( prob, nd ): #*****************************************************************************80 # ## PWL_INTERP_1D_TEST02 plots the basis functions. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 02 July 2015 # # Author: # # John Burkardt # # Parameters: # # Input, integer PROB, the problem index. # # Input, integer ND, the number of data points to use. # import matplotlib.pyplot as plt import numpy as np import platform from pwl_basis_1d import pwl_basis_1d from p00_data_num import p00_data_num from p00_data import p00_data from p00_data_num import p00_data_num from r8vec_norm_affine import r8vec_norm_affine from r8vec_print import r8vec_print print ( '' ) print ( 'PWL_INTERP_1D_TEST02:' ) print ( ' Plot the basis functions for TEST_INTERP problem #%d.' % ( prob ) ) nd = p00_data_num ( prob ) print ( ' Number of data points = %d' % ( nd ) ) xy = p00_data ( prob, 2, nd ) xd = np.zeros ( nd ) for i in range ( 0, nd ): xd[i] = xy[0,i] r8vec_print ( nd, xd, ' X data locations:' ) # # #4: Plot the piecewise linear and polynomial interpolants. # ni = 501 xmin = np.min ( xd ) xmax = np.max ( xd ) xi = np.linspace ( xmin, xmax, ni ) bk = pwl_basis_1d ( nd, xd, ni, xi ) for k in range ( 0, nd ): plt.plot ( xi, bk[:,k], 'r-', linewidth = 4.0 ) t = 'p0' + str ( prob ) + ' basis functions ' + str ( nd ) + ' nodes.' plt.title ( t ) plt.grid ( True ) plt.xlabel ( '<---X--->' ) plt.ylabel ( '<---Y--->' ) filename = 'p0' + str ( prob ) + '_pwl_basis_' + str ( nd ) + '.png' plt.savefig ( filename ) plt.clf ( ) print ( ' Created plot file "%s".' % ( filename ) ) # # Terminate. # print ( '' ) print ( 'PWL_INTERP_1D_TEST02:' ) print ( ' Normal end of execution.' ) return if ( __name__ == '__main__' ): from timestamp import timestamp timestamp ( ) pwl_interp_1d_test ( ) timestamp ( )