The Buffon’s Needle Problem

Attached is a short write-up on the very interesting geometric probability problem commonly referred to as the Buffon’s Needle problem.  The solution gives a general outline for a Monte Carlo method of approximating pi. neato!Buffons Needle\

An R implementation of the monte carlo simulation is:

#Enter the desired number of trials as n

Buffon_Needle<-function(n){
	L=2;D=L;

	thetas<-c(rep(0,n))
	for(i in 1:n){thetas[i]=runif(1,0,pi/2)}

	X<-c(rep(0,n))
	for(i in 1:n){X[i]=runif(1,0,L/2)	}

	COS<-c(rep(0,n)); COS=cos(thetas)
	Compare<-c(rep(0,n))
	Compare=(X/(2/L))
	Crossover<-c(rep(0,n))
for(i in 1:n){
if(COS[i]>=Compare[i]){Crossover[i]=1}
if(COS[i]<Compare[i]){Crossover[i]=0}
	}
Total=sum(Crossover)
Pi_Est=(2*n)/Total

cat("For ",n," trials the estimate of pi is: ",Pi_Est)
}
########################################

Buffon_Needle(100)

A SAS implementation of the monte carlo simulation is:

%macro Buffon_Needle(n=);
%let D=2; %let L=2;
 
data BN;
do i=1 to &n;
     angle=rand("Uniform",0,3.14159/2);
     X=rand("Uniform",0,&D/2);
     COS=cos(angle);
     L=&L;
output;
end;
 
data BN; set BN;
if(COS >=(X/(2/L))) then Cross = 1;
else Cross = 0;
 
proc sql;
create table Results as
select (sum(t1.Cross)) as Total_Crosses
from work.BN t1;
quit;
 
data Results; set Results;
n=&n;
pi_est = (2*&n)/Total_Crosses;
 
run;
 
%mend;
 
%Buffon_Needle(n=1000000);

Advertisements

Math 63 HW #6

math-063

For next Saturday (October 12th), please do all of the odd problems from sections 4.4, 4.5, 4.6, 4.7, & 4.8.  Remember that your Exam 1 corrections are also due next Saturday (October 12th).  In order to receive partial credit for the problems that you missed, you will need to rework the missed problem and bring them in on a separate sheet of paper during next weeks class.  Have a great week!

 

Math 63: Homework #4

math-063The homework for this week is:

  • 3.7 (all odds)
  • 4.1 (all odds)
  • 4.2 (all odds)

We will be having our first exam this saturday, which will cover all of the material through chapter 3.6.  Please review the practice exam that I handed out during class and utilize the Urban Campus’ AAC if you have difficulties.  The exam will be closed book and closed notes, so you will need to be able to remember how to do each problem without any reference materials.