{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Questions 1 - 6"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Q1 Area under curve\n",
"Calculate the area under the curve $\\displaystyle e^{-x} \\sin(x^2)$ \n",
"\n",
"in the range $0 \\to 6$ using the mean-value method. \n",
"\n",
"**Strategy:** Use the algorithm in the text modified for the new function and limits.\n",
"\n",
"## Q2 Mean value method\n",
"Integrate $\\displaystyle \\int_0^2\\int_0^1\\int_0^{3/2}\\sqrt{16-x^2-y^2-z^2}dxdydz$ \n",
"\n",
"using the mean value, Monte Carlo method and accurate to two significant figures.\n",
"\n",
"**Strategy:** Use equation 5 modified for three variables. Repeat the calculation until the answer becomes accurate enough for your needs.\n",
"\n",
"\n",
"## Q3 Importance sampling\n",
"Integrate $\\displaystyle \\int_o^\\infty e^{-x}\\sin(x^2)dx$ using importance sampling.\n",
"\n",
"**Strategy:** The distribution function can be chosen as $e^{-x}$ and the importance sampling algorithm followed. This method also enables us to calculate from zero to infinity; however, a cut-off value has to be determined and $e^{-10}$ can be chosen because it is so small compared to $1$. The limits are therefore $a = 0 ,\\; b = 10$.\n",
"\n",
"## Q4 Importance sampling\n",
"Calculate the integral $\\displaystyle \\int_0^2 e^{-x^2}dx$\n",
"\n",
"using importance sampling and the distribution function $p(x) = 1/(x^2 + 1/4)$, \n",
"\n",
"which has a similar shape to the function in the integral.\n",
"\n",
"**Strategy:** Following the text on importance sampling, $p(x)$ has to be made into a distribution. The integral from $0 \\to t$ is \n",
"\n",
"$$\\displaystyle \\int_0^t (x^2+1/4)^{-1}dx = 2\\tan^{-1}(2t)$$\n",
"\n",
"and the normalization is therefore $N = 2 \\tan^{-1}(4)$. Working out the cumulative distribution and solving for $r$ produces $r = \\tan^{-1}(2t)/\\tan^{-1}(4)$. The variable $r$ is sampled from a uniform distribution in the range $0 \\to 1$.\n",
"\n",
"## Q5 Accuracy\n",
"Apparently innocent functions, when calculated by simple Monte Carlo methods, can give incorrect results even when huge numbers of sampling points are taken. One example is the integral \n",
"\n",
"$$\\displaystyle \\gamma = \\int_0^1x^\\gamma dx=\\frac{1}{1+\\gamma}\\quad\\text{ when }\\quad\\gamma \\gt 0$$\n",
"\n",
"(Krauth 2006, p66).\n",
"\n",
"(a) Accurate values are obtained when $0.5 \\lt \\gamma \\lt 1$. Show that this is the case using a simple,\n",
"or mean-value Monte Carlo method with $\\gamma = 1/2$ and $20000$ samples.\n",
"\n",
"(b) Using the same method show that an incorrect result is produced when $\\gamma = -0.8$, and similar values close to $-1$, even though tens or even hundreds of thousands of samples are taken. (Note that this may take several minutes to complete depending on the computer used and how efficient the algorithm is.)\n",
"\n",
"(c) Next, use the importance sampling method with the distribution $p(x) = x\\lambda$ where $\\gamma \\lt \\lambda \\lt 0$, and obtain an accurate result.\n",
"\n",
"## Q6 Lennard Jones potential & virial coefficient\n",
"The Virial Coefficients are used in the description of real gases. The compression factor $Z = 1$ for an ideal gas, but is expanded as a series for a real gas, \n",
"\n",
"$$\\displaystyle Z=\\frac{pV}{nRT}=1+B_2\\left(\\frac{n}{V} \\right)+ B_3\\left(\\frac{n}{V} \\right)^2 +\\cdots$$\n",
"\n",
"The constants $B_2,\\; B_3$, and so forth are the virial coefficients. The second coefficient $B_2$ can be related to the potential energy of interaction between molecules, which leads to non-ideal behaviour. The constant is (Rigby et al. 1986; Murrell & Jenkins 1994; Stone 1996). \n",
"\n",
"$$\\displaystyle B_2= \\int_0^\\infty \\left(1-e^{U(r)/k_BT} \\right)r^2dr$$\n",
"\n",
"where $U$ is the interaction potential energy at the separation of $r$ between molecules. In the case of a\n",
"Lennard-Jones 6-12 potential, \n",
"\n",
"$$\\displaystyle U(r)= 4\\epsilon\\left(\\left(\\frac{\\sigma}{r}\\right)^{12}-\\left(\\frac{\\sigma}{r}\\right)^{6} \\right) $$\n",
"\n",
"the integral has no analytic (algebraic) solution and has to be calculated numerically.\n",
"\n",
"Using the Monte Carlo method, calculate $B_2$ with the parameters for CO$_2$, which are $\\epsilon = 140 \\,\\mathrm{cm^{-1}},\\; \\sigma = 0.3943$ nm. Boltzmann's constant is $0.693\\,\\mathrm{ cm^{-1}\\, K^{-1}}$.\n",
"\n",
"**Strategy:** The limits of the integration need to be addressed, because the limit of infinity is not generally possible with the Monte Carlo or other numerical methods. Additionally, the limit when $r = 0$ needs to be checked because here $U(0) = \\infty$. In the latter case, the exponential term rescues the situation because $0(1 - e^{-\\infty}) = 0$, therefore the limit $r = 0$ is calculable. When $r = \\infty$, $U$ is zero, and the whole expression inside the integral (the integrand) is also zero. This can be seen by plotting\n",
"\n",
"\n",
"$$\\displaystyle f (r) = (1 - e^{-U(r)/k_BT})r^2$$\n",
"\n",
"and $U(r)$. In practice, a maximum value has to be put on $r$, and using the values given in the question, a plot indicates that $r = 2$ nm is quite sufficient. The function $f (r)$ is zero initially, and rises as $r^2$ for small $r$; however, this term is overwhelmed by the exponential at larger values of $r$.\n"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.6"
}
},
"nbformat": 4,
"nbformat_minor": 2
}