Comments on: Estimating pi Using Buffon’s Method https://blogs.mathworks.com/loren/2019/08/22/estimating-pi-using-buffons-method/?s_tid=feedtopost Loren Shure is interested in the design of the MATLAB language. She is an application engineer and writes here about MATLAB programming and related topics. Mon, 26 Aug 2019 09:51:48 +0000 hourly 1 https://wordpress.org/?v=6.2.2 By: Loren Shure https://blogs.mathworks.com/loren/2019/08/22/estimating-pi-using-buffons-method/#comment-48226 Mon, 26 Aug 2019 09:51:48 +0000 https://blogs.mathworks.com/loren/?p=3416#comment-48226 @Daniel-

Thanks for the snippet. You can control N by generating a large enough collection by increasing N_rand – but it’s not deterministic hope my tries. Just keep adding more attempts until you reach N suitable ones.

–loren

]]>
By: Daniel M https://blogs.mathworks.com/loren/2019/08/22/estimating-pi-using-buffons-method/#comment-48220 Sat, 24 Aug 2019 13:39:58 +0000 https://blogs.mathworks.com/loren/?p=3416#comment-48220 I was also not very confortable using cosd and sind to calculate pi ;) but you can replace it with:

N_rand = 1000;
x_rand = rand(1, N_rand) - 0.5;
y_rand = rand(1, N_rand) - 0.5;
r_rand = sqrt(x_rand.^2 + y_rand.^2);
ix = r_rand <= 0.5;
cos_rand = x_rand(ix)./r_rand(ix);
sin_rand = y_rand(ix)./r_rand(ix);
N = sum(ix);

The only problem that you will not control N.

Daniel

]]>
By: Loren Shure https://blogs.mathworks.com/loren/2019/08/22/estimating-pi-using-buffons-method/#comment-48218 Sat, 24 Aug 2019 10:49:10 +0000 https://blogs.mathworks.com/loren/?p=3416#comment-48218 @Cris-

OK, thanks for the update!

]]>
By: Cris Luengo https://blogs.mathworks.com/loren/2019/08/22/estimating-pi-using-buffons-method/#comment-48216 Fri, 23 Aug 2019 17:06:52 +0000 https://blogs.mathworks.com/loren/?p=3416#comment-48216 Loren, I was wrong, it looks like your code doesn’t produce a biased estimate. Sorry for the knee-jerk reaction there. :)

This is the same concept, simplified to some extreme. It is equivalent to generating needles of length 2 by selecting a center point in the range x=[-1,1], where it could intersect the line at x=0. Note that the y-axis location is irrelevant!

x = rand(1,N) * 2 - 1;
angs = rand(1,N)*360;
n = sum(abs(x) < abs(cosd(angs)));
results(ii,2) = 2 * N / n;
]]>
By: Loren Shure https://blogs.mathworks.com/loren/2019/08/22/estimating-pi-using-buffons-method/#comment-48214 Thu, 22 Aug 2019 22:49:11 +0000 https://blogs.mathworks.com/loren/?p=3416#comment-48214 @Cris- You are right – we are missing possibilities this way, e.g., going across the corners at a diagonal… I’ll have to think more about that.

–loren

]]>
By: Cris Luengo https://blogs.mathworks.com/loren/2019/08/22/estimating-pi-using-buffons-method/#comment-48212 Thu, 22 Aug 2019 17:00:35 +0000 https://blogs.mathworks.com/loren/?p=3416#comment-48212 You’ve introduced a bias by having all needles start on the right of the leftmost grid line, and on the left of the rightmost one. This experiment is best performed with a single grid line, producing needles that fall within reach.

Though I think it’s silly to use cos and sin to get an estimate of pi… :)

]]>