In [6]:
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
The wavefunction of a 2d quantum well is:
$$ \psi_{n_x,n_y}(x,y) = \frac{2}{L} \sin{\left( \frac{n_x \pi x}{L} \right)} \sin{\left( \frac{n_y \pi y}{L} \right)} $$This is a scalar field and $n_x$ and $n_y$ are quantum numbers that measure the level of excitation in the x and y directions. $L$ is the size of the well.
Define a function well2d that computes this wavefunction for values of x and y that are NumPy arrays.
In [7]:
def well2d(x, y, nx, ny, L=1.0):
"""Compute the 2d quantum well wave function."""
psi = (2 / L) * np.sin(nx * np.pi * x / L) * np.sin( ny * np.pi * y / L)
return psi
#raise NotImplementedError()
In [8]:
psi = well2d(np.linspace(0,1,10), np.linspace(0,1,10), 1, 1)
assert len(psi)==10
assert psi.shape==(10,)
The contour, contourf, pcolor and pcolormesh functions of Matplotlib can be used for effective visualizations of 2d scalar fields. Use the Matplotlib documentation to learn how to use these functions along with the numpy.meshgrid function to visualize the above wavefunction:
First make a plot using one of the contour functions:
In [10]:
gridx, gridy = np.meshgrid(np.arange(0.0, 1.0, .01), np.arange(0.0, 1.0, .01))
wave = well2d(gridx, gridy, nx = 3.0, ny = 2.0, L = 1.0)
plt.contour(gridx, gridy, wave)
plt.xlim(0,1)
plt.ylim(0,1)
plt.title("Wave Function of 2D Quantum Well")
plt.xlabel("x")
plt.ylabel("y")
#raise NotImplementedError()
Out[10]:
In [ ]:
assert True # use this cell for grading the contour plot
Next make a visualization using one of the pcolor functions:
In [13]:
gridx, gridy = np.meshgrid(np.arange(0.0, 1.0, .01), np.arange(0.0, 1.0, .01))
wave = well2d(gridx, gridy, nx = 3.0, ny = 2.0, L = 1.0)
plt.pcolor(gridx, gridy, wave)
plt.xlim(0,1)
plt.ylim(0,1)
plt.title("Wave Function of 2D Quantum Well")
plt.xlabel("x")
plt.ylabel("y")
#raise NotImplementedError()
Out[13]:
In [ ]:
assert True # use this cell for grading the pcolor plot