Skip to content

Commit 0d2542d

Browse files
RodolfoFerroLogan1x
authored andcommitted
Approximation of pi and plotting example (Logan1x#11)
* Uploaded pi approximation and plotting example scripts. Updated README. * Updated README. * Previous status. * Uploaded pi approximation and plotting example scripts. Updated README.
1 parent 38b25bf commit 0d2542d

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

README.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,25 @@ Directly prints results if run directly.
111111
May also be imported, yielding results one by one.
112112

113113

114+
### Approximating *pi*
115+
116+
This script is useful to show a way to approximate the value of pi using a Monte Carlo method. It is also optimized using the `@jit` (*just-in-time*) decorator from the [numba](https://numba.pydata.org/) library.
117+
118+
To see different approximations you just need to modify the argument passed to the main function.
119+
120+
```bash
121+
python pi.py
122+
```
123+
124+
125+
### Plotting a function
126+
127+
This script contains an example of plotting a function using [`matplotlib`](http://matplotlib.org/). Feel free to modify the value of `y` to obtain different functions that depend on `x`.
128+
129+
```bash
130+
python plot_example.py
131+
```
132+
114133
## Release History
115134

116135
* 0.0.1
@@ -141,4 +160,4 @@ The following people helped in creating the above content.
141160
* <a href="https://github.com/KayvanMazaheri" target="_blank">Kayvan Mazaheri</a>
142161
* <a href="https://github.com/kalbhor" target="_blank">Lakshay Kalbhor</a>
143162
* <a href="https://github.com/Pradhvan">Pradhvan Bisht</a>
144-
* <a href="https://github.com/toonarmycaptain" target="_blank">David Antonini</a>
163+
* <a href="https://github.com/toonarmycaptain" target="_blank">David Antonini</a>

pi.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import numpy as np
2+
from numba import jit
3+
4+
@jit
5+
def aprox_pi(N):
6+
points = 2 * np.random.rand(N, 2) - 1
7+
M = 0
8+
9+
for k in range(N):
10+
if points[k,0]**2 + points[k,1]**2 < 1.:
11+
M += 1
12+
13+
return 4.*M/N
14+
15+
print(aprox_pi(1e8))

plot_example.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
4+
x = np.linspace(-3, 3, 100)
5+
y = x**2
6+
7+
plt.plot(x, y)
8+
plt.show()

0 commit comments

Comments
 (0)