MC.hitormiss: Hit or Miss Monte Carlo integration

Description Usage Arguments Details Value Note Author(s) References See Also

View source: R/MC.hitormiss.R

Description

Integrate a function using the Hit-or-Miss Monte Carlo algorithm.

Usage

1
2
3
4
5
6
7
8
9
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),
  ...
)

Arguments

FUN

the function to be integrated

n

number of points to be sampled from the Uniform(0, 1) distribution

from, to

the limits of integration

col.points, pch.points

colors and point characters for points which “hit” or “miss” the area under the curve

...

other arguments passed to points

Details

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).

Value

A list containing

x1

the Uniform random numbers generated on x-axis

x2

the Uniform random numbers generated on y-axis

y

function values evaluated at x1

n

number of points drawn from the Uniform distribtion

est

the estimated value of the integral

Note

This function is for demonstration purpose only; the integral might be very inaccurate when n is small.

ani.options('nmax') specifies the maximum number of trials.

Author(s)

Yihui Xie

References

Examples at https://yihui.org/animation/example/mc-hitormiss/

See Also

integrate, MC.samplemean


animation documentation built on Oct. 7, 2021, 9:18 a.m.