An alternative to Indexed
to deal with matrices is to prevent f
from evaluating until a matrix value is substituted for rho[t]
:
f[r_?MatrixQ, t_] := r Cos[5 t] + r[[1, 2]] - Tr[r] Exp[t];rho0 = {{1, 2}, {3, 4}};sol = NDSolve[{D[rho[t], t] == f[rho[t], t], rho[0] == rho0}, rho, {t, 0, 10}];