Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding the maximum of a function

Tags:

python

numpy

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?

like image 881
Nick T Avatar asked Apr 13 '12 19:04

Nick T


People also ask

How do you find the maximum value of a function?

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.

How do you find the maximum and minimum of a function?

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.

What is the maximum or minimum of the function?

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.


3 Answers

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.])
like image 152
ely Avatar answered Sep 20 '22 03:09

ely


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
like image 29
MRocklin Avatar answered Sep 18 '22 03:09

MRocklin


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.

like image 35
phasor Avatar answered Sep 22 '22 03:09

phasor