import javax.swing.JOptionPane;
/*
 * Created on Jan 26, 2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */

/**
 * @author User
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class HW8_Q2 {
     public static void main(String[] args) 
    { 
        // Default values provided in the problem statement
        double s = 0.0002;
        double n = 0.03;
        double w = 20.0;
        double Q = 5.0;
        double eps = 1.0e-10;
        
        // Output parameters for bisection
        double dBisection     = 0.0;
        double vBisection     = 0.0;
        int numItersBisection = 0;
        double errBisection   = 0.0;
        
        // Output parameters for Newton-Raphson
        double dNewton     = 0.0;
        double vNewton     = 0.0;
        int numItersNewton = 0;
        double errNewton   = 0.0;
        
        try{
            s = Double.parseDouble(JOptionPane.showInputDialog("Enter the slope:"));
            n = Double.parseDouble(JOptionPane.showInputDialog("Enter the roughness:"));
            w = Double.parseDouble(JOptionPane.showInputDialog("Enter the width:"));
            Q = Double.parseDouble(JOptionPane.showInputDialog("Enter the flow rate:"));
            eps = Double.parseDouble(JOptionPane.showInputDialog("Enter the tolerance: "));
            
            if(s < 0.0 || n < 0.0 || w < 0.0 || Q < 0.0 || eps < 0.0)
                throw new NumberFormatException("Only positive values for the input are allowed.");
            
            FlowFunction mFlowFunction = new FlowFunction(s,n,w,Q);
            
            dBisection        = RootFinder.rtbis(mFlowFunction, 0.0, w, eps);
            numItersBisection = RootFinder.j;
            errBisection      = mFlowFunction.f(dBisection);
            vBisection        = mFlowFunction.velocity(dBisection);
            
            
            dNewton           = Newton.newt(mFlowFunction, 0.0, w, eps);
            numItersNewton    = Newton.j;
            errNewton         = mFlowFunction.f(dNewton);
            vNewton           = mFlowFunction.velocity(dNewton);      
                      
            // Ouput the results to the command line
            System.out.println("\n\n");
            System.out.println("Solver    : Bisection");
            System.out.println("Depth     : "+dBisection);
            System.out.println("Velocity  : "+vBisection);
            System.out.println("Iterations: "+numItersBisection);
            System.out.println("Error     : "+errBisection);
            System.out.println("\n");
            System.out.println("Solver    : Newton");
            System.out.println("Depth     : "+dNewton);
            System.out.println("Velocity  : "+vNewton);
            System.out.println("Iterations: "+numItersNewton);
            System.out.println("Error     : "+errNewton);
            
        }catch(NumberFormatException ex1){
            JOptionPane.showMessageDialog(null,"Invalid input:"+ex1.getMessage(),"Error",JOptionPane.ERROR_MESSAGE);
        }
      
        System.exit(0);
  
    }
}
