开发者

Converting vector equation to a list of equations in Mathematica

开发者 https://www.devze.com 2023-01-30 13:30 出处:网络
Due to DSolve syntax, systems of differential equations have to be given as lists of equations and not as a vector equation (Unlike Solve, which accepts both).

Due to DSolve syntax, systems of differential equations have to be given as lists of equations and not as a vector equation (Unlike Solve, which accepts both). So my simpl开发者_如何学运维e question is how to convert a vector equation such as:

{f'[t],g'[t]}=={{a,b},{c,d}}.{f[t],g[t]}

To list of equations:

{f'[t]==a*f[t]+b*g[t],g'[t]==c*f[t]+d*g[t]}

I think I knew once the answer, but I can't find it now and I think it could benefit others as well.


Try using Thread:

Thread[{f'[t], g'[t]} == {{a, b}, {c, d}}.{f[t], g[t]}]
(* {f'[t] == a f[t] + b g[t], g'[t] == c f[t] + d g[t] *)

It takes the equality operator == and applies it to each item within a list with the same Head.


The standard answer to this question is that which Brett presented, i.e., using Thread. However, I find that for use in DSolve, NDSolve, etc... the command LogicalExpand is better.

eqn = {f'[t], g'[t]} == {{a, b}, {c, d}}.{f[t], g[t]};

LogicalExpand[eqn]

(* f'[t] == a f[t] + b g[t] && g'[t] == c f[t] + d g[t] *)

It doesn't convert a vector equation to a list, but it is more useful since it automatically flattens out matrix/tensor equations and combinations of vector equations. For example, if you wanted to add initial conditions to the above differential equation, you'd use

init = {f[0], g[0]} == {f0, g0};

LogicalExpand[eqn && init]

(* f[0] == f0 && g[0] == g0 && 
  f'[t] == a f[t] + b g[t] && g'[t] == c f[t] + d g[t] *)

An example of a matrix equation is

mEqn = Array[a, {2, 2}] == Partition[Range[4], 2];

Using Thread here is awkward, you need to apply it multiple times and Flatten the result. Using LogicalExpand is easy

LogicalExpand[mEqn]

(* a[1, 1] == 1 && a[1, 2] == 2 && a[2, 1] == 3 && a[2, 2] == 4 *)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号