.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/numerical_analysis/root_finding/plot_03_halley_method.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_root_finding_plot_03_halley_method.py: Halley's method: cubic convergence ===================================== Edmond Halley's 1694 iteration uses the second derivative as well as the first, stepping to the root of an osculating hyperbola instead of a tangent line. Near a simple root it *triples* the number of correct digits per step, against Newton's doubling. This script solves :math:`x^3 = 2` with both methods from the same starting guess and compares their error histories and empirical convergence orders. .. GENERATED FROM PYTHON SOURCE LINES 14-19 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from mathematicskit.numerical_analysis import Halley, NewtonRaphson, estimate_convergence_order .. GENERATED FROM PYTHON SOURCE LINES 20-22 Newton vs. Halley on :math:`f(x) = x^3 - 2` ---------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 22-42 .. code-block:: Python f = lambda x: x**3 - 2.0 fp = lambda x: 3.0 * x**2 fpp = lambda x: 6.0 * x root = 2.0 ** (1.0 / 3.0) newton = NewtonRaphson(f, fp, x0=3.0, tol=1e-15).solve() halley = Halley(f, fp, fpp, x0=3.0, tol=1e-15).solve() fig, ax = plt.subplots(figsize=(7, 4.5)) for result, color, label in ((newton, "steelblue", "Newton (order 2)"), (halley, "firebrick", "Halley (order 3)")): err = np.abs(result.history - root) err = np.where(err == 0.0, np.finfo(float).eps * root, err) ax.semilogy(err, "o-", color=color, label=label) ax.set_xlabel("iteration $n$") ax.set_ylabel(r"$|x_n - 2^{1/3}|$") ax.set_title("Halley's method converges in fewer steps") ax.legend() fig.tight_layout() .. image-sg:: /api/gallery/numerical_analysis/root_finding/images/sphx_glr_plot_03_halley_method_001.png :alt: Halley's method converges in fewer steps :srcset: /api/gallery/numerical_analysis/root_finding/images/sphx_glr_plot_03_halley_method_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 43-45 Empirical convergence orders ------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 45-50 .. code-block:: Python print(f"Newton: {newton.iterations} iterations, order ~ {estimate_convergence_order(newton.history[:-1], root):.2f}") print(f"Halley: {halley.iterations} iterations, order ~ {estimate_convergence_order(halley.history[:-1], root):.2f}") plt.show() .. rst-class:: sphx-glr-script-out .. code-block:: none Newton: 8 iterations, order ~ 1.99 Halley: 5 iterations, order ~ 2.86 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.053 seconds) .. _sphx_glr_download_api_gallery_numerical_analysis_root_finding_plot_03_halley_method.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_halley_method.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_03_halley_method.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_03_halley_method.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_