void test02 ( int nd ) //****************************************************************************80 // // Purpose: // // TEST_INTERP_1D_TEST02 evaluates each test function at ND sample points. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 28 August 2012 // // Author: // // John Burkardt // // Parameters: // // Input, int ND, the number of sample points. // { double a; double b; double *f; int j; int prob; int prob_num; double *x; printf ( "\n" ); printf ( "TEST_INTERP_1D_TEST02\n" ); printf ( " Use P00_F to sample each function.\n" ); prob_num = p00_prob_num ( ); a = 0.0; b = 1.0; x = r8vec_linspace_new ( nd, a, b ); printf ( "\n" ); for ( prob = 1; prob <= prob_num; prob++ ) { f = p00_f ( prob, nd, x ); printf ( "\n" ); printf ( " X, F(X) for problem %d\n", prob ); printf ( "\n" ); for ( j = 0; j < nd; j++ ) { printf ( " %2d %10f %10g\n", j, x[j], f[j] ); } free ( f ); } free ( x ); return; }
void test02 ( void ) /******************************************************************************/ /* Purpose: TEST02 evaluates the objective function at each starting point. Licensing: This code is distributed under the GNU LGPL license. Modified: 17 February 2012 Author: John Burkardt */ { double *a; double *b; double *f; double *fs; int i; int know; int m; int n = 100000; int problem; int problem_num; int seed; char title[100]; double *x; double *xs; printf ( "\n" ); printf ( "TEST02\n" ); printf ( " For each problem, evaluate the function at many points.\n" ); printf ( " Number of sample points = %d\n", n ); /* Get the number of problems. */ problem_num = p00_problem_num ( ); for ( problem = 1; problem <= problem_num; problem++ ) { printf ( "\n" ); printf ( " Problem %d\n", problem ); p00_title ( problem, title ); printf ( " %s\n", title ); m = p00_m ( problem ); printf ( " M = %d\n", m ); a = ( double * ) malloc ( m * sizeof ( double ) ); b = ( double * ) malloc ( m * sizeof ( double ) ); p00_ab ( problem, m, a, b ); printf ( "\n" ); printf ( " I A(i) B(i)\n" ); printf ( "\n" ); for ( i = 0; i < m; i++ ) { printf ( " %4d %10g %10g\n", i, a[i], b[i] ); } seed = 123456789; x = r8col_uniform_new ( m, n, a, b, &seed ); f = p00_f ( problem, m, n, x ); printf ( "\n" ); printf ( " Max(F) = %g\n", r8vec_max ( n, f ) ); printf ( " Min(F) = %g\n", r8vec_min ( n, f ) ); know = 0; xs = p00_sol ( problem, m, &know ); if ( know != 0 ) { fs = p00_f ( problem, m, 1, xs ); printf ( " F(X*) = %g\n", fs[0] ); free ( fs ); free ( xs ); } else { printf ( " X* is not given.\n" ); } free ( a ); free ( b ); free ( f ); free ( x ); } return; }
void test01 ( int prob, int nd ) /******************************************************************************/ /* Purpose: BARYCENTRIC_INTERP_1D_TEST01 tests LAGCHEBY1_INTERP_1D. Licensing: This code is distributed under the GNU LGPL license. Modified: 30 September 2012 Author: John Burkardt Parameters: Input, int PROB, the problem index. Input, int ND, the number of data points to use. */ { double a; double b; double int_error; int ni; double *xd; double *xi; double *yd; double *yi; printf ( "\n" ); printf ( "BARYCENTRIC_INTERP_1D_TEST01:\n" ); printf ( " Interpolate data from TEST_INTERP_1D problem #%d\n", prob ); printf ( " Use Chebyshev Type 1 spacing for data points.\n" ); printf ( " Number of data points = %d\n", nd ); /* Define the data. */ a = 0.0; b = +1.0; xd = r8vec_cheby1space_new ( nd, a, b ); yd = p00_f ( prob, nd, xd ); if ( nd < 10 ) { r8vec2_print ( nd, xd, yd, " Data array:" ); } /* #1: Does the interpolant match the function at the interpolation points? */ ni = nd; xi = r8vec_copy_new ( ni, xd ); yi = lagcheby1_interp_1d ( nd, xd, yd, ni, xi ); int_error = r8vec_norm_affine ( ni, yi, yd ) / ( double ) ( ni ); printf ( "\n" ); printf ( " L2 interpolation error averaged per interpolant node = %g\n", int_error ); free ( xd ); free ( xi ); free ( yd ); free ( yi ); return; }
void test06 ( void ) /******************************************************************************/ /* Purpose: TEST06 carries out a version of Brent's derivative-free minimizer. Licensing: This code is distributed under the GNU LGPL license. Modified: 03 February 2012 Author: John Burkardt */ { double a; double b; double fa; double fb; double fx; int problem_num; int problem; char title[50]; double tol = 0.000001; double x; printf ( "\n" ); printf ( "TEST06\n" ); printf ( " For each problem, use Brent's method.\n" ); /* Get the number of problems. */ problem_num = p00_problem_num ( ); for ( problem = 1; problem <= problem_num; problem++ ) { p00_title ( problem, title ); printf ( "\n" ); printf ( " Problem %d\n", problem ); printf ( " %s\n", title ); p00_interval ( problem, &a, &b ); fa = p00_f ( problem, a ); fb = p00_f ( problem, b ); printf ( "\n" ); printf ( " Initial interval [A,B]:\n" ); printf ( "\n" ); printf ( " A, B: %16g %16g\n", a, b ); printf ( " FA, FB: %16g %16g\n", fa, fb ); x = p00_fmin ( &a, &b, problem, tol ); fa = p00_f ( problem, a ); fb = p00_f ( problem, b ); fx = p00_f ( problem, x ); printf ( "\n" ); printf ( " Final interval [A,X*,B]:\n" ); printf ( "\n" ); printf ( " A, X*, B: %16g %16g %16g\n", a, x, b ); printf ( " FA, FX*, FB: %16g %16g %16g\n", fa, fx, fb ); } return; }
void test05 ( void ) /******************************************************************************/ /* Purpose: TEST05 carries out a simple bisection method. Licensing: This code is distributed under the GNU LGPL license. Modified: 03 February 2012 Author: John Burkardt */ { double a; double b; double c; double d; double e; double fa; double fb; double fc; double fd; double fe; int i; int max_step = 10; int problem_num; int problem; char title[50]; printf ( "\n" ); printf ( "TEST05\n" ); printf ( " For each problem, take a few steps of \n" ); printf ( " the bisection method.\n" ); /* Get the number of problems. */ problem_num = p00_problem_num ( ); for ( problem = 1; problem <= problem_num; problem++ ) { p00_title ( problem, title ); printf ( "\n" ); printf ( " Problem %d\n", problem ); printf ( " %s\n", title ); p00_interval ( problem, &a, &c ); b = 0.5 * ( a + c ); fa = p00_f ( problem, a ); fc = p00_f ( problem, c ); fb = p00_f ( problem, b ); i = 0; printf ( "\n" ); printf ( " %d\n", i ); printf ( " X: %10g %10g %10g\n", a, b, c ); printf ( " F: %10g %10g %10g\n", fa, fb, fc ); for ( i = 1; i <= max_step; i++ ) { d = 0.5 * ( a + b ); fd = p00_f ( problem, d ); e = 0.5 * ( b + c ); fe = p00_f ( problem, e ); if ( fd <= fb ) { c = b; fc = fb; b = d; fb = fd; } else if ( fe <= fb ) { a = b; fa = fb; b = e; fb = fe; } else { a = d; fa = fd; c = e; fc = fe; } printf ( " %d\n", i ); printf ( " X: %10g %10g %10g\n", a, b, c ); printf ( " F: %10g %10g %10g\n", fa, fb, fc ); } } return; }
void test02 ( void ) /******************************************************************************/ /* Purpose: TEST02 evaluates the objective function at each starting point. Licensing: This code is distributed under the GNU LGPL license. Modified: 03 February 2012 Author: John Burkardt */ { double f_sol; double f_start; int know; int problem_num; int problem; char title[50]; double x; printf ( "\n" ); printf ( "TEST02\n" ); printf ( " For each problem, evaluate the function\n" ); printf ( " at the starting point and the solution.\n" ); /* Get the number of problems. */ problem_num = p00_problem_num ( ); for ( problem = 1; problem <= problem_num; problem++ ) { p00_title ( problem, title ); printf ( "\n" ); printf ( " Problem %d\n", problem ); printf ( " %s\n", title ); printf ( "\n" ); x = p00_start ( problem ); f_start = p00_f ( problem, x ); printf ( " F(X_START) = %g\n", f_start ); p00_sol ( problem, &know, &x ); if ( 0 < know ) { f_sol = p00_f ( problem, x ); printf ( " F(X_SOL) = %g\n", f_sol ); } } return; }