.. 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 ` 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-23 .. code-block:: default 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) y = np.linspace(*ylim) xg, yg = np.meshgrid(x, y) .. GENERATED FROM PYTHON SOURCE LINES 24-27 A 2D image plot of the function ########################################################### Simple visualization in 2D .. GENERATED FROM PYTHON SOURCE LINES 27-34 .. code-block:: default import matplotlib.pyplot as plt plt.figure() plt.imshow(sixhump([xg, yg]), extent=xlim+ylim, origin="lower") 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 .. GENERATED FROM PYTHON SOURCE LINES 35-37 A 3D surface plot of the function ########################################################### .. GENERATED FROM PYTHON SOURCE LINES 37-48 .. code-block:: default from mpl_toolkits.mplot3d import Axes3D fig = plt.figure() ax = fig.add_subplot(111, projection='3d') surf = ax.plot_surface(xg, yg, sixhump([xg, yg]), rstride=1, cstride=1, cmap=plt.cm.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 49-51 Find minima ########################################################### .. GENERATED FROM PYTHON SOURCE LINES 51-71 .. code-block:: default 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") 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.288 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-python :download:`Download Python source code: plot_2d_minimization.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_2d_minimization.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_