/*

LHS/RHS Method with nested loop for inc

*/

public class nested_lhs{

   public static void main(String[] args){
   
   
      // Declarations
      double phideg=30.;
      double phi=(Math.PI/180.)*phideg;     
      double P=30.;
      double L=3.;
      double gamma=18.;
      double Ka=Math.pow((Math.tan(Math.PI/4-phi/2)),2);
      double Kp=Math.pow((Math.tan(Math.PI/4+phi/2)),2);
      double nom=gamma*(Kp-Ka);      // nominator
      double TOL=0.001;
  
      // coefficients of Polynomial
      double C1=1;
      double C3=-8*P/nom;
      double C4=-12*P*L/nom;
      double C5=-Math.pow(2*P/nom,2);
      
      // check=0 : Root has not been found 
      // check=1 : Root has been found
      int check=0;
     
      double inc=0.0001;
	
      // decreasing increment until increment is small enough 
      // to find a root with LHS-method  
      while (inc>=.00001 && check==0){

	 double x=3.3;
	 int iter=0;
	 double f_x= C1* Math.pow(x,4) + C3*Math.pow(x,2) + C4*x + C5;
	    
	 // check stopping criterium and bound for x
	 while (Math.abs(f_x)>TOL && x<=4){
	    x=x+inc;
	    f_x= C1* Math.pow(x,4) + C3*Math.pow(x,2) + C4*x + C5;
	    iter=iter+1;
	 } 
	  
	 System.out.println("current Increment " + inc);
	 
	 // root has been found!
	 if (x<4){
	    System.out.println("The root, " + x + ", was found.");
	    System.out.println("Number of iterations: " + iter);
	    System.out.println(" ");
	    check=1;
         }

	 // decrease increment
	 inc -= 0.00001;

      } //while-loop

   }// main method

} // nested_lhs class
