# include # include # include # include # include # include # include using namespace std; int main ( ); void timestamp ( ); /******************************************************************************/ int main ( ) /******************************************************************************/ // // Purpose: // // grid_surface() draws a surface from a table Z(X,Y). // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 26 September 2026 // // Author: // // John Burkardt // { string command_filename = "grid_commands.txt"; ofstream command; string data_filename = "grid_data.txt"; ofstream data; string plot_filename = "grid_surface.png"; int i; int j; int m; int n; double r2; double x; double y; double z; timestamp ( ); cout << "\n"; cout << "grid_surface():\n"; cout << " C++ version\n"; cout << " Make a surface plot Z(X,Y) for a 41x41 table.\n"; // // Set number of points in plot. // m = 41; n = 41; // // Define the data. // data.open ( data_filename.c_str ( ) ); for ( i = 0; i < m; i++ ) { if ( 0 < i ) { data << "\n"; } x = ( ( m - i - 1 ) * ( -2.0 ) + ( i ) * ( 2.0 ) ) / ( m - 1 ); for ( j = 0; j < n; j++ ) { y = ( ( n - j - 1 ) * ( -2.0 ) + ( j ) * ( 2.0 ) ) / ( n - 1 ); r2 = x * x + y * y; z = exp ( - r2 ) * cos ( 0.25 * x ) * sin ( y ) * cos ( 2 * r2 ); data << " " << x << " " << y << " " << z << "\n"; } } data.close ( ); cout << "\n"; cout << " Data stored in '" << data_filename << "'.\n"; // // Create the command file. // command.open ( command_filename.c_str ( ) ); command << "# " << command_filename << "\n"; command << "#\n"; command << "# Usage:\n"; command << "# gnuplot < " << command_filename << "\n"; command << "#\n"; command << "set term png\n"; command << "set output '" << plot_filename << "'\n"; command << "set xlabel 'X'\n"; command << "set ylabel 'Y'\n"; command << "set zlabel 'Z'\n"; command << "set title 'Grid surface'\n"; command << "set view 77, 77\n"; command << "set xyplane -0.5\n"; command << "set hidden3d\n"; command << "set grid\n"; command << "set timestamp\n"; command << "set style data lines\n"; command << "splot '" << data_filename << "' with lines\n"; command << "quit\n"; command.close ( ); cout << " plot commands stored in '" << command_filename << "'.\n"; // // Terminate. // cout << "\n"; cout << "grid_surface():\n"; cout << " Normal end of execution.\n"; cout << "\n"; timestamp ( ); return 0; } //****************************************************************************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: // // 08 July 2009 // // 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 }