.. 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_03_remez_minimax.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_03_remez_minimax.py: Remez's algorithm and best uniform approximation =================================================== Chebyshev showed that the polynomial of degree :math:`n` closest to :math:`f` in the maximum norm is characterized by an error curve that reaches its peak magnitude :math:`n + 2` times with alternating sign. Evgeny Remez's 1934 exchange algorithm finds it by repeatedly levelling the error on a set of reference points. This script computes the best degree-6 approximation to :math:`e^x \sin(3x)` on :math:`[-1, 1]` and compares its error with Chebyshev interpolation of the same degree. .. GENERATED FROM PYTHON SOURCE LINES 15-20 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from mathematicskit.numerical_analysis import ChebyshevInterpolant, remez_minimax .. GENERATED FROM PYTHON SOURCE LINES 21-23 The equioscillating error curve ---------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 23-47 .. code-block:: Python f = lambda x: np.exp(x) * np.sin(3.0 * x) n = 6 best = remez_minimax(f, n) cheb = ChebyshevInterpolant(f, n + 1) # degree n through n + 1 Chebyshev points x = np.linspace(-1.0, 1.0, 1000) err_best = f(x) - best.evaluate(x) err_cheb = f(x) - cheb(x) fig, ax = plt.subplots(figsize=(7, 4.5)) ax.plot(x, err_cheb, color="steelblue", label="Chebyshev interpolant") ax.plot(x, err_best, color="firebrick", label="Remez minimax") ax.scatter(best.reference, f(best.reference) - best.evaluate(best.reference), color="firebrick", zorder=3, label="reference points") ax.axhline(best.max_error, color="gray", ls="--", lw=0.8) ax.axhline(-best.max_error, color="gray", ls="--", lw=0.8) ax.set_title(f"Degree {n}: the minimax error equioscillates at {n + 2} points") ax.legend(fontsize=8) fig.tight_layout() print(f"Remez: {best.iterations} exchanges, max error {best.max_error:.3e}") print(f"Chebyshev interpolation: max error {np.max(np.abs(err_cheb)):.3e}") plt.show() .. image-sg:: /api/gallery/numerical_analysis/approximation/images/sphx_glr_plot_03_remez_minimax_001.png :alt: Degree 6: the minimax error equioscillates at 8 points :srcset: /api/gallery/numerical_analysis/approximation/images/sphx_glr_plot_03_remez_minimax_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Remez: 4 exchanges, max error 3.860e-03 Chebyshev interpolation: max error 8.326e-03 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.057 seconds) .. _sphx_glr_download_api_gallery_numerical_analysis_approximation_plot_03_remez_minimax.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_03_remez_minimax.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_03_remez_minimax.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_03_remez_minimax.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_