{"id":3416,"date":"2019-08-22T08:37:13","date_gmt":"2019-08-22T13:37:13","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/?p=3416"},"modified":"2019-08-24T05:51:10","modified_gmt":"2019-08-24T10:51:10","slug":"estimating-pi-using-buffons-method","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2019\/08\/22\/estimating-pi-using-buffons-method\/","title":{"rendered":"Estimating pi Using Buffon&#8217;s Method"},"content":{"rendered":"<div class=\"content\"><!--introduction--><p>I recently attended the ICIAM meeting in Valencia, Spain which meant I got to hang out with my pals Carlos Sanchis and <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/1296922-lucas-garc%C3%ADa\">Lucas Garcia<\/a> :-)!  Carlos showed me a problem he was working with Professor Fernando Gim&eacute;nez from UPV regarding an app for estimating $\\pi$ using <a href=\"https:\/\/en.wikipedia.org\/wiki\/Buffon%27s_needle_problem\">Buffon's method<\/a>. Here's the problem statement from Wikipedia:<\/p><p><i>Suppose we have a floor made of parallel strips of wood, each the same width, and we drop a needle onto the floor.<\/i> <i>What is the probability that the needle will lie across a line between two strips?<\/i><\/p><p>Interesting that the original intention had nothing to do with computing $\\pi$ !  There's some fun, powerful, yet fairly easy code to demonstrate the algorithm.<\/p><!--\/introduction--><h3>Contents<\/h3><div><ul><li><a href=\"#5805d265-951e-4fa3-8a6c-27d11f50213d\">Set Up Parameters<\/a><\/li><li><a href=\"#1cfe2015-18ad-4b2f-a4c5-23eb22e510c6\">Visualize the Lines<\/a><\/li><li><a href=\"#e12d864a-df64-4790-8472-4d5593c810ba\">Show the Vertical Grid Lines Defined by L Spacing<\/a><\/li><li><a href=\"#0844cc90-8bfb-4034-9d5e-c81dcf614cdc\">Count the Segments Intersecting the Grid<\/a><\/li><li><a href=\"#e23623de-21fe-454a-93c5-c4ecad9a165d\">Annotate Final Plot<\/a><\/li><li><a href=\"#c1df259b-620c-48e3-a782-371be5a8b27d\">What Happens as L and N change?<\/a><\/li><\/ul><\/div><h4>Set Up Parameters<a name=\"5805d265-951e-4fa3-8a6c-27d11f50213d\"><\/a><\/h4><p>How many line segments?<\/p><pre class=\"codeinput\">N = 1000;\r\n<\/pre><p>Length of each line?<\/p><pre class=\"codeinput\">L = 0.20;\r\n<\/pre><p>We want the beginning points of the lines to lie between L and 1-L so we don't go outside the unit square.<\/p><pre class=\"codeinput\">xb = L + rand(1,N)*(1-2*L);\r\nyb = L + rand(1,N)*(1-2*L);\r\nangs = rand(1,N)*360;\r\nxe = xb + L*cosd(angs);\r\nye = yb + L*sind(angs);\r\n<\/pre><h4>Visualize the Lines<a name=\"1cfe2015-18ad-4b2f-a4c5-23eb22e510c6\"><\/a><\/h4><pre class=\"codeinput\">ax = axes;\r\nplot(ax,[xb;xe],[yb;ye])\r\naxis <span class=\"string\">square<\/span>\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/2019\/estPI_01.png\" alt=\"\"> <h4>Show the Vertical Grid Lines Defined by L Spacing<a name=\"e12d864a-df64-4790-8472-4d5593c810ba\"><\/a><\/h4><pre class=\"codeinput\">hold <span class=\"string\">on<\/span>\r\nglines = 0:L:1;\r\n<span class=\"keyword\">for<\/span> i = 1:length(glines)\r\n   xline(ax, glines(i));\r\n<span class=\"keyword\">end<\/span>\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/2019\/estPI_02.png\" alt=\"\"> <h4>Count the Segments Intersecting the Grid<a name=\"0844cc90-8bfb-4034-9d5e-c81dcf614cdc\"><\/a><\/h4><pre class=\"codeinput\">n = sum(floor(xb\/L) ~= floor(xe\/L));\r\npiEstimate = 2 * N \/ n\r\n<\/pre><pre class=\"codeoutput\">piEstimate =\r\n    3.1153\r\n<\/pre><h4>Annotate Final Plot<a name=\"e23623de-21fe-454a-93c5-c4ecad9a165d\"><\/a><\/h4><pre class=\"codeinput\">title(<span class=\"string\">\"Estimate of \\pi is \"<\/span> + piEstimate)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/2019\/estPI_03.png\" alt=\"\"> <h4>What Happens as L and N change?<a name=\"c1df259b-620c-48e3-a782-371be5a8b27d\"><\/a><\/h4><p>This could be a great exercise for the classroom - seeing how the estimates depend on how many line segments and the spacing of the grid. Not to mention running a bunch of times with different random numbers each time.  What simple estimation problems do you like to use?  Let me know <a href=\"https:\/\/blogs.mathworks.com\/loren\/?p=3416#respond\">here<\/a>.<\/p><script language=\"JavaScript\"> <!-- \r\n    function grabCode_f0fb3f13c680488dbcbba4837bed6338() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='f0fb3f13c680488dbcbba4837bed6338 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' f0fb3f13c680488dbcbba4837bed6338';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        copyright = 'Copyright 2019 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add copyright line at the bottom if specified.\r\n        if (copyright.length > 0) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n\r\n        d.title = title + ' (MATLAB code)';\r\n        d.close();\r\n    }   \r\n     --> <\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_f0fb3f13c680488dbcbba4837bed6338()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n      the MATLAB code <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; R2019a<br><\/p><\/div><!--\r\nf0fb3f13c680488dbcbba4837bed6338 ##### SOURCE BEGIN #####\r\n%% Estimating pi Using Buffon's Method\r\n% I recently attended the ICIAM meeting in Valencia, Spain which meant I\r\n% got to hang out with my pals Carlos Sanchis and\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/1296922-lucas-garc%C3%ADa\r\n% Lucas Garcia> :-)!  Carlos showed me a problem he was working with\r\n% Professor Fernando Gim\u00c3\u00a9nez from UPV regarding an app for estimating $\\pi$\r\n% using <https:\/\/en.wikipedia.org\/wiki\/Buffon%27s_needle_problem Buffon's\r\n% method>. Here's the problem statement from Wikipedia:\r\n% \r\n% _Suppose we have a floor made of parallel strips of wood, each the same width, and we drop a needle onto the floor._\r\n% _What is the probability that the needle will lie across a line between two strips?_\r\n%\r\n% Interesting that the original intention had nothing to do with computing\r\n% $\\pi$ !  There's some fun, powerful, yet fairly easy code to demonstrate\r\n% the algorithm.\r\n%% Set Up Parameters\r\n% How many line segments?\r\nN = 1000;\r\n%%\r\n% Length of each line?\r\nL = 0.20;\r\n%%\r\n% We want the beginning points of the lines to lie between L and 1-L so we\r\n% don't go outside the unit square.\r\nxb = L + rand(1,N)*(1-2*L);\r\nyb = L + rand(1,N)*(1-2*L);\r\nangs = rand(1,N)*360;\r\nxe = xb + L*cosd(angs);\r\nye = yb + L*sind(angs);\r\n%% Visualize the Lines\r\nax = axes;\r\nplot(ax,[xb;xe],[yb;ye])\r\naxis square\r\n%% Show the Vertical Grid Lines Defined by L Spacing\r\nhold on\r\nglines = 0:L:1;\r\nfor i = 1:length(glines)\r\n   xline(ax, glines(i));\r\nend\r\n%% Count the Segments Intersecting the Grid\r\nn = sum(floor(xb\/L) ~= floor(xe\/L));\r\npiEstimate = 2 * N \/ n\r\n%% Annotate Final Plot\r\ntitle(\"Estimate of \\pi is \" + piEstimate)\r\n%% What Happens as L and N change?\r\n% This could be a great exercise for the classroom - seeing how the\r\n% estimates depend on how many line segments and the spacing of the grid.\r\n% Not to mention running a bunch of times with different random numbers\r\n% each time.  What simple estimation problems do you like to use?  Let me\r\n% know <https:\/\/blogs.mathworks.com\/loren\/?p=3416#respond here>.\r\n##### SOURCE END ##### f0fb3f13c680488dbcbba4837bed6338\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/2019\/estPI_03.png\" onError=\"this.style.display ='none';\" \/><\/div><!--introduction--><p>I recently attended the ICIAM meeting in Valencia, Spain which meant I got to hang out with my pals Carlos Sanchis and <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/1296922-lucas-garc%C3%ADa\">Lucas Garcia<\/a> :-)!  Carlos showed me a problem he was working with Professor Fernando Gim&eacute;nez from UPV regarding an app for estimating $\\pi$ using <a href=\"https:\/\/en.wikipedia.org\/wiki\/Buffon%27s_needle_problem\">Buffon's method<\/a>. Here's the problem statement from Wikipedia:... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2019\/08\/22\/estimating-pi-using-buffons-method\/\">read more >><\/a><\/p>","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[33],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/3416"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/users\/39"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/comments?post=3416"}],"version-history":[{"count":8,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/3416\/revisions"}],"predecessor-version":[{"id":3434,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/3416\/revisions\/3434"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=3416"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=3416"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=3416"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}