Bisection
bisect.cpp
Go to the documentation of this file.
1 #include "bisect.h"
2 #include <cassert>
3 #include <cmath> // abs()
4 #include <functional> // function; C++11
5 
6 double Bisect(const std::function<double(double)>& func,
7  const double a, const double b, const double eps)
8 {
9  assert(func(a)>0 && 0>func(b));
10  double c = (a + b) / 2, fc = func(c), x0;
11 
12  if ( std::abs(fc) < eps ) {
13  x0 = c; // end of recursion
14  }
15  else if ( fc > 0.0 ) {
16  x0 = Bisect(func, c, b, eps); // search in right intervall
17  }
18  else { // i.e., fc < 0.0
19  x0 = Bisect(func, a, c, eps); // search in left intervall
20  }
21 
22  return x0; // return with solution
23 }
double Bisect(const std::function< double(double)> &func, const double a, const double b, const double eps)
Returns one solution for the equation with .
Definition: bisect.cpp:6
b
Definition: bisect.m:4
a
Definition: bisect.m:3