4
$\begingroup$

I am trying to numerically integrate a system of nonlinear differential equations.

Say for simplicity it is $\dot x_1 = x_2, \dot x_2 = (1-x_1) x_2$. I hand this to NDSolve as

NDSolve[{X'[t] == { X[t][[2]], (1-X[t][[1]])X[t][[2]] }, X[0] == {1,1} }, X[t], {t,0,1}]

However I run into a problem as X[t][[1]] evaluates to t and NDsolve integrates the different equation $\dot x_1 = x_2, \dot x_2 = (1-t) x_2$.

Is their a simple way around this problem? More generally is their a way to stop Part from taking function arguments, but instead to only act as a vector index?

I know in this case I could just write system explicitly, but in the case I am studying the dimensionality of the system is not known when writing the code, and we have X'[t] == f[X[t]] where f is a function that returns a vector of nonlinear combinations of the parts of X[t] according to a pattern. Hold prevents Part evaluating (which is good) however it also prevents fevaluating (which is bad).

$\endgroup$
  • 4
    $\begingroup$ Try NDSolve[{x'[t] == {Indexed[x[t], 2], (1 - Indexed[x[t], 1]) Indexed[x[t], 2]}, x[0] == {1, 1}}, x, {t, 0, 1}]. $\endgroup$ – J. M. will be back soon Apr 24 '17 at 2:34
  • $\begingroup$ Gar, that looks like basic knowledge I should have had, but it has made my life so much easier. If you put it as an answer I will accept it. $\endgroup$ – ComptonScattering Apr 24 '17 at 3:06
4
$\begingroup$

As J.M. pointed it out, if you want to refer to variables of a vectorial ODE, use Indexed as the symbolic counterpart of Part.

NDSolve[{
     x'[t] == {Indexed[x[t], 2], (1 - Indexed[x[t], 1]) Indexed[x[t], 2]},     
     x[0] == {1, 1}}, x, {t, 0, 1}]

Note, that it is the initial condition that explicitly tells NDSolve that the dynamical system is vector-valued.

$\endgroup$

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.