Using this technique from Simon, I want to solve a matrix differential equation with a matrix that contains an explicit time-dependence(our variable), but I want to be able to solve this problem for a generic number of functions K+1 thus auto-indexing them. My code, stripped of a few things which don't seem to be the problem, (with I= imaginary unit):


dl = ConstantArray[ 1/2 Exp[-I t], K];       %defining M
dm = Array[(# - 1)^2 &, K + 1];
dr = ConstantArray[1/2 Exp[I t], K];
M = SparseArray[{Band[{1, 2}] -> dr, Band[{1, 1}] -> dm, 
Band[{2, 1}] -> dl}] // Normal

a[t_] := Table[g[k][t], {k, 0, K}]          %defining a as a list of functions g

NDSolve[{a'[t] == I *M.a[t], g[0][0] == 0, g[1][0] == 1, g[2][0] == 0}, {g[0], g[1], g[2]}, {t, 0, 10}][[1, All, 2]] //Abs
%solving the diff. eq.

Which returns a list of three InterpolatingFunctions with output scalar as I would expect. However,


Only draws axes without any graphs. But I solved these equations for K=2 beforehands in another way(but still with DSolve) and saw some results which should also be shown the domain of this graph, but are missing. Similarly, function evaluation for a random value of t


Doesn't give a number either.

My intention when this works for a generic K is add a manipulate to change some parameters I put equal to one now and make the whole thing a function of K.

  • $\begingroup$ Welcome to Mathematica.SE! I suggest the following: 0) Browse the common pitfalls question 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Read the faq! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$ Oct 22 '15 at 13:32
  • $\begingroup$ Just a reminder: (*comments look like this in Mathematica*), single line comments aren't really used. The answer here provides some discussion on the topic. $\endgroup$
    – dionys
    Oct 22 '15 at 13:40
sol = NDSolveValue[{a'[t] == I*M.a[t], g[0][0] == 0, g[1][0] == 1, 
                    g[2][0] == 0}, {g[0], g[1], g[2]}, {t, 0, 10}];

Plot[Abs /@ Through[sol[t]], {t, 0, 10}, Evaluated -> True]

Mathematica graphics

  • $\begingroup$ Thanks for your answer @belisarius is forth. A curious thing however is that Evaluated is not in the documentation as an option for plot $\endgroup$
    – Wouter
    Oct 22 '15 at 14:02
  • $\begingroup$ @Wouter 1) See mathematica.stackexchange.com/q/11772/193 2) Learn how to upvote answers $\endgroup$ Oct 22 '15 at 14:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.