void test01 ( )

/******************************************************************************/
/*
  Purpose:

    TEST01 tests P00_PROBLEM_NUM and P00_TITLE.

  Licensing:

    This code is distributed under the GNU LGPL license. 

  Modified:

    11 September 2012

  Author:

    John Burkardt
*/
{
  int problem;
  int problem_num;
  char *title;

  printf ( "\n" );
  printf ( "TEST01\n" );
  printf ( "  P00_PROBLEM_NUM returns the number of problems.\n" );
  printf ( "  P00_TITLE returns the title of a problem.\n" );

  problem_num = p00_problem_num ( );

  printf ( "\n" );
  printf ( "  P00_PROBLEM_NUM: number of problems is %d\n", problem_num );
  printf ( "\n" );
  printf ( "   Problem       Title\n" );
  printf ( "\n" );

  for ( problem = 1; problem <= problem_num; problem++ )
  {
    title = p00_title ( problem );

    printf ( "  %8d  \"%s\".\n", problem, title );

    free ( title );
  }

  return;
}
void test02 ( )

/******************************************************************************/
/*
  Purpose:

    TEST02 tests P00_EXACT.

  Licensing:

    This code is distributed under the GNU LGPL license. 

  Modified:

    11 September 2012

  Author:

    John Burkardt
*/
{
  double exact;
  int m;
  int problem;
  int problem_num;

  printf ( "\n" );
  printf ( "TEST02\n" );
  printf ( "  P00_EXACT returns the \"exact\" integral.\n" );

  problem_num = p00_problem_num ( );

  m = 4;
  p06_param ( 'S', 'M', &m );

  printf ( "\n" );
  printf ( "   Problem       EXACT\n" );
  printf ( "\n" );

  for ( problem = 1; problem <= problem_num; problem++ )
  {
    exact = p00_exact ( problem );

    printf ( "  %8d  %24.16g\n", problem, exact );
  }

  return;
}
void test02 ( )

/******************************************************************************/
/*
  Purpose:

    TEST02 tests P00_ALPHA and P00_EXACT.

  Licensing:

    This code is distributed under the GNU LGPL license. 

  Modified:

    14 September 2012

  Author:

    John Burkardt
*/
{
  double alpha;
  double exact;
  int problem;
  int problem_num;

  printf ( "\n" );
  printf ( "TEST02\n" );
  printf ( "  P00_ALPHA returns the lower limit of integration.\n" );
  printf ( "  P00_EXACT returns the \"exact\" integral.\n" );

  problem_num = p00_problem_num ( );

  printf ( "\n" );
  printf ( "   Problem       ALPHA           EXACT\n" );
  printf ( "\n" );

  for ( problem = 1; problem <= problem_num; problem++ )
  {
    alpha = p00_alpha ( problem );

    exact = p00_exact ( problem );

    printf ( "  %8d  %14g  %24.16g\n", problem, alpha, exact );
  }

  return;
}
void test01 ( void )

/******************************************************************************/
/*
  Purpose:

    TEST01 simply prints the title of each problem.

  Licensing:

    This code is distributed under the GNU LGPL license. 

  Modified:

    17 February 2012

  Author:

    John Burkardt
*/
{
  int problem_num;
  int problem;
  char title[100];

  printf ( "\n" );
  printf ( "TEST01\n" );
  printf ( "  For each problem, print the title.\n" );
/*
  Get the number of problems.
*/
  problem_num = p00_problem_num ( );

  printf ( "\n" );
  printf ( "  Problem    Title\n" );
  printf ( "\n" );

  for ( problem = 1; problem <= problem_num; problem++ )
  {
    p00_title ( problem, title );

    printf ( "  %6d  %s\n", problem, title );
  }

  return;
}
void test06 ( )

