I have this function to get the eigenvectors and eigenvalues of a matrix, in the last part I try to normalize to eigenvectors, and to make sure it worked I tried to check the orthonormality of them as shown down here:
import numpy as np
import math
from scipy import sparse
from scipy.sparse import linalg as sla
def harmonic_potential(x, pert):
V_pert = []
if pert == False:
return x**2
else:
for i in x:
V_pert.append(( i**2 + 4*math.exp(-10*i**2) ) )
return V_pert
def schrodinger1D(xmin, xmax, Nx, neigs, pert=False):
x = np.linspace(xmin, xmax, Nx)
dx = x[1] - x[0]
V = harmonic_potential(x, pert)
H = sparse.eye(Nx, Nx, format='lil') * 2
for i in range(Nx - 1):
H[i, i + 1] = -1
H[i + 1, i] = -1
H = H / (dx ** 2)
for i in range(Nx):
H[i, i] = H[i, i] + V[i]
H = H.tocsc()
[evl, evt] = sla.eigs(H, k=neigs, which='SM')
for i in range(0, neigs):
evt[:, i] = evt[:, i] / np.sqrt(
np.trapz(np.conj(
evt[:,i])*evt[:,i],x))
evl = np.real(evl)
return evl, evt, x
evl_test , evt_test , x_test = schrodinger1D(-10, 10, 500, 5)
product_evt = []
for i in range(0, 500):
product_evt.append( np.conjugate(evt_test[:, 0][i]) * evt_test[:, 0][i] )
sum_product = sum(product_evt)
But for some reason it is not working for evt_test[:, 0], the other eigenvectors are working just fine (returning 0 or 1), why is the first eigenvector not normalized? i.e why is the first iteration in the for loop not working?
1