From 4523f1173798057bb88653e7070bdb15bcc8b809 Mon Sep 17 00:00:00 2001 From: 973469d19e8da3800b283676fe06a1b5 <973469d19e8da3800b283676fe06a1b5@app-learninglab.inria.fr> Date: Fri, 20 Jun 2025 14:42:34 +0000 Subject: [PATCH] entrega 1 --- module2/exo1/toy_notebook_en.ipynb | 131 ++++++++++++++++++++++++++++- 1 file changed, 128 insertions(+), 3 deletions(-) diff --git a/module2/exo1/toy_notebook_en.ipynb b/module2/exo1/toy_notebook_en.ipynb index 0bbbe37..20dd65c 100644 --- a/module2/exo1/toy_notebook_en.ipynb +++ b/module2/exo1/toy_notebook_en.ipynb @@ -1,6 +1,132 @@ { - "cells": [], + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "hideCode": false, + "hidePrompt": false + }, + "source": [ + "# On the computation of $\\pi$" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "hideCode": false, + "hidePrompt": false + }, + "source": [ + "## Asking the maths library" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "hideCode": false, + "hidePrompt": false + }, + "source": [ + "My computer tells me that $\\pi$ *is approximatively*" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3.141592653589793\n" + ] + } + ], + "source": [ + "from math import *\n", + "print(pi)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Buffon’s needle" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "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": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "3.128911138923655" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "import numpy as np\n", + "np.random.seed(seed=42)\n", + "N = 10000\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)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Using a surface fraction argument" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "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 ∼ \\mathcal{U}(0, 1)$$ and $$\\mathcal{Y} ∼ \\mathcal{U}(0, 1)$$, then $$\\mathcal{P}[X^2 + Y^2 ≤ 1] = π/4$$ (see \"[Monte Carlo method]\"(https://en.wikipedia.org/wiki/Monte_Carlo_method)\n", + "on Wikipedia). The following code uses this approach:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + " %matplotlib inline\n", + "import matplotlib.pyplot as plt\n", + "np.random.seed(seed=42)\n", + "N = 1000\n", + "x = np.random.uniform(size=N, low=0, high=1)\n", + "y = np.random.uniform(size=N, low=0, high=1)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], "metadata": { + "hide_code_all_hidden": false, "kernelspec": { "display_name": "Python 3", "language": "python", @@ -16,10 +142,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.3" + "version": "3.6.4" } }, "nbformat": 4, "nbformat_minor": 2 } - -- 2.18.1