I am using NDSolve[]
to solve a first-order system of linear ODEs. The system is a 12-equation system and I have other applications that will be larger. I typically store the coefficients of the ODEs in a matrix so that I can write the system like:
$$ \mathbf{n}^{\prime}(t) = \underline{\underline{\mathbf{m}}} \cdot \mathbf{n}(t)$$
Where $\mathbf{n}(t)$ is a vector of 12 functions and $\underline{\underline{\mathbf{m}}}$ is a 12x12 matrix of coefficients.
To get this into Mathematica I do something like:
m[l1_, l2_, l3_, l4_, l5_, l6_, l7_, l8_, l9_, l10_, l11_, b9a_,
b9b_] := -{{l1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {-l1, l2, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0}, {0, -l2, l3, 0, 0, 0, 0, 0, 0, 0, 0,
0}, {0, 0, -l3, l4, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, -l4, l5, 0,
0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, -l5, l6, 0, 0, 0, 0, 0, 0}, {0,
0, 0, 0, 0, -l6, l7, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, -l7, l8,
0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, -l8, l9, 0, 0, 0}, {0, 0, 0, 0,
0, 0, 0, 0, -l9*b9b, l10, 0, 0}, {0, 0, 0, 0, 0, 0, 0,
0, -l9*b9a, 0, l11, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, -l10, -l11,
0}}
f := {n1[t], n2[t], n3[t], n4[t], n5[t], n6[t], n7[t], n8[t], n9[t],
n10[t], n11[t], n12[t]}
LHS = m[λ1, λ2, λ3, λ4, λ5, λ6, λ7, λ8, λ9, λ10, λ11, b9a, b9b] .f
Then I follow by creating each equation, doing a command like the one below 12 times.
Eqns1 = LHS[[1]] == n1'[t]
...
Finally I use the NDSolve[]
to get the solution:
sol = NDSolve[{Eqns1, Eqns2, Eqns3, Eqns4, Eqns5, Eqns6, Eqns7, Eqns8,
Eqns9, Eqns10, Eqns11, Eqns12, n1[0] == 1, n2[0] == 0,
n3[0] == 0, n4[0] == 0, n5[0] == 0, n6[0] == 0, n7[0] == 0,
n8[0] == 0, n9[0] == 0, n10[0] == 0, n11[0] == 0,
n12[0] == 0}, {n1, n2, n3, n4, n5, n6, n7, n8, n9, n10, n11,
n12}, {t, 0, 1}];
I have a couple of questions about this setup.
- Can I write the equations more elegantly so that I don't have to write out each equation (12 separate ones in this case)?
- Is there a way to read the matrix coefficients from a file and make this into a procedure that will work for arbitrary system size?
l1
,l2
etc are specified numerically above the matrix definition in my notebook. @Nasser Yes, all linear. $\endgroup$