.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/calculus/finite_differences/plot_01_richardson_extrapolation.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_calculus_finite_differences_plot_01_richardson_extrapolation.py: Richardson extrapolation: cancelling error terms at a fixed step ================================================================= Richardson's idea: if an estimate :math:`D(h)` has error :math:`c_1 h^2 + c_2 h^4 + \dots`, then combining :math:`D(h)` and :math:`D(h/2)` as :math:`(4D(h/2) - D(h))/3` cancels the :math:`h^2` term. Repeating the combination on successively halved steps removes one error order per level. This example applies :func:`~mathematicskit.calculus.richardson_extrapolation` to a central difference and shows it reaching near machine precision from a coarse starting step, where no plain difference quotient can. .. GENERATED FROM PYTHON SOURCE LINES 16-25 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from mathematicskit.calculus import central_difference, richardson_extrapolation f = np.exp x0 = 1.0 exact = np.exp(1.0) .. GENERATED FROM PYTHON SOURCE LINES 26-31 One extrapolation step by hand ------------------------------ Two central differences, each only :math:`O(h^2)`, combine into an :math:`O(h^4)` estimate at no extra cost. .. GENERATED FROM PYTHON SOURCE LINES 31-40 .. code-block:: Python h = 0.2 d_h = central_difference(f, x0, h) d_h2 = central_difference(f, x0, h / 2) combined = (4 * d_h2 - d_h) / 3 print(f"D(h) error: {abs(d_h - exact):.3e}") print(f"D(h/2) error: {abs(d_h2 - exact):.3e}") print(f"(4D(h/2) - D(h))/3 error: {abs(combined - exact):.3e}") .. rst-class:: sphx-glr-script-out .. code-block:: none D(h) error: 1.816e-02 D(h/2) error: 4.533e-03 (4D(h/2) - D(h))/3 error: 9.072e-06 .. GENERATED FROM PYTHON SOURCE LINES 41-47 Each level cancels one more error order --------------------------------------- Starting from the same coarse :math:`h = 0.2`, every extra level of extrapolation gains roughly two more orders of accuracy, until floating-point round-off takes over. .. GENERATED FROM PYTHON SOURCE LINES 47-53 .. code-block:: Python levels = np.arange(1, 7) errs = [abs(richardson_extrapolation(f, x0, h=0.2, levels=int(k)).value - exact) for k in levels] for k, e in zip(levels, errs): print(f"levels={k}: error={e:.3e}") .. rst-class:: sphx-glr-script-out .. code-block:: none levels=1: error=1.816e-02 levels=2: error=9.072e-06 levels=3: error=5.397e-10 levels=4: error=4.885e-15 levels=5: error=2.709e-14 levels=6: error=6.972e-14 .. GENERATED FROM PYTHON SOURCE LINES 54-60 Extrapolation beats simply shrinking the step --------------------------------------------- A plain central difference at step :math:`h` bottoms out near :math:`10^{-11}` because of cancellation; Richardson reaches that accuracy while its smallest step is still large. .. GENERATED FROM PYTHON SOURCE LINES 60-75 .. code-block:: Python hs = np.logspace(-1, -7, 25) err_ctr = [abs(central_difference(f, x0, hh) - exact) for hh in hs] smallest_step = [0.2 / 2 ** (k - 1) for k in levels] fig, ax = plt.subplots(figsize=(6, 4.5)) ax.loglog(hs, err_ctr, "o-", label="central difference alone") ax.loglog(smallest_step, np.maximum(errs, 1e-17), "s-", label="Richardson, levels 1..6") ax.set_xlabel("smallest step used") ax.set_ylabel("|error in f'(1)|") ax.set_title("Richardson extrapolation from a coarse step h = 0.2") ax.legend() fig.tight_layout() plt.show() .. image-sg:: /api/gallery/calculus/finite_differences/images/sphx_glr_plot_01_richardson_extrapolation_001.png :alt: Richardson extrapolation from a coarse step h = 0.2 :srcset: /api/gallery/calculus/finite_differences/images/sphx_glr_plot_01_richardson_extrapolation_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.107 seconds) .. _sphx_glr_download_api_gallery_calculus_finite_differences_plot_01_richardson_extrapolation.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_richardson_extrapolation.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_01_richardson_extrapolation.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_01_richardson_extrapolation.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_