      SUBROUTINE MRQCOF(X,Y,SIG,NDATA,A,MA,LISTA,MFIT,ALPHA,BETA,NALP,
     *CHISQ,FUNCS,modef)
      implicit real*8 (a-h,o-z)
      implicit integer*4 (i-n)
      PARAMETER (MMAX=16)
      PARAMETER (MXREC2=9000)
      DIMENSION X(NDATA),Y(NDATA),SIG(NDATA),ALPHA(NALP,NALP),BETA(MA),
     *    DYDA(MXREC2,MMAX),LISTA(MFIT),A(MA),YMOD(MXREC2)
      EXTERNAL FUNCS
      DO 12 J=1,MFIT
        DO 11 K=1,J
          ALPHA(J,K)=0.D0
11      CONTINUE
        BETA(J)=0.D0
12    CONTINUE
      CHISQ=0.D0
      CALL FUNCS(X,A,YMOD,DYDA,MA,MMAX,NDATA,MXREC2,MFIT,LISTA)
      DO 15 I=1,NDATA
        SIG2I=1.D0/(SIG(I)*SIG(I))
        DY=Y(I)-YMOD(I)
        DO 14 J=1,MFIT
          WT=DYDA(I,LISTA(J))*SIG2I
          DO 13 K=1,J
            ALPHA(J,K)=ALPHA(J,K)+WT*DYDA(I,LISTA(K))
13        CONTINUE
          BETA(J)=BETA(J)+DY*WT
14      CONTINUE
        z=dy*sqrt(sig2i)
        if(modef.eq.1)then
         chisq=chisq+abs(z)
        else if(modef.eq.2)then
         chisq=chisq+log(1.d0+z**2/2.d0)*5.d0
        else
         CHISQ=CHISQ+DY*DY*SIG2I
        endif
15    CONTINUE
      DO 17 J=2,MFIT
        DO 16 K=1,J-1
          ALPHA(K,J)=ALPHA(J,K)
16      CONTINUE
17    CONTINUE
      RETURN
      END

