"My computer tells me that $\\pi$ is approximately"
"My computer tells me that $\\pi$ is approximately"
]
]
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count": 10,
"execution_count": 14,
"metadata": {},
"metadata": {},
"outputs": [
"outputs": [
{
{
...
@@ -44,13 +37,13 @@
...
@@ -44,13 +37,13 @@
"cell_type": "markdown",
"cell_type": "markdown",
"metadata": {},
"metadata": {},
"source": [
"source": [
"### 1.2 Buffon’s needle\n",
"## 1.2 Buffon’s needle\n",
"Applying the method of [Buffon’s needle](https://en.wikipedia.org/wiki/Buffon%27s_needle_problem), we get the approximation"
"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": 11,
"execution_count": 15,
"metadata": {},
"metadata": {},
"outputs": [
"outputs": [
{
{
...
@@ -59,7 +52,7 @@
...
@@ -59,7 +52,7 @@
"3.128911138923655"
"3.128911138923655"
]
]
},
},
"execution_count": 11,
"execution_count": 15,
"metadata": {},
"metadata": {},
"output_type": "execute_result"
"output_type": "execute_result"
}
}
...
@@ -73,9 +66,19 @@
...
@@ -73,9 +66,19 @@
"2/(sum((x+np.sin(theta))>1)/N)"
"2/(sum((x+np.sin(theta))>1)/N)"
]
]
},
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 1.3 Using a surface fraction argument\n",
"\n",
"A method that is easier to understand and does not make use of the sin function is based on the\n",
"fact that if $X$ $\\sim$ $U(0, 1)$ and $Y$ $\\sim$ $U(0,1)$, then $P[X^2 + Y^2 ≤ 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": 12,
"execution_count": 16,
"metadata": {},
"metadata": {},
"outputs": [
"outputs": [
{
{
...
@@ -107,6 +110,34 @@
...
@@ -107,6 +110,34 @@
"ax.set_aspect('equal')"
"ax.set_aspect('equal')"
]
]
},
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"It is then straightforward to obtain a (not really good) approximation to $\\pi$ by counting how\n",
"many times, on average, $X^2 + Y^2$ is smaller than 1:"