      PROGRAM RECTAN
*
* To parakatw programma ypologizei se dipln akribeia
* to oloklhrwma ths f sto diasthma [a,b] xrhsimopoiwntas 
* to syn8eto kanona tou orthogwviou gia omoiomorfo diamerismo 
* apo  N = 2, 4, 8,...,512  diastnmata.
* A & B eivai ta akra tou diastnmatos.
*
      IMPLICIT REAL*8 (A-H, O-Z)
      EXTERNAL F
      A = 0.D0
      B = 2.D0
      EXACT = 0.5D0 * (1.D0 - DEXP(-4.0D0))
C
      N = 1      
      NLAST = 9      
C
      ERR2 = 0.D0
C
      WRITE(*,100)
      WRITE(*,110)
      DO 10 I = 1,NLAST
         N = 2 * N
         ERR1 = ERR2
         CALL RECT(N,A,B,F,RI)
         ERR2 = ABS(RI - EXACT)
         ORDCON =  DLOG(ERR1/ERR2) / DLOG(2.0D0)
         IF (I .GT. 1) WRITE(*,120) N,RI,ABS(RI - EXACT),ORDCON        
 10   CONTINUE
      WRITE(*,*)
 100  FORMAT(/,3X,'n',6X,'T(f)',9X,'|R(f)-I(f)|',5X,'Order')
 110  FORMAT(1X,'----',2X,' ------------',3x,'------------',
     &       3X,'--------')
 120  FORMAT(1X,I4,2E15.6,F11.6)
      STOP
      END

      SUBROUTINE RECT(N, A, B, F, RI)
      IMPLICIT REAL*8 (A-H, O-Z)
      EXTERNAL F
      H = (B - A)/DFLOAT(N)
      RI = 0.0D0
      DO 10 I = 1, N
        XI = A + ((I-1) + 0.5D0)*H
        RI = RI +  F(XI)
 10   CONTINUE
      RI = H * RI
      RETURN
      END
        

      FUNCTION F(X)
      IMPLICIT REAL*8 (A-H, O-Z)
      F = X * DEXP(-1.D0 * X * X)
      RETURN
      END
