Answer

/* Computes an approximate root {\tt x} of the function {\tt f} using the
   bisection method.  We assume that f(neg) is negative and that f(pos) is
   positive.  Guarantees that a true root will be somewhere in the interval
   [x-.5e-5 ... x+.5e-5]. */

float bisection (float neg, float pos) {

  ...

  while (fabs(pos-neg) > 1e-5) {
    ave = (pos + neg) / 2;
    if (f(ave) < 0) {
      neg = ave;
    }
    else {
      pos = ave;
    }    
  }

  ...

}

Return to lesson.



Joseph L. Zachary
Hamlet Project
Department of Computer Science
University of Utah