cannonball approximation to pi
This year, my daughter started writing algorithms in her math class (she is in seconde, which could correspond to the 10th grade). The one she had to write down last weekend was Buffon’s neddle and the approximation of π by Monte Carlo (throwing cannon balls was not mentioned!). Here is the short R code I later wrote to show her the outcome (as the class has not yet learned a computer language):
n=10^6 counter=0 #uniforms over the unit square ray=runif(n)^2+runif(n)^2 #proportion within the quarter circle conv=cumsum((ray<1))/(1:n) plot(conv,type="l",col="steelblue",ylim=c(pi/4-2/sqrt(n), pi/4+2/sqrt(n)),xlab="n",ylab="proportion") abline(h=pi/4,col="gold3")
and here is an outcome of the convergence of the approximation to π/4:
October 29, 2011 at 4:25 am
Why is it
?
October 29, 2011 at 7:12 am
– if you mean the occurrence of a < instead of < into the code, thanks, I just corrected this HTML issue;
– if you mean why I only take rays less than 1 it is because my circle has a radius of 1;
– if you mean why I use cumsum, it is to show the convergence of the estimator as n increases…
October 28, 2011 at 5:59 pm
This is cool. For younger kids, there’s a nice activity on determination of the area of a penny by Monte Carlo methods at the Jefferson Nat’l Lab.
http://education.jlab.org/beamsactivity/6thgrade/differentwayofmeasuring/index.html
October 9, 2011 at 12:00 pm
Actually, they started programming their calculator in a sort of Basic. However, the teacher has not addressed this exercise so far, maybe because she does not want to cover graphical commands that early.