Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Particle in a box

It is sometimes useful to plot data generated from theory and carry out analysis automatically. We will try this out on the solutions to the “particle in a box” problem from your first year.

The wavefunctions to solve the particle in a box have the form:

Ψn(x)=Asin(nπxL)\Psi_n (x) = A\sin{}\left(\frac{n\pi{}x}{L}\right)

where nn takes integer values from 1

Our goal is to write a program that plots multiple wavefunctions and counts how many nodes they have.

Learning Outcomes

Data analysis
Visualisation of data
Calculation of Quantum properties

Calculating the wavefunctions and energies

At this stage, you should be comfortable writing a function to calculate the energies. However, the wavefunctions are not simple scalar values but functions.

To plot a function, we will have to store it as numbers (not just as a programming function). We will have to capture a range of values where xx will be evaluated and the corresponding f(x)f(x). In other words, two lists.

For instance, using the code below we evaluate a function f(x)=sin(x)f(x)=sin(x) over a range [0,1][0,1] at 10 points.

import math

# number of points we will evaluate sin(x) at
n_points = 10

# create n_points evenly spaced as a list
x_data_unnormalised = list(range(n_points))

# normalise the range so that it goes from 0 to 1
x_data = []
for i in x_data_unnormalised:
    x_data.append(i/n_points)

# apply the sin() function to each value of x
y_data = []
for i in x_data:
    y_data.append(math.sin(i))

# print lists
print(x_data)
print(y_data)

Visualising multiple data series

It will take too much space if you plot every wavefunction on a separate plot. For better comparison, you may prefer to overlay the plots. We can achieve this easily using matplotlib. Here is an example of plotting xx, x2x^2, and x3x^3 all in one plot.

The code for generating the functions is similar to the code above for sin(x)\sin(x). Note that in the plotting we can also add a label to each of the data series that we plot, using the label parameter of plot, we then put the labels into the plot as a legend, before displaying the plot, using plt.legend(). Further information on matplotlib legends.

import math
import matplotlib.pyplot as plt

# number of points we will evaluate f(x) at
n_points = 10

# create n_points evenly spaced
x_data_unnormalised = list(range(n_points))

# normalise the range so that it goes from 0 to 1
x_data = []
for i in x_data_unnormalised:
    x_data.append(i/n_points)

fig, ax = plt.subplots()

# apply the functions and plot
# note, here we loop over the powers i=1, 2 and 3
for i in range(1,4):
    y_data = []
    for j in x_data:
        y_data.append(j**i)
    ax.plot(x_data, y_data, label = 'Power of %s'%i)

# plot the values
plt.legend()
plt.xlabel('x')
plt.ylabel('$f(x)$')
plt.show()

Exercise

Write a program that calculates an arbitrary number of particle in a box wavefunctions for different values of nn and plots them all in one same figure. Also write a function which counts how many nodes this function has.

Hint: choose a value for LL and calculate Ψ\Psi for each xx value in the range [0,L][0, L].