# Finding the first five eigenvectors and values for a 500×500 matrix

I'm trying to find the first five eigenvalues and vectors of a matrix that has the following proporties:

L = 10;
NN = 500;
ℏ = 1;
a = 1;
V0 = 5;
dx = L/NN;
Clear[x]
x[n_] := -L/2 + (n - 1/2) dx
Vsw = -V0* i Piecewise[{{1, -1/2 < x < 1/2}, {0, x > 1/2}, {0, x < -1/2}}];
i = SparseArray[{{i_, i_} -> 1, {i_, j_} /; Abs[i - j] == 1 -> 0}, {500, 500}];
s = SparseArray[{{i_, i_} -> -2, {i_, j_} /; Abs[i - j] == 1 -> 1}, {500, 500}];
Hkin = 1/(dx)^2 s;
Hsw = Hkin + Vsw;


In simple English this means I have two matrices:

• one that is allmost diagonal with constant values
• another that is diagonal with a constant value, but only for a some region of definition of x[n]

I think using piecewise is not good here, but I can't seem to find anything else that works. Also, I'm not sure if my use of n is OK here.

-
It looks like you are trying to set up a discretised version of the Schroedinger equation. It's difficult to help here because it's not clear what each bit is supposed to do and what it is you're stuck with. You basically construct two matrices, presumably aiming to use one for the kinetic term and the other for the potential, but eg it's not necessary to have {i_, j_} /; Abs[i - j] == 1 -> 0} in the i matrix, and you don't say what you are hoping to do with i and s. So it'll probably be helpful if you clarify and simplify your question a bit. –  acl May 30 at 12:24
@acl Thats exactly right! if you look closley, youll see that Hsw = Hkin + Vsw is contructed by the matrices, and im trying to use EigenSystem[Hsw, 5] in order to find what i need –  eyal May 30 at 12:26
@eyal Is Vsw supposed to contain numeric entries? Yours has an x. See my answer, and please clarify if I've misinterpreted your intention. –  Michael E2 May 31 at 21:42

There probably is, as the OP seems to suspect, a problem with the way x and n are used. The appearances of x[n] and x in the OP's code suggest a lack of clarity in how x should be used. It looks to me that Vsw should be

x[n_] := -L/2 + (n - 1/2) dx    (* L, dx as in OP *)
Vsw = SparseArray[{{i_, i_} :> -V0 * Piecewise[{{1, -1/2 < x[i] < 1/2}}]}, {500, 500}];


If that guess is correct, then the first 5 eigenvectors or eigenvalues, or both as below, may be approximated as follows:

Hsw = Hkin + Vsw;               (* Hkin as in OP *)

{val, vec} = Eigensystem[N@Normal@Hsw, 5]; // AbsoluteTiming
val
(*
{0.030559, Null}
{-10002.5, -9999.66, -9999.47, -9998.61, -9997.98}
*)


The "Arnoldi" method may be used, too. The "Arnoldi" method lets you shift the eigenvalues in the algorithm, which controls which eigenvectors are the "first five". To get the above, use something like "Shift" -> -10000; to get the eigenvalues near zero, use "Shift" -> 0. (There are no nonnegative eigenvalues.)

{val, vec} = Eigensystem[N@Hsw, 5,
Method -> {"Arnoldi", "MaxIterations" -> 10000, "Shift" -> -10000}]; // AbsoluteTiming
val
(*
{0.001820, Null}
{-10002.5, -9999.66, -9999.47, -9998.61, -9997.98}
*)

{val, vec} = Eigensystem[N@Hsw, 5,
Method -> {"Arnoldi", "MaxIterations" -> 10000, "Shift" -> 0}]; // AbsoluteTiming
val
(*
{0.001433, Null}
{-3.33445, -1.66045, -1.51436, -0.415767, -0.383297}
*)


Oddly, specifying the "Shift" speeds things up, even from a fresh kernel:

{val, vec} = Eigensystem[N@Hsw, 5,
Method -> {"Arnoldi", "MaxIterations" -> 10000}]; // AbsoluteTiming
val
(*
{0.181837, Null}
{-10002.5, -9999.66, -9999.47, -9998.61, -9997.98}
*)


Exact solutions take longer than I waited.

Arbtirary precision reals may be used by setting the precision like this: N[Hsw, 20]. For 20-digit reals, the calculation takes 1.67 seconds.

-
Would you reopen? You clearly put a lot of effort in this answer... –  Yves Klett May 31 at 19:20
@YvesKlett Thanks. I've voted to reopen. I was going to wait for the OP to come back, so that the question could be stated a little more clearly. –  Michael E2 May 31 at 19:30