{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Archimedes and Pi\n", "by [Paulo Marques](http://pmarques.eu), 2014/03/09 (Adapted in 2018/10/15 to Python from Julia)\n", "\n", "---\n", "\n", "Since high school I've been fascinated with $\\pi$ -- this infinite non-repeating irrational transcendent number. In fact, not only was I fascinated with $\\pi$ but I was fascinated about everything related to it. In 11th grade I asked my math teacher about how to deduce the area of a circle. Her answer: for that you need to learn how to integrate – wait until university. But I couldn't wait and head to the library where I found a single book that talked about the subject – an obscure calculus book by an author named [Piskunov]( https://archive.org/details/DifferentialAndIntegralCalculus_109). And, to integrate I've learned – just because of $\\pi$. But I digress. ..\n", "\n", "This story is not about calculus or \"symbolic\" integration. It's about how [Archimedes](http://en.wikipedia.org/wiki/Archimedes) calculated $\\pi$ circa 200 B.C. In the \"Measurement of a Circle\" Archimedes states that: \n", "\n", "> \n", "> \"The ratio of the circumference of any circle to its diameter is greater than $3\\tfrac{10}{71}$ but less than $3\\tfrac{1}{7}$\"\n", "> \n", "\n", "This is the first really accurate estimation of $\\pi$. I.e., he calcuated $3.140845070422535 < \\pi < 3.142857142857143$. A good approximation of $\\pi$ is 3.141592653589793. So, this is two decimal places correct. That's pretty impressive.\n", "\n", "According to the story, Archimedes did this by inscribing and circumscribing a circle with regular polygons and measuring their perimeter. As the number of sides increases, the better these polygons approximate a circle. In the end Archimedes was using a 96-sided polygon. The next image illustrates the idea.\n", "\n", "![Polygons](/files/imgs/Archimedes_pi.svg)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "One of the annoying things when books talk about this is that they always show this nice picture but never ever do the actual calculation. So, using Python how can we do this?\n", "\n", "Let's start by assuming that we are going to use a circle with a radius of 1 and we inscribe a square in it. (The square's side is going to be $\\sqrt{2}$.)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![Triangle_Simple](/files/imgs/triang_1.png)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now, assume that the side of this polygon is $s_n$ and you are going to double the number of sides where the length of each new side is $s_{n+1}$. We can draw several triangles in the figure that will help us out:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![Triangle_Simple](/files/imgs/triang_2.png)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If we take the side $\\overline{AB}$, which measures $s_n$, and break it in two, we get the triangle $\\overline{ACD}$. This triangle has a hypotenuse of $s_{n+1}$, an adjacent side of $s_n/2$ and a height of $h$. Note that the new polygon that we are forming is going to have eight sides (i.e., double the number of sides we had), each one measuring $s_{n+1}$. From this we can write:\n", "\n", "$$ h^2 + (\\frac{s_n}{2})^2 = s_{n+1}^2 $$\n", "\n", "\n", "Looking at the triangle $\\overline{BCO}$, which is rectangle, we note that: its hypotenuse is 1, one side measures $1-h$ and the other measures $s_n/2$. Thus, we can write:\n", "\n", "$$ (1-h)^2 + (\\frac{s_n}{2})^2 = 1^2 $$ \n", "\n", "These two relations will always apply as we contantly break the polygons into smaller polygons. As we progress, the perimeter of the polygon $P_n$, obtained after $n$ iterations, will approximate the perimeter of the circle, measuring $2 \\pi$. What this means is that $\\lim_{n \\to \\infty} P_n = 2 \\pi $. \n", "\n", "Also note that every time we create a new polygon the number of sides doubles. Thus, after n steps we have a $2^n$ sided polygon and $P_n$ is:\n", "\n", "$ P_n = 2^n \\times s_n $\n", "\n", "Manipulating the two expressions above we get:\n", "\n", "$$ s_{n+1} = \\sqrt{ 2 - \\sqrt{4 - s_n^2} } $$ \n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Since we started with a square we have: $s_2 = \\sqrt 2$. We can also consider $s_1 = 2$ representing a diameter line.\n", "\n", "So, with this we have all equations needed to iteratively approximate $\\pi$. Let's start by coding a function that gives us $s_{n+1}$:\n" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from math import sqrt, pi\n", "\n", "def side_next(side):\n", " return sqrt(2. - sqrt(4. - side**2.0))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Our function `aprox_pi()` will compute the aproximation of $\\pi$ for a $2^n$ polygon:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "def aprox_pi(n = 10):\n", " s = 2.0\n", " for i in range(1, n):\n", " s = side_next(s)\n", " return 2.0**(n-1) * s" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And the result is:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3.141587725279961" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a_pi = aprox_pi()\n", "a_pi" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "So, in 10 iterations we got a very good approximation of $\\pi$ using a 1024-sided polygon. (Note that since $P_\\infty \\rightarrow 2 \\pi$ we need to divide the final result by two. `aprox_pi()` is automatically doing so.)\n", "The error of the result is:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4.928309832230582e-06" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "abs(pi - a_pi)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "That's Interesting. Let's see how good are the approximations generated by `aprox_pi()`." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " i \t Sides \t Pi \t Error\n", "===================================================================\n", " 1 \t 2 \t 2.0000000000 \t 1.14e+00\n", " 2 \t 4 \t 2.8284271247 \t 3.13e-01\n", " 3 \t 8 \t 3.0614674589 \t 8.01e-02\n", " 4 \t 16 \t 3.1214451523 \t 2.01e-02\n", " 5 \t 32 \t 3.1365484905 \t 5.04e-03\n", " 6 \t 64 \t 3.1403311570 \t 1.26e-03\n", " 7 \t 128 \t 3.1412772509 \t 3.15e-04\n", " 8 \t 256 \t 3.1415138011 \t 7.89e-05\n", " 9 \t 512 \t 3.1415729404 \t 1.97e-05\n", " 10 \t 1024 \t 3.1415877253 \t 4.93e-06\n", " 11 \t 2048 \t 3.1415914215 \t 1.23e-06\n", " 12 \t 4096 \t 3.1415923456 \t 3.08e-07\n", " 13 \t 8192 \t 3.1415925765 \t 7.70e-08\n", " 14 \t 16384 \t 3.1415926335 \t 2.01e-08\n", " 15 \t 32768 \t 3.1415926548 \t 1.22e-09\n", " 16 \t 65536 \t 3.1415926453 \t 8.27e-09\n", " 17 \t 131072 \t 3.1415926074 \t 4.62e-08\n", " 18 \t 262144 \t 3.1415929109 \t 2.57e-07\n", " 19 \t 524288 \t 3.1415941252 \t 1.47e-06\n", " 20 \t 1048576 \t 3.1415965537 \t 3.90e-06\n", " 21 \t 2097152 \t 3.1415965537 \t 3.90e-06\n", " 22 \t 4194304 \t 3.1416742650 \t 8.16e-05\n", " 23 \t 8388608 \t 3.1418296819 \t 2.37e-04\n", " 24 \t 16777216 \t 3.1424512725 \t 8.59e-04\n", " 25 \t 33554432 \t 3.1424512725 \t 8.59e-04\n", " 26 \t 67108864 \t 3.1622776602 \t 2.07e-02\n", " 27 \t 134217728 \t 3.1622776602 \t 2.07e-02\n", " 28 \t 268435456 \t 3.4641016151 \t 3.23e-01\n", " 29 \t 536870912 \t 4.0000000000 \t 8.58e-01\n", " 30 \t 1073741824 \t 0.0000000000 \t 3.14e+00\n" ] } ], "source": [ "print(\"%10s \\t %10s \\t %20s \\t %10s\" % (\"i\", \"Sides\", \"Pi\", \"Error\"))\n", "print(\"===================================================================\")\n", "for i in range(1, 31):\n", " sides = 2.**i\n", " a_pi = aprox_pi(i)\n", " err = abs(pi - a_pi)\n", " print(\"%10d \\t %10d \\t %20.10f \\t %10.2e\" % (i, sides, a_pi, err))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "So, what's going on? We should expect better approximations as the number of sides increases, right? But, the best result we get is with 14 iterations and a polygon of 16384 sides. After that the approximations of $\\pi$ get worse.\n", "\n", "The problem is that our algorithm is not very good in terms of producing the end result. If you look at the expression $P_n = 2^n \\times s_n$ what we are doing is multiplying a very large number (the number of sides) by a very small number (the length of a side). After a certain point, because we are using floating point, this is a recipe for disaster. In particular, for a 16384-sided polygon, the length of a side is approximatly:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.0003834951969714103" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2*pi/16384" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "----\n", "\n", "On a final note. Archimedes didn't have access to computers nor sofisticated ways of calculating square roots or sofisticated algebra. While in this notebook I've used his ideas of inscribing a polygon inside of a circle, his method was a little different in terms of calculation. He started with an hexagon (not a square) and ended-up with a 96-sided polygon. At the same time he ended up calculating his approximation of $\\pi$ using fractions (which I still think is impressive):\n", "\n", "> \"The ratio of the circumference of any circle to its diameter is greater than $3\\tfrac{10}{71}$ but less than $3\\tfrac{1}{7}$\"\n", "\n", "He did this by using rational approximations of certain square roots. Actually, no one knows where he got his roots! [Check this page](http://itech.fgcu.edu/faculty/clindsey/mhf4404/archimedes/archimedes.html) for a good description of this process of finding out $\\pi$." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.1" } }, "nbformat": 4, "nbformat_minor": 1 }