I am trying to compute the eigenfunctions of the Laplace operator, i.e. finding $u$ in
$$ -\nabla^2 u = \lambda u .$$
For now I am trying to do this in 1D, so
$$ \nabla^2 = \partial_{xx} .$$
I am using the finite difference method. My boundary conditions are $u=0$ at $x = -1, 1$. Here is my code, and the results:
import numpy as np
from scipy.sparse.linalg import eigs
import matplotlib.pyplot as plt
n = 200
h = 2/(n-1) # domain for x and y is [-1, 1]
L = np.diag(np.ones(n-1), k=-1) + np.diag(-2*np.ones(n)) + \
np.diag(np.ones(n-1), k=1)
L *= -1/h**2
eigvals, eigvecs = eigs(L)
eig = np.real(eigvecs[:, 0])
x = np.linspace(-1, 1, num=n)
plt.plot(x, eig)
plt.show()
Can anyone see what I am doing wrong, or give suggestions as to how I might be able to fix my code?