# include # include # include # include # include "zero_chandrupatla.h" int main ( ); void zero_chandrupatla_example ( double f ( double x ), double a, double b, char *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 ( ); /******************************************************************************/ int main ( ) /******************************************************************************/ /* 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; char *title; timestamp ( ); printf ( "\n" ); printf ( "zero_chandrupatla_test():\n" ); printf ( " C version\n" ); printf ( " zero_chandrupatla() seeks a root of a function f(x)\n" ); printf ( " 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. */ printf ( "\n" ); printf ( "zero_chandrupatla_test():\n" ); printf ( " Normal end of execution.\n" ); printf ( "\n" ); timestamp ( ); return 0; } /******************************************************************************/ void zero_chandrupatla_example ( double f ( double x ), double a, double b, char *title ) /******************************************************************************/ /* 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. char *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 ); printf ( "\n" ); printf ( " %s\n", title ); printf ( "\n" ); printf ( " A Z B\n" ); printf ( " F(A) F(Z) F(B)\n" ); printf ( "\n" ); printf ( " %14f %14f %14f\n", a, z, b ); printf ( " %14e %14e %14e\n", fa, fz, fb ); printf ( " Number of calls to F = %d\n", calls ); return; } /******************************************************************************/ double f_01 ( double x ) /******************************************************************************/ /* Purpose: f_01() evaluates test function 1. */ { double fx; fx = x * x * x - 2.0 * x - 5.0; return fx; } /******************************************************************************/ double f_02 ( double x ) /******************************************************************************/ /* Purpose: f_02() evaluates test function 2. */ { double fx; fx = 1.0 - 1.0 / x / x; return fx; } /******************************************************************************/ double f_03 ( double x ) /******************************************************************************/ /* Purpose: f_03() evaluates test function 3. */ { double fx; fx = pow ( x - 3.0, 3 ); return fx; } /******************************************************************************/ double f_04 ( double x ) /******************************************************************************/ /* Purpose: f_04() evaluates test function 4. */ { double fx; fx = 6.0 * pow ( x - 2.0, 5 ); return fx; } /******************************************************************************/ double f_05 ( double x ) /******************************************************************************/ /* Purpose: f_05() evaluates test function 5. */ { double fx; fx = pow ( x, 9 ); return fx; } /******************************************************************************/ double f_06 ( double x ) /******************************************************************************/ /* Purpose: f_06() evaluates test function 6. */ { double fx; fx = pow ( x, 19 ); return fx; } /******************************************************************************/ double f_07 ( double x ) /******************************************************************************/ /* 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; } /******************************************************************************/ double f_08 ( double x ) /******************************************************************************/ /* 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; } /******************************************************************************/ double f_09 ( double x ) /******************************************************************************/ /* 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; } /******************************************************************************/ 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 }