# include # include # include # include # include "golden_section.h" int main ( ); double humps_fun ( double x ); double test_fun ( double x ); void timestamp ( ); /******************************************************************************/ int main ( ) /******************************************************************************/ /* Purpose: golden_section_test() tests golden_section(). Licensing: This code is distributed under the MIT license. Modified: 23 February 2026 Author: John Burkardt */ { double a; double b; int it; int n; double x_tol; timestamp ( ); printf ( "\n" ); printf ( "golden_section_test():\n" ); printf ( " C version\n" ); printf ( " golden_section() seeks a minimizer\n" ); printf ( " of a function f(x) in the interval [a,b],\n" ); printf ( " 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; printf ( "\n" ); printf ( " f = humps_fun(x)\n" ); printf ( " f(%g) = %g\n", a, humps_fun ( a ) ); printf ( " f(%g) = %g\n", b, humps_fun ( b ) ); printf ( " iteration limit n = %d\n", n ); printf ( " x_tol = %g\n", x_tol ); golden_section ( humps_fun, n, x_tol, &a, &b, &it ); printf ( "\n" ); printf ( " Number of iterations: %d\n", it ); printf ( " f(%g) = %g\n", a, humps_fun ( a ) ); printf ( " f(%g) = %g\n", b, humps_fun ( b ) ); /* test_fun(x)=x^4+10*x*sin(x^2) */ a = -2.0; b = 1.0; n = 35; x_tol = 0.000001; printf ( "\n" ); printf ( " f = test_fun(x)\n" ); printf ( " f(%g) = %g\n", a, test_fun ( a ) ); printf ( " f(%g) = %g\n", b, test_fun ( b ) ); printf ( " iteration limit n = %d\n", n ); printf ( " x_tol = %g\n", x_tol ); golden_section ( test_fun, n, x_tol, &a, &b, &it ); printf ( "\n" ); printf ( " Number of iterations: %d\n", it ); printf ( " f(%g) = %g\n", a, test_fun ( a ) ); printf ( " f(%g) = %g\n", b, test_fun ( b ) ); /* Terminate. */ printf ( "\n" ); printf ( "golden_section_test():\n" ); printf ( " Normal end of execution.\n" ); timestamp ( ); return 0; } /******************************************************************************/ double humps_fun ( double x ) /******************************************************************************/ /* 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; } /******************************************************************************/ double test_fun ( double x ) /******************************************************************************/ /* 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; } /******************************************************************************/ void timestamp ( ) /******************************************************************************/ /* Purpose: timestamp() prints the current YMDHMS date as a time stamp. Example: 17 June 2014 09:45:54 AM Licensing: This code is distributed under the MIT license. Modified: 01 May 2021 Author: John Burkardt */ { # define TIME_SIZE 40 static char time_buffer[TIME_SIZE]; const struct tm *tm; time_t now; now = time ( NULL ); tm = localtime ( &now ); strftime ( time_buffer, TIME_SIZE, "%d %B %Y %I:%M:%S %p", tm ); printf ( "%s\n", time_buffer ); return; # undef TIME_SIZE }