// Bisect5.cc // Recursive function // Example: Find the point of zero by bisection // Version 5: func(x) and epsilon as parameters // more robust wrt. intervall bounds // choose from a collection of functions // #include #include double f(const double x) // declaration and { return sin(x) - 0.5*x ; } // definition of function f(x) double g(const double x) // declaration and { return -(x-1.234567)*(x+0.987654) ; }// definition of function g(x) double h(const double x) // declaration and { return 3.0 - exp(x) ; } // definition of function h(x) double t(const double x) // declaration and { return 1-x*x ; } // definition of function t(x) // declaration of Bisect3 double Bisect3(double (*func)(double), const double a, const double b, const double eps); main() { const double EPS = 1e-6; // accuracy constant double a,b,x0,fa,fb; char choice; double (*ff)(double); cout << endl; cout << " Determine point of zero in [a,b] by bisection " << endl; cout << " f(x) := sin(x) - x/2" << endl; cout << " g(x) := (1.234567-x)*(x+0.987654)" << endl; cout << " h(x) := 3.0 - exp(x)" << endl; cout << " t(x) := 1-x*x" << endl; cout << endl << "Which function do you prefer ? "; cin >> choice; switch (choice) { case 'f': ff = f; break; case 'g': ff = g; break; case 't': ff = t; break; default: choice = 'h'; cout << " no correct choice. h(x) is used." << endl; case 'h': ff = h; break; } cout << " " << choice << "(a) > 0, a : "; cin >> a; cout << " " << choice << "(b) < 0, b : "; cin >> b; cout << endl; fa = ff(a); fb = ff(b); if ( fabs(fa) 0 { cout << "I have to swap a and b" << endl; x0 = Bisect3(ff,b,a,EPS); // call recursive function } else // f(a) > 0 && f(b) < 0 { x0 = Bisect3(ff,a,b,EPS); // call recursive function } cout << endl << " point of zero = " << x0 << endl; } cout << endl; } // --------------------------------------------------------------- // Recursive function Bisect3 // --------------------------------------------------------------- // definition of Bisect3 double Bisect3(double (*func)(double), const double a, const double b, const double eps) { double x0, fc, c = (a+b)/2; fc = func(c); if ( fabs(fc) < eps ) { x0 = c; // end of recursion } else if ( fc > 0.0 ) { x0 = Bisect3(func,c,b,eps); // search in right intervall } else // i.e., fc < 0.0 { x0 = Bisect3(func,a,c,eps); // search in left intervall } return x0; // return with solution }