Learn R Programming

animation (version 2.0-5)

buffon.needle: Simulation of Buffon's Needle

Description

This function provides a simulation for the problem of Buffon's Needle, which is one of the oldest problems in the field of geometrical probability.

Usage

buffon.needle(l = 0.8, d = 1, redraw = TRUE, mat = matrix(c(1, 
    3, 2, 3), 2), heights = c(3, 2), col = c("lightgray", "red", 
    "gray", "red", "blue", "black", "red"), expand = 0.4, type = "l", 
    ...)

Arguments

l
numerical. length of the needle; shorter than d.
d
numerical. distances between lines; it should be longer than l.
redraw
logical. redraw former `needles' or not for each drop.
mat,heights
arguments passed to layout to set the layout of the three graphs.
col
a character vector of length 7 specifying the colors of: background of the area between parallel lines, the needles, the sin curve, points below / above the sin curve, estimated $\pi$ values, and the true $\pi$ value.
expand
a numerical value defining the expanding range of the y-axis when plotting the estimated $\pi$ values: the ylim will be (1 +/- expand) * pi.
type
an argument passed to plot when plotting the estimated $\pi$ values (default to be lines).
...
other arguments passed to plot when plotting the values of estimated $\pi$.

Value

  • The values of estimated $\pi$ are returned as a numerical vector (of length nmax).

Details

This is quite an old problem in probability. For mathematical background, please refer to http://en.wikipedia.org/wiki/Buffon's_needle or http://www.mste.uiuc.edu/reese/buffon/buffon.html.

`Needles' are denoted by segments on the 2D plane, and dropped randomly to check whether they cross the parallel lines. Through many times of `dropping' needles, the approximate value of $\pi$ can be calculated out.

There are three graphs made in each step: the top-left one is a simulation of the scenario, the top-right one is to help us understand the connection between dropping needles and the mathematical method to estimate $\pi$, and the bottom one is the result for each drop.

References

Ramaley, J. F. (Oct 1969). Buffon's Noodle Problem. The American Mathematical Monthly 76 (8): 916-918.

http://animation.yihui.name/prob:buffon_s_needle

Examples

Run this code
## it takes several seconds if 'redraw = TRUE'
oopt = ani.options(nmax = ifelse(interactive(), 500, 
    2), interval = 0.05)
par(mar = c(3, 2.5, 0.5, 0.2), pch = 20, mgp = c(1.5, 
    0.5, 0))
buffon.needle()

# this will be faster
buffon.needle(redraw = FALSE)

## create an HTML animation page
saveHTML({
    par(mar = c(3, 2.5, 1, 0.2), pch = 20, mgp = c(1.5, 0.5, 
        0))
    ani.options(nmax = ifelse(interactive(), 300, 10), interval = 0.1)
    buffon.needle(type = "S")
}, img.name = "buffon.needle", htmlfile = "buffon.needle.html", 
    ani.height = 500, ani.width = 600, title = "Simulation of Buffon's Needle", 
    description = c("There are three graphs made in each step: the", 
        "top-left, one is a simulation of the scenario, the top-right one", 
        "is to help us understand the connection between dropping needles", 
        "and the mathematical method to estimate pi, and the bottom one is", 
        "the result for each dropping."))

ani.options(oopt)

Run the code above in your browser using DataLab