# include # include # include using namespace std; // // We can declare BISECT1 and F1 here, and they will be usable by // any function within this file. // double bisect1 ( double a, double b ); double f1 ( double x ); int main ( ) // // BISECT1_F1 is a program which uses BISECT1 to solve for a solution // of F1(X) = 0. // // Note that we had to rename "F()" to "F1()" in BISECT1 in order for // this to work. // { // // A and B are the "bracket" values we've chosen for our function. // double a = -10.0, b = +10.0, c; c = bisect1 ( a, b ); cout << "\n"; cout << "BISECT1 returned solution estimate C = " << c << "\n"; cout << "F1(C) = " << f1 ( c ) << "\n"; return 0; } double bisect1 ( double a, double b ) // // BISECT1 is our simple bisection solver. // { double c; while ( true ) { // // C = midpoint. // c = ( a + b ) / 2.0; // // Is F(C) extremely close to 0? // if ( fabs ( f1 ( c ) ) <= 0.000001 ) { break; } // // If F(C) is opposite in sign to F(A), C replaces B. // if ( f1 ( c ) * f1 ( a ) < 0.0 ) { b = c; } else { a = c; } } return c; } double f1 ( double x ) // // F1 is our "cosine" function. // { double value; value = cos ( x ) - x; return value; }