.. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_intro_scipy_auto_examples_plot_optimize_example1.py: ========================================= Finding the minimum of a smooth function ========================================= Demos various methods to find the minimum of a function. .. code-block:: python 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)) .. image:: /intro/scipy/auto_examples/images/sphx_glr_plot_optimize_example1_001.png :class: sphx-glr-single-img Now find the minimum with a few methods .. code-block:: python from scipy import optimize # The default (Nelder Mead) print(optimize.minimize(f, x0=0)) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none fun: -7.945823375615215 hess_inv: array([[0.08589237]]) jac: array([-1.1920929e-06]) message: 'Optimization terminated successfully.' nfev: 18 nit: 5 njev: 6 status: 0 success: True x: array([-1.30644012]) .. code-block:: python print(optimize.minimize(f, x0=0, method="L-BFGS-B")) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none fun: array([-7.94582338]) hess_inv: <1x1 LbfgsInvHessProduct with dtype=float64> jac: array([-1.42108547e-06]) message: b'CONVERGENCE: NORM_OF_PROJECTED_GRADIENT_<=_PGTOL' nfev: 12 nit: 5 status: 0 success: True x: array([-1.30644013]) .. code-block:: python plt.show() **Total running time of the script:** ( 0 minutes 0.016 seconds) .. _sphx_glr_download_intro_scipy_auto_examples_plot_optimize_example1.py: .. only :: html .. container:: sphx-glr-footer :class: sphx-glr-footer-example .. container:: sphx-glr-download :download:`Download Python source code: plot_optimize_example1.py ` .. container:: sphx-glr-download :download:`Download Jupyter notebook: plot_optimize_example1.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_