.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/numerical_analysis/polynomials/plot_02_wilkinson_polynomial.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_polynomials_plot_02_wilkinson_polynomial.py: Wilkinson's perfidious polynomial ==================================== :math:`w(x) = (x - 1)(x - 2) \cdots (x - 20)` has well separated integer roots, yet James Wilkinson found that subtracting :math:`2^{-23}` from its :math:`x^{19}` coefficient, :math:`-210`, sends ten of the roots far into the complex plane. The root condition numbers explain why: the roots near 16 amplify a relative change in that coefficient by a factor of about :math:`3 \times 10^{10}`. .. GENERATED FROM PYTHON SOURCE LINES 14-19 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from mathematicskit.numerical_analysis import root_condition_numbers, wilkinson_polynomial .. GENERATED FROM PYTHON SOURCE LINES 20-22 Roots before and after a tiny perturbation --------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 22-36 .. code-block:: Python w = wilkinson_polynomial(20) perturbed = w.copy() perturbed[1] -= 2.0**-23 roots = np.roots(perturbed) fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 4.2)) ax1.scatter(np.arange(1, 21), np.zeros(20), color="black", label="roots of $w$") ax1.scatter(roots.real, roots.imag, color="firebrick", marker="x", label=r"$a_{19} \to a_{19} - 2^{-23}$") ax1.set_xlabel("real part") ax1.set_ylabel("imaginary part") ax1.set_title("Wilkinson's perturbation experiment") ax1.legend(fontsize=8) .. image-sg:: /api/gallery/numerical_analysis/polynomials/images/sphx_glr_plot_02_wilkinson_polynomial_001.png :alt: Wilkinson's perturbation experiment :srcset: /api/gallery/numerical_analysis/polynomials/images/sphx_glr_plot_02_wilkinson_polynomial_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 37-39 Condition number of each root with respect to :math:`a_{19}` --------------------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 39-51 .. code-block:: Python kappa = [root_condition_numbers(w, float(r))[1] for r in range(1, 21)] ax2.semilogy(np.arange(1, 21), kappa, "o-") ax2.set_xlabel("root $r$") ax2.set_ylabel(r"$\kappa_{19}(r) = |a_{19}|\, r^{18} / |w'(r)|$") ax2.set_title("Relative sensitivity of each root") fig.tight_layout() print("roots with |imag| > 0.5:", np.sort_complex(roots[np.abs(roots.imag) > 0.5]).round(3)) print(f"largest condition number: {max(kappa):.2e} at r = {1 + int(np.argmax(kappa))}") plt.show() .. rst-class:: sphx-glr-script-out .. code-block:: none roots with |imag| > 0.5: [10.095-0.64j 10.095+0.64j 11.793-1.651j 11.793+1.651j 13.992-2.519j 13.992+2.519j 16.731-2.813j 16.731+2.813j 19.502-1.94j 19.502+1.94j ] largest condition number: 3.16e+10 at r = 16 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.072 seconds) .. _sphx_glr_download_api_gallery_numerical_analysis_polynomials_plot_02_wilkinson_polynomial.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_wilkinson_polynomial.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_02_wilkinson_polynomial.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_02_wilkinson_polynomial.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_