Skip to main content
4 of 4
added 89 characters in body
Anjan Kumar
  • 5k
  • 1
  • 17
  • 29

If I understand it correctly, I think the following is what you want:

Solve the differential equation of n variables, with initial conditions defined using the previous differential equation solution of n-1 variables and with an initial condition for the last variable (which randomly depends on one of the previous variables).

The following code uses iniCond which generates initial conditions for a given i, diffGen generates equations with the initial conditions, and finally solveDiffGen solves the differential equations.

T = 50;
nu = 0.05;
vars[i_] := Subscript[x, #][t] & /@ Range[i];
iniCond[1] = {Subscript[x, 1][0] == 0.7};
diffGen[i_] := 
  Join[Table[{Subscript[x, j]'[t] == 
       Subscript[x, j][
         t] (1 - Subscript[x, j][t] - 
          nu (Sum[Subscript[x, k][t] Boole[k != j], {k, i}]))}, {j, 
      i}], iniCond[i]] // Flatten;
iniCond[i_] := 
  Module[{prevSolConds, lastCond}, 
   prevSolConds = 
    NDSolve[diffGen[i - 1], 
       vars[i - 1], {t, 0, 
        T}] /. (x_ -> g_) :> {(x /. {t -> 0}) == (g /. {t -> T})} // 
     Flatten;
   lastCond = 
    Subscript[x, i][0] == 
      0.01 Subscript[x, RandomInteger[{1, i - 1}]][t] /. t -> T;
   {prevSolConds, lastCond} // Flatten];
solveDiffGen[i_] := NDSolve[diffGen[i], vars[i], {t, 0, T}];
Anjan Kumar
  • 5k
  • 1
  • 17
  • 29