Commit 32a618d0 authored by Dorinel Bastide's avatar Dorinel Bastide

Proceeded to changes following comparison with solution

parent d4b18a45
...@@ -11,42 +11,36 @@ ...@@ -11,42 +11,36 @@
#+HTML_HEAD: <script type="text/javascript" src="http://www.pirilampo.org/styles/lib/js/jquery.stickytableheaders.js"></script> #+HTML_HEAD: <script type="text/javascript" src="http://www.pirilampo.org/styles/lib/js/jquery.stickytableheaders.js"></script>
#+HTML_HEAD: <script type="text/javascript" src="http://www.pirilampo.org/styles/readtheorg/js/readtheorg.js"></script> #+HTML_HEAD: <script type="text/javascript" src="http://www.pirilampo.org/styles/readtheorg/js/readtheorg.js"></script>
* Table of Contents #+PROPERTY: header-args :session :exports both
1. Asking the math libraryo * Asking the math library
2. * Buffon's needle
3. Using a surface fraction argument
* 1 Asking the math library
My computer tells me that $\pi$ is /approximatively/ My computer tells me that $\pi$ is /approximatively/
#+begin_src python :results output :exports both #+begin_src python :results value :session *python* :exports both
import math from math import *
print(math.pi) pi
#+end_src #+end_src
#+RESULTS: #+RESULTS:
: 3.141592653589793 : 3.141592653589793
* 2 * Buffon's needle * * Buffon's needle
Applying the method of [[https://en.wikipedia.org/wiki/Buffon%27s_needle_problem][Buffon's needle]], we get the approximation Applying the method of [[https://en.wikipedia.org/wiki/Buffon%27s_needle_problem][Buffon's needle]], we get the *approximation*
#+begin_src python :results output :exports both #+begin_src python :results value :session *python* :exports both
import math
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=math.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. Using a surface fraction argument * Using a surface fraction argument
A method that is easier to understand and does not make use of the 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
$\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" 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: 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 :session :var matplot_lib_filename="C:/Users/Utilisateur/mooc-rr/module2/exo1/PictureRes.png" :exports results
......
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