AttributeError:'numpy.ndarray'对象没有属性'cos'

时间:2015-09-07 18:13:13

标签: python numpy

我正在试图弄清楚为什么我的代码中出现了上述错误。该程序假设使用Newton Raphson方法在给定两个输入参数的情况下找到4 bar连杆中链接的位置。

错误发生在这一行

g1 = L1 * np.cos(theta) + L2 * np.cos(alpha) - L3

提前感谢您的帮助。

import numpy as np

L1=1
L2=1.5 * L1
theta = 40 * np.pi / 180

#initial guesses

L3 = 1.5
alpha = 30 * np.pi / 180
epsilon = 1
n = 0

while epsilon > 0.0001:
    g1 = L1 * np.cos(theta) + L2 * np.cos(alpha) - L3
    dg1dalpha = -L2 * np.sin(alpha)
    dg1dL3 = -1;

    g2 = L1 * np.sin(theta) - L2 * np.sin(alpha)
    dg2dalpha = -L2 * np.cos(alpha);
    dg2dL3 = 0

    J = np.array([[dg1dalpha, dg1dL3], [dg2dalpha, dg2dL3]])

    s = np.array([[alpha], [L3]]) - J/np.array([[g1], [g2]])

    epsilon_alpha = abs(s[0] - alpha)

    epsilon_L3 = abs(s[1] - L3)

    epsilon = max(epsilon_alpha.all, epsilon_L3.all)

    alpha = s[0]

    L3 = s[1]

    n = n + 1

print(n, alpha, L3)

1 个答案:

答案 0 :(得分:3)

在Python2.7中,在循环开始时添加print('alpha',alpha)会产生:

('alpha', 0.5235987755982988)
('alpha', array([ 1.85083849,  2.29325173]))
('alpha', array([[array([ 1.98227296,  1.95343536]), 1.7138098231972174],
       [array([ 1.81303794,  1.7604074 ]), 2.2932517265367176]], dtype=object))
Traceback (most recent call last):
  File "stack32444132.py", line 17, in <module>
    g1 = L1 * np.cos(theta) + L2 * np.cos(alpha) - L3
AttributeError: 'numpy.ndarray' object has no attribute 'cos'

因此,错误是由调用np.cos(alpha)引起的,其中alpha是一个对象数组。 alpha(2,2);第一列包含长度为2的数组;第二个包含花车。

因此,在循环中的某个时刻,您要附加或混合不同长度的数组或列表。

s = np.array([[alpha], [L3]]) - J/np.array([[g1], [g2]])
alpha = s[0]

添加更多打印件(s之前)

('J', (2, 2), dtype('float64'))
('alpha', 0.5235987755982988)
('L3', 1.5)
....
('J', (2, 2), dtype('O'))   
('alpha', array([ 1.85083849,  2.29325173]))
('L3', array([-10.61649234,   1.5       ]))

在第二个循环J中,从2x2浮点矩阵变为2x2个对象。

Python3在第一次遇到epsilon = max(epsilon_alpha.all, epsilon_L3.all)表达式时提出错误。 epsilon_alpha.all是一种方法; epsilon_alpha.all()是一个布尔值。但是,当epsilon_alpha成为数组时,即使这会产生错误。

好的,这个循环运行(alpha仍然是标量);它并没有停止,大概是因为epsilon不够小;但是我会把它留给你。

while epsilon > 0.0001:
    # print('alpha', alpha)
    g1 = L1 * np.cos(theta) + L2 * np.cos(alpha) - L3
    dg1dalpha = -L2 * np.sin(alpha)
    dg1dL3 = -1;

    g2 = L1 * np.sin(theta) - L2 * np.sin(alpha)
    dg2dalpha = -L2 * np.cos(alpha);
    dg2dL3 = 0

    J = np.array([[dg1dalpha, dg1dL3], [dg2dalpha, dg2dL3]])
    print('J', J.shape,J.dtype)  # (2,2) floats
    s = np.array([[alpha], [L3]]) - J/np.array([[g1], [g2]])
    s = s[:,0]  # fudge to turn (2,2) array into a (2,) array

    epsilon_alpha = abs(s[0] - alpha)
    epsilon_L3 = abs(s[1] - L3) 
    epsilon = max(epsilon_alpha, epsilon_L3)
    # max on 2 scalars is ok

    alpha = s[0] # scalar
    L3 = s[1]    # scalar
    n = n + 1

问题的根源在

 s = np.array([[alpha], [L3]]) - J/np.array([[g1], [g2]])

如果alphaL3是标量,则np.array([[alpha], [L3]])(2,1)np.array([[g1], [g2]])也是如此。但由于J是(2,2),s也是(2,2)。但是你一直在使用s[0]s[1],显然假设s是`(2,)。

 s = s[:,0] 

使s成为一个(2,),以便其余的代码可以工作。由于epsilon不会收敛,因此可能是错误的解决方法。

我可以强调 - 在开发numpy代码时,请密切关注数组形状。如果形状错误,你会得到这样的错误。根据我的经验,获得正确的形状是调试工作的80%。

相关问题