# include # include # include # include # include using namespace std; # include "golden_section.hpp" int main ( ); double humps_fun ( double x ); double test_fun ( double x ); void timestamp ( ); //****************************************************************************80 int main ( ) //****************************************************************************80 // // Purpose: // // golden_section_test() tests golden_section(). // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 24 February 2026 // // Author: // // John Burkardt // { double a; double b; int it; int n; double x_tol; timestamp ( ); cout << "\n"; cout << "golden_section_test():\n"; cout << " C++ version\n"; cout << " golden_section() seeks a minimizer\n"; cout << " of a function f(x) in the interval [a,b],\n"; cout << " assuming f(x) is unimodal over [a,b],\n"; // // humps(x) = 1 / ((x-0.3)^2 + 0.01) + 1 / ((x-0.9)^2 + 0.04) - 6.0 // a = 0.3; b = 0.9; n = 25; x_tol = 0.000001; cout << "\n"; cout << " f = humps_fun(x)\n"; cout << " f(" << a << ") = " << humps_fun ( a ) << "\n"; cout << " f(" << b << ") = " << humps_fun ( b ) << "\n"; cout << " iteration limit n = " << n << "\n"; cout << " x_tol = " << x_tol << "\n"; golden_section ( humps_fun, n, x_tol, a, b, it ); cout << "\n"; cout << " Number of iterations: " << it << "\n"; cout << " f(" << a << ") = " << humps_fun ( a ) << "\n"; cout << " f(" << b << ") = " << humps_fun ( b ) << "\n"; // // test_fun(x)=x^4+10*x*sin(x^2) // a = -2.0; b = 1.0; n = 35; x_tol = 0.000001; cout << "\n"; cout << " f = test_fun(x)\n"; cout << " f(" << a << ") = " << test_fun ( a ) << "\n"; cout << " f(" << b << ") = " << test_fun ( b ) << "\n"; cout << " iteration limit n = " << n << "\n"; cout << " x_tol = " << x_tol << "\n"; golden_section ( test_fun, n, x_tol, a, b, it ); cout << "\n"; cout << " Number of iterations: " << it << "\n"; cout << " f(" << a << ") = " << test_fun ( a ) << "\n"; cout << " f(" << b << ") = " << test_fun ( b ) << "\n"; // // Terminate. // cout << "\n"; cout << "golden_section_test():\n"; cout << " Normal end of execution.\n"; timestamp ( ); return 0; } //****************************************************************************80 double humps_fun ( double x ) //****************************************************************************80 // // Purpose: // // humps_fun() evaluates a function used for demonstrations. // // Discussion: // // The "interesting" portion of the function is visible over the range // 0 <= x <= 2. // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 29 June 2019 // // Author: // // John Burkardt // // Input: // // double x: the evaluation point. // // Output: // // double humps_fun: the function value. // { double value; value = 1.0 / ( pow ( x - 0.3, 2 ) + 0.01 ) + 1.0 / ( pow ( x - 0.9, 2 ) + 0.04 ) - 6.0; return value; } //****************************************************************************80 double test_fun ( double x ) //****************************************************************************80 // // Purpose: // // test_fun() evaluates a function used for demonstrations. // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 23 February 2026 // // Author: // // John Burkardt // // Input: // // double x: the evaluation point. // // Output: // // double test_fun: the function value. // { double value; value = pow ( x, 4 ) + 10.0 * x * sin ( pow ( x, 2 ) ); return value; } //****************************************************************************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 }