.. 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_example2.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_example2.py: =============================== Minima and roots of a function =============================== Demos finding minima and roots of a function. .. GENERATED FROM PYTHON SOURCE LINES 10-12 Define the function ########################################################### .. GENERATED FROM PYTHON SOURCE LINES 12-20 .. code-block:: default import numpy as np x = np.arange(-10, 10, 0.1) def f(x): return x**2 + 10*np.sin(x) .. GENERATED FROM PYTHON SOURCE LINES 21-23 Find minima ########################################################### .. GENERATED FROM PYTHON SOURCE LINES 23-35 .. code-block:: default import scipy as sp # Global optimization grid = (-10, 10, 0.1) xmin_global = sp.optimize.brute(f, (grid, )) print(f"Global minima found {xmin_global}") # Constrain optimization xmin_local = sp.optimize.fminbound(f, 0, 10) print(f"Local minimum found {xmin_local}") .. rst-class:: sphx-glr-script-out .. code-block:: none Global minima found [-1.30641113] Local minimum found 3.837467119498474 .. GENERATED FROM PYTHON SOURCE LINES 36-38 Root finding ########################################################### .. GENERATED FROM PYTHON SOURCE LINES 38-44 .. code-block:: default root = sp.optimize.root(f, 1) # our initial guess is 1 print(f"First root found {root.x}") root2 = sp.optimize.root(f, -2.5) print(f"Second root found {root2.x}") .. rst-class:: sphx-glr-script-out .. code-block:: none First root found [0.] Second root found [-2.47948183] .. GENERATED FROM PYTHON SOURCE LINES 45-47 Plot function, minima, and roots ########################################################### .. GENERATED FROM PYTHON SOURCE LINES 47-69 .. code-block:: default import matplotlib.pyplot as plt fig = plt.figure(figsize=(6, 4)) ax = fig.add_subplot(111) # Plot the function ax.plot(x, f(x), 'b-', label="f(x)") # Plot the minima xmins = np.array([xmin_global[0], xmin_local]) ax.plot(xmins, f(xmins), 'go', label="Minima") # Plot the roots roots = np.array([root.x, root2.x]) ax.plot(roots, f(roots), 'kv', label="Roots") # Decorate the figure ax.legend(loc='best') ax.set_xlabel('x') ax.set_ylabel('f(x)') ax.axhline(0, color='gray') plt.show() .. image-sg:: /intro/scipy/auto_examples/images/sphx_glr_plot_optimize_example2_001.png :alt: plot optimize example2 :srcset: /intro/scipy/auto_examples/images/sphx_glr_plot_optimize_example2_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.064 seconds) .. _sphx_glr_download_intro_scipy_auto_examples_plot_optimize_example2.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_optimize_example2.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_optimize_example2.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_