      SUBROUTINE C05NCV(FCN,N,X,FVEC,FJAC,LDFJAC,IFLAG,ML,MU,EPSFCN,WA1,
     *                  WA2)
C     MARK 9 RELEASE.  NAG COPYRIGHT 1981
C     MARK 11.5(F77) REVISED. (SEPT 1985.)
C     MARK 13 REVISED. USE OF MARK 12 X02 FUNCTIONS (APR 1988).
C     **********
C
C     SUBROUTINE C05NCV(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 C05NCV(FCN,N,X,FVEC,FJAC,LDFJAC,IFLAG,ML,MU,EPSFCN,
C     WA1,WA2)
C
C     WHERE
C
C     FCN IS THE NAME OF THE USER-SUPPLIED SUBROUTINE WHICH
C     CALCULATES THE FUNCTIONS. FCN MUST BE DECLARED
C     IN AN EXTERNAL STATEMENT IN THE USER CALLING
C     PROGRAM, AND SHOULD BE WRITTEN AS FOLLOWS.
C
C     SUBROUTINE FCN(N,X,FVEC,IFLAG)
C     INTEGER N,IFLAG
C     REAL X(N),FVEC(N)
C     ----------
C     CALCULATE THE FUNCTIONS AT X AND
C     RETURN THIS VECTOR IN FVEC.
C     ----------
C     RETURN
C     END
C
C     THE VALUE OF IFLAG SHOULD NOT BE CHANGED BY FCN UNLESS
C     THE USER WANTS TO TERMINATE EXECUTION OF C05NCV.
C     IN THIS CASE SET IFLAG TO A NEGATIVE INTEGER.
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     IFLAG IS AN INTEGER VARIABLE WHICH CAN BE USED TO TERMINATE
C     THE EXECUTION OF C05NCV. SEE DESCRIPTION OF FCN.
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     FCN
C     .. Scalar Arguments ..
      DOUBLE PRECISION  EPSFCN
      INTEGER           IFLAG, LDFJAC, ML, MU, N
C     .. Array Arguments ..
      DOUBLE PRECISION  FJAC(LDFJAC,N), FVEC(N), WA1(N), WA2(N), X(N)
C     .. Subroutine Arguments ..
      EXTERNAL          FCN
C     .. Local Scalars ..
      DOUBLE PRECISION  EPS, EPSMCH, H, TEMP, ZERO
      INTEGER           I, J, K, MSUM
C     .. External Functions ..
      DOUBLE PRECISION  X02AJF
      EXTERNAL          X02AJF
C     .. Intrinsic Functions ..
      INTRINSIC         ABS, MAX, SQRT
C     .. Data statements ..
      DATA              ZERO/0.0D0/
C     .. Executable Statements ..
      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
      DO 40 J = 1, N
         TEMP = X(J)
         H = EPS*ABS(TEMP)
         IF (H.EQ.ZERO) H = EPS
         X(J) = TEMP + H
         CALL FCN(N,X,WA1,IFLAG)
         IF (IFLAG.LT.0) GO TO 60
         X(J) = TEMP
         DO 20 I = 1, N
            FJAC(I,J) = (WA1(I)-FVEC(I))/H
   20    CONTINUE
   40 CONTINUE
   60 CONTINUE
      GO TO 200
   80 CONTINUE
C
C     COMPUTATION OF BANDED APPROXIMATE JACOBIAN.
C
      DO 160 K = 1, MSUM
         DO 100 J = K, N, MSUM
            WA2(J) = X(J)
            H = EPS*ABS(WA2(J))
            IF (H.EQ.ZERO) H = EPS
            X(J) = WA2(J) + H
  100    CONTINUE
         CALL FCN(N,X,WA1,IFLAG)
         IF (IFLAG.LT.0) GO TO 180
         DO 140 J = K, N, MSUM
            X(J) = WA2(J)
            H = EPS*ABS(WA2(J))
            IF (H.EQ.ZERO) H = EPS
            DO 120 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
  120       CONTINUE
  140    CONTINUE
  160 CONTINUE
  180 CONTINUE
  200 CONTINUE
      RETURN
      END
