v2

parent bb9b9181
...@@ -4,19 +4,16 @@ ...@@ -4,19 +4,16 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"toy_notebook_en \n",
"March 28, 2019\n",
"\n",
"## 1 On the computation of π\n", "## 1 On the computation of π\n",
"\n", "\n",
"### 1.1 Asking the maths library\n", "### 1.1 Asking the maths library\n",
"\n", "\n",
"My computer tells me that π is approximately" "My computer tells me that $\\pi$ is *approximatively*"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 7, "execution_count": 14,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
...@@ -38,32 +35,32 @@ ...@@ -38,32 +35,32 @@
"source": [ "source": [
"### 1.2 Buffon’s needle\n", "### 1.2 Buffon’s needle\n",
"\n", "\n",
"Applying the method of Buffon’s needle, we get the approximation\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": 8, "execution_count": 15,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
"name": "stdout", "data": {
"output_type": "stream", "text/plain": [
"text": [ "3.128911138923655"
"3.128911138923655\n"
] ]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
} }
], ],
"source": [ "source": [
"import numpy as np\n", "import numpy as np\n",
"\n",
"np.random.seed(seed=42)\n", "np.random.seed(seed=42)\n",
"N = 10000\n", "N = 10000\n",
"x = np.random.uniform(low=0, high=1, size=N)\n", "x = np.random.uniform(size=N, low=0, high=1)\n",
"theta = np.random.uniform(low=0, high=pi/2, size=N)\n", "theta = np.random.uniform(size=N, low=0, high=pi/2)\n",
"\n", "2/(sum((x+np.sin(theta))>1)/N)"
"approx_pi_buffon = 2 / (np.sum((x + np.sin(theta)) > 1) / N)\n",
"print(approx_pi_buffon)\n"
] ]
}, },
{ {
...@@ -74,12 +71,12 @@ ...@@ -74,12 +71,12 @@
"\n", "\n",
"A method that is easier to understand and does not make use of the sin function is based on the\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 ∼ U(0, 1) and Y ∼ U(0, 1), then P[X² + Y² ≤ 1] = π/4 (see \"Monte Carlo method\" on Wikipedia). \n", "fact that if X ∼ U(0, 1) and Y ∼ U(0, 1), then P[X² + Y² ≤ 1] = π/4 (see \"Monte Carlo method\" on Wikipedia). \n",
"The following code uses this approach:\n" "The following code uses this approach:"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 9, "execution_count": 18,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
...@@ -102,25 +99,24 @@ ...@@ -102,25 +99,24 @@
"N = 1000\n", "N = 1000\n",
"x = np.random.uniform(size=N, low=0, high=1)\n", "x = np.random.uniform(size=N, low=0, high=1)\n",
"y = np.random.uniform(size=N, low=0, high=1)\n", "y = np.random.uniform(size=N, low=0, high=1)\n",
"1\n",
"accept = (x*x+y*y) <= 1\n", "accept = (x*x+y*y) <= 1\n",
"reject = np.logical_not(accept)\n", "reject = np.logical_not(accept)\n",
"fig, ax = plt.subplots(1)\n", "fig, ax = plt.subplots(1)\n",
"ax.scatter(x[accept], y[accept], c='b', alpha=0.2, edgecolor=None)\n", "ax.scatter(x[accept], y[accept], c='b', alpha=0.2, edgecolor=None)\n",
"ax.scatter(x[reject], y[reject], c='r', alpha=0.2, edgecolor=None)\n", "ax.scatter(x[reject], y[reject], c='r', alpha=0.2, edgecolor=None)\n",
"ax.set_aspect('equal')\n" "ax.set_aspect('equal')"
] ]
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"It is then straightforward to obtain a (not really good) approximation to π by counting how many times, on average, X² + Y² is smaller than 1:" "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:"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 10, "execution_count": 19,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
...@@ -129,7 +125,7 @@ ...@@ -129,7 +125,7 @@
"3.112" "3.112"
] ]
}, },
"execution_count": 10, "execution_count": 19,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
......
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