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?
If you are given the formula y = ax2 + bx + c, then you can find the maximum value using the formula max = c - (b2 / 4a). If you have the equation y = a(x-h)2 + k and the a term is negative, then the maximum value is k.
If f"(x) < 0 for some value of x, say x = a, then the function f(x) is maximum at x = a. If f"(x) > 0 for some value of x, say x = b, then the function f(x) is minimum at x = b.
A minimum or a maximum is called an extreme point. A local extreme point is the smallest or largest value in its neighborhood. If it is also the smallest or largest at the entire domain of the function, it is called a global extreme point. The local minima and maxima can be found by solving f'(x) = 0.
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.])
If your function is solvable analytically try SymPy. I'll use EMS's example above.
In [1]: from sympy import *
In [2]: x = Symbol('x', real=True)
In [3]: f = -2 * x**2 + 4*x
In [4]: fprime = f.diff(x)
In [5]: fprime
Out[5]: -4*x + 4
In [6]: solve(fprime, x) # solve fprime = 0 with respect to x
Out[6]: [1]
Of course, you'll still need to check that 1 is a maximizer and not a minimizer of f
In [7]: f.diff(x).diff(x) < 0
Out[7]: True
I think scipy.optimize.minimize_scalar
and scipy.optimize.minimize
are the preferred ways now, that give you access to the range of techniques, e.g.
solution = scipy.optimize.minimize_scalar(lambda x: -f(x), bounds=[0,1], method='bounded')
for a single variable function that must lie between 0 and 1.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With