      implicit none
      integer maxn, n, nin, nout, ioptns, ifail, inform, iter, j
      parameter (maxn = 1000, n = 1000, nin = 5, nout = 6, ioptns = nin)
      double precision objf, rj, objgrd(n), user(1), work(13*n), x(n)
      integer iuser(1), iwork(n+1)
      external e04dgf, e04dkf, objfun, x04abf
c
      call x04abf(1,nout)
c                           x = alkuarvaus ratkaisulle
      do 10 j = 1, n-1, 2
         rj = j
         x( j ) = - 1.2d0 - 0.1d0*cos(rj)
         x(j+1) = 1.d0  + 0.1d0*cos(rj)
 10   continue
c                           Parametrit ratkaisurutiinille
      call e04dkf('   Maximum step length = 100')
      call e04dkf('   Optimality tolerance = 1.0d-14')
      call e04dkf('   Iteration limit = 1000')
      call e04dkf('   Print level = 1')
c                           Ratkaistaan ongelma
      ifail = -1
      call e04dgf(n, objfun, iter, objf, objgrd, x, iwork, work, iuser, 
     %            user, ifail)
c
      write (nout,*) 'Minimiarvo: ', objf
      stop
      end
c                           Minimoitava funktio ja gradienttivektori
      subroutine objfun(mode, n, x, objf, objgrd, nstate, iuser, user)
      implicit none
      integer mode, n, nstate, j, iuser(*)
      double precision objf, objgrd(n), user(*), x(n)
      double precision  t1, t2
c                           Kohdefunktion arvo
      objf = 0.d0
      do 10 j = 1, n-1, 2
         t1 =  1.d0 - x(j)
         t2 = 10.d0 * ( x(j+1) - x(j)**2 )
         objf = objf + (t1**2) + (t2**2)
 10   continue
c                           Gradienttivektori
      do 20 j = 1, n-1, 2
         t1 =  1.d0 - x(j)
         t2 = 10.d0 * ( x(j+1) - x(j)**2 )
         objgrd(j+1) =  20.d0 * t2
         objgrd( j ) =  -2.d0 * ( x(j) * objgrd(j+1) + t1 )
 20   continue
      return
      end
