# Visualization of a particle-in-a-box quantum physics problem

In this article, the translation of which we share before the start of the course about Fullstack development in Python, I decided to look at some college-level quantum chemistry issues for electron orbital injection. What is the interest? The wave functions and electron probabilities will be visualized using Python.

### Schrödinger’s equation

In 1926, Erwin Schrödinger derived the famous wave equation that relates the energy of a system to its wave properties. Its application to the hydrogen atom is quite difficult, so first we use the wave equation to solve the particle-in-a-box problem. Expressed in one-dimensional space, the Schrödinger wave equation has the form:

### Rendering a particle in a box

Now, for the sake of simplicity, we will assume for a particle in a box that:

The particle-in-a-box problem does not correspond to any real chemical system. Its usefulness in the context of a situation is to illustrate several quantum mechanical features. The potential energy on the barrier is infinite by the condition (i.e., the particle cannot escape), and the potential energy inside the box is 0. Under these conditions, classical mechanics predicts that the particle has an equal probability of being at any point in the box, and the kinetic energy of the particle may be any. Taking this assumption into account, we obtain different equations for the energy of the particle at the barrier and inside the box. At the barrier, V is infinite, and, therefore, the particle does not exist:

Inside the box, V is zero, and therefore the wave can have any finite value:

The equation of conditions inside the box can be rewritten like this:

It can be seen above that the wave function turns out to be such that double differentiation results in the same function multiplied by E. This behavior is possessed by the sine function:

Now we need to calculate the values ​​of the constants α and A. In the case, we use the wave equations on the barriers, where the wave functions are equal to 0.

Substitute the value for α:

By requiring the normalization of the wave function, it is possible to determine the value of A. This statement is true because the particle must exist somewhere in the box. Therefore, the sum of the probabilities of finding a particle in the box is 1:

Substituting the values, we get the final wave and energy equations:

Visualizing energy and wave functions using Python:

``````import matplotlib.pyplot as plt
import numpy as np
#Constants
h = 6.626e-34
m = 9.11e-31
#Values for L and x
x_list = np.linspace(0,1,100)
L = 1
def psi(n,L,x):
return np.sqrt(2/L)*np.sin(n*np.pi*x/L)
def psi_2(n,L,x):
return np.square(psi(n,L,x))
plt.figure(figsize=(15,10))
plt.suptitle("Wave Functions", fontsize=18)
for n in range(1,4):
#Empty lists for energy and psi wave
psi_2_list = []
psi_list = []
for x in x_list:
psi_2_list.append(psi_2(n,L,x))
psi_list.append(psi(n,L,x))
plt.subplot(3,2,2*n-1)
plt.plot(x_list, psi_list)
plt.xlabel("L", fontsize=13)
plt.ylabel("Ψ", fontsize=13)
plt.xticks(np.arange(0, 1, step=0.5))
plt.title("n="+str(n), fontsize=16)
plt.grid()
plt.subplot(3,2,2*n)
plt.plot(x_list, psi_2_list)
plt.xlabel("L", fontsize=13)
plt.ylabel("Ψ*Ψ", fontsize=13)
plt.xticks(np.arange(0, 1, step=0.5))
plt.title("n="+str(n), fontsize=16)
plt.grid()
plt.tight_layout(rect=[0, 0.03, 1, 0.95])``````

Please note that there are areas where both and Ψ * Ψ are equal to zero. These are known as knots. Orbital energy levels are not continuous. They exist at different levels, which is demonstrated by their location. In addition, as the value of n increases, the density of the wave inside the box also increases.

### Orbital visualization

Now, to get the wave equation for quantum numbers, we have to convert it into the following three-dimensional format:

Separation of variables depends on the type of atom and is too complicated for this article. Instead, we just write the solution directly for plotting. Next, we will use the functions R and Y for the hydrogen atom without deriving them. Consider the 1s orbital first:

The wave function of the 1s-orbital shows that the probability of an electron appearing exponentially decreases with distance from the nucleus. It also exhibits a spherical orbital shape.

``````import matplotlib.pyplot as plt
import numpy as np
#Probability of 1s
def prob_1s(x,y,z):
r=np.sqrt(np.square(x)+np.square(y)+np.square(z))
#Remember.. probability is psi squared!
return np.square(np.exp(-r)/np.sqrt(np.pi))
#Random coordinates
x=np.linspace(0,1,30)
y=np.linspace(0,1,30)
z=np.linspace(0,1,30)
elements = []
probability = []
for ix in x:
for iy in y:
for iz in z:
#Serialize into 1D object
elements.append(str((ix,iy,iz)))
probability.append(prob_1s(ix,iy,iz))

#Ensure sum of probability is 1
probability = probability/sum(probability)
#Getting electron coordinates based on probabiliy
coord = np.random.choice(elements, size=100000, replace=True, p=probability)
elem_mat = [i.split(',') for i in coord]
elem_mat = np.matrix(elem_mat)
x_coords = [float(i.item()[1:]) for i in elem_mat[:,0]]
y_coords = [float(i.item()) for i in elem_mat[:,1]]
z_coords = [float(i.item()[0:-1]) for i in elem_mat[:,2]]
#Plotting
fig = plt.figure(figsize=(10,10))