//======================================================
// Numerical Analysis package in java
// Newton-cotes 3 integration (Simpson 1/3 rule)
// with aitken interpolation formula
// I=I4n-(I4n-I2n)*(I4n-I2n)/((I4n-I2n)-(I2n-In));
// Dr. Turhan Coban
// =====================================================
import java.io.*;


class f1 extends f_x
{
  double func(double x)
  {return x*Math.sqrt(x);}
}


class integ11
{

public static double newton_cotes3(f_x ff,double a,double b, int n)
{       // Newton-cotes integral 3 points
        // Simpson 1/3 rule	
        // ff integrating function
        // a,b integral limits
        // n : number of sun division h=(b-a)/n;    
        double h=(b-a)/n;
        double h1=h/2.0;
        double sum=0;
        int p,i;
        double x[]=new double[3];
        double f[]=new double[3];
        for(p=0;p<n;p++)
        {  {for(i=0;i<3;i++) {x[i]=a+i*h1+p*h;f[i]=ff.func(x[i]);}}
           sum+=h*(f[0]+4.0*f[1]+f[2])/6.0; 
        }
        return sum;
}

public static double Aitken_simpson1_3(f_x ff,double a,double b, int n)
{   //Aitken interpolation formula
	double I4n=newton_cotes3(ff,a,b,4*n);
    double I2n=newton_cotes3(ff,a,b,2*n);
    double In=newton_cotes3(ff,a,b,n);
    double I=I4n-(I4n-I2n)*(I4n-I2n)/((I4n-I2n)-(I2n-In));
    //System.out.println("I4n="+I4n+"I2n="+I2n+"In="+In+"I="+I);
    return I;
}

  public static void main(String args[]) throws IOException
  {
  f1 b1=new f1();
  
  for(int n=2;n<257;n*=2)
    System.out.println("n : "+n+"I = "+Aitken_simpson1_3(b1,-1.0,1.0,n));
}
}
