Commit 25820baf authored by Victor-M-Gomes's avatar Victor-M-Gomes

Commit exo1 module2

parent 9a17825f
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"> <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head> <head>
<!-- 2020-04-28 mar. 11:54 --> <!-- 2020-04-28 mar. 12:01 -->
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" /> <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" /> <meta name="viewport" content="width=device-width, initial-scale=1" />
<title>On the computation of pi</title> <title>On the computation of pi</title>
...@@ -248,17 +248,17 @@ ...@@ -248,17 +248,17 @@
<h2>Table of Contents</h2> <h2>Table of Contents</h2>
<div id="text-table-of-contents"> <div id="text-table-of-contents">
<ul> <ul>
<li><a href="#org6dcdf57">1. Asking the math library</a></li> <li><a href="#org20331b2">1. Asking the math library</a></li>
<li><a href="#orga3f9d3b">2. * Buffon&rsquo;s needle</a></li> <li><a href="#org0a446f5">2. * Buffon&rsquo;s needle</a></li>
<li><a href="#org41c13a8">3. Using a surface fraction argument</a></li> <li><a href="#orgc15380a">3. Using a surface fraction argument</a></li>
</ul> </ul>
</div> </div>
</div> </div>
<div id="outline-container-org6dcdf57" class="outline-2"> <div id="outline-container-org20331b2" class="outline-2">
<h2 id="org6dcdf57"><span class="section-number-2">1</span> Asking the math library</h2> <h2 id="org20331b2"><span class="section-number-2">1</span> Asking the math library</h2>
<div class="outline-text-2" id="text-1"> <div class="outline-text-2" id="text-1">
<p> <p>
My computer tells me that &pi; is approximatively My computer tells me that \(\pi\) is <i>approximatively</i>
</p> </p>
<div class="org-src-container"> <div class="org-src-container">
...@@ -273,8 +273,8 @@ pi ...@@ -273,8 +273,8 @@ pi
</div> </div>
</div> </div>
<div id="outline-container-orga3f9d3b" class="outline-2"> <div id="outline-container-org0a446f5" class="outline-2">
<h2 id="orga3f9d3b"><span class="section-number-2">2</span> * Buffon&rsquo;s needle</h2> <h2 id="org0a446f5"><span class="section-number-2">2</span> * Buffon&rsquo;s needle</h2>
<div class="outline-text-2" id="text-2"> <div class="outline-text-2" id="text-2">
<p> <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> 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>
...@@ -295,11 +295,11 @@ np.random.seed(seed=<span style="color: #da8548; font-weight: bold;">42</span>) ...@@ -295,11 +295,11 @@ np.random.seed(seed=<span style="color: #da8548; font-weight: bold;">42</span>)
</div> </div>
</div> </div>
<div id="outline-container-org41c13a8" class="outline-2"> <div id="outline-container-orgc15380a" class="outline-2">
<h2 id="org41c13a8"><span class="section-number-2">3</span> Using a surface fraction argument</h2> <h2 id="orgc15380a"><span class="section-number-2">3</span> Using a surface fraction argument</h2>
<div class="outline-text-2" id="text-3"> <div class="outline-text-2" id="text-3">
<p> <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: 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 \sim U(0,1)\) and \(Y \sim 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&rdquo; on Wikipedia</a>). The following code uses this approach:
</p> </p>
<div class="org-src-container"> <div class="org-src-container">
<pre class="src src-python"><span style="color: #51afef;">import</span> matplotlib.pyplot <span style="color: #51afef;">as</span> plt <pre class="src src-python"><span style="color: #51afef;">import</span> matplotlib.pyplot <span style="color: #51afef;">as</span> plt
...@@ -330,7 +330,7 @@ plt.savefig(matplot_lib_filename) ...@@ -330,7 +330,7 @@ plt.savefig(matplot_lib_filename)
<p> <p>
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: 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:
</p> </p>
<div class="org-src-container"> <div class="org-src-container">
<pre class="src src-python"><span style="color: #da8548; font-weight: bold;">4</span>*np.mean(accept) <pre class="src src-python"><span style="color: #da8548; font-weight: bold;">4</span>*np.mean(accept)
...@@ -345,7 +345,7 @@ It is then straightforward to obtain a (not really good) approximation to &pi; b ...@@ -345,7 +345,7 @@ It is then straightforward to obtain a (not really good) approximation to &pi; b
</div> </div>
<div id="postamble" class="status"> <div id="postamble" class="status">
<p class="author">Author: Victor Martins Gomes</p> <p class="author">Author: Victor Martins Gomes</p>
<p class="date">Created: 2020-04-28 mar. 11:54</p> <p class="date">Created: 2020-04-28 mar. 12:01</p>
</div> </div>
</body> </body>
</html> </html>
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
#+LANGUAGE: en #+LANGUAGE: en
# #+PROPERTY: header-args :session :exports both # #+PROPERTY: header-args :session :exports both
* Asking the math library * Asking the math library
My computer tells me that \pi is approximatively My computer tells me that $\pi$ is /approximatively/
#+begin_src python :results value :session *python* :exports both #+begin_src python :results value :session *python* :exports both
from math import * from math import *
...@@ -27,7 +27,7 @@ theta = np.random.uniform(size=N, low=0, high=pi/2) ...@@ -27,7 +27,7 @@ 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 \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: 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 code uses this approach:
#+begin_src python :results output file :var matplot_lib_filename="figure_pi_mc2.png" :exports both :session *python* #+begin_src python :results output file :var matplot_lib_filename="figure_pi_mc2.png" :exports both :session *python*
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
...@@ -52,7 +52,7 @@ print(matplot_lib_filename) ...@@ -52,7 +52,7 @@ print(matplot_lib_filename)
[[file:figure_pi_mc2.png]] [[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: 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 #+begin_src python :results output :session *python* :exports both
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