Integrate a function using the Hit-or-Miss Monte Carlo algorithm.
MC.hitormiss(
FUN = function(x) x - x^2,
n = ani.options("nmax"),
from = 0,
to = 1,
col.points = c("black", "red"),
pch.points = c(20, 4),
...
)
the function to be integrated
number of points to be sampled from the Uniform(0, 1) distribution
the limits of integration
colors and point characters for points which ``hit'' or ``miss'' the area under the curve
other arguments passed to points
A list containing
the Uniform random numbers generated on x-axis
the Uniform random numbers generated on y-axis
function values evaluated at x1
number of points drawn from the Uniform distribtion
the estimated value of the integral
We compute the proportion of points hitting the area under the curve, and the integral can be estimated by the proportion multiplied by the total area of the rectangle (from xmin to xmax, ymin to ymax).
Examples at https://yihui.org/animation/example/mc-hitormiss/