"My computer tells me that $\\pi$ is *approximatively*"
]
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count": 2,
"execution_count": 2,
...
@@ -18,6 +37,16 @@
...
@@ -18,6 +37,16 @@
"print(pi)"
"print(pi)"
]
]
},
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"## Buffon's needle\n",
"Applying the method of [Buffon's needle](https://en.wikipedia.org/wiki/Buffon%27s_needle_problem), we get the __approximation__"
]
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count": 4,
"execution_count": 4,
...
@@ -43,6 +72,16 @@
...
@@ -43,6 +72,16 @@
"2/(sum((x+np.sin(theta))>1)/N)"
"2/(sum((x+np.sin(theta))>1)/N)"
]
]
},
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"## Using a surface fraction argument\n",
"A method that is easier to understand and does not make use of the $\\sin$ function is based on the fact that if $X\\sim U(0,1)$ and $Y\\sim U(0,1)$, then $P[X^2+Y^2\\leq 1] = \\pi/4$ (see [\"Monte Carlo method\" on Wikipedia](https://en.wikipedia.org/wiki/Monte_Carlo_method)). The following code uses this approach:"
]
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count": 5,
"execution_count": 5,
...
@@ -79,6 +118,24 @@
...
@@ -79,6 +118,24 @@
"ax.set_aspect('equal')"
"ax.set_aspect('equal')"
]
]
},
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"ename": "SyntaxError",
"evalue": "invalid syntax (<ipython-input-7-46b60a496a9b>, line 1)",
"output_type": "error",
"traceback": [
"\u001b[0;36m File \u001b[0;32m\"<ipython-input-7-46b60a496a9b>\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m It is then straightforward to obtain a (not really good) approximation to $\\pi$ by counting how many times, on average, $X^2 + Y^2$ is smaller than 1:\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n"
]
}
],
"source": [
"It is then straightforward to obtain a (not really good) approximation to $\\pi$ by counting how many times, on average, $X^2 + Y^2$ is smaller than 1:"