ValueError(“分母多项式必须是rank-1数组。”)

时间:2012-02-21 11:12:10

标签: python numpy scipy

我在lti transient response analysis using Python(numpy, scipy, matplotlib)中有以下代码。我是python的新手。我有一个转移矩阵,我必须绘制。

我遇到了mathwork: tf。我正在尝试如下:

from numpy import min, max
from scipy import linspace
from scipy.signal import lti, step, impulse

num00 = [0.0]
den00 = [0.0]

num01 = [-2383.3]
den01 = [1.0,160.3460,-1962.0,-314598.852]

num10 = [1.0]
den10 = [1.0]

num11 = [31.9361,0,111320.0]
den11 = [1.0,160.3460,-1962.0,-314598.852]

num = [[num00,num01],[num10,num11]]
den = [[den00,den01],[den10,den11]]

tf = lti(num,den)

t = 0    
s = 0

# get t = time, s = unit-step response
t , s = step(tf)

t , s = step(tf, T = linspace(min(t), t[-1], 1000))

t , i = impulse(tf, T = linspace(min(t), t[-1], 1000))

from matplotlib import pyplot as plt

plt.plot(t, s, t, i)

plt.title('Transient-Response Analysis')
plt.xlabel('Time(sec)')
plt.ylabel('Amplitude')
plt.hlines(1, min(t), max(t), colors='r')
plt.hlines(0, min(t), max(t))
plt.xlim(xmax=max(t))
plt.legend(('Unit-Step Response', 'Unit-Impulse Response'), loc=0)
plt.grid()
plt.show()

我收到以下错误:

>>> tf = lti(num,den)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python26\lib\site-packages\scipy\signal\ltisys.py", line 236, in __init__self.__dict__['num'], self.__dict__['den'] = normalize(*args)
  File "C:\Python26\lib\site-packages\scipy\signal\filter_design.py", line 276, in normalize raise ValueError("Denominator polynomial must be rank-1 array.") ValueError: Denominator polynomial must be rank-1 array.

1 个答案:

答案 0 :(得分:0)

部分问题是你传递的num / den不是一个格式良好的矩阵。在您的代码中,您有:

num01 = [-2383.3]
den01 = [1.0,160.3460,-1962.0,-314598.852]

这不会很好用,因为就numpy而言,你正试图创建一个矩阵,我意识到它只是传递函数矩阵的一个组成部分,分子中只有1个元素,分母中只有4个元素。所以你需要这样的东西:

num01 = [  0,       0,      0,-2383.3]

或者你想要有一个极高阶的分子。当我尝试步骤时,我得到: Step 这可能不是你所期望的。我还建议您查看python-control包。当然,您需要获得该包的所有先决条件,例如SLICOT python package。我相信它最终会为你服务。