Commit 9a17825f authored by Victor-M-Gomes's avatar Victor-M-Gomes

Commit exo1 module2

parent b8727620
......@@ -4,7 +4,7 @@
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<!-- 2020-04-28 mar. 11:29 -->
<!-- 2020-04-28 mar. 11:54 -->
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>On the computation of pi</title>
......@@ -248,14 +248,14 @@
<h2>Table of Contents</h2>
<div id="text-table-of-contents">
<ul>
<li><a href="#orgec675a6">1. 1 Asking the math library</a></li>
<li><a href="#org9cc90ed">2. 2 * Buffon&rsquo;s needle</a></li>
<li><a href="#org8060f5c">3. 3. Using a surface fraction argument</a></li>
<li><a href="#org6dcdf57">1. Asking the math library</a></li>
<li><a href="#orga3f9d3b">2. * Buffon&rsquo;s needle</a></li>
<li><a href="#org41c13a8">3. Using a surface fraction argument</a></li>
</ul>
</div>
</div>
<div id="outline-container-orgec675a6" class="outline-2">
<h2 id="orgec675a6"><span class="section-number-2">1</span> 1 Asking the math library</h2>
<div id="outline-container-org6dcdf57" class="outline-2">
<h2 id="org6dcdf57"><span class="section-number-2">1</span> Asking the math library</h2>
<div class="outline-text-2" id="text-1">
<p>
My computer tells me that &pi; is approximatively
......@@ -266,11 +266,15 @@ My computer tells me that &pi; is approximatively
pi
</pre>
</div>
<pre class="example">
3.141592653589793
</pre>
</div>
</div>
<div id="outline-container-org9cc90ed" class="outline-2">
<h2 id="org9cc90ed"><span class="section-number-2">2</span> 2 * Buffon&rsquo;s needle</h2>
<div id="outline-container-orga3f9d3b" class="outline-2">
<h2 id="orga3f9d3b"><span class="section-number-2">2</span> * Buffon&rsquo;s needle</h2>
<div class="outline-text-2" id="text-2">
<p>
Applying the method of <a href="https://en.wikipedia.org/wiki/Buffon%27s_needle_problem">Buffon&rsquo;s needle</a>, we get the <b>approximation</b>
......@@ -284,11 +288,15 @@ np.random.seed(seed=<span style="color: #da8548; font-weight: bold;">42</span>)
<span style="color: #da8548; font-weight: bold;">2</span>/(<span style="color: #c678dd;">sum</span>((x+np.sin(theta))&gt;<span style="color: #da8548; font-weight: bold;">1</span>)/N)
</pre>
</div>
<pre class="example">
3.128911138923655
</pre>
</div>
</div>
<div id="outline-container-org8060f5c" class="outline-2">
<h2 id="org8060f5c"><span class="section-number-2">3</span> 3. Using a surface fraction argument</h2>
<div id="outline-container-org41c13a8" class="outline-2">
<h2 id="org41c13a8"><span class="section-number-2">3</span> Using a surface fraction argument</h2>
<div class="outline-text-2" id="text-3">
<p>
A method that is easier to understand and does not make use of the <code>sin</code> 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 <a href="https://en.wikipedia.org/wiki/Monte_Carlo_method">&ldquo;Monte Carlo method on Wikipedia&rdquo;</a>). The following code uses this approach:
......@@ -309,14 +317,16 @@ ax.scatter(x[accept], y[accept], c=<span style="color: #98be65;">'b'</span>, alp
ax.scatter(x[reject], y[reject], c=<span style="color: #98be65;">'r'</span>, alpha=<span style="color: #da8548; font-weight: bold;">0.2</span>, edgecolor=<span style="color: #a9a1e1;">None</span>)
ax.set_aspect(<span style="color: #98be65;">'equal'</span>)
plt.savefig(<span style="color: #98be65;">'matplot_lib_filename'</span>)
<span style="color: #51afef;">print</span>(<span style="color: #98be65;">'matplot_lib_filename'</span>)
plt.savefig(matplot_lib_filename)
<span style="color: #51afef;">print</span>(matplot_lib_filename)
</pre>
</div>
<pre class="example">
matplot_lib_filename
</pre>
<div class="figure">
<p><img src="figure_pi_mc2.png" alt="figure_pi_mc2.png" />
</p>
</div>
<p>
......@@ -335,7 +345,7 @@ It is then straightforward to obtain a (not really good) approximation to &pi; b
</div>
<div id="postamble" class="status">
<p class="author">Author: Victor Martins Gomes</p>
<p class="date">Created: 2020-04-28 mar. 11:29</p>
<p class="date">Created: 2020-04-28 mar. 11:54</p>
</div>
</body>
</html>
#+TITLE: On the computation of pi
#+LANGUAGE: en
# #+PROPERTY: header-args :session :exports both
* 1 Asking the math library
* Asking the math library
My computer tells me that \pi is approximatively
#+begin_src python :results value :session *python* :exports both
......@@ -12,9 +12,9 @@ pi
#+RESULTS:
: 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*
#+begin_src python :results value :session :*python* :exports both
#+begin_src python :results value :session *python* :exports both
import numpy as np
np.random.seed(seed=42)
N = 10000
......@@ -26,9 +26,9 @@ theta = np.random.uniform(size=N, low=0, high=pi/2)
#+RESULTS:
: 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 =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
#+begin_src python :results output file :var matplot_lib_filename="figure_pi_mc2.png" :exports both :session *python*
import matplotlib.pyplot as plt
np.random.seed(seed=42)
......@@ -49,15 +49,8 @@ print(matplot_lib_filename)
#+end_src
#+RESULTS:
[[file:Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/tmp/babel-ekv1Vl/python-2ZIXqL", line 4, in <module>
np.random.seed(seed=42)
NameError: name 'np' is not defined]]
File "<stdin>", line 1, in <module>
File "/tmp/babel-ekv1Vl/python-xlXPMX", line 4, in <module>
np.random.seed(seed=42)
NameError: name 'np' is not defined]]
[[file:figure_pi_mc2.png]]
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
......
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