コード例 #1
0
int main_old ( int argc, char *argv[] )
{
  double a;
  double alpha;
  double b;
  double beta;
  char filename[255];
  int kind;
  int order;
  double *r;
  double *w;
  double *x;

  printf ( "\n" );
  printf ( "legendre_rule\n" );
  printf ( "  c version\n" );
  printf ( "\n" );
  /*printf ( "  compiled on %s at %s.\n", __date__, __time__ );*/
  printf ( "\n" );
  printf ( "  compute a gauss-legendre rule for approximating\n" );
  printf ( "\n" );
  printf ( "    integral ( a <= x <= b ) f(x) dx\n" );
  printf ( "\n" );
  printf ( "  of order order.\n" );
  printf ( "\n" );
  printf ( "  the user specifies order, a, b and filename.\n" );
  printf ( "\n" );
  printf ( "  order is the number of points.\n" );
  printf ( "\n" );
  printf ( "  a is the left endpoint.\n" );
  printf ( "\n" );
  printf ( "  b is the right endpoint.\n" );
  printf ( "\n" );
  printf ( "  filename is used to generate 3 files:\n" );
  printf ( "\n" );
  printf ( "    filename_w.txt - the weight file\n" );
  printf ( "    filename_x.txt - the abscissa file.\n" );
  printf ( "    filename_r.txt - the region file.\n" );
/*
  initialize parameters;
*/
  alpha = 0.0;
  beta = 0.0;
/*
  get order.
*/
  if ( 1 < argc )
  {
    order = atoi ( argv[1] );
  }
  else
  {
    printf ( "\n" );
    printf ( "  enter the value of order (1 or greater)\n" );
    scanf ( "%d", &order );
  }
/*
  get a.
*/
  if ( 2 < argc )
  {
    a = atof ( argv[2] );
  }
  else
  {
    printf ( "\n" );
    printf ( "  enter the value of a:\n" );
    scanf ( "%lf", &a );
  }
/*
  get b.
*/
  if ( 3 < argc )
  {
    b = atof ( argv[3] );
  }
  else
  {
    printf ( "\n" );
    printf ( "  enter the value of b:\n" );
    scanf ( "%lf", &b );
  }
/*
  get filename:
*/
  if ( 4 < argc )
  {
    strcpy ( filename, argv[4] );
  }
  else
  {
    printf ( "\n" );
    printf ( "  enter filename, the \"root name\" of the quadrature files).\n" );
    scanf ( "%s", filename );
  }
/*
  input summary.
*/
  printf ( "\n" );
  printf ( "  order = %d\n", order );
  printf ( "  a = %g\n", a );
  printf ( "  b = %g\n", b );
  printf ( "  filename = \"%s\".\n", filename );
/*
  construct the rule.
*/
  w = ( double * ) malloc ( order * sizeof ( double ) );
  x = ( double * ) malloc ( order * sizeof ( double ) );
  
  kind = 1;
  cgqf ( order, kind, alpha, beta, a, b, x, w );
/*
  write the rule.
*/
  r = ( double * ) malloc ( 2 * sizeof ( double ) );
  r[0] = a;
  r[1] = b;

  rule_write ( order, filename, x, w, r );
/*
  free memory.
*/
  free ( r );
  free ( w );
  free ( x );
/*
  terminate.
*/
  printf ( "\n" );
  printf ( "legendre_rule:\n" );
  printf ( "  normal end of execution.\n" );

  printf ( "\n" );

  return 0;
}
コード例 #2
0
ファイル: toms655_prb.c プロジェクト: spino327/jburkardt-c
void test02 ( void )

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

    TEST02 tests CIQFS.

  Licensing:

    This code is distributed under the GNU LGPL license.

  Modified:

    11 January 2010

  Author:

    Original FORTRAN77 version by Sylvan Elhay, Jaroslav Kautsky.
    C version by John Burkardt.
*/
{
  double a;
  double alpha;
  double b;
  double beta;
  int i;
  int key;
  int kind;
  int lu;
  int *mlt;
  int *ndx;
  int nt;
  int nwts;
  double *t;
  double *wts;

  printf ( "  ----------------------------------------\n" );
  printf ( "\n" );
  printf ( "TEST02\n" );
  printf ( "  Test CIQF, CIQFS, CGQF and CGQFS\n" );
  printf ( "  with all classical weight functions.\n" );
/*
  Try all 8 weight functions.
*/
  for ( kind = 1; kind <= 8; kind++ )
  {
/*
  Number of knots.
*/
    nt = 5;
/*
  Set parameters ALPHA and BETA.
*/
    alpha = 0.5;
    if ( kind != 8 )
    {
      beta  = 2.0;
    }
    else
    {
      beta = - 16.0;
    }
/*
  Set A and B.
*/
    a = - 0.5;
    b = 2.0;
/*
  LU controls printing.
  A positive value requests that we compute and print weights, and
  conduct a moments check.
*/
    lu = 6;
/*
  Have CGQF compute the knots and weights.
*/
    t = ( double * ) malloc ( nt * sizeof ( double ) );
    wts = ( double * ) malloc ( nt * sizeof ( double ) );

    printf ( "\n" );
    printf ( "  Knots and weights of Gauss quadrature formula\n" );
    printf ( "  computed by CGQF.\n" );
    cgqf ( nt, kind, alpha, beta, a, b, lu, t, wts );
/*
  Now compute the weights for the same knots by CIQF.

  Set the knot multiplicities.
*/
    mlt = ( int * ) malloc ( nt * sizeof ( int ) );
    for ( i = 0; i < nt; i++ )
    {
      mlt[i] = 2;
    }
/*
  Set the size of the weights array.
*/
    nwts = 0;
    for ( i = 0; i < nt; i++ )
    {
      nwts = nwts + mlt[i];
    }
/*
  We need to deallocate and reallocate WTS because it is now of
  dimension NWTS rather than NT.
*/
    free ( wts );
    wts = ( double * ) malloc ( nwts * sizeof ( double ) );
/*
  Because KEY = 1, NDX will be set up for us.
*/
    ndx = ( int * ) malloc ( nt * sizeof ( int ) );
/*
  KEY = 1 indicates that the WTS array should hold the weights
  in the usual order.
*/
    key = 1;

    printf ( "\n" );
    printf ( "  Weights of Gauss quadrature formula computed from the\n" );
    printf ( "  knots by CIQF.\n" );

    wts = ciqf ( nt, t, mlt, nwts, ndx, key, kind, alpha, beta, a, b, lu );

    free ( mlt );
    free ( ndx );
    free ( t );
    free ( wts );
  }
  return;
}