Finding the minimum of a smooth functionΒΆ

Demos various methods to find the minimum of a function.

import numpy as np
import matplotlib.pyplot as plt
def f(x):
return x**2 + 10*np.sin(x)
x = np.arange(-10, 10, 0.1)
plt.plot(x, f(x))

Now find the minimum with a few methods

from scipy import optimize
# The default (Nelder Mead)
print(optimize.minimize(f, x0=0))


fun: 0.0
hess_inv: array([[1021.1]])
jac: array([10.00000001])
message: 'Maximum number of iterations has been exceeded.'
nfev: 3
nit: 200
njev: 1
status: 1
success: False
x: array([-1.01])
print(optimize.minimize(f, x0=0, method="L-BFGS-B"))


fun: array([-7.94582338])
hess_inv: <1x1 LbfgsInvHessProduct with dtype=float64>
jac: array([-1.42108547e-06])
nfev: 12
nit: 5
status: 0
success: True
x: array([-1.30644013])

Total running time of the script: ( 0 minutes 0.049 seconds)

Gallery generated by Sphinx-Gallery