# [SciPy-user] Minimizing functions of two variables with fmin_bfgs

Nils Wagner nwagner at mecha.uni-stuttgart.de
Mon Nov 14 09:02:58 CST 2005

LOPEZ GARCIA DE LOMANA, ADRIAN wrote:
>Hi all,
>
>I have a problem using the optimization modules. I'm using fmin_bfgs. It works very well for minimizing functions of just one parameter:
>
>import Numeric
>import scipy
>from scipy.optimize import fmin_bfgs
>
>def fitness(p):
>    return p**2
>
>def fitness_der(p):
>    return 2 * p
>
>p = [158.0]
>popt = fmin_bfgs(fitness, p, fprime = fitness_der)
>print popt
>
>but while I pretend to expand it to a multiparameter function using a vector,
>
>import Numeric
>import scipy
>from scipy.optimize import fmin_bfgs
>
>def fitness(p):
>    return p[0]**2 + p[1]
>
>def fitness_der(p):
>    return [2 * p[0] + 1, 1]
>
>p = [158.0, 314.0]
>popt = fmin_bfgs(fitness, p, fprime = fitness_der)
>print popt
>
>it crashes:
>
>Traceback (most recent call last):
>  File "hybrid.py", line 12, in ?
>    popt = fmin_bfgs(fitness, p, fprime = fitness_der)
>  File "/usr/local/lib/python2.4/site-packages/scipy/optimize/optimize.py", line 675, in fmin_bfgs
>    yk = gfkp1 - gfk
>TypeError: unsupported operand type(s) for -: 'list' and 'list'
>
>Some ideas? How can I minimize a function of several parameters? "fprime = fitness_der" does no understand that the partial derivatives are a list of same dimension of "p"?
>
>
>
>_______________________________________________
>SciPy-user mailing list
>SciPy-user at scipy.net
>http://www.scipy.net/mailman/listinfo/scipy-user
>
Try

import scipy
from scipy.optimize import fmin_bfgs

def fitness(p):
return p[0]**2 + p[1]**2

def fitness_der(p):
return scipy.array(([2 * p[0] , 2.0*p[1]] ))

p = [1.0, 1.0]
popt = fmin_bfgs(fitness, p, fprime = fitness_der)
print popt