Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

Folder full of pertinent coursework

1666 views
1
class Schrodinger(object):
2
3
def __init__(self, V, start=-1, end=1, npts=100, mass=1.0):
4
self.m = mass
5
self.n = npts
6
self.x = np.linspace(start,end,npts)
7
self.dx = abs(self.x[1]-self.x[0])
8
self.__init_laplacian__()
9
10
self.Vx = V(self.x)
11
self.H = (-0.5/self.m)*self.laplace + np.diag(self.Vx)
12
self.E = np.array([])
13
self.psi = []
14
15
def __init_laplacian__(self):
16
M = -2*np.identity(self.n,'d')
17
for i in xrange(1,self.n):
18
M[i,i-1] = M[i-1,i] = 1
19
self.laplace = M / self.dx**2
20
21
def diagonalize(self):
22
E,U = np.linalg.eigh(self.H)
23
psi = []
24
for i, e in enumerate(E):
25
psi.append(U[:,i]/np.sqrt(self.dx) + e)
26
self.E = E
27
self.psi = psi
28
29
def plot_spectra(self,levels=3):
30
if not self.psi: return
31
plt.plot(self.x, self.Vx ,color='k')
32
for i in xrange(0,levels):
33
plt.axhline(y=self.E[i],color='k',ls=":")
34
plt.plot(self.x,self.psi[i])
35
plt.title("Spectra and Eigenfunctions")
36
plt.xlabel("Position")
37
plt.ylabel("Energy")
38
39
def wavefunction(s = Schrodinger, f = lambda x: no.exp(-100*(x-1)**2)):
40
dt = s.dx
41
now = f(s.x.astype(complex))
42
while True:
43
yield now
44
now = now - dt*(1j)*np.dot(s.H, now)
45
46
def wave_to_prob(a):
47
p = (np.conjugate(a)*a).real
48
return p/p.sum()
49
50
def plot_prob(s, w):
51
wave_to_prob(w)
52
53