.. 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_02_newton_raphson.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_02_newton_raphson.py: Newton-Raphson's method: tangent lines and quadratic convergence ================================================================ Newton and Raphson's iteration replaces :math:`f` by its tangent line at the current guess and steps to where that line crosses zero, :math:`x_{n+1} = x_n - f(x_n)/f'(x_n)`. This script draws the first tangent steps for :math:`f(x) = x^3 - 2x - 5` (the cubic Newton himself used as his example) and shows the number of correct digits roughly doubling at every step, i.e. convergence of order 2. .. GENERATED FROM PYTHON SOURCE LINES 14-26 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from mathematicskit.numerical_analysis import NewtonRaphson, estimate_convergence_order f = lambda x: x**3 - 2.0 * x - 5.0 fp = lambda x: 3.0 * x**2 - 2.0 result = NewtonRaphson(f, fp, x0=3.0, tol=1e-15).solve() root = result.root print(f"root = {root:.15f} after {result.iterations} iterations") .. rst-class:: sphx-glr-script-out .. code-block:: none root = 2.094551481542327 after 7 iterations .. GENERATED FROM PYTHON SOURCE LINES 27-29 Tangent-line steps ------------------ .. GENERATED FROM PYTHON SOURCE LINES 29-45 .. code-block:: Python x = np.linspace(1.8, 3.2, 400) fig1, ax1 = plt.subplots(figsize=(7, 4.5)) ax1.axhline(0.0, color="black", lw=0.8) ax1.plot(x, f(x), color="black", label=r"$f(x) = x^3 - 2x - 5$") for n, xn in enumerate(result.history[:3]): x_next = xn - f(xn) / fp(xn) ax1.plot([xn, x_next], [f(xn), 0.0], color="steelblue", lw=1.2) ax1.plot([xn, xn], [0.0, f(xn)], ":", color="gray") ax1.plot(xn, f(xn), "o", color="steelblue") ax1.annotate(f"$x_{n}$", (xn, 0.0), textcoords="offset points", xytext=(0, -14), ha="center") ax1.set_xlabel("$x$") ax1.set_title("Newton-Raphson: follow the tangent to its zero") ax1.legend() fig1.tight_layout() .. image-sg:: /api/gallery/numerical_analysis/root_finding/images/sphx_glr_plot_02_newton_raphson_001.png :alt: Newton-Raphson: follow the tangent to its zero :srcset: /api/gallery/numerical_analysis/root_finding/images/sphx_glr_plot_02_newton_raphson_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 46-48 Correct digits double every step -------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 48-63 .. code-block:: Python err = np.abs(result.history - root) for n, e in enumerate(err): if e > 0: print(f"step {n}: error = {e:.2e} correct digits ~ {-np.log10(e):.1f}") print(f"empirical order ~ {estimate_convergence_order(result.history[:-1], root):.2f}") fig2, ax2 = plt.subplots(figsize=(6, 4)) ax2.semilogy(np.where(err == 0, np.nan, err), "o-", color="steelblue") ax2.set_xlabel("iteration $n$") ax2.set_ylabel(r"$|x_n - x^*|$") ax2.set_title("Quadratic convergence: the error exponent doubles") fig2.tight_layout() plt.show() .. image-sg:: /api/gallery/numerical_analysis/root_finding/images/sphx_glr_plot_02_newton_raphson_002.png :alt: Quadratic convergence: the error exponent doubles :srcset: /api/gallery/numerical_analysis/root_finding/images/sphx_glr_plot_02_newton_raphson_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none step 0: error = 9.05e-01 correct digits ~ 0.0 step 1: error = 2.65e-01 correct digits ~ 0.6 step 2: error = 3.26e-02 correct digits ~ 1.5 step 3: error = 5.85e-04 correct digits ~ 3.2 step 4: error = 1.92e-07 correct digits ~ 6.7 step 5: error = 2.09e-14 correct digits ~ 13.7 empirical order ~ 1.99 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.081 seconds) .. _sphx_glr_download_api_gallery_numerical_analysis_root_finding_plot_02_newton_raphson.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_02_newton_raphson.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_02_newton_raphson.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_02_newton_raphson.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_