0
$\begingroup$

The commands below

T = 100;
n = 5;
m = 5;

vars = Table[Subscript[x, j][t], {i, n}, {j, i}];

eqns = Table[{Subscript[x, j]'[t] == 
     Subscript[x, j][
       t] (1 - (Sum[
          If[j == k, 
            RandomReal[{$MachineEpsilon, 1 - $MachineEpsilon}], 
            RandomReal[]] Subscript[x, k][t], {k, i}]) ), 
    Subscript[x, j][0] == RandomReal[]}, {i, n}, {j, i}];

sol = Table[s = NDSolve[eqns[[l]], vars[[l]], {t, 0, T}], {l, m}];

Interpolates the following results, for example:

 {{x1}, {x1,x2}, {x1,x2,x3}, {x1,x2,x3,x4}, {x1,x2,x3,x4,x5}}

In the code above, I have parameters and initial conditions that are random numbers. To do some statistical analysis I need 100 different results.

I thought about doing

Table[sol,{q,100}]

However, it returns 100 equal results for

 {{x1}, {x1,x2}, {x1,x2,x3}, {x1,x2,x3,x4}, {x1,x2,x3,x4,x5}}

Can someone help me get 100 different lists?

$\endgroup$
2
  • 1
    $\begingroup$ Try modifying your definition of eqns to use SetDelayed (:=) instead of Set (=). This should force regeneration of your random values each time eqns is evaluated. Is that what you are looking for? $\endgroup$
    – MarcoB
    Commented Jan 25, 2017 at 19:11
  • $\begingroup$ I add SetDelayed instead of Set in eqns and sol. I got the result I wanted. Thanks @MarcoB $\endgroup$
    – SAC
    Commented Jan 25, 2017 at 19:26

1 Answer 1

1
$\begingroup$

Let us say your differential equation is of the form $ay''(t)+by'(t)+c=0$ with initial condition $y(0)=d$ and $y'(0)=e$. We will now solve the system with random coefficients $a$,$b$,$c$,$d$ and $e$.

We set up and solve our system using ParametricNDSolve.

sol = ParametricNDSolve[{a y''[t] + b y[t] + c == 0, y[0] == d, 
   y'[0] == e}, {y}, {t, 0, 10}, {a, b, c, d, e}]

Now we randomize all the parameters, substitute them into the solution and evaluate to get random solution functions in terms of $t$.

xt = Flatten[Evaluate[Table[y[a, b, c, d, e][t] /. sol, {a, RandomReal[1, 2]}, 
     {b,RandomReal[1, 2]}, {c, RandomReal[1, 2]}, {d, RandomReal[1, 2]},
     {e, RandomReal[1, 2]}]]];

We then evaluate all the functions (now in the form of $f(t)$) in the declared time interval.

mat = Table[Table[Evaluate[xt[[a]]], {t, 0, 10, 2}], {a, Range[Length[xt]]}]

This will give you 32 random solutions to the same differential equation with random coefficients.

The output will look something like this :

enter image description here

Each list being a solution to the given differential equation with random coefficients.

You can change the equation, initial condition and number of random solutions required as per your convenience.

$\endgroup$

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