Home » Programming » Monte Carlo Simulation: Buffon’s Needle

Monte Carlo Simulation: Buffon’s Needle

Monte Carlo method is often used for simulating physical and mathematical systems. The basic idea of Monte Carlo method is generating random points, then perform a deterministic computation on the inputs. Here we attach a simple MATLAB to solve Buffon’s needle problem using Monte Carlo method.

MATLAB CODE

montecarlo.m

 

%**************************************************************************

%                    Monte Carlo Simulation: Buffon’s needle

% ————————————————————————-

% By     : Sutrisno

% Contact: sutrisno_link@yahoo.com             Last update: March 10, 2011

%**************************************************************************

%**************************************************************************

% This program is developed to simulate buffon’s needle(stick). In this

% case, the length of the needle (stick) is one unit and the distance

% between the lines is L.

% There are two variables, the angle at which the needle falls (alpha) and

% the distance from the center of the needle to the upper line (d).

% Alpha can vary from 0 to 360 degrees. While d vary from 0 to L.

%

% Inputs:

% L= lenght of stick

% N= number of throwing

% Outputs:

% n= number of touching

% c= constant to be evaluated (N/n)

%**************************************************************************

%**************************************************************************

%                   The Program Start Here

%**************************************************************************

function [n c]= montecarlo(L,N)

% Variables:

n=0;

c=0;

d=0.5; % center position of stick

E1=0;       % position of edge1

E2=0;       % position of edge2

alpha=0;    % angle between stick and x-axis positive

for i=1:N

% Random center position of stick (d) and angle

d= L*rand;          % 0<d<L

alpha = pi*rand;    % 0<alpha<2*pi

% Determine position of edges

E1 = L-d+0.5*sin(alpha);

E2 = L-d-0.5*sin(alpha);

% Examine does the stick touch line or not.

if ((E1<0) |(E2<0) | (E1>L) | (E2>L))

% add number of touching

n=n+1;

end

end

c=N./n;

end

%**************************************************************************

%                   Ending The Program

%**************************************************************************

simulation.m

%**************************************************************************

%       This program developed just to make computation of Buffon’s

% problem easier. Example we want to calculate all results given L vary

% from 1 to 10 and N vary from 100 to 1000 (or L and N vary in certain

% interval). The results given in matrix form.

%**************************************************************************

function [matrixn matrixc]= simulation(L,N)

matrixn=zeros(10);

matrixc=zeros(10);

for i=1:10

for j=1:10

ll=i*(L/10);nn=j*(N/10);

[n c]=montecarlo(ll,nn);

matrixn(i,j)=n;

matrixc(i,j)=c;

end

end

end

%**************************************************************************

%                   Ending The Program

%**************************************************************************

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s