TransWikia.com

How to make a for loop to automatically make a series of m differential equations?

Mathematica Asked by phycobilly on August 9, 2021

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] ...);
.
.
.

One Answer

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

Answered by Daniel Huber on August 9, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP