%N powell %F $MIR/src/subs/powell.for
%D Minimization of a function, without derivative information.
%P bpw
%: optimization, mathematics
%B
      subroutine powell(x,e,n,f,escale,icon,mxiter,calcfx,w,maxfun,
     *                  ifail)

      implicit none
      integer n,icon,mxiter,maxfun,ifail
      real f,escale
      real x(n),e(n),w(n*(n+3))
      external calcfx

  1) Purpose.
  This subroutine finds the minimum of a function of several
  variables. It requires no derivatives. the user must provide a
  routine to evaluate the function for any values of the variables.
  i.e. the program will find a minimum of a function , f ,of n
  variables where
            f = f(x(1),x(2),x(5),...x(n))

  Briefly the method used is:
  Starting from a point x(1),x(2),...,x(n) the program constructs
  n+1 lines through the point and searches for a minimum of f along
  each line calling calcfx for appropriate values of f as it goes.
  x(1),x(2),x(5),x(6),....,x(n) are then re-set.   each set of n+1
  searches is called an iteration. Iterations continue until the
  minimum is found.

  2) Arguments:

    n          must be set to the number of variables.

    x and e    These are one dimensional arrays.  On entry to the
               routine x(i) must be set to an approximation to the ith
               variable and e(i) to the accuracy to which its optimum
               value is required.  On exit x(i) will be set to the
               calculated optimum value of the ith variable.  It is
               assumed that the magnitudes of the parameters e(i) are
               approximately proportional to the magnitudes of the
               corresponding variables x(i).

    f          Thus will be set to the minimum value of the function.

    escale     This limits the maximum change in the variables at a
               single step.  x(i) will not be changed by more than
               escale*e(i).

    icon       This must be set to 1 or 2. It controls the ultimate
               convergence criterion (see section 5).

    maxit      The routine will be left regarless after maxit
    maxfun     iterations have been completed, or maxfun function
               evaluations.

    w          Scratch array.

    ifail      Error return.
               Ifail = 0:   Success
                       1    Maximum change does not alter function
                       2    Accuracy limited by errors in calcfx.
                       3    Max iterations performed within Powell
                       4    Max function values evaluated in Powell

  3) Method.
  This will be published by m.j.d. powell in the july or october
  1964. computer journal. The minimum will practically never be
  found in less than n iteration. The function is calculated at
  least 2n times per iteration. The method is such that each
  iteration causes the function to decrease, except when the
  ultimate convergence criterion is being applied with icon=2.

  4) Subroutine calcfx.
     subroutine calcfx (n,x,f)
  must be provided by the user. n is the number of variables. x(i)
  x(2),.....x(n) are the current values of the variables. It must
  set f to the corresponding value of the function to be minimized.
  Remember that as calcfx is the name of a subroutine and is one
  of the arguments of powell  calcfx must appear in an external
  statement in any subprogram calling powell.
  e.g.
     external calcfx

  5) The ultimate convergence criterion.
  This will normally be satisfactory if icon is set to one.
  However if low accuracy is required or if it is suspected that the
  required accuracy is not being achieved. icon should be set to two
  and a more thorough check on the ultimate convergence will be
  made at the expense of increasing the execution time by maybe
  as much as 30 per cent. With icon=1 convergence will be assumed
  when an iteration changes each variable by less than 10 per cent
  of the required accuracy. With icon=2 such a point is found and it
  is then displaced by ten times the required accuracy in each
  variable. Minimization is continued from the new point until a
  change of less than 10per cent is again made by an iteration. The
  two estimate of the minimum are then compared.

  6) Recommendations.
  a) Do not set escale so small that powell must creep slowly to the
  minimum, it is only there to prevent the unlikely circumstance of
  a jump from one local minimum to another. this usually wont
  happen even if  escale is large(10**9).
  b) Set the required accuracy so that sscale is at least 100,
  c) If the answers appear to be unreasonable, try different
  initial values of the variables x(i).

  7) Reference.
     Powell M.J.D. (1964)  An efficient method for finding the
     minimum of a function of several variables without calculating
     derivatives. The Computer Journal, vol. 7, p. 303.
