登录论坛

查看完整版本 : Estimating pi Using Buffon’s Method


poster
2019-11-24, 13:22
<header>
<h1>
<a href="https://blogs.mathworks.com/loren/2019/08/22/estimating-pi-using-buffons-method/">Estimating pi Using Buffon’s Method</a>
<span><a href="https://blogs.mathworks.com/loren/2019/08/22/estimating-pi-using-buffons-method/#view_comments">6</a></span></h1>

<p>Posted by <b><a href="https://www.mathworks.com/matlabcentral/profile/authors/136425-loren-shure?s_tid=blg_to_profile">Loren Shure</a></b>, <time>August 22, 2019</time>
</p>
</header>


<div>
<div>
<div><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é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><h3>Contents</h3><div><ul><li><a href="https://blogs.mathworks.com/loren/2019/08/22/estimating-pi-using-buffons-method/?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+mathworks%2Floren+%28Loren+on+the+Art+of+MATLAB%29#5805d265-951e-4fa3-8a6c-27d11f50213d">Set Up Parameters</a></li><li><a href="https://blogs.mathworks.com/loren/2019/08/22/estimating-pi-using-buffons-method/?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+mathworks%2Floren+%28Loren+on+the+Art+of+MATLAB%29#1cfe2015-18ad-4b2f-a4c5-23eb22e510c6">Visualize the Lines</a></li><li><a href="https://blogs.mathworks.com/loren/2019/08/22/estimating-pi-using-buffons-method/?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+mathworks%2Floren+%28Loren+on+the+Art+of+MATLAB%29#e12d864a-df64-4790-8472-4d5593c810ba">Show the Vertical Grid Lines Defined by L Spacing</a></li><li><a href="https://blogs.mathworks.com/loren/2019/08/22/estimating-pi-using-buffons-method/?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+mathworks%2Floren+%28Loren+on+the+Art+of+MATLAB%29#0844cc90-8bfb-4034-9d5e-c81dcf614cdc">Count the Segments Intersecting the Grid</a></li><li><a href="https://blogs.mathworks.com/loren/2019/08/22/estimating-pi-using-buffons-method/?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+mathworks%2Floren+%28Loren+on+the+Art+of+MATLAB%29#e23623de-21fe-454a-93c5-c4ecad9a165d">Annotate Final Plot</a></li><li><a href="https://blogs.mathworks.com/loren/2019/08/22/estimating-pi-using-buffons-method/?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+mathworks%2Floren+%28Loren+on+the+Art+of+MATLAB%29#c1df259b-620c-48e3-a782-371be5a8b27d">What Happens as L and N change?</a></li></ul></div><h4>Set Up Parameters<a></a></h4><p>How many line segments?</p><pre>N = 1000;
</pre><p>Length of each line?</p><pre>L = 0.20;
</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>xb = L + rand(1,N)*(1-2*L);
yb = L + rand(1,N)*(1-2*L);
angs = rand(1,N)*360;
xe = xb + L*cosd(angs);
ye = yb + L*sind(angs);
</pre><h4>Visualize the Lines<a></a></h4><pre>ax = axes;
plot(ax,[xb;xe],[yb;ye])
axis <span>square</span>
</pre><img src="https://blogs.mathworks.com/images/loren/2019/estPI_01.png"/> <h4>Show the Vertical Grid Lines Defined by L Spacing<a></a></h4><pre>hold <span>on</span>
glines = 0:L:1;
<span>for</span> i = 1:length(glines)
xline(ax, glines(i));
<span>end</span>
</pre><img src="https://blogs.mathworks.com/images/loren/2019/estPI_02.png"/> <h4>Count the Segments Intersecting the Grid<a></a></h4><pre>n = sum(floor(xb/L) ~= floor(xe/L));
piEstimate = 2 * N / n
</pre><pre>piEstimate =
3.1153
</pre><h4>Annotate Final Plot<a></a></h4><pre>title(<span>"Estimate of \pi is "</span> + piEstimate)
</pre><img src="https://blogs.mathworks.com/images/loren/2019/estPI_03.png"/> <h4>What Happens as L and N change?<a></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><p><br/><a><span>Get the MATLAB code </span></a><br/><br/>
Published with MATLAB® R2019a<br/></p></div>




</div>
</div>


</div>


More... (http://feedproxy.google.com/~r/mathworks/loren/~3/9H782fbwtTQ/)