% Author: John Burkardt % Discussion: % In the Buffon-Laplace needle experiment, we suppose that the plane has been % tiled into a grid of rectangles of width A and height B, and that a % needle of length L is dropped "at random" onto this grid. % % We may assume that one end, the "eye" of the needle falls at the point % (X1,Y1), taken uniformly at random in the cell [0,A]x[0,B]. % % ANGLE, the angle that the needle makes is taken to be uniformly random. % The point of the needle, (X2,Y2), therefore lies at % % (X2,Y2) = ( X1+L*cos(ANGLE), Y1+L*sin(ANGLE) ) % % The needle will have crossed at least one grid line if any of the % following are true: % % X2 <= 0, A <= X2, Y2 <= 0, B <= Y2. % % This routine simulates the tossing of the needle, and returns the number % of times that the needle crossed at least one grid line. % % If L is larger than sqrt ( A*A + B*B ), then the needle will % cross every time, and the computation is uninteresting. However, if % L is smaller than this limit, then the probability of a crossing on % a single trial is % % P(L,A,B) = ( 2 * L * ( A + B ) - L * L ) / ( PI * A * B ) % % and therefore, a record of the number of hits for a given number of % trials can be used as a very roundabout way of estimating PI. % (Particularly roundabout, since we actually will use a good value of % PI in order to pick the random anglesc) % Reference: % Sudarshan Raghunathan, % Making a Supercomputer Do What You Want: High Level Tools for % Parallel Programming, % Computing in Science and Engineering, % Volume 8, Number 5, September/October 2006, pages 70-80. % Parameters: % Input, double precision A, B, the horizontal and vertical dimensions % of each cell of the grid. 0 <= A, 0 <= B. % Input, double precision L, the length of the needle. % 0 <= L <= min ( A, B ). % Input, integer TRIAL_NUM, the number of times the needle is % to be dropped onto the grid. % Input/output, integer SEED, a seed for the random number generator. % Output, integer BUFFON_LAPLACE_SIMULATE, the number of times the needle % crossed at least one line of the grid of cells. function hits = buffon_laplace_simulate(a,b,l,trial_num,seed) hits = 0; for trial = 1:trial_num % Randomly choose the location of the eye of the needle in [0,0]x[A,B], % and the angle the needle makes. % x1 = a * r8_uniform_01( seed ); % y1 = b * r8_uniform_01( seed ); % angle = 2.0 * pi * r8_uniform_01 ( seed ); x1 = a * rand;%(seed); y1 = b * rand;%(seed); angle = 2.0 * pi * rand;%(seed); % Compute the location of the point of the needle. x2 = x1 + l * cos( angle ); y2 = y1 + l * sin( angle ); % Count the end locations that lie outside the cell. % figure(1),clf % plot the nedle % line([0,1],[0,0],'LineWidth',2),hold % line([1,1],[0,1],'LineWidth',2) % line([0,1],[1,1],'LineWidth',2) % line([0,0],[0,1],'LineWidth',2) %% axes('position', [-0.5, -0.5, 2, 2]) % line([x1,x2],[y1,y2],'LineWidth',2,'Color','r') % axis('equal') if (x2 <= 0 | a <= x2 | y2 <= 0 | b <= y2 ), hits = hits + 1; end end % for loop return