Imagine I want to solve the following matrix system of equations
$$
\begin{pmatrix}
\dot{x}_{11} & \dot{x}_{12} & \dot{x}_{13}\\
\dot{x}_{21} & \dot{x}_{22} & \dot{x}_{23}\\
\dot{x}_{31} & \dot{x}_{32} & \dot{x}_{33}
\end{pmatrix}=
\begin{pmatrix}
a_{11}x_{11} & a_{12}x_{12} & a_{13}x_{13}\\
a_{21}x_{21} & a_{22}x_{22} & a_{23}x_{23}\\
a_{31}x_{31} & a_{32}x_{32} & a_{33}x_{33}
\end{pmatrix}
$$
where $x_{ij}\equiv x_{ij}(t)$ and $\{a_{ij}\}$ are some real coefficients. When $\{a_{ij}\}=1$, we can solve this with NDSolveValue
as follows
ini = RandomReal[1, {3, 3}];
sol = NDSolveValue[{
x'[t] == x[t],
x[0] == ini
}, x, {t, 0, 1}]
for some random initial conditions ini
(also a matrix). Now, how can I include custom coefficients? Specifically, what if the coefficient matrix is such that the diagonal is zero and all the other entries are 1? That is,
$$ \begin{pmatrix} \dot{x}_{11} & \dot{x}_{12} & \dot{x}_{13}\\ \dot{x}_{21} & \dot{x}_{22} & \dot{x}_{23}\\ \dot{x}_{31} & \dot{x}_{32} & \dot{x}_{33} \end{pmatrix}= \begin{pmatrix} 0 & x_{12} & x_{13}\\ x_{21} & 0 & x_{23}\\ x_{31} & x_{32} & 0 \end{pmatrix} $$
I tried setting
coeff = ConstantArray[1, {3, 3}] - IdentityMatrix[3]
followed simply by
ini = RandomReal[1, {3, 3}];
sol = NDSolveValue[{
x'[t] == coeff * x[t],
x[0] == ini
}, x, {t, 0, 1}]
However this doesn't seem to work and I get the error message
which seems to be related with how NDSolveValue
is interpreting coeff
I've noticed that, although x[t]
within the NDSolve
environment can be tretaed as a list or a list of lists, it fails its interpretation in some cases. Any ideas?
{x11, x12, x13, x21, x22, x23, x31, x32, x33}
instead? Then you can use a matrix-vector dot product on the rhs. If you really need to use a matrix formulation, you can define a functionfoo[x_List] := coeff x
and use that on the rhs. $\endgroup$foo
function? $\endgroup$x'[t] == foo[x[t]]
$\endgroup$x'[t] == coeff x[t]
. Sincex[t]
is not a matrix yet, it is treated as a scalar and x[t] is threaded into the matrix. Usingfoo
prevents this early multiplication. $\endgroup$List
s. So if you givefoo
orDot
orTranspose
the argumentx[t]
, it won't evaluate and everything will work like you want. $\endgroup$