I think possible issue here was the fact that Piecewise
is used here to distinguish between different matrices inside NDSolve
. Since Piecewise
remains unevaluated until you put in specific time values, this hides the matrix structure from NDSolve
at the initial step where it parses the differential equation.
Also, I think the periodicity you want to achieve is formulated in a way that can't be used in an actual computation, so I would suggest rewriting it with Mod
instead.
To be on the safe side, I used MapThread
to put the Piecewise
inside the matrices (acting element-wise). Then I also defined the solution vector element-wise and used Thread
in the vectorial differential equation to convert it into a system of equations. Here is the result:
H1 = {{x, y, 0}, {y, x, 0}, {0, 0, x}}; H2 = {{x, y, z}, {y, x,
0}, {z, 0, x}};
x = 2; y = 3; z = 4; z = 4; n = 20; τ = 0.5;
h[t_] = MapThread[
Piecewise[{{#1, Mod[t, τ] <= τ/2}, {#2, True}}] &, {H1,
H2}, 2]
$$\left(
\begin{array}{ccc}
2 & 3 &
\begin{array}{cc}
\{ &
\begin{array}{cc}
0 & (t \bmod 0.5)\leq 0.25 \\
4 & \text{True} \\
\end{array}
\\
\end{array}
\\
3 & 2 & 0 \\
\begin{array}{cc}
\{ &
\begin{array}{cc}
0 & (t \bmod 0.5)\leq 0.25 \\
4 & \text{True} \\
\end{array}
\\
\end{array}
& 0 & 2 \\
\end{array}
\right)$$
ψ[t_] = Through[Array["ψ", 3][t]];
sol[t_] = ψ[t] /.
First@NDSolve[{Thread[
I ψ'[t] == h[t].ψ[t]], ψ[0] == {0, 1,
0}}, ψ[t], {t, 0, n τ}];
ParametricPlot3D[Re@sol[t], {t, 0, n τ}, PlotStyle -> Tube[.01]]

NDSolve
. $\endgroup$Piecewise
issue. This does indeed look like a separate issue, and since the Hamiltonian is now fixed it's worth keeping as a question in its own right, I think. $\endgroup$