.. 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_03_complex_step.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_03_complex_step.py: The complex-step derivative: no cancellation ================================================== Compares the central difference with the complex-step derivative Im f(x + ih)/h as the step shrinks. The central difference is ruined by subtractive cancellation, while the complex step stays accurate to machine precision at any tiny step. .. GENERATED FROM PYTHON SOURCE LINES 12-17 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from mathematicskit.calculus import central_difference, complex_step_derivative .. GENERATED FROM PYTHON SOURCE LINES 18-20 Error against step size ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 20-40 .. code-block:: Python def f(x): return np.exp(x) / np.sqrt(np.sin(x) ** 3 + np.cos(x) ** 3) # Squire and Trapp's test function x0 = 1.5 reference = complex_step_derivative(f, x0, h=1e-200) hs = np.logspace(-20, -1, 60) central = [max(abs(central_difference(f, x0, h=h) - reference), 1e-17) for h in hs] complex_step = [max(abs(complex_step_derivative(f, x0, h=h) - reference), 1e-17) for h in hs] print(f"f'({x0}) = {reference:.15f}") fig, ax = plt.subplots() ax.loglog(hs, central, "o-", ms=3, label="central difference") ax.loglog(hs, complex_step, "s-", ms=3, label="complex step") ax.set_xlabel("step h") ax.set_ylabel("|error| (floored at 1e-17)") ax.legend() ax.set_title("Complex-step differentiation (Lyness and Moler, 1967)") .. image-sg:: /api/gallery/calculus/finite_differences/images/sphx_glr_plot_03_complex_step_001.png :alt: Complex-step differentiation (Lyness and Moler, 1967) :srcset: /api/gallery/calculus/finite_differences/images/sphx_glr_plot_03_complex_step_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none f'(1.5) = 4.497780053946161 Text(0.5, 1.0, 'Complex-step differentiation (Lyness and Moler, 1967)') .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.049 seconds) .. _sphx_glr_download_api_gallery_calculus_finite_differences_plot_03_complex_step.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_complex_step.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_03_complex_step.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_03_complex_step.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_