Suppose I have a vector equation:




where "rhs[Y[t]]" is a black box function which numerically calculates a N-length vector given a N-length vector Y[t].

How can I solve such an equation in "NDSolve" ?

Update: Say my rhs is :

  • $\begingroup$ An example of exactly this kind of equation is in my answer to "I'd like to display field lines for a point charge in 3 dimensions". The equation for a field line is exactly of the type you mention. If you need more details on what I did there, let me know. $\endgroup$
    – Jens
    Jul 6, 2012 at 5:00
  • $\begingroup$ Many Thanks. It would be great if you let me know more. Let me tell you what I am not looking for, $\endgroup$ Jul 6, 2012 at 5:26
  • $\begingroup$ I just added an answer with the relevant parts of my earlier link, applied to a simple example rhs. The added ingredient here is to use Array to generate the vectors. $\endgroup$
    – Jens
    Jul 6, 2012 at 5:32
  • $\begingroup$ Sorry that I did not make my question clear. I want to know if rhs[Y[t]] does not have a symbolic form. That is what I meant by "numerically calculates". $\endgroup$ Jul 6, 2012 at 5:47
  • $\begingroup$ Hello & welcome! If you register for this site you will be able to access more features, for example voting. $\endgroup$ Jul 6, 2012 at 5:49

1 Answer 1


This is a classic example from documentation center. I will modify this slightly, to make it more complex.

A = RandomReal[{0, 1}, {5, 5}];

Now you can actually mix scalar and vector functions - 'NDSolve' will understand it:

fs = x /.First[NDSolve[{x'[t] == 1 - Norm[x[t]] A.Sin[x[t]], 
      x[0] == RandomReal[1, 5]}, x, {t, 0, 23}]];

Visualize typically:

Plot[fs[t], {t, 0, 23}, ColorFunction -> Hue, PlotStyle -> Thick, 
 Frame -> True, FillingStyle -> Opacity[0.05], Filling -> 0]

enter image description here

Or visualize in an interesting way:

ParametricPlot3D[ fs[t][[#1 ;; #2]] & @@@ {{1, 3}, {2, 4}, {3, 5}}, {t, 0, 23}, 
  PlotRange -> All, PlotStyle -> Thick, ColorFunction -> Hue, 
  ImageSize -> 450] /. Line[pts_, rest___] :> Tube[pts, 0.1, rest]

enter image description here

  • $\begingroup$ It works perfectly, $\endgroup$ Jul 6, 2012 at 5:52
  • $\begingroup$ Many thanks it works. $\endgroup$ Jul 6, 2012 at 5:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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