#! /usr/bin/env python # def vandermonde_coef_1d ( n, x, y ): #*****************************************************************************80 # ## VANDERMONDE_COEF_1D computes coefficients of a 1D Vandermonde interpolant. # # Discussion: # # We assume the interpolant has the form # # p(x) = c1 + c2 * x + c3 * x^2 + ... + cn * x^(n-1). # # We have n data values (x(i),y(i)) which must be interpolated: # # p(x(i)) = c1 + c2 * x(i) + c3 * x(i)^2 + ... + cn * x(i)^(n-1) = y(i) # # This can be cast as an NxN linear system for the polynomial # coefficients: # # [ 1 x1 x1^2 ... x1^(n-1) ] [ c1 ] = [ y1 ] # [ 1 x2 x2^2 ... x2^(n-1) ] [ c2 ] = [ y2 ] # [ ...................... ] [ ... ] = [ ... ] # [ 1 xn xn^2 ... xn^(n-1) ] [ cn ] = [ yn ] # # and if the x values are distinct, the system is theoretically # invertible, so we can retrieve the coefficient vector c and # evaluate the interpolant. # # The polynomial could be evaluated at the n-vector x by the command # # pval = polyval ( c, x ) # # ...except that MATLAB assumes that c(1) multiplies x^(n-1). # # so instead, you might use # # pval = r8poly_value ( n - 1, c, n, x ) # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 19 July 2012 # # Author: # # John Burkardt # # Parameters: # # Input, integer N, the number of data points. # # Input, real X(N,1), Y(N,1), the data values. # # Output, real C(N,1), the coefficients of the interpolating # polynomial. C(1) is the constant term, and C(N) multiplies X^(N-1). # import numpy as np from vandermonde_matrix_1d import vandermonde_matrix_1d ad = vandermonde_matrix_1d ( n, x ) c = np.linalg.solve ( ad, y ) return c def vandermonde_coef_1d_test ( ): #*****************************************************************************80 # ## VANDERMONDE_COEF_1D_TEST tests VANDERMONDE_COEF_1D. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 04 July 2015 # # Author: # # John Burkardt # import numpy as np import platform from r8poly_print import r8poly_print 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 ( 'VANDERMONDE_COEF_1D_TEST' ) print ( ' Python version: %s' % ( platform.python_version ( ) ) ) print ( ' VANDERMONDE_COEF_1D sets the Vandermonde coefficients for 1D interpolation.' ) r8vec2_print ( nd, xd, yd, ' Interpolation data:' ) cd = vandermonde_coef_1d ( nd, xd, yd ) r8vec_print ( nd, cd, ' Vandermonde interpolant coefficients:' ) r8poly_print ( nd - 1, cd, ' Vandermonde interpolant polynomial:' ) # # Terminate. # print ( '' ) print ( 'VANDERMONDE_COEF_1D_TEST:' ) print ( ' Normal end of execution.' ) return if ( __name__ == '__main__' ): from timestamp import timestamp timestamp ( ) vandermonde_coef_1d_test ( ) timestamp ( )