void test03 ( int prob ) /******************************************************************************/ /* Purpose: TEST03 tests PWL_INTERP_2D_SCATTERED_VALUE. Licensing: This code is distributed under the GNU LGPL license. Modified: 25 October 2012 Author: John Burkardt */ { int *element_neighbor; int element_num; int g; int i; int j; int k; int nd; int ni = 25; double rms; int *triangle; double x; double *xd; double xi[25]; double *xyd; double xyi[2*25]; double y; double *yd; double yi[25]; double *zd; double *ze; double *zi; g = 2; nd = g00_size ( g ); printf ( "\n" ); printf ( "TEST03\n" ); printf ( " PWL_INTERP_2D_SCATTERED_VALUE evaluates a\n" ); printf ( " piecewise linear interpolant to scattered data.\n" ); printf ( " Here, we use grid number %d\n", g ); printf ( " with %d scattered points in the unit square\n", nd ); printf ( " on problem %d\n", prob ); /* Get the data points and evaluate the function there. */ xd = ( double * ) malloc ( nd * sizeof ( double ) ); yd = ( double * ) malloc ( nd * sizeof ( double ) ); g00_xy ( g, nd, xd, yd ); zd = ( double * ) malloc ( nd * sizeof ( double ) ); f00_f0 ( prob, nd, xd, yd, zd ); xyd = ( double * ) malloc ( 2 * nd * sizeof ( double ) ); for ( i = 0; i < nd; i++ ) { xyd[0+i*2] = xd[i]; xyd[1+i*2] = yd[i]; } /* Set up the Delaunay triangulation. */ element_neighbor = ( int * ) malloc ( 3 * 2 * nd * sizeof ( int ) ); triangle = ( int * ) malloc ( 3 * 2 * nd * sizeof ( int ) ); r8tris2 ( nd, xyd, &element_num, triangle, element_neighbor ); for ( j = 0; j < element_num; j++ ) { for ( i = 0; i < 3; i++ ) { if ( 0 < element_neighbor[i+j*3] ) { element_neighbor[i+j*3] = element_neighbor[i+j*3] - 1; } } } /* Define the interpolation points. */ k = 0; for ( i = 1; i <= 5; i++ ) { for ( j = 1; j <= 5; j++ ) { xyi[0+k*2] = ( 2 * i - 1 ) / 10.0; xyi[1+k*2] = ( 2 * j - 1 ) / 10.0; k = k + 1; } } for ( k = 0; k < ni; k++ ) { xi[k] = xyi[0+k*2]; yi[k] = xyi[1+k*2]; } ze = ( double * ) malloc ( ni * sizeof ( double ) ); f00_f0 ( prob, ni, xi, yi, ze ); /* Evaluate the interpolant. */ zi = pwl_interp_2d_scattered_value ( nd, xyd, zd, element_num, triangle, element_neighbor, ni, xyi ); rms = 0.0; for ( k = 0; k < ni; k++ ) { rms = rms + pow ( zi[k] - ze[k], 2 ); } rms = sqrt ( rms / ( double ) ( ni ) ); printf ( "\n" ); printf ( " RMS error is %g\n", rms ); printf ( "\n" ); printf ( " K Xi(K) Yi(K) Zi(K) Z(X,Y)\n" ); printf ( "\n" ); for ( k = 0; k < ni; k++ ) { printf ( " %4d %10.4f %10.4f %10.4f %10.4f\n", k, xyi[0+k*2], xyi[1+k*2], zi[k], ze[k] ); } free ( element_neighbor ); free ( triangle ); free ( xd ); free ( xyd ); free ( yd ); free ( zd ); free ( ze ); return; }
void test01 ( int prob, int n ) /******************************************************************************/ /* Purpose: PWL_INTERP_2D_TEST01 tests PWL_INTERP_2D. Licensing: This code is distributed under the GNU LGPL license. Modified: 15 October 2012 Author: John Burkardt Parameters: Input, int PROB, the problem number. Input, int N, the grid size in each dimension. */ { double app_error; int i; int ij; double int_error; int j; int nd; int ni; int nxd; int nyd; double *xd; double *xd_1d; double *xi; double *xi_1d; double *yd; double *yd_1d; double *yi; double *yi_1d; double *zd; double *zdm; double *zi; nxd = n; nyd = n; printf ( "\n" ); printf ( "PWL_INTERP_2D_TEST01:\n" ); printf ( " Interpolate data from TEST_INTERP_2D problem #%d\n", prob ); printf ( " Using polynomial interpolant of product degree %d x %d\n", nxd, nyd ); nd = nxd * nyd; printf ( " Number of data points = %d\n", nd ); xd_1d = r8vec_linspace_new ( nxd, 0.0, 1.0 ); yd_1d = r8vec_linspace_new ( nyd, 0.0, 1.0 ); xd = ( double * ) malloc ( nxd * nyd * sizeof ( double ) ); yd = ( double * ) malloc ( nxd * nyd * sizeof ( double ) ); zd = ( double * ) malloc ( nxd * nyd * sizeof ( double ) ); ij = 0; for ( j = 0; j < nyd; j++ ) { for ( i = 0; i < nxd; i++ ) { xd[ij] = xd_1d[i]; yd[ij] = yd_1d[j]; ij = ij + 1; } } f00_f0 ( prob, nd, xd, yd, zd ); if ( nd <= 20 ) { r8vec3_print ( nd, xd, yd, zd, " X, Y, Z data:" ); } /* #1: Does interpolant match function at data points? */ ni = nd; xi = r8vec_copy_new ( ni, xd ); yi = r8vec_copy_new ( ni, yd ); zi = pwl_interp_2d ( nxd, nyd, xd_1d, yd_1d, zd, ni, xi, yi ); if ( ni <= 20 ) { r8vec3_print ( ni, xi, yi, zi, " X, Y, Z interpolation:" ); } int_error = r8vec_norm_affine ( ni, zi, zd ) / ( double ) ( ni ); printf ( "\n" ); printf ( " RMS data interpolation error = %g\n", int_error ); free ( xi ); free ( yi ); free ( zi ); /* #2: Does interpolant approximate data at midpoints? */ if ( 1 < nd ) { xi_1d = ( double * ) malloc ( ( nxd - 1 ) * sizeof ( double ) ); yi_1d = ( double * ) malloc ( ( nyd - 1 ) * sizeof ( double ) ); for ( i = 0; i < nxd - 1; i++ ) { xi_1d[i] = 0.5 * ( xd_1d[i] + xd_1d[i+1] ); } for ( i = 0; i < nyd - 1; i++ ) { yi_1d[i] = 0.5 * ( yd_1d[i] + yd_1d[i+1] ); } ni = ( nxd - 1 ) * ( nyd - 1 ); xi = ( double * ) malloc ( ni * sizeof ( double ) ); yi = ( double * ) malloc ( ni * sizeof ( double ) ); zdm = ( double * ) malloc ( ni * sizeof ( double ) ); ij = 0; for ( j = 0; j < nyd - 1; j++ ) { for ( i = 0; i < nxd - 1; i++ ) { xi[ij] = xi_1d[i]; yi[ij] = yi_1d[j]; ij = ij + 1; } } f00_f0 ( prob, ni, xi, yi, zdm ); zi = pwl_interp_2d ( nxd, nyd, xd_1d, yd_1d, zd, ni, xi, yi ); app_error = r8vec_norm_affine ( ni, zi, zdm ) / ( double ) ( ni ); printf ( " RMS data approximation error = %g\n", app_error ); free ( xi ); free ( xi_1d ); free ( yi ); free ( yi_1d ); free ( zdm ); free ( zi ); } free ( xd ); free ( xd_1d ); free ( yd ); free ( yd_1d ); free ( zd ); return; }
void test01 ( int prob, int grd, int m ) /******************************************************************************/ /* Purpose: VANDERMONDE_APPROX_2D_TEST01 tests VANDERMONDE_APPROX_2D_MATRIX. Licensing: This code is distributed under the GNU LGPL license. Modified: 11 October 2012 Author: John Burkardt Parameters: Input, int PROB, the problem number. Input, int GRD, the grid number. (Can't use GRID as the name because that's also a plotting function.) Input, int M, the total polynomial degree. */ { double *a; double app_error; double *c; int nd; int ni; int tm; double *xd; double *xi; double *yd; double *yi; double *zd; double *zi; printf ( "\n" ); printf ( "TEST01:\n" ); printf ( " Approximate data from TEST_INTERP_2D problem #%d\n", prob ); printf ( " Use grid from TEST_INTERP_2D with index #%d\n", grd ); printf ( " Using polynomial approximant of total degree %d\n", m ); nd = g00_size ( grd ); printf ( " Number of data points = %d\n", nd ); xd = ( double * ) malloc ( nd * sizeof ( double ) ); yd = ( double * ) malloc ( nd * sizeof ( double ) ); g00_xy ( grd, nd, xd, yd ); zd = ( double * ) malloc ( nd * sizeof ( double ) ); f00_f0 ( prob, nd, xd, yd, zd ); if ( nd < 10 ) { r8vec3_print ( nd, xd, yd, zd, " X, Y, Z data:" ); } /* Compute the Vandermonde matrix. */ tm = triangle_num ( m + 1 ); a = vandermonde_approx_2d_matrix ( nd, m, tm, xd, yd ); /* Solve linear system. */ c = qr_solve ( nd, tm, a, zd ); /* #1: Does approximant match function at data points? */ ni = nd; xi = r8vec_copy_new ( ni, xd ); yi = r8vec_copy_new ( ni, yd ); zi = r8poly_value_2d ( m, c, ni, xi, yi ); app_error = r8vec_norm_affine ( ni, zi, zd ) / ( double ) ( ni ); printf ( "\n" ); printf ( " L2 data approximation error = %g\n", app_error ); free ( a ); free ( c ); free ( xd ); free ( xi ); free ( yd ); free ( yi ); free ( zd ); free ( zi ); return; }
void test02 ( void ) /******************************************************************************/ /* Purpose: TEST02 samples each function using each grid. Licensing: This code is distributed under the GNU LGPL license. Modified: 30 January 2012 Author: John Burkardt */ { double *f; double f_ave; double f_max; double f_min; int f_num; int fi; char ft[100]; int g_num; int gi; int gn; char gt[100]; double *gx; double *gy; printf ( "\n" ); printf ( "TEST02\n" ); printf ( " Sample each function over each grid.\n" ); g_num = g00_num ( ); f_num = f00_num ( ); for ( fi = 1; fi <= f_num; fi++ ) { f00_title ( fi, ft ); printf ( "\n" ); printf ( " %2d %s\n", fi, ft ); printf ( " Grid Title " ); printf ( "Min(F) Ave(F) Max(F)\n" ); printf ( "\n" ); for ( gi = 1; gi <= g_num; gi++ ) { g00_title ( gi, gt ); gn = g00_size ( gi ); gx = ( double * ) malloc ( gn * sizeof ( double ) ); gy = ( double * ) malloc ( gn * sizeof ( double ) ); g00_xy ( gi, gn, gx, gy ); f = ( double * ) malloc ( gn * sizeof ( double ) ); f00_f0 ( fi, gn, gx, gy, f ); f_max = r8vec_max ( gn, f ); f_min = r8vec_min ( gn, f ); f_ave = r8vec_sum ( gn, f ); f_ave = f_ave / ( double ) ( gn ); printf ( " %4d %25s %14g %14g %14g\n", gi, gt, f_min, f_ave, f_max ); free ( f ); free ( gx ); free ( gy ); } } return; }