.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "intro/scipy/auto_examples/plot_2d_minimization.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end <sphx_glr_download_intro_scipy_auto_examples_plot_2d_minimization.py>` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_intro_scipy_auto_examples_plot_2d_minimization.py: ========================================= Optimization of a two-parameter function ========================================= .. GENERATED FROM PYTHON SOURCE LINES 7-27 .. code-block:: Python import numpy as np # Define the function that we are interested in def sixhump(x): return ( (4 - 2.1 * x[0] ** 2 + x[0] ** 4 / 3) * x[0] ** 2 + x[0] * x[1] + (-4 + 4 * x[1] ** 2) * x[1] ** 2 ) # Make a grid to evaluate the function (for plotting) xlim = [-2, 2] ylim = [-1, 1] x = np.linspace(*xlim) # type: ignore[call-overload] y = np.linspace(*ylim) # type: ignore[call-overload] xg, yg = np.meshgrid(x, y) .. GENERATED FROM PYTHON SOURCE LINES 28-31 A 2D image plot of the function ########################################################### Simple visualization in 2D .. GENERATED FROM PYTHON SOURCE LINES 31-37 .. code-block:: Python import matplotlib.pyplot as plt plt.figure() plt.imshow(sixhump([xg, yg]), extent=xlim + ylim, origin="lower") # type: ignore[arg-type] plt.colorbar() .. image-sg:: /intro/scipy/auto_examples/images/sphx_glr_plot_2d_minimization_001.png :alt: plot 2d minimization :srcset: /intro/scipy/auto_examples/images/sphx_glr_plot_2d_minimization_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none <matplotlib.colorbar.Colorbar object at 0x7f6342233dd0> .. GENERATED FROM PYTHON SOURCE LINES 38-40 A 3D surface plot of the function ########################################################### .. GENERATED FROM PYTHON SOURCE LINES 40-60 .. code-block:: Python from mpl_toolkits.mplot3d import Axes3D fig = plt.figure() ax: Axes3D = fig.add_subplot(111, projection="3d") surf = ax.plot_surface( xg, yg, sixhump([xg, yg]), rstride=1, cstride=1, cmap="viridis", linewidth=0, antialiased=False, ) ax.set_xlabel("x") ax.set_ylabel("y") ax.set_zlabel("f(x, y)") ax.set_title("Six-hump Camelback function") .. image-sg:: /intro/scipy/auto_examples/images/sphx_glr_plot_2d_minimization_002.png :alt: Six-hump Camelback function :srcset: /intro/scipy/auto_examples/images/sphx_glr_plot_2d_minimization_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Text(0.5, 1.0, 'Six-hump Camelback function') .. GENERATED FROM PYTHON SOURCE LINES 61-63 Find minima ########################################################### .. GENERATED FROM PYTHON SOURCE LINES 63-80 .. code-block:: Python import scipy as sp # local minimization res_local = sp.optimize.minimize(sixhump, x0=[0, 0]) # global minimization res_global = sp.optimize.differential_evolution(sixhump, bounds=[xlim, ylim]) plt.figure() # Show the function in 2D plt.imshow(sixhump([xg, yg]), extent=xlim + ylim, origin="lower") # type: ignore[arg-type] plt.colorbar() # Mark the minima plt.scatter(res_local.x[0], res_local.x[1], label="local minimizer") plt.scatter(res_global.x[0], res_global.x[1], label="global minimizer") plt.legend() plt.show() .. image-sg:: /intro/scipy/auto_examples/images/sphx_glr_plot_2d_minimization_003.png :alt: plot 2d minimization :srcset: /intro/scipy/auto_examples/images/sphx_glr_plot_2d_minimization_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.396 seconds) .. _sphx_glr_download_intro_scipy_auto_examples_plot_2d_minimization.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_2d_minimization.ipynb <plot_2d_minimization.ipynb>` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_2d_minimization.py <plot_2d_minimization.py>` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_2d_minimization.zip <plot_2d_minimization.zip>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_