After generating 1000 random points, I computed the average value of \(e^{-(x_{1} + x_{2} + x_{3})}\) for each 3D point \(x = (x_{1}, x_{2}, x_{3})\) and plotted them.
integrand <- function(x) {
exp(-sum(x))
}
monte_carlo_integration <- function(num_samples) {
estimates <- numeric(num_samples)
errors <- numeric(num_samples)
for (i in 1:num_samples) {
points <- matrix(runif(i * 3), ncol = 3)
values <- apply(points, 1, integrand)
estimates[i] <- mean(values)
errors[i] <- abs(estimates[i] - 0.25258)
}
return(list(estimates = estimates, errors = errors))
}