Simulation and Modeling (CSCI 3010U)
Faculty of Science, Ontario Tech University
http://vclab.science.ontariotechu.ca
Discuss in class
Monte Carlo integration is a technique for computing the value of a multidimensional definite integral \[ I = \int_\Omega f(\mathbf{x}) d\mathbf{x}, \] where \(\Omega\), as subset of \(\mathbb{R}^m\), has volume \[ V \int_\Omega d \mathbf{x}. \] In naive Monte Carlo approach, points \[ \mathbf{x}_1, \mathbf{x}_2, \cdots, \mathbf{x}_n \in \Omega \] are sampled uniformly on \(\Omega\). Then, \(I\) can be approximated by \[ I \approx V \frac{1}{N} \sum_{i=1}^N f(\mathbf{x}_i) = V \langle f \rangle \]
Compute \[ \int_{0}^{1} x^2 dx \] using Monte Carlo integration method. Compare the estimated integral with the exact solution provided below.
Recall that we can solve this integral analytically as follows:
\[ \begin{align} \int_{0}^{1} x^2 dx &= \left. \frac{x^3}{3} \right|_{0}^{1} \\ &= \frac{1}{3} - 0 \\ &= \frac{1}{3} \end{align} \]
Compute \[ \int_{0}^{1} e^{x^2} \sin(x^3) dx \] using Monte Carlo integration method.
Note that this definite integral is not easy to compute analytically
Complete the following method that computes \[ \int_a^b f(x) dx \]
def monte_carlo_integration(f, a, b, num_samples=10000):
# TODO
return 0
We can, for example, use this function as follows to solve Task 1 above:
def f(x):
return x**2
= 0
a = 1
b = monte_carlo_integration(f, a, b) estimated_integral
The exercise will be completed in class. Be prepared to show your work to the instructor.