Second try

parent e7a50142
#+TITLE: À propos du calcul de \Pi #+TITLE: À propos du calcul de $\pi$
#+AUTHOR: Konrad Jinsen
#+DATE: 2019-03-28 Thu 11:06
#+LANGUAGE: fr #+LANGUAGE: fr
# Table des matières #+HTML_HEAD: <link rel="stylesheet" type="text/css" href="http://www.pirilampo.org/styles/readtheorg/css/htmlize.css"/> #+LANGUAGE: fr
#+HTML_HEAD: <link rel="stylesheet" type="text/css" href="http://www.pirilampo.org/styles/readtheorg/css/readtheorg.css"/>
#+HTML_HEAD: <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> # Table des matières
#+HTML_HEAD: <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
#+HTML_HEAD: <script type="text/javascript" src="http://www.pirilampo.org/styles/lib/js/jquery.stickytableheaders.js"></script> # 1 En demandant à la lib maths
#+HTML_HEAD: <script type="text/javascript" src="http://www.pirilampo.org/styles/readtheorg/js/readtheorg.js"></script>
#+PROPERTY: header-args :session :exports both
# 1 En demandant à la lib maths * Table des matières
* En demandant à la lib maths
Mon ordinateur m'indique que \Pi vaut /approximativement/: Mon ordinateur m'indique que $\pi$ vaut /approximativement/:
#+begin_src python :results output :session :exports both #+begin_src python :results value :session *python* :exports both
from math import * from math import *
print(pi) pi
#+end_src #+end_src
#+RESULTS: #+RESULTS:
: Python 3.8.5 (default, Jul 28 2020, 12:59:40)
: [GCC 9.3.0] on linux
: Type "help", "copyright", "credits" or "license" for more information.
: 3.141592653589793 : 3.141592653589793
# 2 En utilisant la méthode des aiguilles de Buffon * En utilisant la méthode des aiguilles de Buffon
Mais calculé avec la *méthode* des [[https://fr.wikipedia.org/wiki/Aiguille_de_Buffon][aiguilles de Buffon]], on comme Mais calculé avec la *méthode* des [[https://fr.wikipedia.org/wiki/Aiguille_de_Buffon][aiguilles de Buffon]], on obtiendrait comme
*approximation* : *approximation* :
#+begin_src python :results output :session :exports both #+begin_src python :results value :session *python* :exports both
import numpy as np import numpy as np
np.random.seed(seed=42) np.random.seed(seed=42)
N = 10000 N = 10000
x = np.random.uniform(size=N, low=0, high=1) x = np.random.uniform(size=N, low=0, high=1)
theta = np.random.uniform(size=N, low=0, high=pi/2) theta = np.random.uniform(size=N, low=0, high=pi/2)
print(2/(sum((x+np.sin(theta))>1)/N)) 2/(sum((x+np.sin(theta))>1)/N)
#+end_src #+end_src
#+RESULTS: #+RESULTS:
: 3.128911138923655 : 3.128911138923655
# 3 Avec un argument "fréquentiel" de surface * Avec un argument "fréquentiel" de surface
Sinon, une méthode plus simple à comprendre et ne faisant pas Sinon, une méthode plus simple à comprendre et ne faisant pas
intervenir à la fonction sinus se base sur le fait que si $X \sim intervenir à la fonction sinus se base sur le fait que si $X \sim
\mathcal{U}(0, 1)$ et $Y \sim U(0, 1)$ alors $P[X^2 + Y^2 \leq 1] = \pi/4$ U(0, 1)$ et $Y \sim U(0, 1)$ alors $P[X^2 + Y^2 \leq 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 Wikipedia]]). Le code suivant illustre (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 Wikipedia]]). Le code suivant illustre
ce fait : ce fait :
...@@ -66,13 +69,17 @@ print(matplot_lib_filename) ...@@ -66,13 +69,17 @@ print(matplot_lib_filename)
#+end_src #+end_src
#+RESULTS: #+RESULTS:
[[file:<matplotlib.collections.PathCollection object at 0x7f78d3efb190>]]
Il est alors aiso d'obtenir une approximation (pas terrible) de \pi en Il est alors aisé d'obtenir une approximation (pas terrible) de $\pi$ en
compant combien de fois, en moyenne, X^2 + Y^2 est inférieur à 1: comptant combien de fois, en moyenne, $X^2 + Y^2$ est inférieur à 1:
#+begin_src python :results output :session :exports both #+begin_src python :results value :session :exports both
4 * np.mean(accept) 4 * np.mean(accept)
#+end_src #+end_src
#+RESULTS: #+RESULTS:
: 3.112 : 3.112
#+DATE: 2019-03-28 Thu 11:06
#+AUTHOR: Konrad Jinsen
\ No newline at end of file
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