

Check out netlib by ftp to netlib.att.com, directory netlib.
There is a lot of numerical code there.  Specifically, if you
like C, in netlib/c/brent.shar.Z and serv.shar.Z there are
routines that will do what you ask.  (I think most of the code
in netlib is FORTRAN; you may prefer that anyway.)

_________________________________________________________________________
From: John D'Errico <[email-masked]>

I know this is not the best solution possible (so please cool the
flames) but a trick that has worked for me when I was looking to
constrain a variable in an optimization without resorting to a
constrained code is to introduce a transformation of variables.
If you want a variable x to be constrained to the interval [a,b],
simply substitute

x = a + (b-a)*(sin(z)+1)/2

When the optimizer returns z, run it through the transformation
to determine x. If all you want is to introduce a lower bound,
this can be accomplished by

x = a + z^2

(Do not try to use an absolute value function as it will introduce
a singularity at z=0. This will confuse gradient based methods to
no end.)

This allows you to use an unconstrained code on the variable z.
Again, I admit this method has its flaws, but it does work in a
pinch. You have to accept that there are suddenly many local
optima, though all of the ones so introduced will be equivalent.
You may also end up introducing significant extra curvature into
what was once a well behaved function. Finally, if you are using
this trick on a nonlinear least squares problem, then estimates
of confidence regions for the variables will no longer be correct.

_________________________________________________________________________
From [email-masked]

John D'Errico suggests using penalty functions as a
quick-and-dirty (but extremely useful) way of getting
an unconstrained optimization software package to
do bounds-constrained problems.  He gives examples
of continuous penalty functions.

I'll point out that if you use an optimization algorithm
such as NelderMead Simplex or HookeJeeves (which doesn't
require the objective function to be differentiable),
then you can use DIScontinuous penalty functions and/or
barrier functions.  These have the advantage of not
introducing any false minima inside the constraint
boundaries, because they only modify the objective
function beyond the boundaries.

A simple example:
  find the minimum of y= tan(log(x) - 3x) * cos(1/x^2)
  subject to the constraints  x>=4.0  and x<10.0

  Discontinuous penalty function approach:
  let f(x) = tan(log(x) - 3x) * cos(1/x^2)

  if(x < 4.0)   then return(  f(4.0) * 10.0 * (1.0 + (4.0-x)^2) ) ;
  if(x >= 10.0) then return( f(10.0) * 10.0 * (1.0 + (x-10.0)^2) ) ;
  return( f(x) );

_________________________________________________________________________