Curriculum
Course: SCIPY
Login
Text lesson

Minimizing a Function

In this context, a function represents a curve with high and low points. High points are referred to as maxima, and low points as minima. The highest point on the entire curve is called the global maximum, while other high points are local maxima. Similarly, the lowest point on the curve is the global minimum, and other low points are local minima.

Finding Minima

We can use the scipy.optimize.minimize() function to minimize a function.

The minimize() function accepts the following arguments:

fun: a function representing the equation.

x0: an initial guess for the solution.

method: the name of the optimization method to use. Valid options include:

‘CG’

‘BFGS’

‘Newton-CG’

‘L-BFGS-B’

‘TNC’

‘COBYLA’

‘SLSQP’

callback: a function called after each iteration of the optimization.

options: a dictionary containing additional parameters.

{

“disp”: boolean – print detailed description

“gtol”: number – the tolerance of the error

}

Example

Minimize the function x2+x+2x^2 + x + 2×2+x+2 using the BFGS method.

from scipy.optimize import minimize

def eqn(x):
  
return x**2 + x + 2

mymin = minimize(eqn, 0, method=‘BFGS’)

print(mymin)