# I want to solve a differential equation in matrix form

I tried something like that. Z is my hamiltonian

n = 5;

σ2 = 0.1;

RR = RandomReal[{-Sqrt[3*σ2], Sqrt[3*σ2]}, n];

Z = Table[
KroneckerDelta[i - j + 1] + KroneckerDelta[i - j - 1], {i, 1,
n}, {j, 1, n}] + DiagonalMatrix[RR];

usol = NDSolveValue[{I D[ψ[x, t], t] ==
Z.ψ[x, t], ψ[0, t] == 0, ψ[n, t] == 0}, ψ, {t,
0, 1}]


I solve the problem finding the eigenstates and eigenvalues, then I choose a one site and evolve in time but now I want to solve the differential equation directly to check my results.

• ψ is undefined.. Mar 2 at 0:26
• You may want to take a look at @Carl Woll's recent answer 240255. Mar 2 at 2:44
• Actually you do not provide initial conditions for ODE solver. Mar 2 at 9:04
• @OkkesDulgerci ψ is an unknown function :) Mar 2 at 9:08
• Your equation says nothing about the x dependence of the function. Mar 2 at 14:09

## 1 Answer

Assuming that $$\psi$$ is a normalized column vector of length $$n$$ that does not depend on "$$x$$", perhaps the OP is looking for something like this (following @Carl Woll's answers 210001 and 240255):

SeedRandom[1234];
n = 5;
tmax = 10;
σ2 = 0.1;
ψinit = Normalize@RandomReal[1, n];
RR = RandomReal[{-Sqrt[3*σ2], Sqrt[3*σ2]}, n];
Z = Table[
KroneckerDelta[i - j + 1] + KroneckerDelta[i - j - 1], {i, 1,
n}, {j, 1, n}] + DiagonalMatrix[RR];
Clear[ψ]
usol = NDSolveValue[{I D[ψ[t], t] ==
Z . ψ[t], ψ[0] == ψinit}, ψ, {t, 0, tmax}];
Plot[Abs[usol[t]], {t, 0, tmax}]
Plot[Abs[Total@usol[t]], {t, 0, tmax}]
Manipulate[
ReImPlot[usol[t][[i]], {t, 0, tmax}, PlotTheme -> "Web",
PlotLabel -> i], {i, 1, n, 1}]


• Thanks @TimLaska is too helpful Mar 2 at 21:01
• You are welcome! Thank you for the Accept. Mar 2 at 21:20