/******************************************************************************/
/*
  Purpose:

    TEST06 tests P00_MONTE_CARLO.

  Licensing:

    This code is distributed under the GNU LGPL license. 

  Modified:

    11 September 2012

  Author:

    John Burkardt
*/
{
  double error;
  double estimate;
  double exact;
  int m;
  int order;
  int order_log;
  int problem;
  int problem_num;

  printf ( "\n" );
  printf ( "TEST06\n" );
  printf ( "  P00_MONTE_CARLO uses a weighted form of the Monte Carlo method\n" );
  printf ( "  to estimate a Hermite integral on (-oo,+oo).\n" );

  problem_num = p00_problem_num ( );

  m = 4;
  p06_param ( 'S', 'M', &m );

  printf ( "\n" );
  printf ( 
    "   Problem     Order          Estimate        Exact          Error\n" );

  for ( problem = 1; problem <= problem_num; problem++ )
  {
    exact = p00_exact ( problem );

    order = 128;

    printf ( "\n" );

    for ( order_log = 0; order_log <= 6; order_log++ )
    {
      estimate = p00_monte_carlo ( problem, order );

      error = r8_abs ( exact - estimate );

      printf ( "  %8d  %8d  %14.6g  %14.6g  %14.6g\n", 
        problem, order, estimate, exact, error );

      order = order * 4;
    }
  }
  return;
}
void test04 ( )

/******************************************************************************/
/*
  Purpose:

    TEST04 tests P00_TURING.

  Licensing:

    This code is distributed under the GNU LGPL license. 

  Modified:

    11 September 2012

  Author:

    John Burkardt
*/
{
  double error;
  double estimate;
  double exact;
  double h;
  int m;
  int n;
  int order_log;
  int problem;
  int problem_num;
  int test;
  double tol;

  printf ( "\n" );
  printf ( "TEST04\n" );
  printf ( "  P00_TURING applies a Turing procedure\n" );
  printf ( "  to estimate an integral on (-oo,+oo).\n" );

  problem_num = p00_problem_num ( );

  m = 4;
  p06_param ( 'S', 'M', &m );

  for ( test = 1; test <= 2; test++ )
  {
    if ( test == 1 )
    {
      tol = 1.0E-4;
    }
    else if ( test == 2 )
    {
      tol = 1.0E-07;
    }
    printf ( "\n" );
    printf ( "  Using a tolerance of TOL = %g\n", tol );
    printf ( "\n" );
    printf ( 
      "   Problem     Order          Estimate        Exact          Error\n" );

    for ( problem = 1; problem <= problem_num; problem++ )
    {
      exact = p00_exact ( problem );

      h = 1.0;

      printf ( "\n" );

      for ( order_log = 0; order_log <= 6; order_log++ )
      {
        estimate = p00_turing ( problem, h, tol, &n );

        error = r8_abs ( exact - estimate );

        printf ( "  %8d  %10g  %8d  %14.6g  %14.6g  %14.6g\n",
          problem, h, n, estimate, exact, error );

        h = h / 2.0;
      }
    }
  }
  return;
}
void test03 ( )

/******************************************************************************/
/*
  Purpose:

    TEST03 tests P00_GAUSS_HERMITE.

  Licensing:

    This code is distributed under the GNU LGPL license. 

  Modified:

    11 September 2012

  Author:

    John Burkardt
*/
{
  double error;
  double estimate;
  double exact;
  int m;
  int order;
  int order_log;
  int problem;
  int problem_num;

  printf ( "\n" );
  printf ( "TEST03\n" );
  printf ( "  P00_GAUSS_HERMITE applies a Gauss-Hermite rule\n" );
  printf ( "  to estimate an integral on (-oo,+oo).\n" );

  problem_num = p00_problem_num ( );

  m = 4;
  p06_param ( 'S', 'M', &m );

  printf ( "\n" );
  printf ( "   Problem     Order          Estimate        Exact          Error\n" );

  for ( problem = 1; problem <= problem_num; problem++ )
  {
    exact = p00_exact ( problem );

    order = 1;

    printf ( "\n" );

    for ( order_log = 0; order_log <= 6; order_log++ )
    {
      estimate = p00_gauss_hermite ( problem, order );

      error = r8_abs ( exact - estimate );

      printf ( "  %8d  %8d  %14.6g  %14.6g  %14.6g\n", 
        problem, order, estimate, exact, error );

      order = order * 2;
    }
  }
  return;
}
void test05 ( )

