.. 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_2d_minimization.py: ========================================= Optimization of a two-parameter function ========================================= .. 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) x = np.linspace(-2, 2) y = np.linspace(-1, 1) xg, yg = np.meshgrid(x, y) A 2D image plot of the function ########################################################### Simple visualization in 2D .. code-block:: python import matplotlib.pyplot as plt plt.figure() plt.imshow(sixhump([xg, yg]), extent=[-2, 2, -1, 1], origin="lower") plt.colorbar() .. image:: /intro/scipy/auto_examples/images/sphx_glr_plot_2d_minimization_001.png :class: sphx-glr-single-img A 3D surface plot of the function ########################################################### .. code-block:: python 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.jet, 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:: /intro/scipy/auto_examples/images/sphx_glr_plot_2d_minimization_002.png :class: sphx-glr-single-img Find the minima ########################################################### .. code-block:: python from scipy import optimize x_min = optimize.minimize(sixhump, x0=[0, 0]) plt.figure() # Show the function in 2D plt.imshow(sixhump([xg, yg]), extent=[-2, 2, -1, 1], origin="lower") plt.colorbar() # And the minimum that we've found: plt.scatter(x_min.x[0], x_min.x[1]) plt.show() .. image:: /intro/scipy/auto_examples/images/sphx_glr_plot_2d_minimization_003.png :class: sphx-glr-single-img **Total running time of the script:** ( 0 minutes 0.146 seconds) .. _sphx_glr_download_intro_scipy_auto_examples_plot_2d_minimization.py: .. only :: html .. container:: sphx-glr-footer :class: sphx-glr-footer-example .. container:: sphx-glr-download :download:`Download Python source code: plot_2d_minimization.py ` .. container:: sphx-glr-download :download:`Download Jupyter notebook: plot_2d_minimization.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_