# include # include # include # include # include # include # include using namespace std; # include "runge.hpp" int main ( ); void runge_antideriv_test ( ); void runge_deriv_test ( ); void runge_deriv2_test ( ); void runge_fun_test ( ); void runge_power_series_test ( ); void gnuplot_fx ( int n, double *x, double *y, string header ); void gnuplot_fxdd ( int n, double *x, double *y1, double *y2, string header ); double *r8vec_linspace_new ( int n, double a, double b ); void timestamp ( ); //****************************************************************************80 int main ( ) //****************************************************************************80 // // Purpose: // // runge_test() tests runge(). // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 25 May 2024 // // Author: // // John Burkardt // { timestamp ( ); cout << "\n"; cout << "runge_test():\n"; cout << " C++ version\n"; cout << " Test runge.\n"; runge_antideriv_test ( ); runge_deriv_test ( ); runge_deriv2_test ( ); runge_fun_test ( ); runge_power_series_test ( ); // // Terminate. // cout << "\n"; cout << "runge_test():\n"; cout << " Normal end of execution.\n"; cout << "\n"; timestamp ( ); return 0; } //****************************************************************************80 void runge_antideriv_test ( ) //****************************************************************************80 // // Purpose: // // runge_antideriv_test() tests runge_antideriv(). // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 24 May 2024 // // Author: // // John Burkardt // { double a; double b; string header; int i; int n = 51; double *x; double *y; cout << "\n"; cout << "runge_antideriv_test():\n"; cout << " runge_antideriv() evaluates the antiderivative\n"; cout << " of the runge function.\n"; a = -1.0; b = +1.0; x = r8vec_linspace_new ( n, a, b ); y = new double[n]; for ( i = 0; i < n; i++ ) { y[i] = runge_antideriv ( x[i] ); } header = "runge_antideriv"; gnuplot_fx ( n, x, y, header ); delete [] x; delete [] y; return; } //****************************************************************************80 void runge_deriv_test ( ) //****************************************************************************80 // // Purpose: // // runge_deriv_test() tests runge_deriv(). // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 24 May 2024 // // Author: // // John Burkardt // { double a; double b; string header; int i; int n = 51; double *x; double *y; cout << "\n"; cout << "runge_deriv_test():\n"; cout << " runge_deriv() evaluates the derivative\n"; cout << " of the runge function.\n"; a = -1.0; b = +1.0; x = r8vec_linspace_new ( n, a, b ); y = new double[n]; for ( i = 0; i < n; i++ ) { y[i] = runge_deriv ( x[i] ); } header = "runge_deriv"; gnuplot_fx ( n, x, y, header ); delete [] x; delete [] y; return; } //****************************************************************************80 void runge_deriv2_test ( ) //****************************************************************************80 // // Purpose: // // runge_deriv2_test() tests runge_deriv2(). // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 24 May 2024 // // Author: // // John Burkardt // { double a; double b; string header; int i; int n = 51; double *x; double *y; cout << "\n"; cout << "runge_deriv2_test():\n"; cout << " runge_deriv2() evaluates the second derivative\n"; cout << " of the runge function.\n"; a = -1.0; b = +1.0; x = r8vec_linspace_new ( n, a, b ); y = new double[n]; for ( i = 0; i < n; i++ ) { y[i] = runge_deriv2 ( x[i] ); } header = "runge_deriv2"; gnuplot_fx ( n, x, y, header ); delete [] x; delete [] y; return; } //****************************************************************************80 void runge_fun_test ( ) //****************************************************************************80 // // Purpose: // // runge_fun_test() tests runge_fun(). // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 24 May 2024 // // Author: // // John Burkardt // { double a; double b; string header; int i; int n = 51; double *x; double *y; cout << "\n"; cout << "runge_fun_test():\n"; cout << " runge_fun() evaluates the runge function.\n"; a = -1.0; b = +1.0; x = r8vec_linspace_new ( n, a, b ); y = new double[n]; for ( i = 0; i < n; i++ ) { y[i] = runge_fun ( x[i] ); } header = "runge_fun"; gnuplot_fx ( n, x, y, header ); delete [] x; delete [] y; return; } //****************************************************************************80 void runge_power_series_test ( ) //****************************************************************************80 // // Purpose: // // runge_power_series_test() tests runge_power_series(). // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 25 May 2024 // // Author: // // John Burkardt // { double a; double b; string header; int i; int n = 51; int n2 = 8; double *x; double *y1; double *y2; cout << "\n"; cout << "runge_power_series_test():\n"; cout << " runge_power_series() compares a power series to\n"; cout << " the runge function.\n"; a = -0.2; b = +0.2; x = r8vec_linspace_new ( n, a, b ); y1 = new double[n]; y2 = new double[n]; for ( i = 0; i < n; i++ ) { y1[i] = runge_fun ( x[i] ); y2[i] = runge_power_series ( x[i], n2 ); } header = "runge_power_series"; gnuplot_fxdd ( n, x, y1, y2, header ); delete [] x; delete [] y1; delete [] y2; return; } //****************************************************************************80 void gnuplot_fx ( int n, double *x, double *y, string header ) //****************************************************************************80 // // Purpose: // // gnuplot_fx() plots functional data x, f(x). // // Discussion: // // Actually, we simply create two files for processing by gnuplot(). // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 24 May 2024 // // Author: // // John Burkardt // { string command_filename; ofstream command_unit; string data_filename; ofstream data_unit; int i; string png_filename; // // Create a graphics data file. // data_filename = header + "_data.txt"; data_unit.open ( data_filename.c_str ( ) ); for ( i = 0; i < n; i++ ) { data_unit << x[i] << " " << y[i] << "\n"; } data_unit.close ( ); cout << " Created graphics data file '" << data_filename << "'\n"; png_filename = header + ".png"; // // Create graphics command file. // command_filename = header + "_commands.txt"; command_unit.open ( command_filename.c_str ( ) ); command_unit << "# " << command_filename << "\n"; command_unit << "#\n"; command_unit << "# Usage:\n"; command_unit << "# gnuplot < " << command_filename << "\n"; command_unit << "#\n"; command_unit << "set term png\n"; command_unit << "set output '" << png_filename << "'\n"; command_unit << "set xlabel '<--- X --->'\n"; command_unit << "set ylabel '<-- Y(X) -->'\n"; command_unit << "set title '" << header << "'\n"; command_unit << "set grid\n"; command_unit << "set style data lines\n"; command_unit << "plot '" << data_filename << "' using 1:2 lw 3 linecolor rgb 'blue'\n"; command_unit.close ( ); cout << " Created command file '" << command_filename << "'\n"; return; } //****************************************************************************80 void gnuplot_fxdd ( int n, double *x, double *y1, double *y2, string header ) //****************************************************************************80 // // Purpose: // // gnuplot_fxdd() plots n values of y1=f(x) (curve) and y2 = x2 (data) // // Discussion: // // Actually, we simply create two files for processing by gnuplot(). // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 24 May 2024 // // Author: // // John Burkardt // { string command_filename; ofstream command_unit; string data_filename; ofstream data_unit; int i; string png_filename; // // Create a graphics data file. // data_filename = header + "_data.txt"; data_unit.open ( data_filename.c_str ( ) ); for ( i = 0; i < n; i++ ) { data_unit << x[i] << " " << y1[i] << " " << y2[i] << "\n"; } data_unit.close ( ); cout << " Created graphics data file '" << data_filename << "'\n"; png_filename = header + ".png"; // // Create graphics command file. // command_filename = header + "_commands.txt"; command_unit.open ( command_filename.c_str ( ) ); command_unit << "# " << command_filename << "\n"; command_unit << "#\n"; command_unit << "# Usage:\n"; command_unit << "# gnuplot < " << command_filename << "\n"; command_unit << "#\n"; command_unit << "set term png\n"; command_unit << "set output '" << png_filename << "'\n"; command_unit << "set xlabel '<--- X --->'\n"; command_unit << "set ylabel '<-- Y(X) -->'\n"; command_unit << "set title '" << header << "'\n"; command_unit << "set grid\n"; command_unit << "set style data lines\n"; command_unit << "plot '" << data_filename << "' using 1:2 lw 3 linecolor rgb 'blue',\\\n"; command_unit << " '" << data_filename << "' using 1:3 with points pointtype 7 linecolor 'red'\n"; command_unit.close ( ); cout << " Created command file '" << command_filename << "'\n"; return; } //****************************************************************************80 double *r8vec_linspace_new ( int n, double a_first, double a_last ) //****************************************************************************80 // // Purpose: // // r8vec_linspace_new() creates a vector of linearly spaced values. // // Discussion: // // An R8VEC is a vector of R8's. // // 4 points evenly spaced between 0 and 12 will yield 0, 4, 8, 12. // // In other words, the interval is divided into N-1 even subintervals, // and the endpoints of intervals are used as the points. // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 29 March 2011 // // Author: // // John Burkardt // // Input: // // int N, the number of entries in the vector. // // double A_FIRST, A_LAST, the first and last entries. // // Output: // // double R8VEC_LINSPACE_NEW[N], a vector of linearly spaced data. // { double *a; int i; a = new double[n]; if ( n == 1 ) { a[0] = ( a_first + a_last ) / 2.0; } else { for ( i = 0; i < n; i++ ) { a[i] = ( ( double ) ( n - 1 - i ) * a_first + ( double ) ( i ) * a_last ) / ( double ) ( n - 1 ); } } return a; } //****************************************************************************80 void timestamp ( ) //****************************************************************************80 // // Purpose: // // timestamp() prints the current YMDHMS date as a time stamp. // // Example: // // 31 May 2001 09:45:54 AM // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 19 March 2018 // // Author: // // John Burkardt // { # define TIME_SIZE 40 static char time_buffer[TIME_SIZE]; const struct std::tm *tm_ptr; std::time_t now; now = std::time ( NULL ); tm_ptr = std::localtime ( &now ); std::strftime ( time_buffer, TIME_SIZE, "%d %B %Y %I:%M:%S %p", tm_ptr ); std::cout << time_buffer << "\n"; return; # undef TIME_SIZE }