# include # include # include # include # include # include using namespace std; # include "zero_chandrupatla.hpp" int main ( ); void zero_chandrupatla_example ( double f ( double x ), double a, double b, 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 ); double f_07 ( double x ); double f_08 ( double x ); double f_09 ( double x ); void timestamp ( ); ///***************************************************************************80 int main ( ) //****************************************************************************80 // // Purpose: // // zero_chandrupatla_test() tests zero_chandrupatla(). // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 19 March 2024 // // Author: // // John Burkardt // { double a; double b; string title; timestamp ( ); cout << "\n"; cout << "zero_chandrupatla_test():\n"; cout << " C++ version\n"; cout << " zero_chandrupatla() seeks a root of a function f(x)\n"; cout << " in an interval [a,b].\n"; a = 2.0; b = 3.0; title = "f_01(x) = x^3 - 2 x - 5"; zero_chandrupatla_example ( f_01, a, b, title ); a = 0.5; b = 1.51; title = "f_02(x) = 1 - 1/x^2"; zero_chandrupatla_example ( f_02, a, b, title ); a = 0.0; b = 5.0; title = "f_03(x) = ( x - 3 )^3"; zero_chandrupatla_example ( f_03, a, b, title ); a = 0.0; b = 5.0; title = "f_04(x) = 6 * ( x - 2 )^5"; zero_chandrupatla_example ( f_04, a, b, title ); a = -1.0; b = 4.0; title = "f_05(x) = x^9"; zero_chandrupatla_example ( f_05, a, b, title ); a = -1.0; b = 4.0; title = "f_06(x) = x^19"; zero_chandrupatla_example ( f_06, a, b, title ); a = -1.0; b = 4.0; title = "f_07(x) = x e^(-1/x2)"; zero_chandrupatla_example ( f_07, a, b, title ); a = 0.0002; b = 2.0; title = "f_08(x) = -(3062(1-xi)e^(-x)/(xi+(1-xi)e^(-x)) - 1013 + 1628/x"; zero_chandrupatla_example ( f_08, a, b, title ); a = 0.0002; b = 1.0; title = "f_09(x) = e^x - 2 - 0.01/x^2 + 0.000002/x^3"; zero_chandrupatla_example ( f_09, a, b, title ); // // Terminate. // cout << "\n"; cout << "zero_chandrupatla_test():\n"; cout << " Normal end of execution.\n"; cout << "\n"; timestamp ( ); return 0; } //****************************************************************************80 void zero_chandrupatla_example ( double f ( double x ), double a, double b, string title ) //****************************************************************************80 // // Purpose: // // zero_chandrupatla_example() tests zero_chandrupatla() on a test function. // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 19 March 2024 // // Author: // // John Burkardt // // Input: // // double f ( double x ): the user-supplied function. // // double a, b: the endpoints of the change of sign interval. // // string title: a title for the problem. // { double fa; double fb; double fz; int calls; double z; zero_chandrupatla ( f, a, b, z, fz, calls ); fz = f ( z ); 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"; return; } //****************************************************************************80 double f_01 ( double x ) //****************************************************************************80 // // Purpose: // // f_01() evaluates test function 1. // { double fx; fx = x * x * x - 2.0 * x - 5.0; return fx; } //****************************************************************************80 double f_02 ( double x ) //****************************************************************************80 // // Purpose: // // f_02() evaluates test function 2. // { double fx; fx = 1.0 - 1.0 / x / x; return fx; } //****************************************************************************80 double f_03 ( double x ) //****************************************************************************80 // // Purpose: // // f_03() evaluates test function 3. // { double fx; fx = pow ( x - 3.0, 3 ); return fx; } //****************************************************************************80 double f_04 ( double x ) //****************************************************************************80 // // Purpose: // // f_04() evaluates test function 4. // { double fx; fx = 6.0 * pow ( x - 2.0, 5 ); return fx; } //****************************************************************************80 double f_05 ( double x ) //****************************************************************************80 // // Purpose: // // f_05() evaluates test function 5. // { double fx; fx = pow ( x, 9 ); return fx; } //****************************************************************************80 double f_06 ( double x ) //****************************************************************************80 // // Purpose: // // f_06() evaluates test function 6. // { double fx; fx = pow ( x, 19 ); return fx; } //****************************************************************************80 double f_07 ( double x ) //****************************************************************************80 // // Purpose: // // f_07() evaluates test function 7. // { double fx; if ( fabs ( x ) < 3.8E-04 ) { fx = 0.0; } else { fx = x * exp ( - ( 1.0 / x / x ) ); } return fx; } //****************************************************************************80 double f_08 ( double x ) //****************************************************************************80 // // Purpose: // // f_08() evaluates test function 8. // { double bot; double fx; double top; double xi; xi = 0.61489; top = ( 3062.0 * ( 1.0 - xi ) * exp ( - x ) ); bot = ( xi + ( 1.0 - xi ) * exp ( - x ) ); fx = - top / bot - 1013.0 + 1628.0 / x; return fx; } //****************************************************************************80 double f_09 ( double x ) //****************************************************************************80 // // Purpose: // // f_09() evaluates test function 9. // { double fx; fx = exp ( x ) - 2.0 - 0.01 / x / x + 0.000002 / x / x / x; return fx; } //****************************************************************************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 }