0

so I am trying to minimize a function of array for a given parameter using minimize of optimize and it gives me this error:

Traceback (most recent call last): File "plot2.py", line 72, in res = minimize(rosen(al,c), c, args=(al)) File "/home/usd/.local/lib/python3.6/site-packages/scipy/optimize/_minimize.py", line 604, in minimize return _minimize_bfgs(fun, x0, args, jac, callback, **options) File "/home/usd/.local/lib/python3.6/site-packages/scipy/optimize/optimize.py", line 1003, in _minimize_bfgs old_fval = f(x0) File "/home/usd/.local/lib/python3.6/site-packages/scipy/optimize/optimize.py", line 327, in function_wrapper return function(*(wrapper_args + args)) TypeError: 'numpy.float64' object is not callable

this is the code:

def rosen(xi,c):
return sum((xi[1:] - xi[:-1]-c)*(xi[1:] - xi[:-1]-c))

for index, k in enumerate(jo):
    for ko in range(len(alp2[index])):
        al = alp[index]
        al2 = alp[index+1]
        al = np.array(al)
        be = alp2[index][ko]
        be2 = alp2[index][ko] 
        c = 5
        print(rosen(al,c))
        res = minimize(rosen(al,c), c, args=(al))

1 Answers1

0

You have to give a callable function as first argument to minimize. In your case you are evaluating the function, therefore you give the return value as the first argument, which is a float - which is not callable, hence the error. Below a simplified example of your code with a working function call:

import numpy as np
from scipy.optimize import minimize

def rosen(xi,c):
    return sum((xi[1:] - xi[:-1]-c)*(xi[1:] - xi[:-1]-c))


c = 5
res = minimize(rosen, x0=np.zeros(2), args=(c,))
print(res)
onodip
  • 635
  • 7
  • 12