      SUBROUTINE MRQMIN(X,Y1,Y2,SIG1,SIG2,NDATA,A,MA,LISTA,MFIT,
     *    COVAR,ALPHA,CHISQ,ALAMDA,DO_1,DO_2)
C FROM NUMERICAL RECIPES; ADAPTED FOR RADIAL VELOCITY CURVE FITTING
C  4 SEPT 1991        J.T. ARMSTRONG

      IMPLICIT UNDEFINED (A-Z)
      INCLUDE 'VELPARM.INC'

      REAL*8 X(MAXVDATA),Y1(MAXVDATA),Y2(MAXVDATA)
      REAL*8 SIG1(MAXVDATA),SIG2(MAXVDATA)
      REAL*8 A(MMAX),DVELDA(MMAX,2),ALPHA(MMAX,MMAX),BETA(MMAX)
      REAL*8 COVAR(MMAX,MMAX),ATRY(MMAX),BETA(MMAX),DA(MMAX),DAA(MMAX,1)
      REAL*8 ALAMDA,CHISQ,OCHISQ

      INTEGER*4 LISTA(MMAX)
      INTEGER*4 I,J,KK,MA,IHIT,K,MFIT,NDATA,NCA

      CHARACTER*1 ANSWER

      LOGICAL DO_1,DO_2,ERRPERM

      NCA=MMAX
      IF(ALAMDA.LT.0.)THEN
        KK=MFIT+1
        DO J=1,MA
          IHIT=0
          DO K=1,MFIT
            IF(LISTA(K).EQ.J)IHIT=IHIT+1
          END DO
          IF (IHIT.EQ.0) THEN
            LISTA(KK)=J
            KK=KK+1
          ELSE IF (IHIT.GT.1) THEN
            WRITE(OUTC,*) ' IHIT > 1 in MRQMIN'
            ERRPERM = .TRUE.
          ENDIF
        END DO
        IF (KK.NE.(MA+1)) THEN
          WRITE(OUTC,*) ' Improper permutation in LISTA'
          ERRPERM = .TRUE.
        END IF
        IF(ERRPERM)THEN
          WRITE(OUTC,'(A,11(1X,I2))') ' LISTA: ',(LISTA(I),I=1,MMAX)
          READ(INC,'(A1)') ANSWER
          RETURN
        END IF
        ALAMDA=0.001
        CALL MRQCOF(X,Y1,Y2,SIG1,SIG2,NDATA,A,MA,LISTA,MFIT,ALPHA,
     *              BETA,CHISQ,DO_1,DO_2)
        OCHISQ=CHISQ
        DO J=1,MA
          ATRY(J)=A(J)
        END DO
      ENDIF
      DO J=1,MFIT
        DO K=1,MFIT
          COVAR(J,K)=ALPHA(J,K)
        END DO
        COVAR(J,J)=ALPHA(J,J)*(1.+ALAMDA)
        DA(J)=BETA(J)
      END DO
C     DO J=1,MFIT
C       WRITE(OUTC,'(1X,2(G12.5,1X))') 
C    *                (ALPHA(J,K),K=1,MFIT)
C     END DO
C     DO J=1,MFIT
C       WRITE(OUTC,'(1X,2(G12.5,1X))') 
C    *                (COVAR(J,K),K=1,MFIT)
C     END DO
      DO J=1,MMAX
        DAA(J,1)=DA(J)
      END DO
      CALL GAUSSJ(COVAR,MFIT,NCA,DAA,1,1)
      DO J=1,MMAX
        DA(J)=DAA(J,1)
      END DO
D     WRITE(OUTC,*) ' MRQMIN:  COVAR = '
D     DO J=1,MFIT
D       WRITE(OUTC,'(1X,2(G12.5,1X))') 
D    *                (COVAR(J,K),K=1,MFIT)
D     END DO
      IF(ALAMDA.EQ.0.)THEN
        CALL COVSRT(COVAR,NCA,MA,LISTA,MFIT)
        RETURN
      ENDIF
      DO J=1,MFIT
D       WRITE(OUTC,'(A,I2,A,I2)') 'DA(',LISTA(J),') = ',DA(J)
        ATRY(LISTA(J))=A(LISTA(J))+DA(J)
      END DO
D     WRITE(OUTC,*) ' <return> to continue'
D     READ(INC,'(A1)') ANSWER
      CALL MRQCOF(X,Y1,Y2,SIG1,SIG2,NDATA,ATRY,MA,LISTA,MFIT,COVAR,
     *            DA,CHISQ,DO_1,DO_2)
      IF(CHISQ.LT.OCHISQ)THEN
        ALAMDA=0.1*ALAMDA
        OCHISQ=CHISQ
        DO J=1,MFIT
          DO K=1,MFIT
            ALPHA(J,K)=COVAR(J,K)
          END DO
          BETA(J)=DA(J)
          A(LISTA(J))=ATRY(LISTA(J))
        END DO
      ELSE
        ALAMDA=10.*ALAMDA
        CHISQ=OCHISQ
      ENDIF
      RETURN
      END

