From 25691a958ab73b156693c0c306a003c80e7e64a7 Mon Sep 17 00:00:00 2001 From: 516971efdfedafbe44b4214ac2bdbd1a <516971efdfedafbe44b4214ac2bdbd1a@app-learninglab.inria.fr> Date: Mon, 24 Jul 2023 11:32:48 +0000 Subject: [PATCH] Second trial --- module2/exo1/toy_notebook_en.ipynb | 59 +++++++++++++++++++++++------- 1 file changed, 45 insertions(+), 14 deletions(-) diff --git a/module2/exo1/toy_notebook_en.ipynb b/module2/exo1/toy_notebook_en.ipynb index a4f72df..db00224 100644 --- a/module2/exo1/toy_notebook_en.ipynb +++ b/module2/exo1/toy_notebook_en.ipynb @@ -4,27 +4,20 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "# A propos de pi" + "# 1. On the computation of $\\pi$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "## 1. On the computation of $\\pi$" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### 1.1 Asking the maths library\n", + "## 1.1 Asking the maths library\n", "My computer tells me that $\\pi$ is approximately" ] }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 14, "metadata": {}, "outputs": [ { @@ -44,13 +37,13 @@ "cell_type": "markdown", "metadata": {}, "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" ] }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 15, "metadata": {}, "outputs": [ { @@ -59,7 +52,7 @@ "3.128911138923655" ] }, - "execution_count": 11, + "execution_count": 15, "metadata": {}, "output_type": "execute_result" } @@ -73,9 +66,19 @@ "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", - "execution_count": 12, + "execution_count": 16, "metadata": {}, "outputs": [ { @@ -107,6 +110,34 @@ "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", "execution_count": null, -- 2.18.1