// put your names here!
The main goal of this assignment is to use https://en.wikipedia.org/wiki/Monte_Carlo_integration - a technique for numerical integration that uses random numbers to compute the value of a definite integral. Monte Carlo integration works well for one-dimensional functions, but is especially helpful for higher-dimensional integrals or complicated functions.
Write a function that uses Monte Carlo integration to $f(x) = 2 x^2 + 3$ from $x_{beg}= -2$ to $x_{end} = +4$. The analytic answer is:
$\int_{-2}^{4} (2x^2 + 3) dx = \left. \frac{2}{3}x^3 + 3x \right|_{-2}^4 = 66$
As you increase the number of samples ($N_{sampple}$) from 10 to $10^6$, how does your calculated solution approach the true answer? In other words, calculate the fractional error defined as $\epsilon = |\frac{I - T}{T}|$, where I is the integrated answer, T is the true (i.e., analytic) answer, and the vertical bars denote that you take the absolute value. This gives you the fractional difference between your integrated answer and the true answer.
In [ ]:
# Put your code here!
A torus that is radially symmetric about the z-axis (think of a donut pierced by the x-y plane) can be described by the equation:
$\large( R - \sqrt{x^2 + y^2} \large)^2 + z^2 = r^2$
where R is the distance from the center of the tube to the center of the torus, and r is the radius of the tube (with the 'tube' meaning the tasty baked part of the donut). Assuming that $R = 12$ cm, $r = 8$ cm, and $\rho_{donut} = 0.8$ g cm$^{-3}$, use Monte Carlo integration to calculate the mass of this excessively large donut. Note that for the situation described here, a point (x,y,z) is inside of the tasty cake part of the donut when:
$\large( R - \sqrt{x^2 + y^2} \large)^2 + z^2 < r^2$
(Try testing this relation in the x-y plane to see that it is true.) Assume that the donut is of uniform density and that the mass of the icing can be neglected. You can use the formulae shown in the Wikipedia page linked above to get the analytic answer. Run the test several times, both repeatedly with the same number of samples and with different numbers of samples. How many points do you have to use to get an answer that converges to within 1%? What about 0.1%?
Hint: does the box that encompasses the donut have to be a cube? I.e., when calculating this problem, what is the minimum practical bounding box that can be described simply and which fully encompasses the donut?
In [ ]:
# Put your code here!
In [ ]:
from IPython.display import HTML
HTML(
"""
<iframe
src="https://goo.gl/forms/NOKKHPQ0oKn1B7e23?embedded=true"
width="80%"
height="1200px"
frameborder="0"
marginheight="0"
marginwidth="0">
Loading...
</iframe>
"""
)