import java.io.*;
import javax.swing.*;
import javax.tools.*;
import java.util.*;
import java.lang.Integer;
import java.awt.*;
import java.awt.event.*;
import java.awt.font.*;
import java.awt.geom.*;
import java.awt.image.*;
import javax.swing.*;
import java.util.Locale;
import java.text.*;
import java.util.*;
import javax.swing.table.*;
package Plot;
class ce2E 
{ 	 public double r;	
	 public ce2E(double x1,double x2) 
	 {     
        f2D ff=new f2D();
        r= ridder(ff,x1,x2);
     }
public static double SIGN(double a,double b)
{
return Math.abs(a)*b/Math.abs(b);
}
public static double ridder(f_x f, double x1, double x2)
{double xacc=1.0e-9;
 return ridder(f, x1,x2,xacc);
}

public static double ridder(f_x f, double x1, double x2, double xacc)
{  
	int NR;
	int MAXITER=200;
	double ABC=-1.0e30;
	int j;
	double answer,f_high,f_low,f_mid,fnew,s,x_high,x_low,x_mid,xnew;
	f_low=f.func(x1);
	f_high=f.func(x2);
	if ((f_low > 0.0 && f_high < 0.0) || (f_low < 0.0 && f_high > 0.0)) {
		x_low=x1;
		x_high=x2;
		answer=ABC;
		for (j=0;j<MAXITER;j++) {
			x_mid=0.5*(x_low+x_high);
			f_mid=f.func(x_mid);
			s=Math.sqrt(f_mid*f_mid-f_low*f_high);
			if (s == 0.0) return answer;
			xnew=x_mid+(x_mid-x_low)*((f_low >= f_high ? 1.0 : -1.0)*f_mid/s);
			if (Math.abs(xnew-answer) <= xacc) return answer;
			answer=xnew;
			fnew=f.func(answer);
			if (fnew == 0.0) return answer;
			if (SIGN(f_mid,fnew) != f_mid) {
				x_low=x_mid;
				f_low=f_mid;
				x_high=answer;
				f_high=fnew;
			} else if (SIGN(f_low,fnew) != f_low) {
				x_high=answer;
				f_high=fnew;
			} else if (SIGN(f_high,fnew) != f_high) {
				x_low=answer;
				f_low=fnew;
			} else System.out.println("we should not reach to this point");
			if (Math.abs(x_high-x_low) <= xacc) return answer;
		}
		JOptionPane.showMessageDialog(null,"Warning : Maximum number of iteration is exceeded \n"+
        " result may not be valid","MAXÝMUM ITERATION NUMBER WARNING",JOptionPane.WARNING_MESSAGE);
	}
	else {
		if (f_low == 0.0) return x1;
		if (f_high == 0.0) return x2;
		JOptionPane.showMessageDialog(null,"Warning : No roots existed between the given limits ","NO ROOTS WARNING",JOptionPane.WARNING_MESSAGE);
	}
	return 0.0;
}
}
