5
$\begingroup$

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.

$\endgroup$
  • 1
    $\begingroup$ 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. $\endgroup$ – Szabolcs Sep 29 '16 at 12:11
  • $\begingroup$ I did mean to do that, thank you! I have changed the post to reflect this. $\endgroup$ – Hefaestion Sep 29 '16 at 12:31
  • $\begingroup$ I think there is an addition step to express the equations as a system of linear equations first? $\endgroup$ – Hefaestion Sep 29 '16 at 12:45
  • 1
    $\begingroup$ 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]}]. $\endgroup$ – Szabolcs Sep 29 '16 at 12:49
7
$\begingroup$
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

enter image description here

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

enter image description here

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]

enter image description here

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

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.