# include # include # include # include "zero_rc.h" int main ( ); void example_test ( double a, double b, double machep, double t, double f ( double x ), 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 ); /******************************************************************************/ int main ( ) /******************************************************************************/ /* Purpose: MAIN is the main program for ZERO_RC_TEST. Discussion: ZERO_RC_TEST tests the ZERO_RC library. Licensing: This code is distributed under the GNU LGPL license. Modified: 04 December 2016 Author: John Burkardt */ { double a; double b; double machep; double t; timestamp ( ); printf ( "\n" ); printf ( "ZERO_RC_TEST\n" ); printf ( " C version\n" ); printf ( " ZERO_RC seeks a root of a function F(X)\n" ); printf ( " in an interval [A,B], using reverse communication (RC).\n" ); machep = r8_epsilon ( ); t = machep; a = 1.0; b = 2.0; example_test ( a, b, machep, t, f_01, "f_01(x) = sin ( x ) - x / 2" ); a = 0.0; b = 1.0; example_test ( a, b, machep, t, f_02, "f_02(x) = 2 * x - exp ( - x )" ); a = -1.0; b = 0.5; example_test ( a, b, machep, t, f_03, "f_03(x) = x * exp ( - x )" ); a = 0.0001; b = 20.0; example_test ( a, b, machep, t, f_04, "f_04(x) = exp ( x ) - 1 / ( 100 * x * x )" ); a = -5.0; b = 2.0; example_test ( a, b, machep, t, f_05, "f_05(x) = (x+3) * (x-1) * (x-1)" ); /* Terminate. */ printf ( "\n" ); printf ( "ZERO_RC_TEST\n" ); printf ( " Normal end of execution.\n" ); printf ( "\n" ); timestamp ( ); return 0; } /******************************************************************************/ void example_test ( double a, double b, double machep, double t, double f ( double x ), char *title ) /******************************************************************************/ /* Purpose: EXAMPLE_TEST tests ZERO_RC on one test function. Licensing: This code is distributed under the GNU LGPL license. Modified: 04 December 2016 Author: John Burkardt Parameters: Input, double A, B, the two endpoints of the change of sign interval. Input, double MACHEP, an estimate for the relative machine precision. Input, double T, a positive error tolerance. Input, double F ( double x ), the name of a user-supplied function which evaluates the function whose zero is being sought. Input, char *TITLE, a title for the problem. */ { double arg; int status; double value; printf ( "\n" ); printf ( "%s\n", title ); printf ( "\n" ); printf ( " STATUS X F(X)\n" ); printf ( "\n" ); status = 0; for ( ; ; ) { zero_rc ( a, b, t, &arg, &status, value ); if ( status < 0 ) { printf ( "\n" ); printf ( " ZERO_RC returned an error flag!\n" ); break; } value = f ( arg ); printf ( " %8d %14e %14e\n", status, arg, value ); if ( status == 0 ) { break; } } return; } /******************************************************************************/ double f_01 ( double x ) /******************************************************************************/ /* Purpose: F_01 evaluates sin ( x ) - x / 2. Licensing: This code is distributed under the GNU LGPL license. Modified: 13 April 2008 Author: John Burkardt Parameters: Input, double X, the point at which F is to be evaluated. Output, double F_01, the value of the function at X. */ { double value; value = sin ( x ) - 0.5 * x; return value; } /******************************************************************************/ double f_02 ( double x ) /******************************************************************************/ /* Purpose: F_02 evaluates 2*x-exp(-x). Licensing: This code is distributed under the GNU LGPL license. Modified: 13 April 2008 Author: John Burkardt Parameters: Input, double X, the point at which F is to be evaluated. Output, double F_02, the value of the function at X. */ { double value; value = 2.0 * x - exp ( - x ); return value; } /******************************************************************************/ double f_03 ( double x ) /******************************************************************************/ /* Purpose: F_03 evaluates x*exp(-x). Licensing: This code is distributed under the GNU LGPL license. Modified: 13 April 2008 Author: John Burkardt Parameters: Input, double X, the point at which F is to be evaluated. Output, double F_03, the value of the function at X. */ { double value; value = x * exp ( - x ); return value; } /******************************************************************************/ double f_04 ( double x ) /******************************************************************************/ /* Purpose: F_04 evaluates exp(x) - 1 / (100*x*x). Licensing: This code is distributed under the GNU LGPL license. Modified: 13 April 2008 Author: John Burkardt Parameters: Input, double X, the point at which F is to be evaluated. Output, double F_04, the value of the function at X. */ { double value; value = exp ( x ) - 1.0 / 100.0 / x / x; return value; } /******************************************************************************/ double f_05 ( double x ) /******************************************************************************/ /* Purpose: F_05 evaluates (x+3)*(x-1)*(x-1). Licensing: This code is distributed under the GNU LGPL license. Modified: 13 April 2008 Author: John Burkardt Parameters: Input, double X, the point at which F is to be evaluated. 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; }