/******************************************************************************/
/*
  Purpose:

    TEST05 tests P00_RAT_TRANSFORM.

  Licensing:

    This code is distributed under the GNU LGPL license. 

  Modified:

    14 September 2012

  Author:

    John Burkardt
*/
{
  double error;
  double estimate;
  double exact;
  int order;
  int order_log;
  int problem;
  int problem_num;

  printf ( "\n" );
  printf ( "TEST05\n" );
  printf ( "  P00_RAT_TRANSFORM applies a rational transform\n" );
  printf ( "  to estimate an integral on [ALPHA,+oo)\n" );
  printf ( "  as a transformed integral on (0,1/(1+ALPHA)],\n" );
  printf ( "  and applying a Gauss-Legendre rule.\n" );

  problem_num = p00_problem_num ( );

  printf ( "\n" );
  printf ( "                              Exact\n" );
  printf ( "   Problem     Order          Estimate        Error\n" );

  for ( problem = 1; problem <= problem_num; problem++ )
  {
    exact = p00_exact ( problem );

    order = 1;

    printf ( "\n" );
    printf ( "  %8d            %14.6g\n", problem, exact );

    for ( order_log = 0; order_log <= 6; order_log++ )
    {
      estimate = p00_rat_transform ( problem, order );

      error = r8_abs ( exact - estimate );

      printf ( "          %8d  %14.6g  %14.6g\n", order, estimate, error );

      order = order * 2;
    }
  }
  return;
}
void test03 ( )

/******************************************************************************/
/*
  Purpose:

    TEST03 tests P00_GAUSS_LAGUERRE.

  Licensing:

    This code is distributed under the GNU LGPL license. 

  Modified:

    14 September 2012

  Author:

    John Burkardt
*/
{
  double error;
  double estimate;
  double exact;
  int order;
  int order_log;
  int problem;
  int problem_num;

  printf ( "\n" );
  printf ( "TEST03\n" );
  printf ( "  P00_GAUSS_LAGUERRE applies a Gauss-Laguerre rule\n" );
  printf ( "  to estimate an integral on [ALPHA,+oo).\n" );

  problem_num = p00_problem_num ( );

  printf ( "\n" );
  printf ( "                              Exact\n" );
  printf ( "   Problem     Order          Estimate        Error\n" );

  for ( problem = 1; problem <= problem_num; problem++ )
  {
    exact = p00_exact ( problem );

    order = 1;

    printf ( "\n" );
    printf ( "  %8d            %14.6g\n", problem, exact );

    for ( order_log = 0; order_log <= 6; order_log++ )
    {
      estimate = p00_gauss_laguerre ( problem, order );

      error = r8_abs ( exact - estimate );

      printf ( "          %8d  %14.6g  %14.6g\n", order, estimate, error );

      order = order * 2;
    }
  }
  return;
}
Beispiel #10
0
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;
}
Beispiel #11
0
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;
}
Beispiel #12
0
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;
}
Beispiel #13
0
void test04 ( void )

/******************************************************************************/
/*
  Purpose:

    TEST04 compares the exact and approximate second derivatives.

  Licensing:

    This code is distributed under the GNU LGPL license.

  Modified:

    03 February 2012

  Author:

    John Burkardt
*/
{
  double f2;
  double f2_dif;
  int problem_num;
  int problem;
  char title[50];
  double x;

  printf ( "\n" );
  printf ( "TEST04\n" );
  printf ( "  For each problem, compare the exact and\n" );
  printf ( "  approximate second derivatives at the starting point.\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 );

    x = p00_start ( problem );

    printf ( "\n" );
    printf ( "  X:\n" );
    printf ( "  %g\n", x );

    f2 = p00_f2 ( problem, x );

    printf ( "  F\"(X) (exact):\n" );
    printf ( "  %g\n", f2 );

    f2_dif = p00_f2_dif ( problem, x );

    printf ( "  F\"(X) (difference):\n" );
    printf ( "  %g\n", f2_dif );
  }

  return;
}
Beispiel #14
0
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;
}