Reputation: 3218
scipy.optimize.fminbound(func, 0, 1)
http://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.fminbound.html#scipy.optimize.fminbound
scipy.optimize.minimize_scalar(func, bounds=(0,1))
http://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.minimize_scalar.html#scipy.optimize.minimize_scalar`
Are these equal?
Upvotes: 0
Views: 1161
Reputation: 76
Sometimes it is, and sometimes it isn't. It depends on the function that you define and the method you choose. The main difference is that the defined func
must return a scalar to use minimize_scalar
. I assume that this is because it allows it to return results more rapidly.
See the following for more concrete explanation:
from scipy import optimize
def func(x):
return (x - 2) * (x + 2)**2
def func2(x):
return (x - 2) * x * (x + 2)**2
min = 0
max = 1
res1 = optimize.fminbound(func, min, max)
res2 = optimize.minimize_scalar(func, bounds=(min,max))
res3 = optimize.fminbound(func2, min, max)
res4 = optimize.minimize_scalar(func2, bounds=(min,max))
print res1, res2.x
print res3, res4.x
import matplotlib.pyplot as plt
import numpy as np
xaxis = np.arange(-15,15)
plt.plot(xaxis, func(xaxis))
plt.plot(xaxis, func2(xaxis))
plt.scatter(res2.x, res2.fun)
plt.scatter(res4.x, res4.fun)
plt.show()
You can take a look at the plots if you want to see the function traces and where the minimum locations. The printed output is:
0.666666844366 0.666666656734
0.999994039139 1.28077640403
Upvotes: 1