      SUBROUTINE trapzd(ainteg2,A,B,S,N)

	print *, 'inside trapzd',A,B,N

      IF (N.EQ.1) THEN
        S=0.5*(B-A)*(ainteg2(A)+ainteg2(B))
        IT=1
      ELSE
        TNM=IT
        DEL=(B-A)/TNM
        X=A+0.5*DEL
        SUM=0.
        DO 11 J=1,IT
          SUM=SUM+ainteg2(X)
          X=X+DEL
11      CONTINUE
        S=0.5*(S+(B-A)*SUM/TNM)
        IT=2*IT
      ENDIF

	print *, 'At end of trapzd', A,B,N,S 
      RETURN
      END
