no commit message

parent 2de543b1
...@@ -6,20 +6,21 @@ ...@@ -6,20 +6,21 @@
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"import random\n", "import matplotlib.pyplot as plt\n",
"import math\n",
"\n", "\n",
"def buffon_needle(n_drops=100000):\n", "# Exemple : afficher la convergence de l'estimation de π\n",
" hits = 0\n", "import numpy as np\n",
" for _ in range(n_drops):\n",
" y = random.random()\n",
" theta = random.uniform(0, math.pi / 2)\n",
" if y <= (math.sin(theta) / 2):\n",
" hits += 1\n",
" return (2 * n_drops) / hits\n",
"\n", "\n",
"pi_est = buffon_needle()\n", "n_values = np.arange(1000, 20000, 1000)\n",
"print(f\"Estimation de π par la méthode de Buffon : {pi_est}\")\n" "pi_estimations = [buffon_needle(n) for n in n_values]\n",
"\n",
"plt.plot(n_values, pi_estimations)\n",
"plt.axhline(y=math.pi, color='r', linestyle='--', label='π réel')\n",
"plt.xlabel(\"Nombre d'expériences\")\n",
"plt.ylabel(\"Estimation de π\")\n",
"plt.legend()\n",
"plt.title(\"Convergence de la méthode de Buffon\")\n",
"plt.show()\n"
] ]
} }
], ],
......
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