Thiago R. Adams website

Home Blog Code-Blog Twitter Downloads Links / Books About

Websites

Bisection Method

The bisection method is a root-finding algorithm which repeatedly divides an interval in half and then selects the subinterval in which a root exists.


// Copyright (C) 2009, Thiago Adams (thiago.adams@gmail.com)
// Permission to copy, use, modify, sell and distribute this software
// is granted provided this copyright notice appears in all copies.
// This software is provided "as is" without express or implied
// warranty, and with no claim as to its suitability for any purpose.
//

inline double Sign(double x)
{
  return x > 0 ? 1.0 : -1.0;
}

template<class F>
double Bisection(const F& func,
                 double xmin, 
                 double xmax, 
                 int maxIterations = 10000,
                 double accuracy = DBL_MIN)
{
  double fxmin = func(xmin);
  double fmid = func(xmax);

  if (xmin >= xmax || Sign(fxmin) == Sign(fmid))
    throw std::runtime_error("invalid range");
  
  double dx;
  double x;

  if (fxmin < 0.0)
  {
    dx = xmax - xmin; 
    x = xmin; 
  }
  else 
  { 
    dx = xmin - xmax;
    x = xmax; 
  };
  
  for (int i = 0; i < maxIterations; ++i)
  {
    dx = dx * 0.5;
    double xmid = x + dx;
    fmid = func(xmid);
    if (fmid <= 0.0) 
    {
      x = xmid;
    }

    if (fabs(dx) < accuracy || fmid == 0.0)
    {
      return x;
    }
  }  
  throw std::runtime_error("number of iterations exceeded");
}

Want to see more? Go to the CodeBlog section.

About the author: I am Thiago Adams. I work as a professional C++ software engineer. I have created this website to share ideas and source code with other people with similar interests.
I would like to hear from you comments, critics, questions and suggestions about this topic or any other part of this website. Email: thiago.adams at gmail dot com