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: 38And 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.