Labfans是一个针对大学生、工程师和科研工作者的技术社区。 论坛首页 | 联系我们(Contact Us)
MATLAB爱好者论坛-LabFans.com
返回   MATLAB爱好者论坛-LabFans.com > 其它 > 资料存档 > MATLAB技术文章
MATLAB技术文章 MATLAB Technical Articles From Mathworks
回复
 
主题工具 显示模式
旧 2019-11-24, 13:22   #1
poster
高级会员
 
注册日期: 2019-11-21
帖子: 3,006
声望力: 66
poster 正向着好的方向发展
默认 Estimating pi Using Buffon’s Method



Estimating pi Using Buffon’s Method
6



Posted by Loren Shure,







I recently attended the ICIAM meeting in Valencia, Spain which meant I got to hang out with my pals Carlos Sanchis and Lucas Garcia :-)! Carlos showed me a problem he was working with Professor Fernando Giménez from UPV regarding an app for estimating $\pi$ using Buffon's method. Here's the problem statement from Wikipedia:

Suppose we have a floor made of parallel strips of wood, each the same width, and we drop a needle onto the floor. What is the probability that the needle will lie across a line between two strips?

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.

Contents

Set Up Parameters

How many line segments?

N = 1000;

Length of each line?

L = 0.20;

We want the beginning points of the lines to lie between L and 1-L so we don't go outside the unit square.

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);

Visualize the Lines

ax = axes;
plot(ax,[xb;xe],[yb;ye])
axis square

Show the Vertical Grid Lines Defined by L Spacing

hold on
glines = 0:L:1;
for i = 1:length(glines)
xline(ax, glines(i));
end

Count the Segments Intersecting the Grid

n = sum(floor(xb/L) ~= floor(xe/L));
piEstimate = 2 * N / n
piEstimate =
3.1153

Annotate Final Plot

title("Estimate of \pi is " + piEstimate)

What Happens as L and N change?

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 here.


Get the MATLAB code


Published with MATLAB® R2019a













More...
poster 当前离线   回复时引用此帖
回复


发帖规则
不可以发表新主题
不可以发表回复
不可以上传附件
不可以编辑自己的帖子

启用 BB 代码
论坛启用 表情符号
论坛启用 [IMG] 代码
论坛启用 HTML 代码



所有时间均为北京时间。现在的时间是 19:53


Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.