// bisection algorithm and f(x) // hussein suleman // 29 march 2004 public class Bisection { static final double Epsilon = 0.000000001; // f(x) = x^7 + 6x^6 - 3x^5 + 4x^2 - x - 6 public double f ( double x ) { return ( Math.pow (x, 7) + 6*Math.pow (x, 6) - 3*Math.pow (x, 5) + 4*Math.pow (x, 2) - x - 6 ); } // find the root using a bisection algorithm public double findRoot ( double left, double right ) { // calculate the midpoint double middle = (left+right) / 2; // iterate while the value is not close to 0 while (Math.abs (f(middle)) > Epsilon) { System.out.println ("left = "+left+" right = "+right); // halve interval if (f(left)*f(middle) < 0) right = middle; else left = middle; // calculate new midpoint middle = (left+right) / 2; } return middle; } }