forked from DPotoyan/Chem324
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
167 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# Cookbook: Using python to tame the math" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import numpy as np \n", | ||
"import matplotlib.pyplot as plt\n", | ||
"from ipywidgets import interact\n", | ||
"import scipy \n", | ||
"from scipy.constants import physical_constants, hbar, h, c, k, m_e, Rydberg, e, N_A\n", | ||
"\n", | ||
"%matplotlib inline\n", | ||
"%config InlineBackend.figure_format='retina'" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### Example-1\n", | ||
"\n", | ||
"- Show that wavefunctions $f_1 = sin(x)$ and $f_2=cos(x)$ are orthogonal on $(0, 2\\pi)$ interval." | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"#### Numerical solution" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import numpy as np\n", | ||
"\n", | ||
"# Define the range of x values\n", | ||
"x = np.linspace(0, 2 * np.pi, 1000)\n", | ||
"\n", | ||
"# Calculate sin(x) and cos(x)\n", | ||
"y_sin = np.sin(x)\n", | ||
"y_cos = np.cos(x)\n", | ||
"\n", | ||
"# Calculate the numerical integral (dot product)\n", | ||
"dot_product = np.trapz(y_sin * y_cos, x)\n", | ||
"\n", | ||
"# Print the result\n", | ||
"print(f\"The numerical integral (dot product) of sin(x) * cos(x) over [0, 2π] is approximately: {dot_product:.4f}\")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"#### Symbolic solution" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"import sympy as sp\n", | ||
"\n", | ||
"# Define the symbolic variable\n", | ||
"x = sp.symbols('x')\n", | ||
"\n", | ||
"# Define sin(x) and cos(x)\n", | ||
"sin_x = sp.sin(x)\n", | ||
"cos_x = sp.cos(x)\n", | ||
"\n", | ||
"# Calculate the symbolic integral of sin(x) * cos(x) over [0, 2π]\n", | ||
"integral = sp.integrate(sin_x * cos_x, (x, 0, 2 * sp.pi))\n", | ||
"\n", | ||
"# Print the result\n", | ||
"print(f\"The symbolic integral of sin(x) * cos(x) over [0, 2π] is: {integral}\")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"#### Visual solution" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"import numpy as np\n", | ||
"import matplotlib.pyplot as plt\n", | ||
"\n", | ||
"def plot_orthogonality():\n", | ||
"\n", | ||
" # Define the range of x values\n", | ||
" x = np.linspace(0, 2 * np.pi, 1000)\n", | ||
" \n", | ||
" # Calculate sin(x) and cos(x)\n", | ||
" y_sin = np.sin(x)\n", | ||
" y_cos = np.cos(x)\n", | ||
" \n", | ||
" # Plotting sin(x) and cos(x)\n", | ||
" plt.plot(x, y_sin, label='sin(x)')\n", | ||
" plt.plot(x, y_cos, label='cos(x)')\n", | ||
" \n", | ||
" # Fill the area between two horizontal curves\n", | ||
" plt.fill_between(x, y_sin * y_cos, alpha=0.3, color='gray', label='sin(x) * cos(x)')\n", | ||
" \n", | ||
" # Labels and title\n", | ||
" plt.xlabel('x')\n", | ||
" plt.ylabel('Function Value')\n", | ||
" plt.title('Orthogonality of sin(x) and cos(x)')\n", | ||
" plt.legend()\n", | ||
" plt.grid(True)\n", | ||
" plt.show()\n", | ||
"\n", | ||
"# Call the function\n", | ||
"plot_orthogonality()" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"language_info": { | ||
"name": "python" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters