diff --git a/module2/exo1/toy_document_orgmode_R_fr.org b/module2/exo1/toy_document_orgmode_R_fr.org index 1bb8f61f1d11b486ceb724afcdd14d11e5329545..38c829ccb55c49ff07b87fc8449784c5cfbc7afc 100644 --- a/module2/exo1/toy_document_orgmode_R_fr.org +++ b/module2/exo1/toy_document_orgmode_R_fr.org @@ -1,6 +1,5 @@ -#+TITLE: Votre titre -#+AUTHOR: Votre nom -#+DATE: La date du jour +#+TITLE: À propos du calcul de \pi +#+AUTHOR: Antoine Richard #+LANGUAGE: fr # #+PROPERTY: header-args :eval never-export @@ -11,7 +10,7 @@ #+HTML_HEAD: #+HTML_HEAD: -* Quelques explications +* Quelques explications :noexport: Ceci est un document org-mode avec quelques exemples de code R. Une fois ouvert dans emacs, ce document peut aisément être @@ -82,3 +81,60 @@ faisant ~1)) +#+end_src + +#+RESULTS: +: +: [1] 3.14327 + +* 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 +qui si $X \sim U(0,1)$ et $Y \sim U(0,1)$ alors $P[X^2 + Y^2 \le 1] = \pi/4$ +(voir la [[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: + +#+begin_src R :results output graphics :file (org-babel-temp-file "figure" ".png") :exports both :width 600 :height 400 :session *R* +set.seed(42) +N = 1000 +df = data.frame(X = runif(N), Y = runif(N)) +df$Accept = (df$X**2 + df$Y**2 <= 1) +library(ggplot2) +ggplot(df, aes(x=X,y=Y,color=Accept)) + geom_point(alpha=.2) + coord_fixed() + theme_bw() +#+end_src + +#+RESULTS: +[[file:/tmp/babel-PuC3cb/figureA0UR4O.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 R :results output :session *R* :exports both +4*mean(df$Accept) +#+end_src + +#+RESULTS: +: [1] 3.156