      implicit none
      integer n, nclin, ncnln, nctotl, nrowa, nrowj, nrowr, liwork, lwork
      parameter (n = 2, nclin = 1, ncnln = 1, nctotl = n+nclin+ncnln, 
     %     nrowa = nclin, nrowj = ncnln, nrowr = n, liwork = 3*n+nclin+2*ncnln,
     %     lwork = 2*n*n+n*nclin+2*n*ncnln+20*n+11*nclin+21*ncnln )
      double precision bigbnd, epsaf, epsmch, eta, ftol, objf, rteps
      integer i, ifail, iter, itmax, j, mode, msglvl, nstate, nout
      data nout, nstate, mode, ifail, msglvl / 6, 1, 1, 1, 1 /
      data bigbnd, itmax, eta / 1.0d10, 100, 0.9d0 /
      logical cold, fealin, orthog
      data cold, fealin, orthog / .true., .true., .true. /
      double precision a(nrowa,n), bl(nctotl), bu(nctotl), c(nrowj),
     %                 cjac(nrowj,n), clamda(nctotl), featol(nctotl),
     %                 objgrd(n), r(nrowr,n), work(lwork), x(n)
c                  Alkuarvoja, lineaariset rajoitteet seka yla- ja alarajat
      data x / 2.0d0, 2.0d0 /, a / 1, -2 /
      data bl / -10.0d0, -5.0d0, -1.0d0, -1.0d0 /
      data bu / 10.0d0, 5.0d0, -1.0d0, 1.0d10 /
      integer istate(nctotl), iwork(liwork)
      double precision x02ajf
      external x02ajf, confun, e04vcf, e04zcf, objfun, x04abf
c                  Tarkistetaan gradientit
      ifail = 1
      call e04zcf(n, nclin, nrowj, confun, objfun, c, cjac, objf, objgrd, x, 
     %     work, lwork, ifail)
      if (ifail.eq.0) then
         write (nout, *) 'Gradienttivektori ok'
c                  Selvitetaan laskentatarkkuus
         epsmch = x02ajf()
         ftol = 10.0d0*epsmch
         rteps = sqrt(epsmch)
         do 20 j = 1, nctotl
            featol(j) = rteps
 20      continue
         call objfun(mode, n, x, objf, objgrd, nstate)
         epsaf = epsmch*abs(objf)
c                  Ratkaistaan tehtava
         ifail = 1
         call e04vcf(itmax, msglvl, n, nclin, ncnln, nctotl, nrowa, nrowj, 
     %        nrowr, bigbnd, epsaf, eta, ftol, a, bl, bu, featol, confun, 
     %        objfun, cold, fealin, orthog, x, istate, r, iter, c, cjac, 
     %        objf, objgrd, clamda, iwork, liwork, work, lwork, ifail)
         if (ifail.ne.0) write (nout, 99998) 'e04vcf:n virhe. ifail =', ifail
      else
         write (nout, 99998) 'Virhe gradienttivektorissa. ifail =', ifail
      end if
99998    format (1x,a,i3)
      stop
      end
