Update toy_notebook_fr.ipynb

parent bae6a1f4
`{
"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
}
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