# Express ODEs in matrix form

Is it possible to use Mathematica to express 2 simultaneous ODEs in matrix form? My objective is to then take the matrices and solve the ODE elsewhere.

I have formulated a simple system of ODEs that represent some electrical system:

eqn = {Ih == C1(D[Va[t],t]) +(Va[t] - Vb[t])/R3,
(Va[t] - Vb[t])/R2 == C2(D[Vb[t],t]) + Vb[t]/R3}


How can I then use Mathematica to rearrange these equations in a matrix form? I want to get to the point where I can solve the system for the next time increment using a matrix inversion.

• This is a good start, after solving for the derivatives: mathematica.stackexchange.com/q/33039/12 Did you mean to write Va[t] - Vb[t] there? It's not a correct ODE otherwise. – Szabolcs Sep 29 '16 at 12:11
• I did mean to do that, thank you! I have changed the post to reflect this. – Hefaestion Sep 29 '16 at 12:31
• I think there is an addition step to express the equations as a system of linear equations first? – Hefaestion Sep 29 '16 at 12:45
• Yes, that's what I meant by solving for the derivatives. Maybe start with Solve[{Ih == C1 (D[Va[t], t]) + (Va[t] - Vb[t])/R3, (Va[t] - Vb[t])/R2 == C2 (D[Vb[t], t]) + Vb[t]/R3}, {Va'[t], Vb'[t]}]. – Szabolcs Sep 29 '16 at 12:49

eqn = {Ih == C1 (D[Va[t], t]) + (Va[t] - Vb[t])/R3,
(Va[t] - Vb[t])/R2 == C2 (D[Vb[t], t]) + Vb[t]/R3} /. {D[Va[t], t] -> Va'[t],
D[Vb[t], t] -> Vb'[t]};
sort = First@Solve[eqn, {Va'[t], Vb'[t]}] /. Rule -> Equal


m = -CoefficientArrays[sort, {Va[t], Vb[t], Ih}][[2]] // Normal;
A = m[[All, 1 ;; 2]];
b = m[[All, 3]];
{A // MatrixForm, b // MatrixForm}


Supplement

There is another way:

eqn = {Ih[t] == C1 (D[Va[t], t]) + (Va[t] - Vb[t])/R3,
(Va[t] - Vb[t])/R2 == C2 (D[Vb[t], t]) + Vb[t]/R3} /. {D[Va[t], t] -> Va'[t],
D[Vb[t], t] -> Vb'[t]};
ss = StateSpaceModel[eqn, {Va[t], Vb[t]}, {Ih[t]}, Vb[t], t]


{aa, bb, cc, dd} = Normal@ss;
{aa // MatrixForm, bb // MatrixForm}