【问题标题】:How to invert a matrix with transfer functions in python?如何在python中用传递函数反转矩阵?
【发布时间】:2022-11-01 21:51:01
【问题描述】:

我知道在 matlab 中我可以执行以下操作:

s = tf('s')
G11 = (s + 1)/(s + 2)
G12 = 1/(2*s + 1)
G21 = 1/(3*s + 1)
G22 = 1/(4*s + 1)

A = [G11 G12; G21, G22]
Ai = inv(A)
bode(A)

它会工作得很好。在 python 中,我尝试做类似的事情:

import control as co
import numpy as np

s = co.tf('s')
G11 = (s + 1)/(s + 2)
G12 = 1/(2*s + 1)
G21 = 1/(3*s + 1)
G22 = 1/(4*s + 1)

A = np.array([[G11, G12], [G21, G22]])
Ai = np.linalg.inv(A)
co.bode(A)

但这不起作用 - numpy 不知道如何反转这个矩阵。

有没有在python中做到这一点的好方法?我知道我可以使用 scipy 作为符号,但我认为在使用控制工具箱中的其他工具时这对我没有帮助。

编辑:

numpy 返回以下错误:

---------------------------------------------------------------------------
UFuncTypeError                            Traceback (most recent call last)
<ipython-input-1-ec46afd90eb6> in <module>
     10 
     11 A = np.array([[G11, G12], [G21, G22]])
---> 12 Ai = np.linalg.inv(A)
     13 co.bode(A)

<__array_function__ internals> in inv(*args, **kwargs)

/usr/local/lib/python3.7/dist-packages/numpy/linalg/linalg.py in inv(a)
    543     signature = 'D->D' if isComplexType(t) else 'd->d'
    544     extobj = get_linalg_error_extobj(_raise_linalgerror_singular)
--> 545     ainv = _umath_linalg.inv(a, signature=signature, extobj=extobj)
    546     return wrap(ainv.astype(result_t, copy=False))
    547 

UFuncTypeError: Cannot cast ufunc 'inv' input from dtype('O') to dtype('float64') with casting rule 'same_kind'

【问题讨论】:

  • 我的朋友,“numpy 不知道如何反转这个矩阵”大胆假设。替代方案:您不知道如何使 numpy 达到您想要的效果。
  • 你应该解释为什么你认为它不起作用。如果您的计算机在您执行此操作时着火,或者如果出现错误,或者数字不是您想要的,则解决方案将有所不同。
  • 谢谢,我将编辑帖子以插入 numpy 返回的错误
  • 那个 2x2 案例是您唯一感兴趣的案例吗?或者您还会使用更大的矩阵吗?
  • 我问是因为对于 2x2 矩阵,无需任何外部库即可轻松找到逆矩阵。如果 A 是 [[a, b], [c, d]],则相反的是 [[d/det, -b/det], [-c/det, a/det]],其中 det = a*d - b*c

标签: python numpy matlab controls linear-algebra


【解决方案1】:

Numpy(提示:它的名字是正确的)只是一个数字库;它不做符号数学。 Sympy(也在名称中)执行符号数学,所以使用它:

import sympy

s = sympy.Symbol('s', imaginary=True)
g11 = (s + 1)/(s + 2)
g12 = 1/(2*s + 1)
g21 = 1/(3*s + 1)
g22 = 1/(4*s + 1)
A = sympy.Matrix((
    (g11, g12),
    (g21, g22),
))
sympy.pprint(A.inv())

带输出

⎡    3       2                       3       2              ⎤
⎢ 6⋅s  + 17⋅s  + 11⋅s + 2      - 12⋅s  - 31⋅s  - 15⋅s - 2   ⎥
⎢ ───────────────────────      ──────────────────────────   ⎥
⎢     3      2                      3      2                ⎥
⎢  6⋅s  + 7⋅s  - 3⋅s - 1         6⋅s  + 7⋅s  - 3⋅s - 1      ⎥
⎢                                                           ⎥
⎢     3       2                 4       3       2           ⎥
⎢- 8⋅s  - 22⋅s  - 13⋅s - 2  24⋅s  + 50⋅s  + 35⋅s  + 10⋅s + 1⎥
⎢─────────────────────────  ────────────────────────────────⎥
⎢     3      2                      3      2                ⎥
⎣  6⋅s  + 7⋅s  - 3⋅s - 1         6⋅s  + 7⋅s  - 3⋅s - 1      ⎦

【讨论】:

  • 谢谢,这有帮助,但我的主要目标是使用 python 来分析这个系统 - 波特图、检查稳定性等,所以我认为这个解决方案仍然不是我想要的
  • @DavidDaminelli numpy 是一个 python 库。 Simpy 是一个 Python 库。如果您的目标是使用 python,您将通过使用此解决方案来使用它。
【解决方案2】:

看起来control.tf 返回一个类control.TransferFunction 的对象。这与返回符号函数对象的 MATLAB 版本不同。

通过查看the documentation,我没有看到将control.TransferFunction 对象转换为符号函数对象的内置方法,但我确实看到了numden 方法,您可以构造一个使用这些值的符号函数。然后你可以申请the answer by Reinderien

【讨论】:

    猜你喜欢
    • 2012-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-20
    • 1970-01-01
    相关资源
    最近更新 更多