In [1]:
%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 [4]:
def well2d(x, y, nx, ny, L=1.0):
"""Compute the 2d quantum well wave function."""
return 2/L*np.sin(nx*np.pi*x/L)*np.sin(ny*np.pi*y/L)
In [5]:
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 [26]:
f=plt.figure(figsize=(10,10))
x=np.linspace(0,1,100)
y=np.linspace(0,1,100)
xx, yy=np.meshgrid(x, y)
z=well2d(xx,yy,3,2,1)
plt.contourf(x,y,z,50,cmap=plt.cm.get_cmap("hot"))
plt.colorbar(label=r"$\Psi (x,y)$")
plt.xlabel("X Position")
plt.ylabel("Y Position")
plt.title("The wavefunction of a 2D inifinite well")
Out[26]:
In [ ]:
assert True # use this cell for grading the contour plot
Next make a visualization using one of the pcolor functions:
In [27]:
f=plt.figure(figsize=(10,10))
x=np.linspace(0,1,100)
y=np.linspace(0,1,100)
xx, yy=np.meshgrid(x, y)
z=well2d(xx,yy,3,2,1)
plt.pcolor(x,y,z,cmap="RdBu")
plt.colorbar(label=r"$\Psi (x,y)$")
plt.xlabel("X Position")
plt.ylabel("Y Position")
plt.title("The wavefunction of a 2D inifinite well")
Out[27]:
In [28]:
assert True # use this cell for grading the pcolor plot
In [ ]: