Instructions: Create a new notebook called MCIntegrationExercises
in your MCIntegration
directory and solve the following problems inside it. Be sure to include the problem statements in a markdown cell above your solution. You don't need to put the "helper" code in the markdown cell, just implement the helper code in your code cell with your solution.
Preliminaries: At the top of your notebook, include a "Heading 1" cell with the title Monte Carlo Integration Exercises. Then include the inline functions and libraries by adding a code cell that invokes the %pylab inline
magic and imports the needed packages.
Let’s compute the electric potential from a plane of charge as shown in the figure using Monte Carlo integration.
The integral comes from summing all the potentials from the chunks of “point” charges. Just as we summed two charges for a dipole, now we’ll sum many charges. The important part is to understand how $dQ$, modeled as a point charge, is related to $\sigma dA$, where $\sigma$ is the surface charge density = charge/area ($\sigma = Q/A$ ) Review your notes from your introductory physics class if you need a refresher.
We want to use our previously defined point_potential()
function from Electrostatics.py
, so we have to import it. Since it was created as part of the Interpolation Tour, we need to add the path to that directory in our Python search path so the module can be found:
In [2]:
#import our point potential function
import sys
sys.path.append("../Interpolation/")
from Electrostatics import *
help(point_potential)
(a) Use the sample mean method with point_potential
to compute the potential at point $P(2,3)$ due to the disk of charge for different numbers of samples $N = (10^1, 10^2, 10^3, 10^4, 10^5, 10^6)$. Report the value and its statistical uncertainty for each sample size. How does the result depend on N?
In [ ]:
#Copy the exercise statement to a markdown cell in your notebook and then implement a solution in a code cell
(b) Now use the same method to compute the electric potential, $V$, at point $P$(2,3), from a disk centered on the origin with radius, $R$=1.2 m, and charge density, $\sigma$ = 2 nC/m$^2$ .
The integral is
$$ V = {\int_0^{2\pi}\int_0^{1.2} \frac{K\sigma}{|r-r_0|}} rd\theta dr $$You'll need to think about how to set this one up for use with the point_potential
function, since that uses Cartesian coordinates and this integral is in polar coordinates.
In [ ]:
#Copy the exercise statement to a markdown cell in your notebook and then implement a solution in a code cell
Use the sample mean method to compute the volume of a sphere with radius 1 using spherical coordinates:
$$ V = \int_0^1\int_0^{\pi}\int_0^{2\pi} r^2\sin(\theta)drd\theta d\phi $$for $N = (10^1,10^2,10^3,10^4,10^5,10^6)$. Report the value and statistical uncertainty for each sample size. How does the result depend on $N$?
In [ ]:
#Copy the exercise statement to a markdown cell in your notebook and then implement a solution in a code cell
(a) Use the hit or miss Monte Carlo method to estimate the integral of $f(x) = 4\sqrt{1-x^2}$ in the interval $0\leq x \leq 1$ as a function of $N$, the number of samples. Let $N$ range from 10$^1$ to 10$^6$ and use at least 100 different values for $N$.
(np.logspace
might be useful here to get evenly spaced values for $N$.)
In [ ]:
#Copy the exercise statement to a markdown cell in your notebook and then implement a solution in a code cell
(b) Calculate the percent error for the $N$ values from part (a) using the exact result $\pi$. Make a log-log plot of the error as a function of $N$.
What is the approximate functional dependence of the error on $N$ for large $N$, for example $N \geq 10^4$?
In [ ]:
#Copy the exercise statement to a markdown cell in your notebook and then implement a solution in a code cell
(c) Estimate the same integral using the sample mean method and compute the statistical uncertainty and error as a function of the number of samples $N$ for $N \geq 10^4$. Use at least 100 different values for $N$.
Make a log-log plot of both the statistical uncertainty and the error as a function of $N$.
How many samples are needed to determine the result to two decimal places? What is the approximate functional dependence of the error on $N$ for large $N$?
In [ ]:
#Copy the exercise statement to a markdown cell in your notebook and then implement a solution in a code cell