How to use bisector stability?

Yesterday I had problems with other enhancement features, but fortunately you guys helped me solve them. Today I will need to know how to use the bisection function correctly.

So, this is how I think it should work, but nonetheless it seems like I'm wrong too. Ok, so I would like to use:

template <class F, class T, class Tol>
 std::pair<T, T> 
 bisect(
    F f, 
    T min, 
    T max, 
    Tol tol);

from here , but my problem is with tolerance because I don’t know how to install it correctly. I tried

double value = boost::math::tools::eps_tolerance<double>(0.00001);

and how to return the value when detecting division in half? If the result is a pair of numbers like std :: pair in a function, and then just calculate min + max / 2?

Thank!

+5
source share
1 answer

bisect. x^2 - 3x + 1 = 0:

struct TerminationCondition  {
  bool operator() (double min, double max)  {
    return abs(min - max) <= 0.000001;
  }
};

struct FunctionToApproximate  {
  double operator() (double x)  {
    return x*x - 3*x + 1;  // Replace with your function
  }
};

// ...
using boost::math::tools::bisect;
double from = 0;  // The solution must lie in the interval [from, to], additionally f(from) <= 0 && f(to) >= 0
double to = 1;
std::pair<double, double> result = bisect(FunctionToApproximate(), from, to, TerminationCondition());
double root = (result.first + result.second) / 2;  // = 0.381966...

EDIT: :

double myF(double x)  {
  return x*x*x;
}

double otherF(double x)  {
  return log(abs(x));
}

// ...
std::pair<double, double> result1 = bisect(&myF, from, to, TerminationCondition());
std::pair<double, double> result2 = bisect(&otherF, 0.1, 1.1, TerminationCondition());
+7

All Articles