      subroutine secant(a,b,f,tol,itmax,flag)
      implicit none
      double precision a,b,f,tol
      integer itmax,flag
c
c  input:
c
c  a,b      two initial guesses
c  f        function subroutine f(x)
c  tol      tolerance
c  itmax    maximum number of iterations
c
c  output:
c  b        approximate root
c  flag     0 if tolerance was achieved
c           -1 if maximum number of iterations used
c              without achieving tolerance
c
c  stopping criterion is that two successive iterates agree to
c  within tol
c
      double precision fa,fb,bnew,delta
      integer j
      fa = f(a)
      fb = f(b)
c main loop
      do j = 1, itmax
c   print out the current status
        delta = -fb*(a-b)/(fa-fb)
        bnew = b + delta
        if (abs(delta) .lt. tol) then
          flag = 0
          return
        endif
        a = b
        b = bnew
        fa = fb
        fb = f(bnew)
      enddo
      flag = -1
      return
      end
