      SUBROUTINE MRQMIN(X,Y,SIG,NDATA,A,MA,LISTA,MFIT,
     *    COVAR,ALPHA,NCA,CHISQ,FUNCS,ALAMDA,modef,TOL)
      implicit real*8 (a-h,o-z)
      implicit integer*4 (i-n)
      save
      PARAMETER (MMAX=16)    ! MUST be equal to MXPARM
      DIMENSION X(NDATA),Y(NDATA),SIG(NDATA),A(MA),LISTA(MA),
     *  COVAR(NCA,NCA),ALPHA(NCA,NCA),ATRY(MMAX),BETA(MMAX),DA(MMAX)
      DIMENSION U(MMAX,MMAX),V(MMAX,MMAX),W(MMAX)
      IF(ALAMDA.LT.0.)THEN
        KK=MFIT+1
        DO 12 J=1,MA
          IHIT=0
          DO 11 K=1,MFIT
            IF(LISTA(K).EQ.J)IHIT=IHIT+1
11        CONTINUE
          IF (IHIT.EQ.0) THEN
            LISTA(KK)=J
            KK=KK+1
          ELSE IF (IHIT.GT.1) THEN
            WRITE(*,*)'Improper permutation in LISTA'
            RETURN
          ENDIF
12      CONTINUE
        IF (KK.NE.(MA+1)) THEN
          WRITE(*,*)'Improper permutation in LISTA'
          RETURN
        ENDIF
        ALAMDA=abs(alamda)
        CALL MRQCOF(X,Y,SIG,NDATA,A,MA,LISTA,MFIT,ALPHA,BETA,NCA,CHISQ,F
     *UNCS,modef)
        OCHISQ=CHISQ
        DO 13 J=1,MA
          ATRY(J)=A(J)
13      CONTINUE
        RETURN
      ENDIF
      DO 15 J=1,MFIT
        DO 14 K=1,MFIT
          COVAR(J,K)=ALPHA(J,K)
14      CONTINUE
        COVAR(J,J)=ALPHA(J,J)*(1.D0+ALAMDA)
        DA(J)=BETA(J)
15    CONTINUE
      CALL SVDCMP(COVAR,MFIT,MFIT,NCA,NCA,W,V)
      DO J=1,MFIT
       DO K=1,MFIT
        U(J,K)=COVAR(J,K)
       ENDDO
      ENDDO
C     IF(ALAMDA.NE.0)THEN
       DO I=1,MFIT,4
        WRITE(6,'(1X,A2,I2,A1,I2,A2,4(F16.4))')
     *           'W(',I,'-',I+3,')=',(REAL(W(J)),J=I,I+3)
       ENDDO
       WMAX=0.D0
       DO J=1,MFIT
        IF(W(J).GT.WMAX)WMAX=W(J)
       ENDDO
       THRESH=TOL*WMAX
       IED=0
       DO J=1,MFIT
        IF(W(J).LT.THRESH)THEN
         W(J)=0.D0
         IED=IED+1
        ENDIF
       ENDDO
       WRITE(6,*)'Singular Value Decomposition: '//
     *           'number of edited values = ',IED
C     ENDIF
      CALL SVBKSB(U,W,V,MFIT,MFIT,NCA,NCA,DA,BETA)
      DO J=1,MFIT
       DA(J)=BETA(J)
      ENDDO
      IF(ALAMDA.EQ.0.)THEN
       DO J=1,MFIT
        DO K=1,MFIT
         COVAR(J,K)=0.D0
         DO L=1,MFIT
          IF(W(L).NE.0)COVAR(J,K)=COVAR(J,K)+V(J,L)/W(L)*U(K,L)
         ENDDO
        ENDDO
       ENDDO
       WRITE(6,*)'Normalized covariance matrix:'
       DO J=1,MFIT
        WRITE(6,'(16(f6.2))')
     &   (COVAR(J,K)/SQRT(COVAR(J,J)*COVAR(K,K)),K=1,MFIT)
       ENDDO
       CALL COVSRT(COVAR,NCA,MA,LISTA,MFIT)
       RETURN
      ENDIF
      DO 16 J=1,MFIT
        ATRY(LISTA(J))=A(LISTA(J))+DA(J)
16    CONTINUE
      CALL MRQCOF(X,Y,SIG,NDATA,ATRY,MA,LISTA,MFIT,COVAR,DA,NCA,CHISQ,FU
     *NCS,modef)
      IF(CHISQ.LT.OCHISQ)THEN
        ALAMDA=0.2D0*ALAMDA
        OCHISQ=CHISQ
        DO 18 J=1,MFIT
          DO 17 K=1,MFIT
            ALPHA(J,K)=COVAR(J,K)
17        CONTINUE
          BETA(J)=DA(J)
          A(LISTA(J))=ATRY(LISTA(J))
18      CONTINUE
      ELSE
        ALAMDA=10.D0*ALAMDA
        CHISQ=OCHISQ
      ENDIF
      RETURN
      END

