Commit eba360d9 authored by Antoine's avatar Antoine

fix: exo1

parent d572847f
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="fr" xml:lang="fr"> <html xmlns="http://www.w3.org/1999/xhtml" lang="fr" xml:lang="fr">
<head> <head>
<!-- 2024-06-04 Tue 14:12 --> <!-- 2024-06-04 Tue 14:30 -->
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" /> <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" /> <meta name="viewport" content="width=device-width, initial-scale=1" />
<title>À propos du calcul de $\pi$</title> <title>À propos du calcul de $\pi$</title>
...@@ -239,21 +239,21 @@ ...@@ -239,21 +239,21 @@
<h2>Table des matières</h2> <h2>Table des matières</h2>
<div id="text-table-of-contents" role="doc-toc"> <div id="text-table-of-contents" role="doc-toc">
<ul> <ul>
<li><a href="#org1250c3a">1. En demandant à la lib maths</a></li> <li><a href="#org005f76c">1. En demandant à la lib maths</a></li>
<li><a href="#orgceb2502">2. En utilisant la méthode des aiguilles de Buffon</a></li> <li><a href="#orgbbee086">2. En utilisant la méthode des aiguilles de Buffon</a></li>
<li><a href="#org7fa4014">3. Avec un argument "fréquentiel" de surface</a></li> <li><a href="#org1625aa3">3. Avec un argument "fréquentiel" de surface</a></li>
</ul> </ul>
</div> </div>
</div> </div>
<div id="outline-container-org1250c3a" class="outline-2"> <div id="outline-container-org005f76c" class="outline-2">
<h2 id="org1250c3a"><span class="section-number-2">1.</span> En demandant à la lib maths</h2> <h2 id="org005f76c"><span class="section-number-2">1.</span> En demandant à la lib maths</h2>
<div class="outline-text-2" id="text-1"> <div class="outline-text-2" id="text-1">
<p> <p>
Mon ordinateur m'indique que \(\pi\) vaut <i>approximativement</i>: Mon ordinateur m'indique que \(\pi\) vaut <i>approximativement</i>:
</p> </p>
<div class="org-src-container"> <div class="org-src-container">
<pre class="src src-python">from math import pi <pre class="src src-python"><span style="color: #b877db;">from</span> math <span style="color: #b877db;">import</span> pi
pi pi
</pre> </pre>
</div> </div>
...@@ -263,20 +263,20 @@ pi ...@@ -263,20 +263,20 @@ pi
</pre> </pre>
</div> </div>
</div> </div>
<div id="outline-container-orgceb2502" class="outline-2"> <div id="outline-container-orgbbee086" class="outline-2">
<h2 id="orgceb2502"><span class="section-number-2">2.</span> En utilisant la méthode des aiguilles de Buffon</h2> <h2 id="orgbbee086"><span class="section-number-2">2.</span> En utilisant la méthode des aiguilles de Buffon</h2>
<div class="outline-text-2" id="text-2"> <div class="outline-text-2" id="text-2">
<p> <p>
Mais calculé avec la <b>méthode</b> des <a href="https://fr.wikipedia.org/wiki/Aiguille_de_Buffon">aiguilles de Buffon</a>, on obtiendrait comme <b>approximation</b> : Mais calculé avec la <b>méthode</b> des <a href="https://fr.wikipedia.org/wiki/Aiguille_de_Buffon">aiguilles de Buffon</a>, on obtiendrait comme <b>approximation</b> :
</p> </p>
<div class="org-src-container"> <div class="org-src-container">
<pre class="src src-python">import numpy as np <pre class="src src-python"><span style="color: #b877db;">import</span> numpy <span style="color: #b877db;">as</span> np
np.random.seed(seed=42) np.random.seed(seed=42)
N = 10000 <span style="color: #e95678;">N</span> = 10000
x = np.random.uniform(size=N, low=0, high=1) <span style="color: #e95678;">x</span> = np.random.uniform(size=N, low=0, high=1)
theta = np.random.uniform(size=N, low=0, high=pi/2) <span style="color: #e95678;">theta</span> = np.random.uniform(size=N, low=0, high=pi/2)
2/(sum((x+np.sin(theta))&gt;1)/N) 2/(<span style="color: #b877db;">sum</span>((x+np.sin(theta))&gt;1)/N)
</pre> </pre>
</div> </div>
...@@ -285,35 +285,40 @@ theta = np.random.uniform(size=N, low=0, high=pi/2) ...@@ -285,35 +285,40 @@ theta = np.random.uniform(size=N, low=0, high=pi/2)
</pre> </pre>
</div> </div>
</div> </div>
<div id="outline-container-org7fa4014" class="outline-2"> <div id="outline-container-org1625aa3" class="outline-2">
<h2 id="org7fa4014"><span class="section-number-2">3.</span> Avec un argument "fréquentiel" de surface</h2> <h2 id="org1625aa3"><span class="section-number-2">3.</span> Avec un argument "fréquentiel" de surface</h2>
<div class="outline-text-2" id="text-3"> <div class="outline-text-2" id="text-3">
<p> <p>
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 <a href="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</a>). Le code suivant illustre ce fait : 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 <a href="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</a>). Le code suivant illustre ce fait :
</p> </p>
<div class="org-src-container"> <div class="org-src-container">
<pre class="src src-python">import matplotlib.pyplot as plt <pre class="src src-python"><span style="color: #b877db;">import</span> matplotlib.pyplot <span style="color: #b877db;">as</span> plt
np.random.seed(seed=42) np.random.seed(seed=42)
N = 1000 <span style="color: #e95678;">N</span> = 1000
x = np.random.uniform(size=N, low=0, high=1) <span style="color: #e95678;">x</span> = np.random.uniform(size=N, low=0, high=1)
y = np.random.uniform(size=N, low=0, high=1) <span style="color: #e95678;">y</span> = np.random.uniform(size=N, low=0, high=1)
accept = (x*x+y*y) &lt;= 1 <span style="color: #e95678;">accept</span> = (x*x+y*y) &lt;= 1
reject = np.logical_not(accept) <span style="color: #e95678;">reject</span> = np.logical_not(accept)
fig, ax = plt.subplots(1) <span style="color: #e95678;">fig</span>, <span style="color: #e95678;">ax</span> = plt.subplots(1)
ax.scatter(x[accept], y[accept], c='b', alpha=0.2, edgecolor=None) ax.scatter(x[accept], y[accept], c=<span style="color: #fab795;">'b'</span>, alpha=0.2, edgecolor=<span style="color: #f09383;">None</span>)
ax.scatter(x[reject], y[reject], c='r', alpha=0.2, edgecolor=None) ax.scatter(x[reject], y[reject], c=<span style="color: #fab795;">'r'</span>, alpha=0.2, edgecolor=<span style="color: #f09383;">None</span>)
ax.set_aspect('equal') ax.set_aspect(<span style="color: #fab795;">'equal'</span>)
plt.savefig("figure.png") plt.savefig(matplot_lib_filename)
"figure.png" <span style="color: #b877db;">print</span>(matplot_lib_filename)
</pre> </pre>
</div> </div>
<div id="org1940497" class="figure">
<p><img src="figure_pi_mc2.png" alt="figure_pi_mc2.png" />
</p>
</div>
<p> <p>
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 : 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 :
</p> </p>
...@@ -332,7 +337,7 @@ Il est alors aisé d'obtenir une approximation (pas terrible) de &pi; en comptan ...@@ -332,7 +337,7 @@ Il est alors aisé d'obtenir une approximation (pas terrible) de &pi; en comptan
<div id="postamble" class="status"> <div id="postamble" class="status">
<p class="date">Date: 2024-06-04 Tue 00:00</p> <p class="date">Date: 2024-06-04 Tue 00:00</p>
<p class="author">Auteur: Antoine Geimer</p> <p class="author">Auteur: Antoine Geimer</p>
<p class="date">Created: 2024-06-04 Tue 14:12</p> <p class="date">Created: 2024-06-04 Tue 14:30</p>
<p class="validation"><a href="https://validator.w3.org/check?uri=referer">Validate</a></p> <p class="validation"><a href="https://validator.w3.org/check?uri=referer">Validate</a></p>
</div> </div>
</body> </body>
......
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
Mon ordinateur m'indique que $\pi$ vaut /approximativement/: 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 from math import pi
pi pi
#+end_src #+end_src
...@@ -27,7 +27,7 @@ pi ...@@ -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* : 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 import numpy as np
np.random.seed(seed=42) np.random.seed(seed=42)
N = 10000 N = 10000
...@@ -43,7 +43,7 @@ theta = np.random.uniform(size=N, low=0, high=pi/2) ...@@ -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 : 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 import matplotlib.pyplot as plt
np.random.seed(seed=42) np.random.seed(seed=42)
...@@ -59,16 +59,16 @@ ax.scatter(x[accept], y[accept], c='b', alpha=0.2, edgecolor=None) ...@@ -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.scatter(x[reject], y[reject], c='r', alpha=0.2, edgecolor=None)
ax.set_aspect('equal') ax.set_aspect('equal')
plt.savefig("figure.png") plt.savefig(matplot_lib_filename)
"figure.png" print(matplot_lib_filename)
#+end_src #+end_src
#+RESULTS: #+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 : 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) 4*np.mean(accept)
#+end_src #+end_src
......
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