1.5.12.1. 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(-5, 5, 0.1)
plt.plot(x, f(x))
plot optimize example1
[<matplotlib.lines.Line2D object at 0x7fb6c962bd10>]

Now find the minimum with a few methods

import scipy as sp
# The default (Nelder Mead)
print(sp.optimize.minimize(f, x0=0))
 message: Optimization terminated successfully.
success: True
status: 0
fun: -7.945823375615215
x: [-1.306e+00]
nit: 5
jac: [-1.192e-06]
hess_inv: [[ 8.589e-02]]
nfev: 12
njev: 6

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

Gallery generated by Sphinx-Gallery