Second trial

parent a77156e7
...@@ -4,27 +4,20 @@ ...@@ -4,27 +4,20 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"# A propos de pi" "# 1. On the computation of $\\pi$"
] ]
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"## 1. On the computation of $\\pi$" "## 1.1 Asking the maths library\n",
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 1.1 Asking the maths library\n",
"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:"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3.112"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"4*np.mean(accept)"
]
},
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment