/*
 * Bisection algoritme:
 *
 *   First check that f(a) and f(b) has opposite signs.
 *
 *   while (fabs(a-b)>xtol and fabs(f(x))>ytol)
 *    {
 *     1) Let x=(a+b)/2
 *     2) Calculate f(x)
 *     3) if (f(x) == 0) a=b=x
 *     4) if ( f(x) same sign as f(a)) then a=x
 *                                 else     b=x
 *    }
 *
 *   Trick: if f(x) and f(a) has the same sign, what does that mean
 *   for   the sign of f(x)*f(x)?
 *
 *   Improvement: Add logic to ensure bisection maximally runs maxiter times.
 *
 *  Plot function to get an idea of where the root might be [0:2]
 *  Nyhoff 178
 */

#include <cmath>
using namespace std;

double func(double x)
{
   return x*x*x+x-5;
}

double Bisection(double a, double b, double (*f)(double), int maxiter=100, double SMALL=1e-5 )
//
// Here I give maxiter and SMALL default argument, such that the
// user does not need to supply these if the defaults are ok.
//
{
  // write the bisection code here
}


int main()
{
   double a=1;
   double b=2;

   double root=Bisection(a,b, func);
   cout << "The root is at " << root << endl;

   return 0;
}

