.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/number_theory/continued_fractions/plot_01_approximating_pi.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_number_theory_continued_fractions_plot_01_approximating_pi.py: Approximating pi by continued fractions ============================================== Expands pi as a continued fraction and shows how quickly its convergents (355/113 famously accurate to 7 digits) approach the true value. .. GENERATED FROM PYTHON SOURCE LINES 11-16 .. code-block:: Python import numpy as np from mathematicskit.number_theory import best_rational_approximation, continued_fraction_expansion from mathematicskit.number_theory.visualizers.plots import plot_convergent_errors .. GENERATED FROM PYTHON SOURCE LINES 17-19 Expand pi and inspect its convergents ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 19-25 .. code-block:: Python result = continued_fraction_expansion(np.pi, max_terms=8) print("terms:", result.terms) for p, q in result.convergents: print(f" {p}/{q} = {p / q:.10f} (error {abs(p / q - np.pi):.2e})") .. rst-class:: sphx-glr-script-out .. code-block:: none terms: [3, 7, 15, 1, 292, 1, 1, 1] 3/1 = 3.0000000000 (error 1.42e-01) 22/7 = 3.1428571429 (error 1.26e-03) 333/106 = 3.1415094340 (error 8.32e-05) 355/113 = 3.1415929204 (error 2.67e-07) 103993/33102 = 3.1415926530 (error 5.78e-10) 104348/33215 = 3.1415926539 (error 3.32e-10) 208341/66317 = 3.1415926535 (error 1.22e-10) 312689/99532 = 3.1415926536 (error 2.91e-11) .. GENERATED FROM PYTHON SOURCE LINES 26-28 Best approximation under a denominator bound ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 28-32 .. code-block:: Python p, q = best_rational_approximation(np.pi, max_denominator=1000) print(f"\nbest approximation with denominator <= 1000: {p}/{q}") .. rst-class:: sphx-glr-script-out .. code-block:: none best approximation with denominator <= 1000: 355/113 .. GENERATED FROM PYTHON SOURCE LINES 33-35 Plot the convergent errors ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 35-37 .. code-block:: Python plot_convergent_errors(result, x=np.pi) .. image-sg:: /api/gallery/number_theory/continued_fractions/images/sphx_glr_plot_01_approximating_pi_001.png :alt: Continued-fraction convergent error :srcset: /api/gallery/number_theory/continued_fractions/images/sphx_glr_plot_01_approximating_pi_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.029 seconds) .. _sphx_glr_download_api_gallery_number_theory_continued_fractions_plot_01_approximating_pi.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_approximating_pi.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_01_approximating_pi.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_01_approximating_pi.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_