      SUBROUTINE C05NDV(IRVNDS,N,X,FVEC,FJAC,LDFJAC,ML,MU,EPSFCN,WA1,
     *                  WA2)
C     MARK 14 RELEASE. NAG COPYRIGHT 1989.
C     **********
C
C     SUBROUTINE C05NDV(BASED ON MINPACK ROUTINE FDJAC1)
C
C     THIS SUBROUTINE COMPUTES A FORWARD-DIFFERENCE APPROXIMATION
C     TO THE N BY N JACOBIAN MATRIX ASSOCIATED WITH A SPECIFIED
C     PROBLEM OF N FUNCTIONS IN N VARIABLES. IF THE JACOBIAN HAS
C     A BANDED FORM, THEN FUNCTION EVALUATIONS ARE SAVED BY ONLY
C     APPROXIMATING THE NONZERO TERMS.
C
C     THE SUBROUTINE STATEMENT IS
C
C     SUBROUTINE C05NDV(IRVNDS,N,X,FVEC,FJAC,LDFJAC,ML,MU,EPSFCN,
C     WA1,WA2)
C
C     WHERE
C
C     IRVNDS IS THE REVERSE COMMUNICATION PARAMETER.
C
C     N IS A POSITIVE INTEGER INPUT VARIABLE SET TO THE NUMBER
C     OF FUNCTIONS AND VARIABLES.
C
C     X IS AN INPUT ARRAY OF LENGTH N.
C
C     FVEC IS AN INPUT ARRAY OF LENGTH N WHICH MUST CONTAIN THE
C     FUNCTIONS EVALUATED AT X.
C
C     FJAC IS AN OUTPUT N BY N ARRAY WHICH CONTAINS THE
C     APPROXIMATION TO THE JACOBIAN MATRIX EVALUATED AT X.
C
C     LDFJAC IS A POSITIVE INTEGER INPUT VARIABLE NOT LESS THAN N
C     WHICH SPECIFIES THE LEADING DIMENSION OF THE ARRAY FJAC.
C
C     ML IS A NONNEGATIVE INTEGER INPUT VARIABLE WHICH SPECIFIES
C     THE NUMBER OF SUBDIAGONALS WITHIN THE BAND OF THE
C     JACOBIAN MATRIX. IF THE JACOBIAN IS NOT BANDED, SET
C     ML TO AT LEAST N - 1.
C
C     EPSFCN IS AN INPUT VARIABLE USED IN DETERMINING A SUITABLE
C     STEP LENGTH FOR THE FORWARD-DIFFERENCE APPROXIMATION. THIS
C     APPROXIMATION ASSUMES THAT THE RELATIVE ERRORS IN THE
C     FUNCTIONS ARE OF THE ORDER OF EPSFCN. IF EPSFCN IS LESS
C     THAN THE MACHINE PRECISION, IT IS ASSUMED THAT THE RELATIVE
C     ERRORS IN THE FUNCTIONS ARE OF THE ORDER OF THE MACHINE
C     PRECISION.
C
C     MU IS A NONNEGATIVE INTEGER INPUT VARIABLE WHICH SPECIFIES
C     THE NUMBER OF SUPERDIAGONALS WITHIN THE BAND OF THE
C     JACOBIAN MATRIX. IF THE JACOBIAN IS NOT BANDED, SET
C     MU TO AT LEAST N - 1.
C
C     WA1 AND WA2 ARE WORK ARRAYS OF LENGTH N. IF ML + MU + 1 IS AT
C     LEAST N, THEN THE JACOBIAN IS CONSIDERED DENSE, AND WA2 IS
C     NOT REFERENCED.
C
C     ARGONNE NATIONAL LABORATORY. MINPACK PROJECT. MARCH 1980.
C     BURTON S. GARBOW, KENNETH E. HILLSTROM, JORGE J. MORE
C
C     **********
C
C     EPSMCH IS THE MACHINE PRECISION.
C
C     .. Scalar Arguments ..
      DOUBLE PRECISION  EPSFCN
      INTEGER           IRVNDS, LDFJAC, ML, MU, N
C     .. Array Arguments ..
      DOUBLE PRECISION  FJAC(LDFJAC,N), FVEC(N), WA1(N), WA2(N), X(N)
C     .. Local Scalars ..
      DOUBLE PRECISION  EPS, EPSMCH, H, TEMP, ZERO
      INTEGER           I, J, K, MSUM
C     .. External Functions ..
      DOUBLE PRECISION  X02AJF
      EXTERNAL          X02AJF
C     .. External Subroutines ..
      EXTERNAL          DSWAP
C     .. Intrinsic Functions ..
      INTRINSIC         ABS, MAX, SQRT
C     .. Save statement ..
      SAVE
C     .. Data statements ..
      DATA              ZERO/0.0D0/
C     .. Executable Statements ..
      GO TO (40,140) IRVNDS
      EPSMCH = X02AJF()
C
      EPS = SQRT(MAX(EPSFCN,EPSMCH))
      MSUM = ML + MU + 1
      IF (MSUM.LT.N) GO TO 80
C
C     COMPUTATION OF DENSE APPROXIMATE JACOBIAN.
C
      J = 1
   20 CONTINUE
      TEMP = X(J)
      H = EPS*ABS(TEMP)
      IF (H.EQ.ZERO) H = EPS
      X(J) = TEMP + H
      CALL DSWAP(N,WA1,1,FVEC,1)
      IRVNDS = 1
      RETURN
   40 CONTINUE
      CALL DSWAP(N,FVEC,1,WA1,1)
      X(J) = TEMP
      DO 60 I = 1, N
         FJAC(I,J) = (WA1(I)-FVEC(I))/H
   60 CONTINUE
      J = J + 1
      IF (J.LE.N) GO TO 20
      GO TO 200
   80 CONTINUE
C
C     COMPUTATION OF BANDED APPROXIMATE JACOBIAN.
C
      K = 1
  100 CONTINUE
      DO 120 J = K, N, MSUM
         WA2(J) = X(J)
         H = EPS*ABS(WA2(J))
         IF (H.EQ.ZERO) H = EPS
         X(J) = WA2(J) + H
  120 CONTINUE
      IRVNDS = 2
      CALL DSWAP(N,WA1,1,FVEC,1)
      RETURN
  140 CONTINUE
      CALL DSWAP(N,FVEC,1,WA1,1)
      DO 180 J = K, N, MSUM
         X(J) = WA2(J)
         H = EPS*ABS(WA2(J))
         IF (H.EQ.ZERO) H = EPS
         DO 160 I = 1, N
            FJAC(I,J) = ZERO
            IF (I.GE.J-MU .AND. I.LE.J+ML) FJAC(I,J) = (WA1(I)-FVEC(I))
     *          /H
  160    CONTINUE
  180 CONTINUE
      K = K + 1
      IF (K.LE.MSUM) GO TO 100
  200 CONTINUE
      IRVNDS = 0
      RETURN
      END
