Add new file

parent f758cebf
# **1 À propos du calcul de π**
## **1.1 En demandant à la lib maths**
Mon ordinateur m’indique que π vaut _approximativement_
\textcolor{blue}{ln[1]}: \textcolor{green}{from} \textcolor{blue}{math} \textcolor{green}{import *}
\textcolor{green}{print} (pi)
3.141592653589793
## **1.2 En utilisant la **méthode** des aiguilles de Buffon**
Mais calculé avec la **méthode** des ...[\textcolor{blue}{aiguilles de Buffon}](https://fr.wikipedia.org/wiki/Aiguille_de_Buffon)..., on obtiendrait comme **approximation** :
\textcolor{blue}{In [2]}: \textcolor{green}{import} \textcolor{blue}{numpy} \textcolor{green}{as} \textcolor{blue}{np}
np.random.seed(seed\textcolor{grey}{=42})
N = \textcolor{grey}{10000}
x\textcolor{grey}{=} np.random.uniform(size\textcolor{grey}{=}N, low\textcolor{grey}{=0}, high\textcolor{grey}{=1})
theta \textcolor{grey}{=} np.random.uniform(size\textcolor{grey}{=}N, low\textcolor{grey}{=0}, high\textcolor{grey}{=0}pi\textcolor{grey}{/2})
\textcolor{grey}{2/}(\textcolor{green}{sum}((x\textcolor{grey}{+}np\textcolor{grey}{.}sin(theta))\textcolor{grey}{>1})\textcolor{grey}{/}N)
\textcolor{red}{Out[2]:} 3.1289111389236548
##**1.3 Avec un argument "fréquentiel" de surface**
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 ∼ U(0, 1) et Y ∼ U(0, 1) alors P[X2 + Y2 ≤ 1] = π/4 (voir
... [\textcolor{blue}{méthode de Monte Carlo sur Wikipedia}](https://fr.wikipedia.org/wiki/M%C3%A9thode_de_Monte-Carlo#D%C3%A9termination_de_la_valeur_de_%CF%80) .... Le code suivant illustre ce fait :
\textcolor{blue}{ln [3]}\textcolor{grey}{:}\textcolor{green}{%matplotlib} inline
\textcolor{green}{impor} \textcolor{blue}{matplotlib.pyplot} \textcolor{green}{as} \textcolor{blue}{plt}
np.random.seed(seed\textcolor{grey}{=42})
N \textcolor{grey}{= 1000}
x \textcolor{grey}{=} np.random.uniform(size\textcolor{grey}{=}N, low\textcolor{grey}{=0}, high\textcolor{grey}{=1})
y \textcolor{grey}{=} np.random.uniform(size\textcolor{grey}{=}N, low\textcolor{grey}{=0}, high\textcolor{grey}{=1})
accept \textcolor{grey}{=} (x\textcolor{grey}{*}x\textcolor{grey}{+}y\textcolor{grey}{*}y) \textcolor{grey}{<= 1}
reject \textcolor{grey}{=} np.logical_not(accept)
fig, ax \textcolor{grey}{=} plt.subplots(\textcolor{grey}{1})
ax.scatter(x[accept], y[accept], c\textcolor{grey}{=}'b', alpha\textcolor{grey}{=0.2}, edgecolor\textcolor{grey}{=}\textcolor{green}{None=})
ax.scatter(x[reject], y[reject], c\textcolor{grey}{=}'r', alpha\textcolor{grey}{=0.2}, edgecolor\textcolor{grey}{=}\textcolor{green}{None=})
ax.set_aspect(\textcolor{red}{'equal'})
Il est alors aisé d’obtenir une approximation (pas terrible) de π en comptant combien de fois,
en moyenne, X2 + Y2 est inférieur à 1 :
\textcolor{blue}{ln [4]:} \textcolor{grey}{4*}np.mean(accept)
\textcolor{red}{Out[4]:} 3.1120000000000001
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