.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "intro/scipy/auto_examples/plot_optimize_example1.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` 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. .. GENERATED FROM PYTHON SOURCE LINES 8-20 .. 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(-5, 5, 0.1) plt.plot(x, f(x)) .. image-sg:: /intro/scipy/auto_examples/images/sphx_glr_plot_optimize_example1_001.png :alt: plot optimize example1 :srcset: /intro/scipy/auto_examples/images/sphx_glr_plot_optimize_example1_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none [] .. GENERATED FROM PYTHON SOURCE LINES 21-22 Now find the minimum with a few methods .. GENERATED FROM PYTHON SOURCE LINES 22-27 .. code-block:: Python import scipy as sp # The default (Nelder Mead) print(sp.optimize.minimize(f, x0=0)) .. rst-class:: sphx-glr-script-out .. code-block:: none 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 .. GENERATED FROM PYTHON SOURCE LINES 28-30 .. code-block:: Python plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.045 seconds) .. _sphx_glr_download_intro_scipy_auto_examples_plot_optimize_example1.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_optimize_example1.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_optimize_example1.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_optimize_example1.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_