5
$\begingroup$

I'd like to set up...

NDSolve[x1'[t]==2,x1[0]==0,
x2'[t]==2,x2[0]==0,
x3'[t]==2,x3[0]==0,
x4'[t]==2,x4[0]==0,
x5'[t]==2,x5[0]==0,
... etc

Can I somehow use x[t,1] instead of x1[t]? I'd like to make all these equations programmatically instead of typing each one out by hand.

$\endgroup$
6
  • 1
    $\begingroup$ ... but those are uncoupled! $\endgroup$ – Dr. belisarius Jul 20 '15 at 19:10
  • 7
    $\begingroup$ try a syntax like this: NDSolve[Table[{x[i]'[t] == 2, x[i][0] == 0}, {i, 2}], Table[x[i][t], {i, 2}], t] $\endgroup$ – george2079 Jul 20 '15 at 19:11
  • $\begingroup$ @belisarius. ... and they might all have identical solutions! $\endgroup$ – m_goldberg Jul 20 '15 at 20:51
  • 3
    $\begingroup$ ...although thank you for posting a simple example rather than 27 equations full of Subscripts that we have to parse on our own. $\endgroup$ – march Jul 20 '15 at 22:33
  • 3
    $\begingroup$ A worked out example can be found in the docs for the Method of lines $\endgroup$ – yohbs Jul 20 '15 at 22:47
2
$\begingroup$

Imagine you want to solve

(*
{3 f[1][t]+  f[2][t]+  f[3][t]+2 f[1]′[t]+3 f[2]′[t]+4 f[3]′[t]==1,
 4 f[1][t]+4 f[2][t]+  f[3][t]+2 f[1]′[t]+  f[2]′[t]+  f[3]′[t]==1,
   f[1][t]+4 f[2][t]+  f[3][t]+3 f[1]′[t]+  f[2]′[t]+  f[3]′[t]==4}
*)

and then you have the matrices for the coefficients:

funM  = {{3, 1, 1}, {4, 4, 1}, {1, 4, 1}};
deriM = {{2, 3, 4}, {2, 1, 1}, {3, 1, 1}};
indep = {1, 1, 4};

Then you may do:

af        = Array[f, n];
taf[t_]  := Through[af[t]]
tafD[t_] := Through[Thread[af' ][t]]
sol = DSolve[Join[Thread[deriM.tafD[t] + funM.taf[t] == indep], Thread[taf[0] == 0]], 
             af, t];
Plot[taf[t] /. sol, {t, 0, .2}, Evaluated -> True]

Mathematica graphics

$\endgroup$
1
$\begingroup$

Here's another way: Have all the functions stored in one. The following computes 100 solutions to an ode satisfying 100 differential initial conditions.

{sol} = NDSolve[{x'[t] == Sin[x[t]], x[0] == Range[100]/8}, x, {t, 0, 7}]

Mathematica graphics

Since they're wrapped up in one function, they're a bit hard disentangle:

Plot[x[t] /. sol // Evaluate, {t, 0, 7}]

Mathematica graphics

But not impossible:

ListLinePlot[
 Transpose[{x["Grid"] /. sol // Flatten, #}] & /@ 
  Transpose[x["ValuesOnGrid"] /. sol]
 ]

Mathematica graphics

If you insist on actual functions, then we can extract the values of the function and its derivatives (which NDSolve stores in the solution), and thread them through individual Interpolations.

xsol = With[{x0 = x["Grid"] /. sol},
   With[{y0 = Transpose[x["ValuesOnGrid"] /. sol],
         p0 = Transpose[x'[t] /. sol /. t -> Flatten[x0]]},
    MapThread[
     Interpolation[Thread[{x0, #1, #2}]] &,
     {y0, p0}]
    ]];

Plot[Evaluate@Through[xsol[t]], {t, 0, 7}]

Mathematica graphics

$\endgroup$
3
  • $\begingroup$ xsol[t_?NumericQ][n_?NumericQ] := (x[t] /. sol)[[n]]; Plot[ Evaluate@Array[xsol[t], 4], {t, 0, 7}] $\endgroup$ – Dr. belisarius Aug 14 '15 at 8:36
  • $\begingroup$ Or Evaluated -> True $\endgroup$ – Dr. belisarius Aug 14 '15 at 8:37
  • $\begingroup$ @belisarius I thought about the extracting parts, but I think that means you're computing all the values and throwing them all away except one. For dimension 100, it would be 100 times slower. But the Plot is only three times slower. Hmm. There something I don't understand. $\endgroup$ – Michael E2 Aug 14 '15 at 10:03

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.