diff --git a/module2/exo1/toy_notebook_fr.ipynb b/module2/exo1/toy_notebook_fr.ipynb index 06ae3e830892f678e2eba9e368ad992e225a8d61..96689e29dd4d9ecbd10f124317e384db4100109f 100644 --- a/module2/exo1/toy_notebook_fr.ipynb +++ b/module2/exo1/toy_notebook_fr.ipynb @@ -6,20 +6,21 @@ "metadata": {}, "outputs": [], "source": [ - "import random\n", - "import math\n", + "import matplotlib.pyplot as plt\n", "\n", - "def buffon_needle(n_drops=100000):\n", - " hits = 0\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", + "# Exemple : afficher la convergence de l'estimation de π\n", + "import numpy as np\n", "\n", - "pi_est = buffon_needle()\n", - "print(f\"Estimation de π par la méthode de Buffon : {pi_est}\")\n" + "n_values = np.arange(1000, 20000, 1000)\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" ] } ],