1

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.

Ben9172
  • 99
  • 3
  • Please try to do some troubleshooting yourself first. This is a lot of code for us to wade through. Try to reduce it to a minimal (non)working example, or to at least identify which part of the code generates the error (e.g. comment pieces out incrementally until you identify where the trouble starts). – MarcoB Jan 12 '20 at 16:39
  • 1
    A few things I see: the dependence of the argument of manipulate (TTpsi[[k]]) on the manipulate variable (k0) should be explicit, rather than buried in the definition of TTpsi (see https://mathematica.stackexchange.com/a/119092/27951). – MarcoB Jan 12 '20 at 16:51
  • i eddited it to show where the problem starts. Also, how can i implement what you said in the code (MarcoB)? i am not really ploting a function, i am using Listplot to plot each row of the matrix seperetly, how can i change psi00 to psi00[x_,k0_] and change the function array acordinly so it wouldnt change the way it build the vector? – Ben9172 Jan 12 '20 at 17:01

0 Answers0