.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/calculus/exhaustion/plot_01_archimedes_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_calculus_exhaustion_plot_01_archimedes_pi.py: Archimedes' method of exhaustion: squeezing pi ==================================================== Doubles the sides of inscribed and circumscribed regular polygons from 6 to 96, reproducing Archimedes' bounds 3 10/71 < pi < 3 1/7, and shows that the gap shrinks by a factor of four with each doubling. .. GENERATED FROM PYTHON SOURCE LINES 11-17 .. code-block:: Python import math import matplotlib.pyplot as plt from mathematicskit.calculus import archimedes_pi_bounds .. GENERATED FROM PYTHON SOURCE LINES 18-20 The 96-gon bounds ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 20-26 .. code-block:: Python result = archimedes_pi_bounds(4) for n, lo, hi in zip(result.sides, result.lower, result.upper): print(f"{n:3d} sides: {lo:.6f} < pi < {hi:.6f}") print(f"Archimedes: {3 + 10 / 71:.6f} < pi < {3 + 1 / 7:.6f}") .. rst-class:: sphx-glr-script-out .. code-block:: none 6 sides: 3.000000 < pi < 3.464102 12 sides: 3.105829 < pi < 3.215390 24 sides: 3.132629 < pi < 3.159660 48 sides: 3.139350 < pi < 3.146086 96 sides: 3.141032 < pi < 3.142715 Archimedes: 3.140845 < pi < 3.142857 .. GENERATED FROM PYTHON SOURCE LINES 27-29 Convergence of the gap ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 29-40 .. code-block:: Python result = archimedes_pi_bounds(15) gaps = [hi - lo for lo, hi in zip(result.lower, result.upper)] fig, ax = plt.subplots() ax.loglog(result.sides, gaps, "o-", label="upper - lower") ax.loglog(result.sides, [gaps[0] * (6 / n) ** 2 for n in result.sides], "--", label=r"$\propto n^{-2}$") ax.axhline(abs(math.pi - 22 / 7), color="0.6", lw=0.8) ax.set_xlabel("polygon sides n") ax.set_ylabel("width of the bracket on pi") ax.legend() ax.set_title("Each doubling cuts the gap by 4") .. image-sg:: /api/gallery/calculus/exhaustion/images/sphx_glr_plot_01_archimedes_pi_001.png :alt: Each doubling cuts the gap by 4 :srcset: /api/gallery/calculus/exhaustion/images/sphx_glr_plot_01_archimedes_pi_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Text(0.5, 1.0, 'Each doubling cuts the gap by 4') .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.047 seconds) .. _sphx_glr_download_api_gallery_calculus_exhaustion_plot_01_archimedes_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_archimedes_pi.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_01_archimedes_pi.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_01_archimedes_pi.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_