diff --git a/README.md b/README.md index 37389ba..3355b90 100644 --- a/README.md +++ b/README.md @@ -111,6 +111,25 @@ Directly prints results if run directly. May also be imported, yielding results one by one. +### Approximating *pi* + +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. + +To see different approximations you just need to modify the argument passed to the main function. + +```bash +python pi.py +``` + + +### Plotting a function + +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`. + +```bash +python plot_example.py +``` + ## Release History * 0.0.1 @@ -141,4 +160,4 @@ The following people helped in creating the above content. * Kayvan Mazaheri * Lakshay Kalbhor * Pradhvan Bisht -* David Antonini \ No newline at end of file +* David Antonini diff --git a/pi.py b/pi.py new file mode 100644 index 0000000..4500089 --- /dev/null +++ b/pi.py @@ -0,0 +1,15 @@ +import numpy as np +from numba import jit + +@jit +def aprox_pi(N): + points = 2 * np.random.rand(N, 2) - 1 + M = 0 + + for k in range(N): + if points[k,0]**2 + points[k,1]**2 < 1.: + M += 1 + + return 4.*M/N + +print(aprox_pi(1e8)) diff --git a/plot_example.py b/plot_example.py new file mode 100644 index 0000000..4aec0a2 --- /dev/null +++ b/plot_example.py @@ -0,0 +1,8 @@ +import numpy as np +import matplotlib.pyplot as plt + +x = np.linspace(-3, 3, 100) +y = x**2 + +plt.plot(x, y) +plt.show()