.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/numerical_analysis/approximation/plot_01_bernstein_polynomials.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_api_gallery_numerical_analysis_approximation_plot_01_bernstein_polynomials.py: Bernstein polynomials and the Weierstrass theorem ===================================================== Weierstrass proved in 1885 that every continuous function on a closed interval is a uniform limit of polynomials. Sergei Bernstein's 1912 proof builds the polynomials explicitly: average :math:`f(k/n)` with binomial weights. This script approximates a function with a kink, :math:`|x - 1/2|`, and shows the uniform error shrinking, slowly, as the degree grows. .. GENERATED FROM PYTHON SOURCE LINES 14-19 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from mathematicskit.numerical_analysis import bernstein_polynomial .. GENERATED FROM PYTHON SOURCE LINES 20-22 Bernstein approximants of increasing degree ---------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 22-33 .. code-block:: Python f = lambda t: np.abs(t - 0.5) x = np.linspace(0.0, 1.0, 501) fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 4.2)) ax1.plot(x, f(x), "k", lw=2, label="$|x - 1/2|$") for n in (4, 16, 64, 256): ax1.plot(x, bernstein_polynomial(f, n, x), label=f"$B_{{{n}}}f$") ax1.set_title("Bernstein polynomials") ax1.legend(fontsize=8) .. image-sg:: /api/gallery/numerical_analysis/approximation/images/sphx_glr_plot_01_bernstein_polynomials_001.png :alt: Bernstein polynomials :srcset: /api/gallery/numerical_analysis/approximation/images/sphx_glr_plot_01_bernstein_polynomials_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 34-36 Uniform error against degree ------------------------------ .. GENERATED FROM PYTHON SOURCE LINES 36-50 .. code-block:: Python degrees = np.array([2, 4, 8, 16, 32, 64, 128, 256, 512]) errors = np.array([np.max(np.abs(bernstein_polynomial(f, n, x) - f(x))) for n in degrees]) ax2.loglog(degrees, errors, "o-", label="max error") ax2.loglog(degrees, 0.4 / np.sqrt(degrees), "--", color="gray", label=r"$\propto n^{-1/2}$") ax2.set_xlabel("degree $n$") ax2.set_title("Convergence is uniform but slow") ax2.legend(fontsize=8) fig.tight_layout() for n, e in zip(degrees, errors): print(f"n = {n:4d}: max |B_n f - f| = {e:.4f}") plt.show() .. rst-class:: sphx-glr-script-out .. code-block:: none n = 2: max |B_n f - f| = 0.2500 n = 4: max |B_n f - f| = 0.1875 n = 8: max |B_n f - f| = 0.1367 n = 16: max |B_n f - f| = 0.0982 n = 32: max |B_n f - f| = 0.0700 n = 64: max |B_n f - f| = 0.0497 n = 128: max |B_n f - f| = 0.0352 n = 256: max |B_n f - f| = 0.0249 n = 512: max |B_n f - f| = 0.0176 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.105 seconds) .. _sphx_glr_download_api_gallery_numerical_analysis_approximation_plot_01_bernstein_polynomials.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_01_bernstein_polynomials.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_01_bernstein_polynomials.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_01_bernstein_polynomials.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_