.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "advanced/mathematical_optimization/auto_examples/plot_constraints.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_advanced_mathematical_optimization_auto_examples_plot_constraints.py>`
        to download the full example code.

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_advanced_mathematical_optimization_auto_examples_plot_constraints.py:


Constraint optimization: visualizing the geometry
==================================================

A small figure explaining optimization with constraints

.. GENERATED FROM PYTHON SOURCE LINES 7-66



.. rst-class:: sphx-glr-horizontal


    *

      .. image-sg:: /advanced/mathematical_optimization/auto_examples/images/sphx_glr_plot_constraints_001.png
         :alt: plot constraints
         :srcset: /advanced/mathematical_optimization/auto_examples/images/sphx_glr_plot_constraints_001.png
         :class: sphx-glr-multi-img

    *

      .. image-sg:: /advanced/mathematical_optimization/auto_examples/images/sphx_glr_plot_constraints_002.png
         :alt: plot constraints
         :srcset: /advanced/mathematical_optimization/auto_examples/images/sphx_glr_plot_constraints_002.png
         :class: sphx-glr-multi-img





.. code-block:: Python


    import numpy as np
    import matplotlib.pyplot as plt
    import scipy as sp

    x, y = np.mgrid[-2.9:5.8:0.05, -2.5:5:0.05]  # type: ignore[misc]
    x = x.T
    y = y.T

    for i in (1, 2):
        # Create 2 figure: only the second one will have the optimization
        # path
        plt.figure(i, figsize=(3, 2.5))
        plt.clf()
        plt.axes((0, 0, 1, 1))

        contours = plt.contour(
            np.sqrt((x - 3) ** 2 + (y - 2) ** 2),
            extent=[-3, 6, -2.5, 5],
            cmap="gnuplot",
        )
        plt.clabel(contours, inline=1, fmt="%1.1f", fontsize=14)
        plt.plot(
            [-1.5, -1.5, 1.5, 1.5, -1.5], [-1.5, 1.5, 1.5, -1.5, -1.5], "k", linewidth=2
        )
        plt.fill_between([-1.5, 1.5], [-1.5, -1.5], [1.5, 1.5], color=".8")
        plt.axvline(0, color="k")
        plt.axhline(0, color="k")

        plt.text(-0.9, 4.4, "$x_2$", size=20)
        plt.text(5.6, -0.6, "$x_1$", size=20)
        plt.axis("equal")
        plt.axis("off")

    # And now plot the optimization path
    accumulator = []


    def f(x):
        # Store the list of function calls
        accumulator.append(x)
        return np.sqrt((x[0] - 3) ** 2 + (x[1] - 2) ** 2)


    # We don't use the gradient, as with the gradient, L-BFGS is too fast,
    # and finds the optimum without showing us a pretty path
    def f_prime(x):
        r = np.sqrt((x[0] - 3) ** 2 + (x[0] - 2) ** 2)
        return np.array(((x[0] - 3) / r, (x[0] - 2) / r))


    sp.optimize.minimize(
        f, np.array([0, 0]), method="L-BFGS-B", bounds=((-1.5, 1.5), (-1.5, 1.5))
    )

    accumulated = np.array(accumulator)
    plt.plot(accumulated[:, 0], accumulated[:, 1])

    plt.show()


.. rst-class:: sphx-glr-timing

   **Total running time of the script:** (0 minutes 0.082 seconds)


.. _sphx_glr_download_advanced_mathematical_optimization_auto_examples_plot_constraints.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: plot_constraints.ipynb <plot_constraints.ipynb>`

    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: plot_constraints.py <plot_constraints.py>`

    .. container:: sphx-glr-download sphx-glr-download-zip

      :download:`Download zipped: plot_constraints.zip <plot_constraints.zip>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_