Wednesday, April 27, 2011

How to find the minimum of a function using fmin from scipy

In this example we will see how to use the function fmin to minimize a function. The function fmin is contained in the optimize module of the scipy library. It uses the downhill simplex algorithm to find the minimum of an objective function starting from a guessing point given by the user. In the example we will start from two different guessing points to compare the results. Here's the code:
import numpy
import pylab
from scipy.optimize import fmin

# objective function
rsinc = lambda x: -1 * numpy.sin(x)/x

x0 = -5 # start from x = -5
xmin0 = fmin(rsinc,x0)

x1 = -4 # start from x = -4
xmin1 = fmin(rsinc,x1)

# plot the function
x = numpy.linspace(-15,15,100)
y = rsinc(x)
# plot of x0 and the minimum found startin from x0
# plot of x1 and the minimum found startin from x1
The function fmin will print some detail about the iterative process performed:
Optimization terminated successfully.
         Current function value: -0.128375
         Iterations: 18
         Function evaluations: 36
Optimization terminated successfully.
         Current function value: -1.000000
         Iterations: 19
         Function evaluations: 38

And the graphical result should be as follows:

The blue dot is the minimum found starting from the blue diamond (x=-5) and the red dot is the minimum found starting from the red diamond (x=-4). In this case, when we start from x=-5 fmin get stuck in a local minum and when we start from x=-4 fmin reaches the global minimum.


  1. Hi.

    I have published a free translation of this post in our scientific python blog in spanish.

    Please, let me know if you agree with that. If you have any inconvenience I will remove our post.

    You can visit:

    Thanks in advance.

  2. Thank you basuradek, as I told you before I think that this is a great idea! :D

  3. Hi, Suppose I have a problem where by I want to keep track of which branch I am on, how can I do that? Consider fig 2 on this page . The branch on which u find the minimum is critical to generating a good hysteresis curve. How can fmin help do that since for a given starting value u could end up in one of the minimums and never jump out. Can fmin get all the minimum values for me? For eg, getting all minimum points is key to generating Fig. 2. Thanks in advance..great work here by the way. (y)

    1. Hi Raphael, fmin just finds a minumum and it's common that it gets stuck in a local minima.

      I invite you to read the documentation of scipy.optimize.minimize:

      From here you have a starting overview about the options that you can use to solve your problem.