diff --git a/module2/exo1/figure_pi_mc2.png b/module2/exo1/figure_pi_mc2.png new file mode 100644 index 0000000000000000000000000000000000000000..5d13d04f3d63e9bba543bc7280110e9f30d21065 Binary files /dev/null and b/module2/exo1/figure_pi_mc2.png differ diff --git a/module2/exo1/toy_document_orgmode_python_fr.html b/module2/exo1/toy_document_orgmode_python_fr.html index 4e576415fd041d7875d16fb6a0d309a6b9cd4ff5..489d76a8dd8fc43003ba06f873f80d25a9df3033 100644 --- a/module2/exo1/toy_document_orgmode_python_fr.html +++ b/module2/exo1/toy_document_orgmode_python_fr.html @@ -3,7 +3,7 @@ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
- +Mon ordinateur m'indique que \(\pi\) vaut approximativement:
from math import pi +from math import pi pi
Mais calculé avec la méthode des aiguilles de Buffon, on obtiendrait comme approximation :
import numpy as np +import numpy as np np.random.seed(seed=42) -N = 10000 -x = np.random.uniform(size=N, low=0, high=1) -theta = np.random.uniform(size=N, low=0, high=pi/2) -2/(sum((x+np.sin(theta))>1)/N) +N = 10000 +x = np.random.uniform(size=N, low=0, high=1) +theta = np.random.uniform(size=N, low=0, high=pi/2) +2/(sum((x+np.sin(theta))>1)/N)
Sinon, une méthode plus simple à comprendre et ne faisant pas intervenir d'appel à la fonction sinus se base sur le fait que si \(X \sim U(0,1)\) et \(Y \sim U(0,1)\) alors \(P[X^2 + Y² \le 1] = \pi/4\) (voir méthode de Monte Carlo sur Wikipédia). Le code suivant illustre ce fait :
import matplotlib.pyplot as plt +import matplotlib.pyplot as plt np.random.seed(seed=42) -N = 1000 -x = np.random.uniform(size=N, low=0, high=1) -y = np.random.uniform(size=N, low=0, high=1) +N = 1000 +x = np.random.uniform(size=N, low=0, high=1) +y = np.random.uniform(size=N, low=0, high=1) -accept = (x*x+y*y) <= 1 -reject = np.logical_not(accept) +accept = (x*x+y*y) <= 1 +reject = np.logical_not(accept) -fig, ax = plt.subplots(1) -ax.scatter(x[accept], y[accept], c='b', alpha=0.2, edgecolor=None) -ax.scatter(x[reject], y[reject], c='r', alpha=0.2, edgecolor=None) -ax.set_aspect('equal') +fig, ax = plt.subplots(1) +ax.scatter(x[accept], y[accept], c='b', alpha=0.2, edgecolor=None) +ax.scatter(x[reject], y[reject], c='r', alpha=0.2, edgecolor=None) +ax.set_aspect('equal') -plt.savefig("figure.png") -"figure.png" +plt.savefig(matplot_lib_filename) +print(matplot_lib_filename)
+
Il est alors aisé d'obtenir une approximation (pas terrible) de π en comptant combien de fois, en moyenne, \(X^2 + Y^2\) est inférieur à 1 :
@@ -332,7 +337,7 @@ Il est alors aisé d'obtenir une approximation (pas terrible) de π en comptan diff --git a/module2/exo1/toy_document_orgmode_python_fr.org b/module2/exo1/toy_document_orgmode_python_fr.org index 30014573bb2208e0e85e9522ffc7b507bfb138ec..4d2e491d3a17c5e2c4c0179360b7d41c28efd66c 100644 --- a/module2/exo1/toy_document_orgmode_python_fr.org +++ b/module2/exo1/toy_document_orgmode_python_fr.org @@ -15,7 +15,7 @@ Mon ordinateur m'indique que $\pi$ vaut /approximativement/: -#+begin_src python :session :results value :exports both +#+begin_src python :session *python* :results value :exports both from math import pi pi #+end_src @@ -27,7 +27,7 @@ pi Mais calculé avec la *méthode* des [[https://fr.wikipedia.org/wiki/Aiguille_de_Buffon][aiguilles de Buffon]], on obtiendrait comme *approximation* : -#+begin_src python :session :results value :exports both +#+begin_src python :session *python* :results value :exports both import numpy as np np.random.seed(seed=42) N = 10000 @@ -43,7 +43,7 @@ theta = np.random.uniform(size=N, low=0, high=pi/2) Sinon, une méthode plus simple à comprendre et ne faisant pas intervenir d'appel à la fonction sinus se base sur le fait que si $X \sim U(0,1)$ et $Y \sim U(0,1)$ alors $P[X^2 + Y² \le 1] = \pi/4$ (voir [[https://fr.wikipedia.org/wiki/M%C3%A9thode_de_Monte-Carlo#D%C3%A9termination_de_la_valeur_de_%CF%80][méthode de Monte Carlo sur Wikipédia]]). Le code suivant illustre ce fait : -#+begin_src python :session :results file link :export none +#+begin_src python :results output file :var matplot_lib_filename="figure_pi_mc2.png" :exports both :session *python* import matplotlib.pyplot as plt np.random.seed(seed=42) @@ -59,16 +59,16 @@ ax.scatter(x[accept], y[accept], c='b', alpha=0.2, edgecolor=None) ax.scatter(x[reject], y[reject], c='r', alpha=0.2, edgecolor=None) ax.set_aspect('equal') -plt.savefig("figure.png") -"figure.png" +plt.savefig(matplot_lib_filename) +print(matplot_lib_filename) #+end_src #+RESULTS: -[[file:figure.png]] +[[file:figure_pi_mc2.png]] Il est alors aisé d'obtenir une approximation (pas terrible) de \pi en comptant combien de fois, en moyenne, $X^2 + Y^2$ est inférieur à 1 : -#+begin_src python :session :results value :exports both +#+begin_src python :session *python* :results value :exports both 4*np.mean(accept) #+end_src