Problem 179. Monte-Carlo integration
Write a function that estimates a d-dimensional integral to at least 1% relative precision.
Inputs:
- d: positive integer. The dimension of the integral.
- fun: function handle. The function accepts a row-vector of length d as an argument and returns a real scalar as a result.
Output:
- I: is the integral over fun from 0 to 1 in each direction.
1 1 1 / / / I = |dx_1 |dx_2 ...| dx_d fun([x_1,x_2,...,x_d]) / / / 0 0 0
Example:
fun = @(x) x(1)*x(2) d = 2
The result should be 0.25. An output I=0.2501 would be acceptable, because the relative deviation would be abs(0.25-0.2501)/0.25 which is smaller than 1%.
The functions in the test-suite are all positive and generally 'well behaved', i.e. not fluctuating too much. Some of the tests hav a relatively large d.
Solution Stats
Problem Comments
-
7 Comments
I'm confused by the 3rd test case. Can the integral inside an n-dimensional hypercube really be greater than 1?
In my comment, I mean an n-dimensional UNIT hypercube, which is what you integration limits impose.
Of course. It depends on the integrand. Even in 1d, if the integrand is e.g. 10x, the result will be 5.
I was confused. Thanks for clarifying.
I found it helpful to think about the problem as involving d+1 dimensions: the d dimensions of the input variables, and one more dimension for the (scalar) output variable. —DIV
This problem makes no sense. fun = @(x) x(1)*x(2) needs two inputs. You do not provide enough information for a solution.
The best problem.
Solution Comments
Show commentsProblem Recent Solvers104
Suggested Problems
-
Find the numeric mean of the prime numbers in a matrix.
8909 Solvers
-
Project Euler: Problem 4, Palindromic numbers
1041 Solvers
-
Back to basics 22 - Rotate a matrix
903 Solvers
-
Given a matrix, swap the 2nd & 3rd columns
1072 Solvers
-
7195 Solvers
More from this Author7
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!