void asset_path_test ( )

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

    ASSET_PATH_TEST tests ASSET_PATH.

  Licensing:

    This code is distributed under the GNU LGPL license.

  Modified:

    18 February 2012

  Author:

    John Burkardt
*/
{
  double mu;
  int n = 100;
  char output_filename[100] = "asset_path.txt";;
  double *s;
  double s0;
  int seed;
  double sigma;
  double t1;

  printf ( "\n" );
  printf ( "ASSET_PATH_TEST:\n" );
  printf ( "  Demonstrate the simulated of an asset price path.\n" );

  s0 = 2.0;
  mu = 0.1;
  sigma = 0.3;
  t1 = 1.0;
  seed = 123456789;

  printf ( "\n" );
  printf ( "  The asset price at time 0      S0    = %g\n", s0 );
  printf ( "  The asset expected growth rate MU    = %g\n", mu );
  printf ( "  The asset volatility           SIGMA = %g\n", sigma );
  printf ( "  The expiry date                T1    = %g\n", t1 );
  printf ( "  The number of time steps       N     = %d\n", n );
  printf ( "  The random number seed was     SEED  = %d\n", seed );

  s = asset_path ( s0, mu, sigma, t1, n, &seed );

  r8vec_print_part ( n + 1, s, 10, "  Partial results:" );

  r8vec_write ( output_filename, n + 1, s );

  printf ( "\n" );
  printf ( "  Full results written to \"%s\".\n", output_filename );

  free ( s );

  return;
}
Пример #2
0
void test03 ( )

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

    TEST03 creates a CC sparse matrix file from an ST file.

  Discussion:

    We assume no prior knowledge about the matrix except the filename.

  Licensing:

    This code is distributed under the GNU LGPL license.

  Modified:

    23 July 2014

  Author:

    John Burkardt
*/
{
  double *acc;
  double *ast;
  int *ccc;
  char filename_acc[] = "west_acc.txt";
  char filename_ccc[] = "west_ccc.txt";
  char filename_icc[] = "west_icc.txt";
  char filename_st[] = "west_st.txt";
  int i_max;
  int i_min;
  int *icc;
  int *ist;
  int j_max;
  int j_min;
  int *jst;
  int m;
  int n;
  int ncc;
  int nst;

  printf ( "\n" );
  printf ( "TEST03\n" );
  printf ( "  Convert a sparse matrix from ST to CC format.\n" );
  printf ( "  ST: sparse triplet,    I, J,  A.\n" );
  printf ( "  CC: compressed column, I, CC, A.\n" );
  printf ( "  The ST matrix is read from the file '%s'\n", filename_st );
  printf ( "  and the CC matrix is written to the files:\n" );
  printf ( "    '%s',\n", filename_icc );
  printf ( "    '%s', and\n", filename_ccc );
  printf ( "    '%s'.\n", filename_acc );
/*
  Get the size of the ST matrix.
*/
  st_header_read ( filename_st, &i_min, &i_max, &j_min, &j_max, &m, &n, &nst );

  st_header_print ( i_min, i_max, j_min, j_max, m, n, nst );
/*
  Allocate space.
*/
  ist = ( int * ) malloc ( nst * sizeof ( int ) );
  jst = ( int * ) malloc ( nst * sizeof ( int ) );
  ast = ( double * ) malloc ( nst * sizeof ( double ) );
/*
  Read the ST matrix.
*/
  st_data_read ( filename_st, m, n, nst, ist, jst, ast );
/*
  Decrement the 1-based data.
*/
  i4vec_dec ( nst, ist );
  i4vec_dec ( nst, jst );
/*
  Get the CC size.
*/
  ncc = st_to_cc_size ( nst, ist, jst );

  printf ( "\n" );
  printf ( "  Number of CC values = %d\n", ncc );
/*
  Create the CC indices.
*/
  icc = ( int * ) malloc ( ncc * sizeof ( int ) );
  ccc = ( int * ) malloc ( ( n + 1 ) * sizeof ( int ) );

  st_to_cc_index ( nst, ist, jst, ncc, n, icc, ccc );
/*
  Create the CC values.
*/
  acc = st_to_cc_values ( nst, ist, jst, ast, ncc, n, icc, ccc );
/*
  Write the CC matrix.
*/
  i4vec_write ( filename_icc, ncc, icc );
  i4vec_write ( filename_ccc, n + 1, ccc );
  r8vec_write ( filename_acc, ncc, acc );
/*
  Free memory.
*/
  free ( acc );
  free ( ast );
  free ( ccc );
  free ( icc );
  free ( ist );
  free ( jst );

  return;
}
void fd1d_heat_explicit_test01 ( )

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

    FD1D_HEAT_EXPLICIT_TEST01 does a simple test problem

  Licensing:

    This code is distributed under the GNU LGPL license. 

  Modified:

    26 January 2012

  Author:

    John Burkardt
*/
{
  double cfl;
  double dt;
  double *h;
  double *h_new;
  double *hmat;
  int i;
  int j;
  double k;
  double *t;
  double t_max;
  double t_min;
  int t_num;
  double *x;
  double x_max;
  double x_min;
  int x_num;

  printf ( "\n" );
  printf ( "FD1D_HEAT_EXPLICIT_TEST01:\n" );
  printf ( "  Compute an approximate solution to the time-dependent\n" );
  printf ( "  one dimensional heat equation:\n" );
  printf ( "\n" );
  printf ( "    dH/dt - K * d2H/dx2 = f(x,t)\n" );
  printf ( "\n" );
  printf ( "  Run a simple test case.\n" );
/*
  Heat coefficient.
*/
  k = 0.002;
/*
  X_NUM is the number of equally spaced nodes to use between 0 and 1.
*/
  x_num = 21;
  x_min = 0.0;
  x_max = 1.0;
  x = r8vec_linspace_new ( x_num, x_min, x_max );
/*
  T_NUM is the number of equally spaced time points between 0 and 10.0.
*/
  t_num = 201;
  t_min = 0.0;
  t_max = 80.0;
  dt = ( t_max - t_min ) / ( double ) ( t_num - 1 );
  t = r8vec_linspace_new ( t_num, t_min, t_max );
/*
  Get the CFL coefficient.
*/
  cfl = fd1d_heat_explicit_cfl ( k, t_num, t_min, t_max, x_num, x_min, x_max );
/*
  Running the code produces an array H of temperatures H(t,x),
  and vectors x and t.
*/
  h = ic_test01 ( x_num, x, t[0] );
  bc_test01 ( x_num, x, t[0], h );

  hmat = ( double * ) malloc ( x_num * t_num * sizeof ( double ) );

  j = 0;
  for ( i = 0; i < x_num; i++ )
  {
    hmat[i+j*x_num] = h[i];
  }

  for ( j = 1; j < t_num; j++ )
  {
    h_new = fd1d_heat_explicit ( x_num, x, t[j-1], dt, cfl, rhs_test01, bc_test01, h );

    for ( i = 0; i < x_num; i++ )
    {
      hmat[i+j*x_num] = h_new[i];
      h[i] = h_new[i];
    }
    free ( h_new );
  }
/*
  Write the data to files.
*/
  r8mat_write ( "h_test01.txt", x_num, t_num, hmat );
  r8vec_write ( "t_test01.txt", t_num, t );
  r8vec_write ( "x_test01.txt", x_num, x );

  free ( h );
  free ( hmat );
  free ( t );
  free ( x );

  return;
}