Commit 362372f2 authored by Dorinel Bastide's avatar Dorinel Bastide

Third attempt, cosmetic changes

parent 32a618d0
#+TITLE: On the computation of pi #+TITLE: On the computation of pi
#+AUTHOR: Dorinel Bastide
#+DATE: Today's date
#+LANGUAGE: en #+LANGUAGE: en
# #+PROPERTY: header-args :eval never-export
#+HTML_HEAD: <link rel="stylesheet" type="text/css" href="http://www.pirilampo.org/styles/readtheorg/css/htmlize.css"/> #+HTML_HEAD: <link rel="stylesheet" type="text/css" href="http://www.pirilampo.org/styles/readtheorg/css/htmlize.css"/>
#+HTML_HEAD: <link rel="stylesheet" type="text/css" href="http://www.pirilampo.org/styles/readtheorg/css/readtheorg.css"/> #+HTML_HEAD: <link rel="stylesheet" type="text/css" href="http://www.pirilampo.org/styles/readtheorg/css/readtheorg.css"/>
...@@ -40,14 +37,13 @@ theta = np.random.uniform(size=N, low=0, high=pi/2) ...@@ -40,14 +37,13 @@ theta = np.random.uniform(size=N, low=0, high=pi/2)
: 3.128911138923655 : 3.128911138923655
* Using a surface fraction argument * Using a surface fraction argument
A method that is easier to understand and does not make use of the $\sin$ function is based on the fact that if $X\sim U(0,1)$ and $Y\sim A method that is easier to understand and does not make use of the $\sin$ function is based on the fact that if $X\sim U(0,1)$ and $Y\sim U(0,1)$, then $P[X^2 + Y^2 \leq 1]=\pi/4$ (see [[https://en.wikipedia.org/wiki/Monte_Carlo_method]["Monte Carlo method" on Wikipedia]]). The following ocde uses this approach:
U(0,1)$, then $P[X^2 + Y^2 \leq 1]=\pi/4$ (see [[https://en.wikipedia.org/wiki/Monte_Carlo_method]["Monte Carlo method"
on Wikipedia]]). The following ocde uses this approach:
#+begin_src python :results output file :session :var matplot_lib_filename="C:/Users/Utilisateur/mooc-rr/module2/exo1/PictureRes.png" :exports results #+begin_src python :results output file :var matplot_lib_filename="C:/Users/Utilisateur/mooc-rr/module2/exo1/PictureRes.png" :exports both :session *python*
import matplotlib import matplotlib
matplotlib.use('Agg') matplotlib.use('Agg')
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
import numpy as np
np.random.seed(seed=42) np.random.seed(seed=42)
N = 1000 N = 1000
x = np.random.uniform(size=N, low=0, high=1) x = np.random.uniform(size=N, low=0, high=1)
...@@ -70,10 +66,9 @@ print(matplot_lib_filename) ...@@ -70,10 +66,9 @@ print(matplot_lib_filename)
Type "help", "copyright", "credits" or "license" for more information. Type "help", "copyright", "credits" or "license" for more information.
C:/Users/Utilisateur/mooc-rr/module2/exo1/PictureRes.png]] C:/Users/Utilisateur/mooc-rr/module2/exo1/PictureRes.png]]
It is then straightforward to obtain a (not really good) approximation It is then straightforward to obtain a (not really good) approximation to $\pi$ by counting how many times, on average, $X^2 + Y^2$ is smaller than 1:
to $\pi$ by counting how many times, on average, $X^2 + Y^2$ is smaller
than $1$: #+begin_src python :results output :session *python* :exports both
#+begin_src python :results output :exports both
import numpy as np import numpy as np
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