{ "cells": [ { "cell_type": "markdown", "metadata": { "tags": [] }, "source": [ "#### Hydrogen atomic orbitals: Part 2\n", "In this notebook we will introduce 2D and 3D plotting. Let's start by copying the function for evaluating the hydrogen wavefunction from the previous notebook." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import scipy as sp\n", "import scipy.special as sps\n", "import matplotlib.pyplot as plt\n", "\n", "def h_orbital(r,theta,phi,n,l,m):\n", " pf = ( (2./n)**3. * sps.factorial(n-l-1) / (2. * n * sps.factorial(n+l)) )**0.5\n", " return pf * np.exp(-r/2.) *r**l * sps.sph_harm(m,l,phi,theta) * sps.eval_genlaguerre(n-l-1,2*l+1,r)" ] }, { "cell_type": "markdown", "metadata": { "tags": [] }, "source": [ "#### Conversion of Cartesian to spherical coordinates\n", "\n", "Before we get started on making 2D and 3D plots, we need a function that can convert Cartesian coordinates into spherical coordinates. Using trigonometric functions, it is easy to derive the expressions for the spherical coordinates from $x, y, z$ as follows:\n", "\n", "$$ r = \\sqrt{x^2 + y^2 + z^2} ; \\quad \\theta = \\cos^{-1} \\frac{z}{r} ; \\quad\\phi = \\tan^{-1} \\frac{y}{x} $$\n", "\n", "However, when applying these formulas there is quickly a problem. Notice that for $\\theta$, there will be a divide-by-zero error at the origin. Even worse, the formula for $\\phi$ returns the same value for $(-x, -y)$ as for $(x, y)$, even though we know the actual $\\phi$ angle differs by $\\pi$ for these two points. \n", "\n", "Both problems can be solved by using the `np.arctan2(y, x)` function. This function takes in two arguments that would normally be divided in the conventional arctangent, thus giving it the ability to return values in the full range of $\\phi$. Also, this function returns standard values when one or both inputs are equal to zero; if we had used `np.arctan`, this would have resulted in errors. \n", "\n", "The boardwork below shows how `np.arctan2` is used in place of `np.arccos` and `np.arcsin` to give a robust conversion from Cartesian to spherical coordinates.\n", "\n", "