# include # include # include # include # include # include # include using namespace std; int main ( ); void timestamp ( ); //****************************************************************************80 int main ( ) //****************************************************************************80 // // Purpose: // // least_squares_plots() compares data, a formula, and a least squares approximation. // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 29 September 2026 // // Author: // // John Burkardt // { string command_filename = "least_squares_commands.txt"; ofstream command; string data1_filename = "least_squares1_data.txt"; string data2_filename = "least_squares2_data.txt"; string data3_filename = "least_squares3_data.txt"; string plot_filename = "least_squares_plots.png"; timestamp ( ); cout << "\n"; cout << "least_squares_plots():\n"; cout << " C++ version\n"; cout << " On a single picture, plot:\n"; cout << " * 15 data points (X,Y).\n"; cout << " * the least squares line Y=A*X+B.\n"; cout << " * the exact formula Y=1/2*X^2 + 1.\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 title 'Formula (green), Data (blue), least squares fit (red)'\n"; command << "set grid\n"; command << "set timestamp\n"; command << "set key on\n"; command << "plot '" << data1_filename << "' using 1:2 with points lw 3 lt rgb 'blue' title 'Data', \\\n"; command << " '" << data2_filename << "' using 1:2 with lines lw 3 lt rgb 'red' title 'Least Squares Line', \\\n"; command << " '" << data3_filename << "' using 1:2 with lines lw 3 lt rgb 'green' title 'Exact Formula'\n"; command << "quit\n"; command.close ( ); // // Terminate. // cout << "\n"; cout << "least_squares_plots():\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: // // 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 }