From 295f0564f96fd80969839240347cdb02f1db3979 Mon Sep 17 00:00:00 2001 From: 00bd05cb42ba6c2a491841c680c81c95 <00bd05cb42ba6c2a491841c680c81c95@app-learninglab.inria.fr> Date: Tue, 3 Jun 2025 15:18:14 +0000 Subject: [PATCH] v2 --- module2/exo1/toy_notebook_en.ipynb | 46 ++++++++++++++---------------- 1 file changed, 21 insertions(+), 25 deletions(-) diff --git a/module2/exo1/toy_notebook_en.ipynb b/module2/exo1/toy_notebook_en.ipynb index 1c79f01..8c88c1b 100644 --- a/module2/exo1/toy_notebook_en.ipynb +++ b/module2/exo1/toy_notebook_en.ipynb @@ -4,19 +4,16 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "toy_notebook_en \n", - "March 28, 2019\n", - "\n", "## 1 On the computation of π\n", "\n", "### 1.1 Asking the maths library\n", "\n", - "My computer tells me that π is approximately" + "My computer tells me that $\\pi$ is *approximatively*" ] }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 14, "metadata": {}, "outputs": [ { @@ -38,32 +35,32 @@ "source": [ "### 1.2 Buffon’s needle\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", - "execution_count": 8, + "execution_count": 15, "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "3.128911138923655\n" - ] + "data": { + "text/plain": [ + "3.128911138923655" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ "import numpy as np\n", - "\n", "np.random.seed(seed=42)\n", "N = 10000\n", - "x = np.random.uniform(low=0, high=1, size=N)\n", - "theta = np.random.uniform(low=0, high=pi/2, size=N)\n", - "\n", - "approx_pi_buffon = 2 / (np.sum((x + np.sin(theta)) > 1) / N)\n", - "print(approx_pi_buffon)\n" + "x = np.random.uniform(size=N, low=0, high=1)\n", + "theta = np.random.uniform(size=N, low=0, high=pi/2)\n", + "2/(sum((x+np.sin(theta))>1)/N)" ] }, { @@ -74,12 +71,12 @@ "\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", - "The following code uses this approach:\n" + "The following code uses this approach:" ] }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 18, "metadata": {}, "outputs": [ { @@ -102,25 +99,24 @@ "N = 1000\n", "x = 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", "reject = np.logical_not(accept)\n", "fig, ax = plt.subplots(1)\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.set_aspect('equal')\n" + "ax.set_aspect('equal')" ] }, { "cell_type": "markdown", "metadata": {}, "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", - "execution_count": 10, + "execution_count": 19, "metadata": {}, "outputs": [ { @@ -129,7 +125,7 @@ "3.112" ] }, - "execution_count": 10, + "execution_count": 19, "metadata": {}, "output_type": "execute_result" } -- 2.18.1