From 812dcb7f23f8a759a28fa1cda25f6b010d1dcc9a Mon Sep 17 00:00:00 2001 From: Jonas Vander Vennet Date: Thu, 28 Jun 2018 10:04:29 +0200 Subject: [PATCH 1/5] Added python3 Monte Carlo integration --- .../monte_carlo/code/python3/monte_carlo.py | 19 +++++++++++++++++++ chapters/monte_carlo/monte_carlo.md | 5 +++++ 2 files changed, 24 insertions(+) create mode 100644 chapters/monte_carlo/code/python3/monte_carlo.py diff --git a/chapters/monte_carlo/code/python3/monte_carlo.py b/chapters/monte_carlo/code/python3/monte_carlo.py new file mode 100644 index 000000000..f4ff71a79 --- /dev/null +++ b/chapters/monte_carlo/code/python3/monte_carlo.py @@ -0,0 +1,19 @@ +import random, math + +# function to determine whether an x, y point is inside a circle with given radius +def in_circle(x, y, radius): + return x**2 + y**2 < radius**2 + +# function to integrate a circle with given radius via monte carlo integration +def monte_carlo(n, radius): + count = 0 + for i in range(n): + count += in_circle(random.uniform(0,radius),random.uniform(0,radius),radius) + estimated_area = 4*(count/n)*radius**2 + exact_area = math.pi*radius**2 + print("percent error: %.10f%%"%(abs(estimated_area-exact_area)/exact_area*100)) + return estimated_area + +#estimate pi by integrating the unit circle +estimated_area = monte_carlo(10**6, 1) +print("Estimation for pi: %.10f"%estimated_area) \ No newline at end of file diff --git a/chapters/monte_carlo/monte_carlo.md b/chapters/monte_carlo/monte_carlo.md index f7e9290a1..761d1927c 100644 --- a/chapters/monte_carlo/monte_carlo.md +++ b/chapters/monte_carlo/monte_carlo.md @@ -41,6 +41,8 @@ each point is tested to see whether it's in the circle or not: [import:2-8, lang:"julia"](code/julia/monte_carlo.jl) {% sample lang="hs" %} [import:7-7, lang:"haskell"](code/haskell/monteCarlo.hs) +{% sample lang="py3" %} +[import:3-5, lang:"python"](code/python3/monte_carlo.py) {% endmethod %} If it's in the circle, we increase an internal count by one, and in the end, @@ -78,6 +80,9 @@ Feel free to submit your version via pull request, and thanks for reading! {% sample lang="hs" %} ### Haskell [import, lang:"haskell"](code/haskell/monteCarlo.hs) +{% sample lang="py3" %} +### Python 3 +[import, lang:"python"](code/python3/monte_carlo.py) {% endmethod %} From 880db16d40046baf359fa4ff390c9f31452ba09f Mon Sep 17 00:00:00 2001 From: Jonas Vander Vennet Date: Thu, 28 Jun 2018 10:07:15 +0200 Subject: [PATCH 2/5] Added myself to contributors.md --- CONTRIBUTORS.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index a1ad4161d..9197efd50 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -5,3 +5,4 @@ Gathros Jeremie Gillet (- Jie -) Salim Khatib Hitesh C +Jonas Vander Vennet From 8fc51f1b26bee0f5ecac08ad8464c661025fc1f0 Mon Sep 17 00:00:00 2001 From: Jonas Vander Vennet Date: Thu, 28 Jun 2018 20:15:34 +0200 Subject: [PATCH 3/5] Relocated print statements, used string formatting --- chapters/monte_carlo/code/python3/monte_carlo.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/chapters/monte_carlo/code/python3/monte_carlo.py b/chapters/monte_carlo/code/python3/monte_carlo.py index f4ff71a79..f6c063cde 100644 --- a/chapters/monte_carlo/code/python3/monte_carlo.py +++ b/chapters/monte_carlo/code/python3/monte_carlo.py @@ -9,11 +9,10 @@ def monte_carlo(n, radius): count = 0 for i in range(n): count += in_circle(random.uniform(0,radius),random.uniform(0,radius),radius) - estimated_area = 4*(count/n)*radius**2 - exact_area = math.pi*radius**2 - print("percent error: %.10f%%"%(abs(estimated_area-exact_area)/exact_area*100)) - return estimated_area + return 4*(count/n)*radius**2 #estimate pi by integrating the unit circle -estimated_area = monte_carlo(10**6, 1) -print("Estimation for pi: %.10f"%estimated_area) \ No newline at end of file +estimated_pi = monte_carlo(10**6, 1) + +print("percent error: {:%}".format(abs(estimated_pi-math.pi)/math.pi)) +print("Estimation for pi: {:f}".format(estimated_pi)) \ No newline at end of file From 42f4e10bf98a449fffd73bd6f5a48a3001481787 Mon Sep 17 00:00:00 2001 From: Jonas Vander Vennet Date: Thu, 28 Jun 2018 20:26:38 +0200 Subject: [PATCH 4/5] Added name to example, as specified in 'How to contribute' --- chapters/monte_carlo/code/python3/monte_carlo.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/chapters/monte_carlo/code/python3/monte_carlo.py b/chapters/monte_carlo/code/python3/monte_carlo.py index f6c063cde..5f0603a05 100644 --- a/chapters/monte_carlo/code/python3/monte_carlo.py +++ b/chapters/monte_carlo/code/python3/monte_carlo.py @@ -1,3 +1,5 @@ +#example submitted by Jonas Vander Vennet, Python 3.5.2 + import random, math # function to determine whether an x, y point is inside a circle with given radius From eb8f6a576eed86a5b79a5ff7a9b6cd4438f13b66 Mon Sep 17 00:00:00 2001 From: Jonas Vander Vennet Date: Thu, 28 Jun 2018 20:27:24 +0200 Subject: [PATCH 5/5] Changed line numbers shown in gitbook --- chapters/monte_carlo/monte_carlo.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/chapters/monte_carlo/monte_carlo.md b/chapters/monte_carlo/monte_carlo.md index 761d1927c..4f484fffd 100644 --- a/chapters/monte_carlo/monte_carlo.md +++ b/chapters/monte_carlo/monte_carlo.md @@ -42,7 +42,7 @@ each point is tested to see whether it's in the circle or not: {% sample lang="hs" %} [import:7-7, lang:"haskell"](code/haskell/monteCarlo.hs) {% sample lang="py3" %} -[import:3-5, lang:"python"](code/python3/monte_carlo.py) +[import:5-7, lang:"python"](code/python3/monte_carlo.py) {% endmethod %} If it's in the circle, we increase an internal count by one, and in the end,