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)
pylab.plot(x,y)
# plot of x0 and the minimum found startin from x0
pylab.plot(x0,rsinc(x0),'bd',xmin0,rsinc(xmin0),'bo')
# plot of x1 and the minimum found startin from x1
pylab.plot(x1,rsinc(x1),'rd',xmin1,rsinc(xmin1),'ro')
pylab.axis([-15,15,-1.3,0.3])
pylab.show()
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.

4 comments:

  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: http://pybonacci.wordpress.com/2012/03/28/como-encontrar-el-minimo-de-una-funcion-usando-scipy/

    Thanks in advance.

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

    ReplyDelete
  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 https://en.wikipedia.org/wiki/Stoner%E2%80%93Wohlfarth_model . 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)

    ReplyDelete
    Replies
    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: http://docs.scipy.org/doc/scipy-0.16.1/reference/generated/scipy.optimize.minimize.html

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

      Delete

Note: Only a member of this blog may post a comment.