      SUBROUTINE mnewt(ntrial,x,n,tolx,tolf)
 !     implicit real *8 (a-h,o-z)
	  common /method/ iflag

      PARAMETER (NP=15)
      dimension x(n),indx(NP)
!     USES lubksb,ludcmp,usrfun
      dimension fjac(NP,NP),fvec(NP),p(NP)
      do 14  k=1,ntrial
         write(*,*)'---------------k=',k
	  if(iflag.eq.2) then
		write(*,*)'Approximate Newton Method'
		write(2,*)'Approximate Newton Method'
        call usrfun(x,n,NP,fvec,fjac)
	  endif
	  if(iflag.eq.1) then
		write(*,*)'Exact Newton Method'
		write(2,*)'Exact Newton Method'
        call usrfunex(x,n,NP,fvec,fjac)
	  endif
        errf=0.
        do 11 i=1,n
          errf=errf+abs(fvec(i))
11      continue
        write(*,*)'errf=',errf
	  write(2,*)'errf=',errf
	if(errf.le.tolf) then
           write(*,*)'converged in ',k,' iterations'
		 write(2,*)'converged in ',k,' iterations'
           return
        endif
!        if(errf.le.tolf)return
        do 12 i=1,n
          p(i)=-fvec(i)
12      continue
        call ludcmp(fjac,n,NP,indx,d)
        call lubksb(fjac,n,NP,indx,p)
        errx=0.
        do 13 i=1,n
          errx=errx+abs(p(i))
          x(i)=x(i)+p(i)
          write(*,*)'x(',i,')=',x(i)
		write(2,*)'x(',i,')=',x(i)
13      continue
        enorm=rnorm2(n,p,np) 
        write(*,*)'enorm=',enorm,'errx=',errx
	  write(2,*)'enorm=',enorm,'errx=',errx
        if(errx.le.tolx.or.enorm.le.tolx) then
           write(*,*)'converged in ',k,' iterations'
		 write(2,*)'converged in ',k,' iterations'
           return
        endif
14    continue
      return
      END

