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.*;
//Adaptive Simpson 1/3 integral
//Integration
class ce2Q
{ 	 public double r;	
	 public ce2Q(double x1,double x2) 
	 {  f2D ff=new f2D();
        r= adaptive_simpson_integral2(ff,x1,x2);
     }
     
public static double adaptive_simpson_integral2(f_x ff,double a,double b)
{   int i;
	Plot pp=new Plot(ff,a,b,100);
    double h=(b-a);
    double h2=h/4.0;
    double h3=h/8.0;
    double m=(a+b)/2.0;
    double x[]=new double[9];
    double y[]=new double[9];
    for(i=0;i<9;i++) {x[i]=a+i*h3;y[i]=ff.func(x[i]);}
    double is=h/28350.0*(989.0*y[0]+5888.0*y[1]-928.0*y[2]+10496.0*y[3]-4540.0*y[4]+
           10496.0*y[5]-928.0*y[6]+5888.0*y[7]+989.0*y[8]); 
    double fa = y[0]; 
    double fm = y[4]; 
    double fb = y[8];
    if(is==0) is = b-a;
    double Q = adaptsimstp(ff,a,b,fa,fm,fb,is);
    return Q;	
}
	
public static double adaptsimstp(f_x ff,double a,double b,double fa,double fm,double fb,double is)
{
  double m = (a + b)/2; 
  double h = (b - a)/4;
  double x[] = {a + h, b - h};
  int n=x.length;
  double y[] = new double[n];
  for(int i=0;i<n;i++) {y[i]=ff.func(x[i]);}
  double fml = y[0];
  double fmr = y[1];
  double i1 = h/1.5 * (fa + 4*fm + fb);
  double i2 = h/3 * (fa + 4*(fml + fmr) + 2*fm + fb);
  double Q=0;
  i1 = (16.0*i2 - i1)/15.0; //romberg integrasyonu ile iyileştirme
  if (((is + (i1-i2)) == is) || (m <= a) || (b<=m))
  {
    if ((m <= a) || (b<=m))
       { System.out.println("istenilen toleransa ulaşılamadı");}
    Q = i1;
  }  
  else
  {
    Q = adaptsimstp(ff,a,m,fa,fml,fm,is)+
        adaptsimstp(ff,m,b,fm,fmr,fb,is);
  }
  return Q;
}




}
