double bisect2 ( double a, double b, double f ( double x ) ) // // BISECT2 is a revised version of BISECT1 which allows us to pass // the name of the function as an extra input. // // This means the actual name of the function does not have to be "F". // // Because we call FABS() the program needs the "#include " statement. // { double c; // // Make sure [A,B] is a change of sign interval. // if ( 0 < f ( a ) * f ( b ) ) { cerr << "\n"; cerr << "BISECT2 - Fatal error!\n"; cerr << " F(A) = " << f(a) << "\n"; cerr << " F(B) = " << f(b) << "\n"; cerr << " [" << a << ", " << b << "] is not a change of sign interval.\n"; } while ( true ) { // // C = midpoint. // c = ( a + b ) / 2.0; // // Is F(C) extremely close to 0? // if ( fabs ( f ( c ) ) <= 0.000001 ) { break; } // // If F(C) is opposite in sign to F(A), C replaces B. // if ( f ( c ) * f ( a ) < 0.0 ) { b = c; } else { a = c; } } return c; }