.. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_advanced_mathematical_optimization_auto_examples_plot_convex.py: Convex function ================ A figure showing the definition of a convex function .. rst-class:: sphx-glr-horizontal * .. image:: /advanced/mathematical_optimization/auto_examples/images/sphx_glr_plot_convex_001.png :class: sphx-glr-multi-img * .. image:: /advanced/mathematical_optimization/auto_examples/images/sphx_glr_plot_convex_002.png :class: sphx-glr-multi-img .. code-block:: python import numpy as np import matplotlib.pyplot as plt x = np.linspace(-1, 2) plt.figure(1, figsize=(3, 2.5)) plt.clf() # A convex function plt.plot(x, x**2, linewidth=2) plt.text(-.7, -.6**2, '$f$', size=20) # The tangent in one point plt.plot(x, 2*x - 1) plt.plot(1, 1, 'k+') plt.text(.3, -.75, "Tangent to $f$", size=15) plt.text(1, 1 - .5, 'C', size=15) # Convexity as barycenter plt.plot([.35, 1.85], [.35**2, 1.85**2]) plt.plot([.35, 1.85], [.35**2, 1.85**2], 'k+') plt.text(.35 - .2, .35**2 + .1, 'A', size=15) plt.text(1.85 - .2, 1.85**2, 'B', size=15) plt.ylim(ymin=-1) plt.axis('off') plt.tight_layout() # Convexity as barycenter plt.figure(2, figsize=(3, 2.5)) plt.clf() plt.plot(x, x**2 + np.exp(-5*(x - .5)**2), linewidth=2) plt.text(-.7, -.6**2, '$f$', size=20) plt.ylim(ymin=-1) plt.axis('off') plt.tight_layout() plt.show() **Total running time of the script:** ( 0 minutes 0.064 seconds) .. _sphx_glr_download_advanced_mathematical_optimization_auto_examples_plot_convex.py: .. only :: html .. container:: sphx-glr-footer :class: sphx-glr-footer-example .. container:: sphx-glr-download :download:`Download Python source code: plot_convex.py ` .. container:: sphx-glr-download :download:`Download Jupyter notebook: plot_convex.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_