diff --git a/module2/exo1/matplot_lib_filename.png b/module2/exo1/matplot_lib_filename.png new file mode 100644 index 0000000000000000000000000000000000000000..be9756d48ef2ece9d72dc7b1c5c6063803a0b425 Binary files /dev/null and b/module2/exo1/matplot_lib_filename.png differ diff --git a/module2/exo1/toy_document_orgmode_python_en.html b/module2/exo1/toy_document_orgmode_python_en.html index 837878be5426b1ee2589e305c54edb33fa6c1027..db0c385fa399f12698af7ab8e38c62d429d85757 100644 --- a/module2/exo1/toy_document_orgmode_python_en.html +++ b/module2/exo1/toy_document_orgmode_python_en.html @@ -4,7 +4,7 @@ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> - + On the computation of pi @@ -244,67 +244,98 @@

On the computation of pi

-
    -
  1. Asking the math library + +
    +

    1 1 Asking the math library

    +
    +

    My computer tells me that π is approximatively -#beginsrc python :results output :exports both -from math import * +

    + +
    +
    from math import *
     pi
    -#endsrc
  2. + +
+ + -
  • * Buffon’s needle +
    +

    2 2 * Buffon’s needle

    +
    +

    Applying the method of Buffon’s needle, we get the approximation -#beginsrc python :results output :exports both -import numpy as np -np.random.seed(seed=42) -N = 10000 -x = np.random.uniform(size=N, low=0, high=1) -theta = np.random.uniform(size=N, low)0, high=pi/2) -2/(sum((x+np.sin(theta))>1)/N) -#endsrc

  • -
  • -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 \approx U(0,1)\) and \(Y \approx U(0,1)\), then \(P[X^2 + Y^2 \leq 1] = \pi/4\) (see “Monte Carlo method on Wikipedia”). The following code uses this approach: - #beginsrc python :results output :exports both -import matplotlib.pyplot as plt

    +
    +
    import numpy as np
    +np.random.seed(seed=42)
    +N = 10000
    +x = np.random.uniform(size=N, low=0, high=1)
    +theta = np.random.uniform(size=N, low=0, high=pi/2)
    +2/(sum((x+np.sin(theta))>1)/N)
    +
    +
    + + +
    +

    3 3. Using a surface fraction argument

    +

    -np.random.seed(seed=42) -N = 1000 -x = np.random.uniform(size=N, low=0, high=1) -y = np.random.uniform(size=N, low=0, high=1) +A method that is easier to understand and does not make use of the sin function is based on the fact that if \(X \approx U(0,1)\) and \(Y \approx U(0,1)\), then \(P[X^2 + Y^2 \leq 1] = \pi/4\) (see “Monte Carlo method on Wikipedia”). The following code uses this approach:

    +
    +
    import matplotlib.pyplot as plt
     
    -

    -accept = (x*x+y*y) <= 1 -reject = np.logicalnot(accept) -

    +np.random.seed(seed=42) +N = 1000 +x = np.random.uniform(size=N, low=0, high=1) +y = np.random.uniform(size=N, low=0, high=1) -

    -fig, ax = plt.subplots(1) -ax.scatter(x[accept], y[accept], c=’b’, alpha=0.2, edgecolor=None) -ax.scatter(x[reject], y[reject], c=’r’, alpha=0.2, edgecolor=None) -ax.setaspect(’equal’) -

    +accept = (x*x+y*y) <= 1 +reject = np.logical_not(accept) + +fig, ax = plt.subplots(1) +ax.scatter(x[accept], y[accept], c='b', alpha=0.2, edgecolor=None) +ax.scatter(x[reject], y[reject], c='r', alpha=0.2, edgecolor=None) +ax.set_aspect('equal') + +plt.savefig('matplot_lib_filename') +print('matplot_lib_filename') +
    +
    + +
    +matplot_lib_filename
    +
    -

    -plt.savefig(matplotlibfilename) -print(matplotlibfilename) -#endsrc -

    It is then straightforward to obtain a (not really good) approximation to π by counting how many times, on average, \(X^2 + Y^2\) is smaller than 1: -#beginsrc python :results output :exports both -4*np.mean(accept) -#endsrc -

  • - +

    +
    +
    4*np.mean(accept)
    +
    +
    + +
    +3.112
    +
    + +

    Author: Victor Martins Gomes

    -

    Created: 2020-04-27 lun. 20:01

    +

    Created: 2020-04-28 mar. 11:29

    diff --git a/module2/exo1/toy_document_orgmode_python_en.org b/module2/exo1/toy_document_orgmode_python_en.org index e903cb060504cb8afbadbc60c087c82dad316a45..9ff0230e07da6b0a96a431d01ada3673487de2d5 100644 --- a/module2/exo1/toy_document_orgmode_python_en.org +++ b/module2/exo1/toy_document_orgmode_python_en.org @@ -1,46 +1,68 @@ #+TITLE: On the computation of pi #+LANGUAGE: en -# #+PROPERTY: header-args :eval never-export -1. Asking the math library - My computer tells me that \pi is approximatively - #begin_src python :results output :exports both - from math import * - pi - #end_src - -2. * Buffon's needle - 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 - import numpy as np - np.random.seed(seed=42) - N = 10000 - x = np.random.uniform(size=N, low=0, high=1) - theta = np.random.uniform(size=N, low)0, high=pi/2) - 2/(sum((x+np.sin(theta))>1)/N) - #end_src -3. 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 \approx U(0,1)$ and $Y \approx 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 code uses this approach: - #begin_src python :results output :exports both - import matplotlib.pyplot as plt - - np.random.seed(seed=42) - N = 1000 - x = np.random.uniform(size=N, low=0, high=1) - y = np.random.uniform(size=N, low=0, high=1) - - accept = (x*x+y*y) <= 1 - reject = np.logical_not(accept) - - fig, ax = plt.subplots(1) - ax.scatter(x[accept], y[accept], c='b', alpha=0.2, edgecolor=None) - ax.scatter(x[reject], y[reject], c='r', alpha=0.2, edgecolor=None) - ax.set_aspect('equal') - - plt.savefig(matplot_lib_filename) - print(matplot_lib_filename) - #end_src - - 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: - #begin_src python :results output :exports both - 4*np.mean(accept) - #end_src +# #+PROPERTY: header-args :session :exports both +* 1 Asking the math library +My computer tells me that \pi is approximatively + +#+begin_src python :results value :session *python* :exports both +from math import * +pi +#+end_src + +#+RESULTS: +: 3.141592653589793 + +* 2 * Buffon's needle +Applying the method of [[https://en.wikipedia.org/wiki/Buffon%27s_needle_problem][Buffon's needle]], we get the *approximation* +#+begin_src python :results value :session :*python* :exports both +import numpy as np +np.random.seed(seed=42) +N = 10000 +x = np.random.uniform(size=N, low=0, high=1) +theta = np.random.uniform(size=N, low=0, high=pi/2) +2/(sum((x+np.sin(theta))>1)/N) +#+end_src + +#+RESULTS: +: 3.128911138923655 + +* 3. 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 \approx U(0,1)$ and $Y \approx 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 code uses this approach: +#+begin_src python :results output file :var matplot_lib_filename="figure_pi_mc2.png" :session *python* :exports both +import matplotlib.pyplot as plt + +np.random.seed(seed=42) +N = 1000 +x = np.random.uniform(size=N, low=0, high=1) +y = np.random.uniform(size=N, low=0, high=1) + +accept = (x*x+y*y) <= 1 +reject = np.logical_not(accept) + +fig, ax = plt.subplots(1) +ax.scatter(x[accept], y[accept], c='b', alpha=0.2, edgecolor=None) +ax.scatter(x[reject], y[reject], c='r', alpha=0.2, edgecolor=None) +ax.set_aspect('equal') + +plt.savefig(matplot_lib_filename) +print(matplot_lib_filename) +#+end_src + + #+RESULTS: + [[file:Traceback (most recent call last): + File "", line 1, in + File "/tmp/babel-ekv1Vl/python-2ZIXqL", line 4, in + np.random.seed(seed=42) + NameError: name 'np' is not defined]] + File "", line 1, in + File "/tmp/babel-ekv1Vl/python-xlXPMX", line 4, in + np.random.seed(seed=42) + NameError: name 'np' is not defined]] + +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: +#+begin_src python :results output :session *python* :exports both +4*np.mean(accept) +#+end_src + + #+RESULTS: + : 3.112