# include # include # include # include # include # include # include using namespace std; # include "zero_itp.hpp" int main ( ); void zero_itp_example ( double f ( double x ), double a, double b, double epsi, double k1, double k2, int n0, bool verbose, string title ); double f_01 ( double x ); double f_02 ( double x ); double f_03 ( double x ); double f_04 ( double x ); double f_05 ( double x ); double f_06 ( double x ); void timestamp ( ); //****************************************************************************80 int main ( ) //****************************************************************************80 // // Purpose: // // zero_itp_test() tests zero_itp(). // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 03 March 2024 // // Author: // // John Burkardt // { double a; double b; double epsi; double k1; double k2; int n0; string title; bool verbose; timestamp ( ); cout << "\n"; cout << "zero_itp_test():\n"; cout << " C++ version\n"; cout << " Test zero_itp(), which seeks a root of a function F(X)\n"; cout << " in an interval [A,B].\n"; a = 1.0; b = 2.0; epsi = sqrt ( DBL_EPSILON ); k1 = 1.0 / ( b - a ) / 5.0; k2 = 2.0; n0 = 1; title = "f_01(x) = sin ( x ) - x / 2"; verbose = false; zero_itp_example ( f_01, a, b, epsi, k1, k2, n0, verbose, title ); a = 0.0; b = 1.0; epsi = sqrt ( DBL_EPSILON ); k1 = 1.0 / ( b - a ) / 5.0; k2 = 2.0; n0 = 1; title = "f_02(x) = 2 * x - exp ( - x )"; verbose = false; zero_itp_example ( f_02, a, b, epsi, k1, k2, n0, verbose, title ); a = -1.0; b = 0.5; epsi = sqrt ( DBL_EPSILON ); k1 = 1.0 / ( b - a ) / 5.0; k2 = 2.0; n0 = 1; title = "f_03(x) = x * exp ( - x )"; verbose = false; zero_itp_example ( f_03, a, b, epsi, k1, k2, n0, verbose, title ); a = 0.0001; b = 20.0; epsi = sqrt ( DBL_EPSILON ); k1 = 1.0 / ( b - a ) / 5.0; k2 = 2.0; n0 = 1; title = "f_04(x) = exp ( x ) - 1 / ( 100 * x * x )"; verbose = false; zero_itp_example ( f_04, a, b, epsi, k1, k2, n0, verbose, title ); a = -5.0; b = 2.0; epsi = sqrt ( DBL_EPSILON ); k1 = 1.0 / ( b - a ) / 5.0; k2 = 2.0; n0 = 1; title = "f_05(x) = (x+3) * (x-1) * (x-1)"; verbose = false; zero_itp_example ( f_05, a, b, epsi, k1, k2, n0, verbose, title ); a = 1.0; b = 2.0; epsi = 0.0005; k1 = 1.0 / ( b - a ) / 5.0; k2 = 2.0; n0 = 1; title = "f_06(x) = x^3 - x - 2"; verbose = true; zero_itp_example ( f_06, a, b, epsi, k1, k2, n0, verbose, title ); // // Terminate. // cout << "\n"; cout << "zero_itp_test():\n"; cout << " Normal end of execution.\n"; cout << "\n"; timestamp ( ); return 0; } //****************************************************************************80 void zero_itp_example ( double f ( double x ), double a, double b, double epsi, double k1, double k2, int n0, bool verbose, string title ) //****************************************************************************80 // // Purpose: // // zero_itp_example() tests zero_itp() on one test function. // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 03 March 2024 // // Author: // // John Burkardt // // Input: // // double F ( double x ), the name of a user-supplied // function which evaluates the function whose zero is being sought. // // double A, B, the endpoints of the change of sign interval. // // string TITLE, a title for the problem. // // bool verbose: true if extra output is desired from zero_itp(); // { int calls; double fa; double fb; double fz; double z; zero_itp ( f, a, b, epsi, k1, k2, n0, verbose, z, fz, calls ); fa = f ( a ); fb = f ( b ); cout << "\n"; cout << " " << title << "\n"; cout << "\n"; cout << " A Z B\n"; cout << " F(A) F(Z) F(B)\n"; cout << "\n"; cout << " " << setw(14) << a << " " << setw(14) << z << " " << setw(14) << b << "\n"; cout << " " << setw(14) << fa << " " << setw(14) << fz << " " << setw(14) << fb << "\n"; cout << " Number of calls to F = " << calls << "\n"; cout << " Tolerance epsi = " << epsi << "\n"; cout << " Parameters k1 = " << k1 << ", k2 = " << k2 << ", n0 = " << n0 << "\n"; return; } //****************************************************************************80 double f_01 ( double x ) //****************************************************************************80 // // Purpose: // // f_01() evaluates sin ( x ) - x / 2. // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 13 April 2008 // // Author: // // John Burkardt // // Input: // // double X, the evaluation point. // // Output: // // double F_01, the value of the function at X. // { double value; value = sin ( x ) - 0.5 * x; return value; } //****************************************************************************80 double f_02 ( double x ) //****************************************************************************80 // // Purpose: // // f_02() evaluates 2*x-exp(-x). // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 13 April 2008 // // Author: // // John Burkardt // // Input: // // double X, the evaluation point. // // Output: // // double F_02, the value of the function at X. // { double value; value = 2.0 * x - exp ( - x ); return value; } //****************************************************************************80 double f_03 ( double x ) //****************************************************************************80 // // Purpose: // // f_03() evaluates x*exp(-x). // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 13 April 2008 // // Author: // // John Burkardt // // Input: // // double X, the evaluation point. // // Output: // // double F_03, the value of the function at X. // { double value; value = x * exp ( - x ); return value; } //****************************************************************************80 double f_04 ( double x ) //****************************************************************************80 // // Purpose: // // f_04() evaluates exp(x) - 1 / (100*x*x). // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 13 April 2008 // // Author: // // John Burkardt // // Input: // // double X, the evaluation point. // // Output: // // double F_04, the value of the function at X. // { double value; value = exp ( x ) - 1.0 / 100.0 / x / x; return value; } //****************************************************************************80 double f_05 ( double x ) //****************************************************************************80 // // Purpose: // // f_05() evaluates (x+3)*(x-1)*(x-1). // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 13 April 2008 // // Author: // // John Burkardt // // Input: // // double X, the evaluation point. // // Output: // // double F_05, the value of the function at X. // { double value; value = ( x + 3.0 ) * ( x - 1.0 ) * ( x - 1.0 ); return value; } //****************************************************************************80 double f_06 ( double x ) //****************************************************************************80 // // Purpose: // // f_06() evaluates x^3 - x - 2. // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 02 March 2024 // // Author: // // John Burkardt // // Input: // // double X, the evaluation point. // // Output: // // double F_06, the value of the function at X. // { double value; value = x * x * x - 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 }