Alex Rivera | Logout

Finding the maximum of a function

Asked 2012-04-13T19:13:10.430
23

How do I find the maximum of a function in Python? I could try to hack together a derivative function and find the zero of that, but is there a method in numpy (or other library) that can do it for me?

Edit
Report

1 Answer

31

You can use scipy.optimize.fmin on the negative of your function.

def f(x): return -2 * x**2 + 4 * x
max_x = scipy.optimize.fmin(lambda x: -f(x), 0)
# array([ 1.])
answered 2012-04-13T19:17:45.493

Your Answer