Skip to content

Commit

Permalink
Make consistent with latest version of scipy by removing trapz
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulWAyers committed Jan 14, 2025
1 parent 509345b commit ad75531
Showing 1 changed file with 43 additions and 33 deletions.
76 changes: 43 additions & 33 deletions book/ParticleIn1DBox.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,21 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 1,
"metadata": {},
"outputs": [],
"outputs": [
{
"ename": "ModuleNotFoundError",
"evalue": "No module named 'autograd'",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[1;32mIn[1], line 7\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[38;5;66;03m# Execute this code block to import required objects.\u001b[39;00m\n\u001b[0;32m 2\u001b[0m \n\u001b[0;32m 3\u001b[0m \u001b[38;5;66;03m# Note: The numpy library from autograd is imported, which behaves the same as\u001b[39;00m\n\u001b[0;32m 4\u001b[0m \u001b[38;5;66;03m# importing numpy directly. However, to make automatic differentiation work,\u001b[39;00m\n\u001b[0;32m 5\u001b[0m \u001b[38;5;66;03m# you should NOT import numpy directly by `import numpy as np`.\u001b[39;00m\n\u001b[1;32m----> 7\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mautograd\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mnumpy\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m \u001b[38;5;21;01mnp\u001b[39;00m\n\u001b[0;32m 8\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mautograd\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m elementwise_grad \u001b[38;5;28;01mas\u001b[39;00m egrad\n\u001b[0;32m 10\u001b[0m \u001b[38;5;66;03m# import numpy as np\u001b[39;00m\n",
"\u001b[1;31mModuleNotFoundError\u001b[0m: No module named 'autograd'"
]
}
],
"source": [
"# Execute this code block to import required objects.\n",
"\n",
Expand All @@ -242,7 +254,7 @@
"from autograd import elementwise_grad as egrad\n",
"\n",
"# import numpy as np\n",
"from scipy.integrate import trapz, quad\n",
"from scipy.integrate import quad\n",
"from scipy import constants\n",
"\n",
"import ipywidgets as widgets\n",
Expand All @@ -255,7 +267,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": null,
"metadata": {
"scrolled": false
},
Expand All @@ -272,14 +284,14 @@
"# Define a function for the wavefunction\n",
"def compute_wavefunction(x, n, a):\n",
" \"\"\"Compute 1-dimensional particle-in-a-box wave-function value(s).\n",
" \n",
"\n",
" Parameters\n",
" ----------\n",
" x: float or np.ndarray\n",
" Position of the particle.\n",
" n: int\n",
" Quantum number value.\n",
" a: float \n",
" a: float\n",
" Length of the box.\n",
" \"\"\"\n",
" # check argument n\n",
Expand All @@ -291,7 +303,7 @@
" # check argument x\n",
" if not (isinstance(x, float) or hasattr(x, \"__iter__\")):\n",
" raise ValueError(\"Argument x should be a float or an array!\")\n",
" \n",
"\n",
" # compute wave-function\n",
" value = np.sqrt(2 / a) * np.sin(n * np.pi * x / a)\n",
"\n",
Expand All @@ -307,7 +319,7 @@
"# Define a function for the wavefunction squared\n",
"def compute_probability(x, n, a):\n",
" \"\"\"Compute 1-dimensional particle-in-a-box probablity value(s).\n",
" \n",
"\n",
" See `compute_wavefunction` parameters.\n",
" \"\"\"\n",
" return compute_wavefunction(x, n, a)**2\n",
Expand All @@ -317,8 +329,8 @@
" #check the computed values of the moments against the analytic formula\n",
" normalization,error = quad(compute_probability, 0, a, args=(n, a))\n",
" print(\"Normalization of wavefunction = \", normalization)\n",
" \n",
" \n",
"\n",
"\n",
"#Principle quantum number:\n",
"n = 1\n",
"\n",
Expand Down Expand Up @@ -428,7 +440,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": null,
"metadata": {
"code_folding": []
},
Expand All @@ -451,7 +463,7 @@
" return n**2 * np.pi**2 / (2 * a**2)\n",
"\n",
"# Define a function for the energy of an electron in a box\n",
"# with length a and quantum number n [in SI units!]. \n",
"# with length a and quantum number n [in SI units!].\n",
"# The length is input in meters.\n",
"def compute_energy_si(n, a):\n",
" \"Compute 1-dimensional particle-in-a-box energy.\"\n",
Expand All @@ -467,14 +479,14 @@
" f'quantum number {n} is {compute_energy(n, a):.2f} a.u..')\n",
" print(f'The energy of an electron in a box of length {a*a0:.2e} m with '\n",
" f'quantum number {n} is {compute_energy_si(n, a*a0):.2e} Joules.')\n",
" \n",
"\n",
"#Principle quantum number:\n",
"n = 1\n",
"\n",
"#Box length:\n",
"a = 0.1\n",
"\n",
"print_energy(a, n) "
"print_energy(a, n)"
]
},
{
Expand Down Expand Up @@ -629,7 +641,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": null,
"metadata": {},
"outputs": [
{
Expand All @@ -647,7 +659,7 @@
"#Compute <x^power>, the expectation value of x^power\n",
"def compute_moment(x, n, a, power):\n",
" \"\"\"Compute the x^power moment of the 1-dimensional particle-in-a-box.\n",
" \n",
"\n",
" See `compute_wavefunction` parameters.\n",
" \"\"\"\n",
" return compute_probability(x, n, a)*x**power\n",
Expand All @@ -662,7 +674,7 @@
" print(f\"<r^2> computed = {avg_r2:.5f}\")\n",
" print(f\"<r^2> analytic = {a**2*(1/3 - 1./(2*n**2*np.pi**2)):.5f}\")\n",
"\n",
" \n",
"\n",
"#Principle quantum number:\n",
"n = 1\n",
"\n",
Expand Down Expand Up @@ -722,7 +734,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": null,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -764,13 +776,13 @@
" else:\n",
" if x < 0.0 or x > a:\n",
" deriv = 0.0\n",
" \n",
"\n",
" return deriv\n",
"\n",
"\n",
"def compute_kinetic_energy_density(x, n, a):\n",
" \"\"\"Compute 1-dimensional particle-in-a-box kinetic energy density.\n",
" \n",
"\n",
" See `compute_wavefunction` parameters.\n",
" \"\"\"\n",
" # evaluate wave-function and its 2nd derivative w.r.t. x\n",
Expand All @@ -785,8 +797,8 @@
" energy = compute_energy(n, a)\n",
" print(f\"The energy computed by integrating the k.e. density is {ke:.5f}\")\n",
" print(f\"The energy computed directly is {energy:.5f}\")\n",
" \n",
" \n",
"\n",
"\n",
"#Principle quantum number:\n",
"n = 1\n",
"\n",
Expand All @@ -806,7 +818,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": null,
"metadata": {
"scrolled": true
},
Expand Down Expand Up @@ -850,8 +862,6 @@
"\n",
" # evaluate kinetic energy density\n",
" kin = compute_kinetic_energy_density(x, n, a)\n",
" #print(\"Integrate KED = \", trapz(kin, x))\n",
"\n",
"\n",
" # set the size of the plot\n",
" plt.rcParams['figure.figsize'] = [15, 10]\n",
Expand Down Expand Up @@ -888,15 +898,15 @@
"\n",
" # adjust spacing between plots\n",
" plt.subplots_adjust(left=0.125,\n",
" bottom=0.1, \n",
" right=0.9, \n",
" top=0.9, \n",
" wspace=0.35, \n",
" bottom=0.1,\n",
" right=0.9,\n",
" top=0.9,\n",
" wspace=0.35,\n",
" hspace=0.35)\n",
" #Show Plot\n",
" plt.show()\n",
" \n",
" \n",
"\n",
"\n",
"#Principle quantum number slider:\n",
"n = 1\n",
"\n",
Expand Down Expand Up @@ -970,7 +980,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -984,7 +994,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.5"
"version": "3.12.2"
},
"toc": {
"base_numbering": "3",
Expand Down

0 comments on commit ad75531

Please sign in to comment.