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.*;
//Cubic polynomial search method
class ce2L
{ 	 public double r;	
	 public ce2L(double x1,double x2) 
	 {     
        f2D ff=new f2D();
        r= cubic_search_opt(ff,x1,x2);
     }
     
public static double df(f_x f,double x,double h)
{ //numerical derivative
  double hh=1.0/h; 
  return (-f.func(x+2.0*h)+8.0*f.func(x+h)-8.0*f.func(x-h)+f.func(x-2.0*h))/12.0*hh;
}
	
public static double cubic_search_opt(f_x f,double x0,double x1)
{
int nmax=500;
double tolerance=1.0e-15;
double f0,f1,df0,df1,fxs,dfxs;
double h=0.00001;
double xs;
int k=0;
double X,F,G,H,a,b,c;
  f0=f.func(x0);
  f1=f.func(x1);
  df0=f.dfunc(x0,1,h);
  df1=f.dfunc(x1,1,h);
while(Math.abs(x0-x1)>tolerance && k<nmax)
{ k++;	  
  X=1.0/(x1-x0);
  F=X*(f1-f0);
  G=X*(df1-df0);
  H=X*(F-df0);
  a=X*(G-2.0*H);
  b=3.0*H-G;
  c=df0;
  xs=x0+(Math.sqrt(b*b-3.0*a*c)-b)/(3.0*a);
  fxs=f.func(xs);
  dfxs=f.dfunc(xs,1,h);
  if(dfxs > 0 ) {x1=xs;f1=fxs;df1=dfxs;}
  else          x0=xs;f0=fxs;df0=dfxs; 
}
if(k>=nmax)  JOptionPane.showMessageDialog(null,"Uyari maximum iterasyon sayisi aşildi \n"+
   " çözüm geçerli olmiyabilir","MAKSİMUM ITERASYON SAYISI UYARISI",JOptionPane.WARNING_MESSAGE);
return (x0+x1)/2.0;
}
     public static double[] inputdata(String s)
     {
     String s1=JOptionPane.showInputDialog(s);        
     StringTokenizer token=new StringTokenizer(s1);
     int n=token.countTokens()-1;
     int m=n+1;
     double a[]=new double[m];
     int j=0;           
     while(token.hasMoreTokens())
     {
     Double ax=new Double(token.nextToken());
     a[j++]=ax.doubleValue();
     }
     return a; 
     }

}
