#! /usr/bin/env python # def vandermonde_interp_1d_test ( ): #*****************************************************************************80 # ## VANDERMONDE_INTERP_1D_TEST tests the VANDERMONDE_INTERP_1D library. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 02 July 2015 # # Author: # # John Burkardt # import platform from vandermonde_coef_1d import vandermonde_coef_1d_test from vandermonde_matrix_1d import vandermonde_matrix_1d_test from vandermonde_value_1d import vandermonde_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 r8poly_print import r8poly_print_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_ab import r8vec_uniform_ab_test from r8vec2_print import r8vec2_print_test print ( '' ) print ( 'VANDERMONDE_INTERP_1D_TEST' ) print ( ' Python version: %s' % ( platform.python_version ( ) ) ) print ( ' Test the VANDERMONDE_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 ( ) r8poly_print_test ( ) r8vec_norm_test ( ) r8vec_norm_affine_test ( ) r8vec_print_test ( ) r8vec_uniform_ab_test ( ) r8vec2_print_test ( ) # # Library functions. # vandermonde_coef_1d_test ( ) vandermonde_matrix_1d_test ( ) vandermonde_value_1d_test ( ) prob_num = p00_prob_num ( ); for prob in range ( 1, prob_num + 1 ): vandermonde_interp_1d_test01 ( prob ) # # Terminate. # print ( '' ) print ( 'VANDERMONDE_INTERP_1D_TEST:' ) print ( ' Normal end of execution.' ) return def vandermonde_interp_1d_test01 ( prob ): #*****************************************************************************80 # ## VANDERMONDE_INTERP_1D_TEST01 tests VANDERMONDE_VALUE_1D. # # 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 vandermonde_coef_1d import vandermonde_coef_1d from vandermonde_value_1d import vandermonde_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 ( 'VANDERMONDE_INTERP_1D_TEST01:' ) print ( ' Python version: %s' % ( platform.python_version ( ) ) ) print ( ' Interpolate data from 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 ) 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:' ) # # Get Vandermonde interpolant coefficients. # cd = vandermonde_coef_1d ( nd, xd, yd ) # # #1: Does interpolant match function at interpolation points? # ni = nd xi = xd yi = vandermonde_value_1d ( nd, cd, 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: Compare estimated curve length to piecewise linear (minimal) curve length. # Assume data is sorted, and normalize X and Y dimensions by (XMAX-XMIN) and # (YMAX-YMIN). # xmin = np.min ( xd ) xmax = np.max ( xd ) ymin = np.min ( yd ) ymax = np.max ( yd ) ni = 501 xi = np.linspace ( xmin, xmax, ni ) yi = vandermonde_value_1d ( nd, cd, ni, xi ) ld = 0.0 for i in range ( 0, nd - 1 ): ld = ld + np.sqrt \ ( \ ( ( xd[i+1] - xd[i] ) / ( xmax - xmin ) ) ** 2 \ + ( ( yd[i+1] - yd[i] ) / ( ymax - ymin ) ) ** 2 \ ) li = 0.0 for i in range ( 0, ni - 1 ): li = li + np.sqrt \ ( \ ( ( xi[i+1] - xi[i] ) / ( xmax - xmin ) ) ** 2 \ + ( ( yi[i+1] - yi[i] ) / ( ymax - ymin ) ) ** 2 \ ) li = np.sqrt ( li ) print ( '' ) print ( ' Normalized length of piecewise linear interpolant = %g' % ( ld ) ) print ( ' Normalized length of Shepard interpolant = %g' % ( li ) ) # # #3: Plot the data. # plt.plot ( xd, yd, 'b-', linewidth = 3.0 ) plt.plot ( xd, yd, 'k.', markersize = 10 ) t = 'p0' + str ( prob ) + ' Piecewise Linear Interpolant' plt.title ( t ) plt.grid ( True ) plt.xlabel ( '<---X--->' ) plt.ylabel ( '<---Y--->' ) filename = 'p0' + str ( prob ) + '_data.png' plt.savefig ( filename ) plt.clf ( ) print ( '' ) print ( ' Created plot file "%s"' % ( filename ) ) # # #4: Plot the piecewise linear and Vandermonde interpolants. # ni = 501 xmin = np.min ( xd ) xmax = np.max ( xd ) xi = np.linspace ( xmin, xmax, ni ) yi = vandermonde_value_1d ( nd, cd, 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 ) + ' Vandermonde Interpolant for ' + str ( nd ) + ' nodes.' plt.title ( t ) plt.grid ( True ) plt.xlabel ( '<---X--->' ) plt.ylabel ( '<---Y--->' ) filename = 'p0' + str ( prob ) + '_vandermonde.png' plt.savefig ( filename ) plt.clf ( ) print ( ' Created plot file "%s".' % ( filename ) ) # # Terminate. # print ( '' ) print ( 'VANDERMONDE_INTERP_1D_TEST01:' ) print ( ' Normal end of execution.' ) return if ( __name__ == '__main__' ): from timestamp import timestamp timestamp ( ) vandermonde_interp_1d_test ( ) timestamp ( )