quantum-dev/ps9-2.py

73 lines
2.5 KiB
Python
Raw Normal View History

2024-02-25 19:34:17 -05:00
import numpy as np
a = 1
2024-02-27 12:43:01 -05:00
hbar = 1 # 1.054572e-34
2024-02-25 19:34:17 -05:00
n_vals = [n for n in range(1, 7)]
N = 1000
dx = 1 / N
x_vals = np.linspace(-a / 2, a / 2, N)
psi_funcs = []
psi_derivs = []
psi_2nd_derivs = []
for n in n_vals:
if n % 2 == 0:
psi_funcs.append(np.sqrt(2 / a) * np.sin(n * np.pi * x_vals / a))
psi_derivs.append(np.sqrt(2) * np.pi * n * np.cos(np.pi * n * x_vals / a) / (a * np.sqrt(a)))
psi_2nd_derivs.append(
2024-02-25 19:51:46 -05:00
(np.sqrt(2) * np.pi ** 2 * n ** 2 * np.sin(np.pi * n * x_vals / a)) / (a ** 2 * np.sqrt(a)))
2024-02-25 19:34:17 -05:00
else:
psi_funcs.append(np.sqrt(2 / a) * np.cos(n * np.pi * x_vals / a))
psi_derivs.append(-(np.sqrt(2) * np.pi * n * np.sin(np.pi * n * x_vals / a)) / (a * np.sqrt(a)))
psi_2nd_derivs.append(
2024-02-25 19:51:46 -05:00
(np.sqrt(2) * np.pi ** 2 * n ** 2 * np.cos(np.pi * n * x_vals / a)) / (a ** 2 * np.sqrt(a)))
2024-02-25 19:34:17 -05:00
print("======= expect_x =======")
2024-02-25 19:51:46 -05:00
expect_x_vals = []
2024-02-25 19:34:17 -05:00
for i in range(len(psi_funcs)):
2024-02-25 19:51:46 -05:00
expect_x = np.sum(psi_funcs[i] * x_vals * psi_funcs[i]) * dx
expect_x_vals.append(expect_x)
2024-02-25 19:34:17 -05:00
print(f'expect_x_{i + 1} = {expect_x}')
print("======= expect_x^2 =======")
2024-02-25 19:51:46 -05:00
expect_x_sqrd_vals = []
2024-02-25 19:34:17 -05:00
for i in range(len(psi_funcs)):
2024-02-25 19:51:46 -05:00
expect_x_sqrd = np.sum(psi_funcs[i] * x_vals ** 2 * psi_funcs[i]) * dx
expect_x_sqrd_vals.append(expect_x_sqrd)
2024-02-25 19:34:17 -05:00
print(f'expect_x^2_{i + 1} = {expect_x_sqrd}')
print("======= expect_p =======")
2024-02-25 19:51:46 -05:00
expect_p_vals = []
2024-02-25 19:34:17 -05:00
for i in range(len(psi_funcs)):
expect_p = np.sum(psi_funcs[i] * 1j * hbar * psi_derivs[i]) * dx
2024-02-25 19:51:46 -05:00
expect_p_vals.append(expect_p)
2024-02-25 19:34:17 -05:00
print(f'expect_p_{i + 1} = {expect_p}')
print("======= expect_p^2 =======")
2024-02-25 19:51:46 -05:00
expect_p_sqrd_vals = []
2024-02-25 19:34:17 -05:00
for i in range(len(psi_funcs)):
expect_p_sqrd = np.sum(psi_funcs[i] * hbar ** 2 * psi_2nd_derivs[i]) * dx
2024-02-25 19:51:46 -05:00
expect_p_sqrd_vals.append(expect_p_sqrd)
2024-02-25 19:34:17 -05:00
print(f'expect_p^2_{i + 1} = {expect_p_sqrd}')
2024-02-25 19:51:46 -05:00
print("======= sigma_x =======")
sigma_x_vals = []
for i in range(len(psi_funcs)):
sigma_x = np.sqrt(expect_x_sqrd_vals[i] - expect_x_vals[i] ** 2)
sigma_x_vals.append(sigma_x)
print(f'sigma_x_{i + 1} = {sigma_x}')
print("======= sigma_p =======")
sigma_p_vals = []
for i in range(len(psi_funcs)):
sigma_p = np.real(np.sqrt(expect_p_sqrd_vals[i] - expect_p_vals[i] ** 2))
sigma_p_vals.append(sigma_p)
print(f'sigma_p_{i + 1} = {sigma_p}')
print("======= uncertainty =======")
print(f'hbar/2 = {hbar / 2}')
for i in range(len(psi_funcs)):
uncertainty = sigma_x_vals[i] * sigma_p_vals[i]
print(f'sigma_x_{i + 1}*sigma_p_{i + 1} = {uncertainty}')