From fb5344912c35b0aa6314d4fca6e63fa9283af843 Mon Sep 17 00:00:00 2001 From: f50a28a8c82bf67ad820bc82fe37aad2 Date: Fri, 4 Jul 2025 16:27:09 +0000 Subject: [PATCH] Update toy_notebook_fr.ipynb --- module2/exo1/toy_notebook_fr.ipynb | 92 +++++++++++++++++++++++++----- 1 file changed, 79 insertions(+), 13 deletions(-) diff --git a/module2/exo1/toy_notebook_fr.ipynb b/module2/exo1/toy_notebook_fr.ipynb index cf231d9..aefdfdb 100644 --- a/module2/exo1/toy_notebook_fr.ipynb +++ b/module2/exo1/toy_notebook_fr.ipynb @@ -1,5 +1,80 @@ -`{ - "cells": [], +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# À propos de π\n", + "\n", + "Dans ce mini-notebook, nous rappelons deux représentations classiques de π :\n", + "\n", + "$$ \\pi = \\frac{C}{D} $$ \n", + "$$ \\displaystyle \\pi = 4 \\sum_{k=0}^{\\infty} \\frac{(-1)^k}{2k+1} . $$\n", + "\n", + "La célèbre expérience des [aiguilles de Buffon](https://fr.wikipedia.org/wiki/Aiguille_de_Buffon) permet aussi d’estimer π de manière probabiliste." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import math\n", + "math.pi" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import random, math\n", + "\n", + "random.seed(42) # pour reproduire la même estimation\n", + "N = 100_000 # nombre de lancers\n", + "l, d = 1.0, 1.0 # longueur de l’aiguille et espacement des lignes\n", + "\n", + "cross = 0\n", + "for _ in range(N):\n", + " x = random.uniform(0, d/2) # distance centre-ligne la plus proche\n", + " theta = random.uniform(0, math.pi/2) # angle avec la verticale\n", + " if x <= (l/2) * math.sin(theta):\n", + " cross += 1\n", + "\n", + "pi_est = (2 * l * N) / (cross * d)\n", + "pi_est" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import matplotlib.pyplot as plt\n", + "\n", + "def buffon_once(n):\n", + " \"\"\"Une estimation de π par Buffon sur n lancers\"\"\"\n", + " crosses = sum(\n", + " random.uniform(0, d/2) <= (l/2) * math.sin(random.uniform(0, math.pi/2))\n", + " for _ in range(n)\n", + " )\n", + " return (2 * l * n) / (crosses * d)\n", + "\n", + "random.seed(0)\n", + "ests = [buffon_once(2_000) for _ in range(1_000)]\n", + "\n", + "plt.hist(ests, bins=30)\n", + "plt.axvline(math.pi, linestyle='--')\n", + "plt.title(\"Distribution des estimations de π (méthode de Buffon)\")\n", + "plt.xlabel(\"π estimé\")\n", + "plt.ylabel(\"Fréquence\")\n", + "plt.show()" + ] + } + ], "metadata": { "kernelspec": { "display_name": "Python 3", @@ -7,19 +82,10 @@ "name": "python3" }, "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.6.3" + "version": "3.10" } }, "nbformat": 4, - "nbformat_minor": 2 + "nbformat_minor": 5 } - -- 2.18.1