mcint2 <- function ( f, xdom, bdom, m = 1000 ) #*****************************************************************************80 # ## mcint2() estimates an integral over a rectangle using a Monte Carlo approach. # # Licensing: # # Copyright 2016 James P. Howard, II # # The computer code and data files on this web page are distributed under # https://opensource.org/licenses/BSD-2-Clause, the BSD-2-Clause license. # # Modified: # # 09 March 2020 # # Author: # # Original R code by James Howard; # This version by John Burkardt. # # Reference: # # James Howard, # Computational Methods for Numerical Analysis with R, # CRC Press, 2017 # ISBN13: 978-1-4987-2363-3. # # Input: # # real F(x,y): the integrand. # # real XDOM(2): the left and right limits of the rectangle. # # real YDOM(2): the bottom and top limits of the rectangle. # # integer M: the number of sample points to use. # # Output: # # real AREA: the integral estimate. # { xmin <- min ( xdom ) xmax <- max ( xdom ) x <- runif ( m, min = xmin, max = xmax ) ymin <- min ( ydom ) ymax <- max ( ydom ) y <- runif ( m, min = ymin, max = ymax ) z <- f ( x, y ) area <- ( xmax - xmin ) * ( ymax - ymin ) * sum ( z ) / m return ( area ) }