/* Find cube root of 9 using fixed point iteration with Newton method
 */
public class CubeRoot {

  public static void main(String[] args) {
    
    int n= 9;            //Number to find cube root of
    double x= 3;         //First guess of n's cube root
    double xnew;         //Next guess of cube root by Newton method
    double err= 1;       //Relative difference between x and xnew;
                         //  assumed a value to start loop
    double tol= 0.0001;  //Error tolerance
    
    System.out.println("Number to find cube root of:  " + n);
    System.out.println("First guess of the cube root:  " + x);
    while (err > tol) {
      xnew= x - (Math.pow(x,3) - n)/(3*x*x);
      err= Math.abs((xnew-x)/x);
      x= xnew;
      System.out.println(x);
    }
    System.out.println("The cube root of " + n + " is " + x + "!");    
  } //end method main
} //end class CubeRoot