Solving Time dependent Schrodinger equation using MATLAB ode45 [closed]

The Schrodinger equation for time-dependent Hamiltonian is

$$i\hbar\frac{d}{dt}\psi(t) = H(t)\psi(t) \, .$$

I try to implement solve the Schrodinger equation for time-dependent Hamiltonian in ODE 45. However, because the Hamiltonian $H(t)$ is dependent on time, I do not know how to do interpolation in ode45. Can you give me some hints?

psi0 = [0 1];
H = [1 0;0 1]*cos(t); %this is wrong, I do not know how to implement this and pass it to ode45
hbar = 1;
t    = [0:1:100];
[T, psi] = ode45(dpsi, t, psi);
function dpsi = f(t, psi, H, psi0)
dpsi = (1/i)*H*psi;

closed as off-topic by Geoff OxberryJul 21 '16 at 21:43

• This question does not appear to be about computational science within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.

• Try the MATLAB ODE function ode15s instead of ode45, mathworks.com/help/matlab/ref/ode15s.html. You can specifiy a time-dependent "mass" matrix with ode15s. – Bill Greene Jun 23 '16 at 0:07
• Thanks. I also heard that mass matrix can be defined for ode45 by adding extra row. Is that true? – Ka-Wa Yip Jun 23 '16 at 3:08
• @kww why do you think there is a need to explicitly pass $H(t)$ into your ode45 call? You can just have the definition for $H(t)$ inside your function "dpsi". dpsi receives the current time stamp as one of its parameters. – okrzysik Jun 23 '16 at 7:13
• Apparently I misunderstood your question. I thought you were looking for a convenient way to deal with the $ih$ term in your equation. Mass matrix would be useful for that. But after more careful reading, I am not sure what your question is. I have no experience with Schrodinger equation and your post doesn't provide much information. Is $\psi$ a vector of length two? – Bill Greene Jun 26 '16 at 12:04
• I'm voting to close this question because it's a cross-post of stackoverflow.com/questions/37981618/…, and cross-posting is frowned upon. – Geoff Oxberry Jul 21 '16 at 21:43

Possibly this is what you want?

function schrodingerEqn
psi0 = [0 1];
hbar = 1;
t    = [0:1:100];
fh = @(t, psi) f(t, psi, hbar);
[T, psi] = ode45(fh, t, psi0);
figure; plot(T, real(psi(:,2)));
end

function dpsi = f(t, psi, hbar)
dpsi = 1/(hbar*i)*[1 0;0 1]*cos(t)*psi;
end