//======================================================
// Numerical Analysis package in java
// Romberg integral 
// 
// Dr. Turhan Coban
// ======================================================
import java.io.*;


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


class integ7A
{

public static double integral_romberg(f_x ff,double a,double b)
{
//integral f(x)dx
//romberg integration
int n=8;//increase n to increase accuracy
double R[][];
R=new double[n+1][n+1];
int m=1;
double h=b-a;
double close=1;
double tol=1e-40;
int j=0,k=0;
double ret=0;
R[0][0]=h/2.0*(ff.func(a)+ff.func(b));
do
{
  j++;
  h/=2.0;
  trap(ff,a,h,j,m,R);
  m*=2;
  for(k=1;k<=j;k++)
  {
    R[j][k]=R[j][k-1]+(R[j][k-1]-R[j-1][k-1])/(Math.pow(4,k)-1.0);
    close=Math.abs(R[j-1][j-1]-R[j][j]);
    ret=R[j][k];
  }
}while(close>tol && j<n);
Text.print(R);
return ret;
}

public static void trap(f_x ff,double a,double h, int j,int m,double R[][])
{
    //Newton-cotes integral 2 points
	//  successive trapezoidal integration rule
    //  this program will be utilised in romberg integration
        double sum=0;
        int p;
        for(p=1;p<=m;p++)
          { sum+=ff.func(a+h*(2*p-1)); }
        R[j][0]=R[j-1][0]/2.0+h*sum;
}


  public static void main(String args[]) throws IOException
  {
  //Romberg integrali
  f1 b1=new f1();
  System.out.println("integral of class         f1 : "+
  integral_romberg(b1,0.0,Math.PI));
}
}
