      SUBROUTINE MRQMIN(X,Y,SIG,NDATA,A,MA,LISTA,MFIT,
     *    COVAR,ALPHA,NCA,CHISQ,FUNCS,ALAMDA)
	implicit real*8(a-h,o-z)
      PARAMETER (MMAX=20)
	common		elong,as,be, 	! parameters used by the_model to calculate qyI
     &                  zs,ze,xs,xe,ccc,PI,au,au2,anu,alamb,freq,twopi,qxf,
     &			L1,L2,L3,L4,L5,L6,L7,L8,L9,A1,A2,A3,A4,A5,A6,A7,A8,A9,
     &			z,z0,R,p,Vx,Xnorm,XMnorm,ahpow,anoise,
     &			er1,er2,er3,er4,er5,er6,er7,er8,er9,er10,
     &			ijsing,iFs(500),iFt(500),iFd(500),aispecq(500),ispecq1,iq1(500),ispecq2,iq2(500)
      DIMENSION X(NDATA),Y(NDATA),SIG(NDATA),A(MA),LISTA(MA),
     *  COVAR(NCA,NCA),ALPHA(NCA,NCA),ATRY(MMAX),BETA(MMAX),DA(MMAX)
      external FUNCS								! put in to declare external by B. Jackson - not needed
      save OCHISQ								! absolutely needed, otherwise chisquare not remembered
C
C	print *, 'Inside MRQMIN', Alamda,A
      IF(ALAMDA.LT.0.0d0)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
            PAUSE 'Improper permutation in LISTA'
          ENDIF
12      CONTINUE
        IF (KK.NE.(MA+1)) PAUSE 'Improper permutation in LISTA'
        ALAMDA=0.001d0
        CALL MRQCOF(X,Y,SIG,NDATA,A,MA,LISTA,MFIT,ALPHA,BETA,NCA,CHISQ,FUNCS)
        OCHISQ=CHISQ
        DO 13 J=1,MA
          ATRY(J)=A(J)
13      CONTINUE
      ENDIF
	write(*,'(A,I3,4E12.4)'),'1st MFIT,ALPHA',MFIT,  ALPHA(1,1),ALPHA(1,2),ALPHA(2,1),ALPHA(2,2)
	write(*,'(A,F10.7,2E12.5,F15.7)'),' Alamda, BETA, CHISQ',Alamda,BETA(1),BETA(2),CHISQ
      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.+ALAMDA)
        DA(J)=BETA(J)
15    CONTINUE
      CALL GAUSSJ(COVAR,MFIT,NCA,DA,1,1)
	if(ijsing.eq.1) return
      IF(ALAMDA.EQ.0.0d0)THEN
        CALL COVSRT(COVAR,NCA,MA,LISTA,MFIT)
        RETURN
      ENDIF
      DO 16 J=1,MFIT
        ATRY(LISTA(J))=A(LISTA(J))+DA(J)
16    CONTINUE
      print *, ' '
      write(*,'(A,F10.4,F10.5,2F10.6)') 'Initial A     ',A(1)/1.0d3,A(2),A(3)/4.84813681d-6,A(4)
      print *, ' '
      write(*,'(A,F10.4,F10.5,2F10.6)') 'Try with new A',ATRY(1)/1.0d3,ATRY(2),ATRY(3)/4.84813681d-6,ATRY(4)
      print *, ' '
C
	XMnorm = -ahpow
	call make_model(Xnorm,ATRY,XMMnorm,MA) 
	XMnorm = XMMnorm
	write(*,'(A,E15.7)') 'XMMnorm =', XMnorm 
C
      CALL MRQCOF(X,Y,SIG,NDATA,ATRY,MA,LISTA,MFIT,COVAR,DA,NCA,CHISQ,FUNCS)
      IF(CHISQ.LT.OCHISQ)THEN
        ALAMDA=0.1d0*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
	write(*,'(A,I3,4E12.4)'),'2nd MFIT,ALPHA',MFIT,  ALPHA(1,1),ALPHA(1,2),ALPHA(2,1),ALPHA(2,2)
	write(*,'(A,F10.7,2E12.5,F15.7)'),' Alamda, BETA, CHISQ',Alamda,BETA(1),BETA(2),CHISQ
      ELSE
        ALAMDA=10.d0*ALAMDA
        CHISQ=OCHISQ
      ENDIF
      RETURN
      END
