减少在 mathematica 中欠定的方程组 [英] reduce system of equations which is under-determined in mathematica

查看:15
本文介绍了减少在 mathematica 中欠定的方程组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了以下方程(作为例子):

I obtained the folloiwng equation (as an example):

{2 w11 + 3 w21 == 2 w12, w11 == 4 w12 + 3 w22, w11 + 2 w21 + w22 == 0,
  2 w12 + w21 + 2 w22 == 0}

我想确定 w11、w12、w21、w22.但是,只需执行以下操作:

And I want to determine w11, w12, w21, w22. However, simply do the following:

Solve[{3 w11 + 2 w21 == 5 w11 + 3 w12, w11 + w21 == 5 w21 + 3 w22, 
  3 w12 + 2 w22 == -2 w11 - w12, w12 + w22 == -2 w21 - w22}, {w11, 
  w12, w21, w22}]

因为方程组是欠定的.我有一个想法,即使用矩阵代数.但是我需要自动将 w11、w12、w21、w22 前面的那些系数分组到一个矩阵(列表列表)中,然后从那里开始.但我不确定如何轻松操纵这些方程来生成这样的矩阵.请帮忙,或者如果您有更好的想法,也请分享.

Because the system of equations is under-determined. I have one thought, i.e. using matrix algebra. But I need to automatically group those coefficients in front of w11, w12, w21, w22 into a matrix (list of lists) then go from there. But I am not sure how to manipulate these equations easily to generate such a matrix. Please help, or if you have better ideas, please share too.

非常感谢.

推荐答案

这是您的方程和变量:

vars = {w11, w12, w21, w22};
eqs = {2 w11 + 3 w21 == 2 w12, w11 == 4 w12 + 3 w22, 
   w11 + 2 w21 + w22 == 0, 2 w12 + w21 + 2 w22 == 0};

这里是矩阵:

In[48]:= matrix =  Transpose[ eqs /. Equal :> Subtract /. 
    Map[Thread[vars -> #] &, IdentityMatrix[Length[vars]]]]

Out[48]= {{2, -2, 3, 0}, {1, -4, 0, -3}, {1, 0, 2, 1}, {0, 2, 1, 2}}

同样适用于您的第二组方程:

The same works for your second group of equations:

In[49]:= eqs = {3 w11 + 2 w21 == 5 w11 + 3 w12,  w11 + w21 == 5 w21 + 3 w22, 
  3 w12 + 2 w22 == -2 w11 - w12,  w12 + w22 == -2 w21 - w22};   

In[50]:= matrix = Transpose[ eqs /. Equal :> Subtract /. 
    Map[Thread[vars -> #] &, IdentityMatrix[Length[vars]]]]

Out[50]= {{-2, -3, 2, 0}, {1, 0, -4, -3}, {2, 4, 0, 2}, {0, 1, 2, 2}}

根据要求扩展解决方案.首先,它是如何工作的:想法是首先将所有变量都放在左边,这是通过用减法替换等于运算符来实现的:

Expanding on the solution, upon request. First, how it works: the idea is to first bring all variables to the left, which is achieved by replacing the equals operator with subtraction:

In[69]:= eqs = {3 w11 + 2 w21 == 5 w11 + 3 w12,  w11 + w21 == 5 w21 + 3 w22, 
     3 w12 + 2 w22 == -2 w11 - w12,  w12 + w22 == -2 w21 - w22};

输入[70]:= eqs/.等于:> 减

In[70]:= eqs /. Equal :> Subtract

输出[70]= {-2 w11 - 3 w12 + 2 w21, w11 - 4 w21 - 3 w22, 2 w11 + 4 w12 + 2 w22, w12 + 2 w21 + 2 w22}

Out[70]= {-2 w11 - 3 w12 + 2 w21, w11 - 4 w21 - 3 w22, 2 w11 + 4 w12 + 2 w22, w12 + 2 w21 + 2 w22}

规则的构造使得对于任何一组规则,只有一个变量设置为 1,其余的设置为 0:

The rules are constructed so that for any group of rules, only one variable is set to 1, and the rest to 0:

 In[71]:= Map[Thread[vars -> #] &, IdentityMatrix[Length[vars]]]

 Out[71]= {{w11 -> 1, w12 -> 0, w21 -> 0, w22 -> 0}, {w11 -> 0, w12 -> 1, w21 -> 0, w22 -> 0}, 
        {w11 -> 0, w12 -> 0, w21 -> 1, w22 -> 0}, {w11 -> 0, w12 -> 0, w21 -> 0, w22 -> 1}}

这允许计算系数:

In[72]:= eqs /. Equal :> Subtract /. Map[Thread[vars -> #] &, IdentityMatrix[Length[vars]]]

Out[72]= {{-2, 1, 2, 0}, {-3, 0, 4, 1}, {2, -4, 0, 2}, {0, -3, 2, 2}}

检查规则的工作方式后,很容易看出我们需要将 Transpose 应用于结果.

Upon inspecting how the rules work, it is easy to see that we need to apply Transpose to the result.

现在,您的第二个请求需要更多工作:

Now, your second request requires more work:

In[53]:= eqs = {3 w11 + 2 w12 == 5 w11 + 3 w21 + a, w11 + w12 == 5 w12 + 3 w22 - c, 
   3 w21 + 2 w22 + b == a - 2 w11 - w21, w21 + w22 == f - 2 w12 - w22};

In[55]:= modifiedEqs = With[{alts = Alternatives @@ vars},
   eqs //. {lhs_ == HoldPattern[Plus[left___, x_, right___]] /; !FreeQ[x, alts] :> 
                    lhs - x == left + right,
            HoldPattern[Plus[left___, x_, right___] == rhs_] /; FreeQ[x, alts] :> 
           (left + right == rhs - x)}]

Out[55]= {-2 w11 + 2 w12 - 3 w21 == a, w11 - 4 w12 - 3 w22 == -c,  
     2 w11 + 4 w21 + 2 w22 == a - b,   2 w12 + w21 + 2 w22 == f}

In[68]:= matrix = {Transpose[# /. (lhs_ == rhs_) :> lhs /. 
    Map[Thread[vars -> #] &, IdentityMatrix[Length[vars]]]], #[[All,2]]} &[modifiedEqs]

Out[68]= {{{-2, 2, -3, 0}, {1, -4, 0, -3}, {2, 0, 4, 2}, {0, 2, 1,  2}}, {a, -c, a - b, f}}

主要区别在于我们需要一个额外的步骤来分离常量并将它们带到 r.h.s.您可能会发现自己弄清楚这是如何工作的细节更有用.

The main difference is that we need an extra step to separate the constants and bring them to the r.h.s. You may find it more useful to figure out the details of how this works yourself.

是的,我忘了提:要理解解决方案,您应该知道在嵌套列表中应用规则时会发生什么 - 在这种情况下,较大列表中的每个规则列表都会导致表达式的转换副本,例如例子:

Yes, I forgot to mention: to understand the solution, you should know what happens when you apply rules in nested lists - in this case, each list of rules inside a larger lists results in a transformed copy of an expression, for example:

In[73]:= {a, b, c} /. {{a -> 1}, {b -> 1}, {c -> 1}}

Out[73]= {{1, b, c}, {a, 1, c}, {a, b, 1}}

HTH

这篇关于减少在 mathematica 中欠定的方程组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