From b4b560623f979971354a8393b79406474516d9c5 Mon Sep 17 00:00:00 2001 From: Foldster Date: Sun, 10 May 2020 21:08:22 -0400 Subject: [PATCH 1/3] adding matlab implementation to monte carlo integration --- .../code/matlab/monte.m | 20 +++++++++++++++++++ .../monte_carlo_integration.md | 4 ++++ 2 files changed, 24 insertions(+) create mode 100644 contents/monte_carlo_integration/code/matlab/monte.m diff --git a/contents/monte_carlo_integration/code/matlab/monte.m b/contents/monte_carlo_integration/code/matlab/monte.m new file mode 100644 index 000000000..dcd26ffb6 --- /dev/null +++ b/contents/monte_carlo_integration/code/matlab/monte.m @@ -0,0 +1,20 @@ +monte_carlo(10000000); + +function monte_carlo(n) + + % a 2 by n array, rows are xs and ys + xy_array = rand(2, n); + + % square every element in the array + squares_array = xy_array.^2; + + % sum the xs and ys and check if it's in the quarter circle + incircle_array = sum(squares_array)<1; + + % determine the average number of points in the circle + pi_estimate = 4*sum(incircle_array)/n; + + fprintf("The pi estimate is: %f\n", pi_estimate); + fprintf("Percent error is: %f%%", 100 * abs(pi_estimate - pi) / pi); + +end \ No newline at end of file diff --git a/contents/monte_carlo_integration/monte_carlo_integration.md b/contents/monte_carlo_integration/monte_carlo_integration.md index 645fc2315..f6cd18611 100644 --- a/contents/monte_carlo_integration/monte_carlo_integration.md +++ b/contents/monte_carlo_integration/monte_carlo_integration.md @@ -91,6 +91,8 @@ each point is tested to see whether it's in the circle or not: [import:2-10, lang:"bash"](code/bash/monte_carlo.bash) {% sample lang="kotlin" %} [import:3-3, lang:"kotlin"](code/kotlin/MonteCarlo.kt) +{% sample lang="m" %} +[import:5-12, lang:"matlab"](code/matlab/monte.m) {% sample lang="scratch" %}

@@ -193,6 +195,8 @@ Feel free to submit your version via pull request, and thanks for reading! [import, lang:"bash"](code/bash/monte_carlo.bash) {% sample lang="kotlin" %} [import, lang:"kotlin"](code/kotlin/MonteCarlo.kt) +{% sample lang="m" %} +[import, lang:"matlab"](code/matlab/monte.m) {% sample lang="scratch" %} The code snippets were taken from this [scratch project](https://scratch.mit.edu/projects/319610349)

From fb83c6ac3df393be116879301eca4bc3660e10fd Mon Sep 17 00:00:00 2001 From: Foldster Date: Sat, 23 May 2020 23:15:55 -0400 Subject: [PATCH 2/3] having monte carlo function return estimate --- .../monte_carlo_integration/code/matlab/monte.m | 13 +++++++------ .../monte_carlo_integration.md | 2 +- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/contents/monte_carlo_integration/code/matlab/monte.m b/contents/monte_carlo_integration/code/matlab/monte.m index dcd26ffb6..800552d86 100644 --- a/contents/monte_carlo_integration/code/matlab/monte.m +++ b/contents/monte_carlo_integration/code/matlab/monte.m @@ -1,6 +1,9 @@ -monte_carlo(10000000); +pi_estimate = monte_carlo(10000000); -function monte_carlo(n) +fprintf("The pi estimate is: %f\n", pi_estimate); +fprintf("Percent error is: %f%%", 100 * abs(pi_estimate - pi) / pi); + +function pi_estimate=monte_carlo(n) % a 2 by n array, rows are xs and ys xy_array = rand(2, n); @@ -14,7 +17,5 @@ function monte_carlo(n) % determine the average number of points in the circle pi_estimate = 4*sum(incircle_array)/n; - fprintf("The pi estimate is: %f\n", pi_estimate); - fprintf("Percent error is: %f%%", 100 * abs(pi_estimate - pi) / pi); - -end \ No newline at end of file +end + diff --git a/contents/monte_carlo_integration/monte_carlo_integration.md b/contents/monte_carlo_integration/monte_carlo_integration.md index f6cd18611..b6bb2b343 100644 --- a/contents/monte_carlo_integration/monte_carlo_integration.md +++ b/contents/monte_carlo_integration/monte_carlo_integration.md @@ -92,7 +92,7 @@ each point is tested to see whether it's in the circle or not: {% sample lang="kotlin" %} [import:3-3, lang:"kotlin"](code/kotlin/MonteCarlo.kt) {% sample lang="m" %} -[import:5-12, lang:"matlab"](code/matlab/monte.m) +[import:8-15, lang:"matlab"](code/matlab/monte.m) {% sample lang="scratch" %}

From 27cf91fb3a73eaf6edd7128505075afc9aa29143 Mon Sep 17 00:00:00 2001 From: Foldster Date: Sun, 24 May 2020 00:33:03 -0400 Subject: [PATCH 3/3] adding newline to last print statement --- contents/monte_carlo_integration/code/matlab/monte.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contents/monte_carlo_integration/code/matlab/monte.m b/contents/monte_carlo_integration/code/matlab/monte.m index 800552d86..dc0ee8092 100644 --- a/contents/monte_carlo_integration/code/matlab/monte.m +++ b/contents/monte_carlo_integration/code/matlab/monte.m @@ -1,7 +1,7 @@ pi_estimate = monte_carlo(10000000); fprintf("The pi estimate is: %f\n", pi_estimate); -fprintf("Percent error is: %f%%", 100 * abs(pi_estimate - pi) / pi); +fprintf("Percent error is: %f%%\n", 100 * abs(pi_estimate - pi) / pi); function pi_estimate=monte_carlo(n)