2
$\begingroup$

I'm trying to construct a series of differential equations that defines the behavior of m mutually interacting species according to the Lotka-Volterra equations. I figure there must be some clever Mathematica way to define them all in one go without having to manually write out each equation but unfortunately I'm quite inexperienced. How would I do this?

n1'[t] == r1 n1[t] (1 + a11 n1[t] + a12 n2[t]+a13 n3[t] ...);
n2'[t] == r2 n2[t] (1 + a22 n2[t] + a21 n1[t]+a23 n3[t] ...);
n3'[t] == r3 n3[t] (1 + a33 n3[t] + a31 n1[t]+a32 n3[t] ...);
n4'[t] == r4 n4[t] (1 + a44 n4[t] + a41 n1[t]+a42 n4[t] ...);
.
.
.
$\endgroup$
2
  • 1
    $\begingroup$ while possible, personally I would use definitions instead of symbol names per se, e.g. n[1]'[t] == r[1] n[1][t] (1 + a[1,1] n[1][t] ...), together with either a Table or Array to vary the indices! $\endgroup$
    – thorimur
    Commented May 14, 2021 at 0:53
  • $\begingroup$ (note by the way that == does not define things; it's merely part of an expression. so, you'll want to keep these expressions around in some kind of container, like a list (as would be produced by a Table), and store that in a variable. in that case you'll want to avoid the semicolon at the end!) $\endgroup$
    – thorimur
    Commented May 14, 2021 at 0:55

2 Answers 2

4
$\begingroup$

Using symbols like "n1" makes it complicated. I would rather use indexed symbols like "n[1]" or subscripted symbols.

Clear["Globals`*"]
num = 4;
as = Table[a[i, j], {i, num}, {j, num}]
ns = Table[n[i][t], {i, num}]
Do[n[i]'[t] = r[i] + n[i][t] (1 + as[[i]].ns), {i, num}]

This does define the derivatives:

n[2]'[t]

(* r[2] + n[2][t] (1 + a[2, 1] n[1][t] + a[2, 2] n[2][t] + a[2, 3] n[3][t] + a[2, 4] n[4][t]) *)

Using subscripted symbols this runs like:

Clear["Globals`*"]
num = 4;
as = Table[Subscript[a, i, j], {i, num}, {j, num}]
ns = Table[Subscript[n, i][t], {i, num}]
Do[Subscript[n, i]'[t] = Subscript[r, i] + Subscript[n, i][t] (1 + as[[i]].ns), {i, num}]

Subscript[n, 2]'[t]

enter image description here

$\endgroup$
1
$\begingroup$

This can be done using Interactions in my EcoEvo package. First, install the package (only needed first time):

PacletInstall["EcoEvo", "Site" -> "http://raw.githubusercontent.com/cklausme/EcoEvo/master"]

Then load the package and define the model:

<< EcoEvo`
SetModel[{
  Guild[n] -> {
    Equation :> Subscript[r, \[FormalI]] Subscript[n, \[FormalI]] (1 + Sum[Subscript[a, \[FormalI], \[FormalJ]] Subscript[n, \[FormalJ]], {\[FormalJ], Subscript[\[ScriptCapitalN], n]}]),
    Traits -> {r}
  },
  Interaction[a] -> {Guilds -> {n, n}}
}];

All those special characters look better in Mathematica:

enter image description here

To generate your equations, use EcoEqns:

EcoEqns[{Subscript[\[ScriptCapitalN], n] -> 3}]

enter image description here

Of course you can do a lot more than generate the equations. EcoSim will solve them:

SeedRandom[1234];
nsp = 20; (* number of species *)
tmax = 2000;
(* random interaction matrix *)
A = Table[RandomReal[{-1, 0.1/nsp}], {i, nsp}, {j, nsp}];
(* random growth rates *)
R = Table[RandomReal[], {nsp}];
sol = EcoSim[{a -> A, r -> R}, Table[Subscript[n, i] -> 0.1/nsp, {i, nsp}], tmax];

PlotDynamics[sol]
PlotGuild[sol, DataRange -> {All, {1, nsp}}]

enter image description here

enter image description here

The Interactions functionality isn't the best-developed yet, but see also this example.

$\endgroup$

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.