I have written the following code:
Clear["Global`*"]
n = 100; (*number of sites*)
c = 1.; (*coupling constant*)
V0 = 1.;
(*k0=0.6 \[Pi]/4; (*initial momentum*)*)
sigma = 5;(*wavepacket width*)
xint = 20.; (*initial position*)
dt = 0.00005;
t = 0.05;
H = IdentityMatrix[n];(*Hemiltonian*)
For[i = 1, i <= n, i++,
For[j = 1, j <= n, j++,
H[[i, j]] = If[i == j, V0 Exp[-(1/2) (i - (n/2))^2], 0];
If[i == j + 1, H[[i, j]] = -c];
If[i == j - 1, H[[i, j]] = -c]]] (*setting the hemiltonian*)
(*a. prepare a gaussian initial state concentrated near the i=1 site \
with some initial momentum to the right*)
psi00[x_] =
Exp[-(x - (xint))^2/(2 sigma^2)] Exp[-
I k0 x]; (*creating the psi function*)
psi0 = Array[psi00, n]; (*making the x axis i (site number)*)
psi = psi0/Norm[psi0]; (*normalizing*)
(*b. setting the potential*)
vfunc[x_] = V0 Exp[-(1/2) (x - (n/2))^2];
v = Array[vfunc, n];
(*c. generate the evolution function*)
U = MatrixExp[I H dt];
(*d. compute the wavefunction at time t and site i*)
Tpsi = Range[t/dt];
For[i = 1, i <= t/dt, i++; U = U.U,
Tpsi[[i]] =
U .psi] (*this gives us matrica, the wave propagate with the row \
number, for example, row 1 is the initial state, row 2 is the state \
after we apply the evolution operator once, row 3 is twice, and so on*)
(*---------------PROBLEM STARTS HERE-------------------*)
(*with the ploting of the function*)
(*e. ploting the results*)
TTpsi = Range[t/dt];
For[i = 1, i <= t/dt, i++, TTpsi[[i]] = Abs[Tpsi[[i]]]^2]
Manipulate[
ListPlot[{TTpsi[[k]], v}, PlotRange -> 1], {k, 1, 100,
1}, {k0, {0.6 \[Pi]/8, 0.6 \[Pi]/4, 0.6 \[Pi]/2, 0.6 \[Pi]}}]
The result should be plotting the TTpsi[[k]] for each k and we should see the wave scattering on the potential.
I get 2 errors.
The first one is when I use constant k0,
Flatten::normal: Nonatomic expression expected at position 1 in Flatten[$Failed].
The second one is when I do not define k0 and put different values in the manipulate function. The error I get is just "aborted" instead of the graph.
Any idea how to fix this? Thank you in advance.