Michael S. Czahor
Introduction
Buffon's Needle is one of the oldest problems in the field of geometrical probability. It was first stated in 1777. It involves dropping a needle on a lined sheet of paper and determining the probability of the needle crossing one of the lines on the page. The remarkable result is that the probability is directly related to the value of pi.This R program will estimate the value of pi based on the situation talked about in the above paragraph
Part A
For part A we are essentially asked to created a data frame that produces random values on 3 different intervals which will represent the ranges of x, y, and our respected angle for each needle drop. This is an easy to implement randomized number situation which calls for the use of the runif function. This function asks for the amount of numbers followed by an interval. Once numbers are generated we will save our values to a data frame.
rneedle <- function(n) {
x = runif(n, 0, 5)
y = runif(n,0, 1)
angle = runif(n,-pi, pi) #angle from -180 to 180
values<-data.frame(cbind(x, y, angle))
return(values)
}
values<-rneedle(50)
#check that a 50 by 3 matrix is generated
values
#Our dataframe has been successfully produced.
Part B
Part B asks us to plot the needles from part one. After reading up on buffons needle it is important to not that there can be more than 2 horizontal lines in this problem. This is a simplified version which gives us a small area to examine to get the general idea of the geometric properties being portrayed here. With that being said let us note that we decided to expand the graph by 1 unit in each direction. The reason for this is imagine a needle started with its tail at y=1 with an angle of pi/2. We need to assume that the range is up to 2 in that direction. The also applies for the other 3 situations on the other 3 sides of the Graph.
plotneedle<-function(bufplot){
bufplot$x1=bufplot$x-0.5*cos(bufplot$angle)
bufplot$y1=bufplot$y-0.5*sin(bufplot$angle)
bufplot$x2=bufplot$x+0.5*cos(bufplot$angle)
bufplot$y2=bufplot$y+0.5*sin(bufplot$angle)
qplot(data=bufplot, x=x1, y=y1, xend=x2, yend=y2, geom="segment", xlab="Randomly generated number on the interval 0 to 5",
ylab="Midpoint Distance from nearest Horizontal Line", xlim=c(-1, 6), ylim=c(-1, 2)) + geom_hline(yintercept=c(0,1), colour="steelblue")
}
plotneedle(values)
Part C
Below you will see the estimates for pi based on the reading of Buffons Needle and the knowledge of basic geometric principles. It is important to not that the more needles we drop the more accurate our estimate may become.
buffon<-function(q){
amount<-(q$y<=0.5*sin(q$angle))|(q$y>1-0.5*sin(q$angle))
perc<-nrow(q)/sum(amount)
return(perc)
}
buffon(values)
Part D
Part D simply asks us to run a given code that responds to previously programmed functions from the above code. After running the code we received the following answer. > buffon(X)[1] 3.846154
set.seed(10312013)
X <- rneedle(50)
plotneedle(X)
buffon(X)
Part E
As mentioned in previous sections, when dropping more needles we expect to get a more accurate answer with minimized variability. After running the code from our Approxpi function we received the values of mean=3.172314 and the variance 0.04751391. For such a simple experiment I find these results to be quite eye opening to the intution that people from hundreds of years ago had. This is a remarkable finding that has gave a great estimate of pi with minimized variability for higher orders of needle drops.
Approxpi<-function(n){
range<-rep(NA, n)
for (i in 1:n){ range[i]=buffon(rneedle(500))}
return(range)
}
Approxpi(500)
mean(Approxpi(500))
var(Approxpi(500))
References from Illinois Website
Cheney, W. and Kincaid, D. (1985). Numerical Mathematics and Computing. 2nd Ed. Pace Grove, California: Brooks/Cole Publishing Company pp. 354-354
Schroeder, L. (1974). Buffon's needle problem: An exciting application of many mathematical concepts. Mathematics Teacher, 67 (2), 183-186.