We know finite difference method (FDM) can replace $y''(x)$ as $\frac{1}{h^2}[y(x+h)+y(x-h)-2y(x)]$ or so. The naive way to write down the matrix of the differential operator is like the following, which somehow simply cuts off at the two edges. Similarly, we can write for $y'$.
But what boundary condition (BC) correspond to this?
I first thought this means zero-value Dirichlet BC (like $y(a)=y(b)=0$). However, it turns out not to be the case. It automatically handles either zero or nonzero boundary values as shown in the solution below.
Based on such naive FDM, I solve the eigenvalue ($\lambda$) problem of this linear ODE $y''+\frac{2}{x}y'+[2\lambda-x^2-\frac{l(l+1)}{x^2}]y=0$ with nonnegative integer $l$.
It is analytically solved in textbooks and we know at the boundary, $y(0)\neq0$ for $l=0$, $y(0)=0$ for $l>0$, and $y(\infty)=0$. Surely we use some large enough interval to mimic the infinity. The following code solves the two cases well. It should work well for $l>0$, but why also for $l=0$ case???
Two things I noticed but not sure if relevant or not:
1. If we really somehow impose a nonzero left boundary value $y(0)$, the value you use doesn't matter for any eigenvalue problem. But we intuitively think it at least should be nonzero.
2. For the strange $l=0$ case, we have $y'(0)=0$ in the analytic solution.
l = 0; a = 1; n = 1001; h = 16/(n - 1);
M1 = -2.0 IdentityMatrix[n] + DiagonalMatrix[Table[1, {n - 1}], 1] +
DiagonalMatrix[Table[1, {n - 1}], -1];(*//MatrixForm*)
M2 = DiagonalMatrix[Table[-1/(i + 1), {i, 1, n - 1}], -1] +
DiagonalMatrix[Table[1/i, {i, 1, n - 1}], 1];
M3 = DiagonalMatrix[Table[(i)^2, {i, 1, n}]];(*//MatrixForm*)
M4 = l (l + 1) DiagonalMatrix[Table[(i)^-2, {i, 1, n}]];
M = 1/h^2 M1 + h^-2 M2 - h^2 M3 - h^-2 M4;
qq = Eigenvectors[M];
ListPlot[qq[[n - a + 1]], PlotRange -> All]
M2 is for this $\frac{2}{x}y'=\frac{2}{ih}\frac{y(x_{i+1})-y(x_{i-1})}{2h}=\frac{y(x_{i+1})-y(x_{i-1})}{i\,h^{2}}$. $a$ means the $a$th smallest eigenvalue and certainly one can change the value of $l$ and $a$.