//======================================================
// Numerical Analysis package in java
// Newton-Cotes integral formula 4 points , n=3
// trapezoidal rule
// Dr. Turhan Coban
// =====================================================
import java.io.*;


class f1 extends f_x
{
  double func(double x)
  {return 4.0/Math.PI*Math.sqrt(1.0-x*x);}
}


class NA51A
{

public static double newton_cotes2(f_x ff,double a,double b, int n)
{       //Newton-cotes integral 2 points
        //trapezoidal rule	    
        double h=(b-a)/n;
        double h1=h;
        double sum=0;
        int p,i;
        int nn=2*n;
        double x[]=new double[2];
        double f[]=new double[2];
        double xi[]=new double[nn];
        double fi[]=new double[nn];
        int j=0;
        for(p=0;p<n;p++)
        {  {for(i=0;i<2;i++) {x[i]=a+i*h1+p*h;xi[j]=x[i];f[i]=ff.func(x[i]);fi[j]=f[i];j++;}}
           sum+=h*(f[0]+f[1])/2.0; 
        }
        Plot pp=new Plot(xi,fi);     
        pp.addFunction(ff,0.0,1.0,2000,2);
        pp.plot();
        return sum;
}

  public static void main(String args[]) throws IOException
  {
  f1 b1=new f1();
  double i1= newton_cotes2(b1,0.0,1.0,100);
  double error=1-i1;
  System.out.println("integral of class         f1 : "+i1+"error = "+error);
  
  }
}
