4
$\begingroup$

I get no solution when I run the following command:

NDSolve[{
  x'[t] == 
    0. {{0}, {0}, {1.}} + {{0, 1., 0.}, {0, 0., 1.}, {0., -0.156816, -0.523512}} . x[t],
  x[0] == {0, 0, 0}}, 
  x, {t, 0, 1}]

The kernel just keeps running forever until I kill it. If I remove the 0. {{0}, {0}, {1.}} term, then it works fine.

I'd appreciate your help with this issue.

$\endgroup$
4
  • $\begingroup$ Perhaps you can use this as a workaround: psol = ParametricNDSolveValue[{x'[t] == {a, b, c} + {{0, 1., 0.}, {0, 0., 1.}, {0., -0.156816, -0.523512}}.x[t], x[0] == {0, 0, 0}}, x, {t, 0, 1}, {a, b, c}]; then psol[1., 2., 3.][t] or whatever vector you want instead of {a, b, c} = {1., 2., 3.}. (NDSolve gets stuck processing the equation -- looks like it might be a bug.) Wait and see if someone else knows.... $\endgroup$
    – Michael E2
    Jul 28, 2014 at 15:26
  • $\begingroup$ I'll use your workaround. Thanks! $\endgroup$
    – Rafael
    Jul 28, 2014 at 15:37
  • $\begingroup$ It works fine, if you put any non-zero term, like: 1.{1,2,3}. $\endgroup$
    – m0nhawk
    Jul 28, 2014 at 18:41
  • $\begingroup$ @m0nhawk To clarify, all the terms must be nonzero (at least for me). I think that's why the workaround in my comment works - {a, b, c} are symbolic and not zero. $\endgroup$
    – Michael E2
    Jul 30, 2014 at 10:49

2 Answers 2

3
$\begingroup$

This has been addressed before and up to Mathematica 9 (I only have 10 at work at the moment and it's a hassle to switch computers) you couldn't handle symbolic, non-explicit matrices in the generic way you want to.

I suggest that if you are happy with the answer to that post you delete this as duplicate or if you are using mathematica 10 (which is probably the case since a non-zero term doesn't work in 9) you tag appropriately.

In any case, one way to handle systems of ODEs in NDSolve (in any version, I hope) is by writing the functions explicitly and making use of Thread:

sol = Flatten@Module[{X, A, B, initialCondition, coef},
    coef = 0;
    X[t_] = {X1[t], X2[t], X3[t]};
    initialCondition = {0, 0, 0};
    A = {{0, 1., 0.}, {0, 0., 1.}, {0., -0.156816, -0.523512}};
    B = coef {0, 0, 1};
    NDSolve[{
      Thread[X'[t] == A.X[t] + B],
      Thread[X[0] == initialCondition]
      },
     X[t],
     {t, 0, 1}
     ]
    ];

Now as @m_goldberg points out, you get a trivial solution with coef=0 but for non-zero values you get a non-trivial solution. You can check the dependence of your equations by setting coef as a parameter to ParametricNDSolve

sol2 = Flatten@Module[{X, A, B, initialCondition},
    X[t_] = {X1[t], X2[t], X3[t]};
    initialCondition = {0, 0, 0};
    A = {{0, 1., 0.}, {0, 0., 1.}, {0., -0.156816, -0.523512}};
    B = coef {0, 0, 1};
    ParametricNDSolve[
     {
      Thread[X'[t] == A.X[t] + B],
      Thread[X[0] == initialCondition]
      },
     X[t],
     {t, 0, 1},
     coef
     ]
    ];

and now you can look at the variation of your solutions with non-zero coefficients:

Manipulate[
 ParametricPlot3D[
  Through[({X1[t], X2[t], X3[t]} /. sol2)[i]],
  {t, 0, 1},
  PlotRange -> {-.5, .5}],
 {i, -2, 2}]

manipulate of solutions vs coef

$\endgroup$
1
  • $\begingroup$ It might be worth pointing out that with coef = 0 the differential equation in question yields the trivial solution X[t_] = {0, 0, 0}. Non-zero values of coef give non-trivial solutions. $\endgroup$
    – m_goldberg
    Jul 30, 2014 at 10:25
3
$\begingroup$

One problem is that the equations evaluate to ones with inconsistent dimensions, because Plus threads the components of the constant vector/list term with the unevaluated Dot product. Since NDSolve is not HoldAll or HoldFirst, what the equation evaluates to is relevant. (V9.0.1 actually issues a warning about inconsistent dimensions).

{x'[t] == 0. {{0}, {0}, {1.}} + {{0, 1., 0.}, {0, 0., 1.}, {0., -0.156816, -0.523512}}.x[t],
 x[0] == {0, 0, 0}}
(*
  {Derivative[1][x][t] ==
    {{0. + {{0, 1., 0.}, {0, 0., 1.}, {0., -0.156816, -0.523512}}.x[t]},
     {0. + {{0, 1., 0.}, {0, 0., 1.}, {0., -0.156816, -0.523512}}.x[t]},
     {0. + {{0, 1., 0.}, {0, 0., 1.}, {0., -0.156816, -0.523512}}.x[t]}},
   x[0] == {0, 0, 0}}
*)

If x'[t] is a 3-vector, then the dimensions of the RHS work out to {3, 1, 3}. And if the constant term is changed to a more consistent-looking 0. {0, 0, 1}, the dimensions of the RHS are {3, 3}. For some reason, if all of the components of the constant term are nonzero, in V10 NDSolve processes the equation "correctly", that is, as intended. If one or more of the components are zero, then either the kernel runs forever or it crashes in V10; in V9 an error is issued. (I'm not sure why it works for nonzero components.)

The following system evaluates to consistent dimensions and works in V10/V9. Using a variable b[t] in place of the list keeps Plus from threading the components of b[t] prematurely.

sol = NDSolveValue[{
   x'[t] == b[t] + {{0, 1., 0.}, {0, 0., 1.}, {0., -0.156816, -0.523512}}.x[t],
   x[0] == {0., 0., 0.},
   b[0] == 0. {0., 0., 1.}},
  x, {t, 0, 1}, 
  DiscreteVariables -> {b}]
$\endgroup$
2
  • $\begingroup$ I didn't know that worked in 9 (you answered while I was editing)! +1 from me. $\endgroup$
    – gpap
    Jul 30, 2014 at 11:06
  • $\begingroup$ @gpap Thanks! I might have addressed a similar problem in a similar way before, but I haven't found it yet. $\endgroup$
    – Michael E2
    Jul 30, 2014 at 12:29

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.