【问题标题】:Why does my function say np.ndarray object not callable?为什么我的函数说 np.ndarray 对象不可调用?
【发布时间】:2021-06-24 17:57:41
【问题描述】:

尝试使用初始条件为“x0”的梯形搭配函数求解倒立摆的状态空间。我不太明白我遇到的错误。

import numpy as np

m1 = 1
m2 = 0.3
g = 9.81
l = 0.5
dmax = 2.0
umax = 20.0
T = 2
d = 1
u=1

#EOM's SS
def statespace(y1,y2,ydot1,ydot2):    # Should only provide the 4 states; the others are fixed paramters.

    y1 = ydot1
    y2 = ydot2
    ydot1 = ((l*m2*np.sin(y1)*y1*y1) + u + (m2*g*np.cos(y1)*np.sin(y1))) / (m1 + m2*(1-np.cos(y1)**2))
    ydot2 = -1*((l*m2*np.cos(y2)*np.sin(y2)*y2*y2) + u*np.cos(y2) + ((m1+m2)*g*np.sin(y2))) / (l*m1 + l*m2*(1-np.cos(y2)**2))
    return np.array([y1,y1,ydot1,ydot2])       # Should return the rate of states, again array of 4 values.



#trapezoidal collocation
def trap(y,f):  # Both y and q_dot are states - treat them as one argument.     
        for k in range(1,5):            # You are hard-coding the # of steps and the step size.  OK for now, but be careful later.
            h = 0.2
            x = y[k+1]-y[k]-(h/2)*(f(y[k+1])+f(y[k]))
        return np.array([x[1],x[2],x[3],x[4],x[5],x[6],x[7],x[8],x[9],x[10],x[11],x[12],x[13],x[14],x[15],x[16],x[17],x[18],x[19],x[20]]) #20 x 1 matrix. Should not return inside the loop; and you should be returning an array of 5 values (assuming 6 steps)

x0 = [0,0,0,0]
y = trap(x0,statespace(0,0,0,0))

【问题讨论】:

  • 无论如何,这个错误意味着你试图调用一个数组call 意味着在其上使用括号,例如调用函数,例如some_func(),所以 somewhere 在你的代码中(错误信息应该告诉你确切的行)你正在做类似my_array(...)
  • f 是一个从您的statespace 函数返回的数组。然而你却像函数一样使用它:f(y[k+1])
  • 抱歉,还是习惯在这里发帖@juanpa.arrivillaga

标签: python for-loop iteration state-space


【解决方案1】:

你调用 trap(x0,statespace(0,0,0,0))

statespace 返回一个 numpy.ndarray ,它是一个包含您的值的对象。 这是不可调用的意思是你不能做:object() 例如 int 也是不可调用的。

但是 trap 以这种方式处理第二个参数:f :函数声明的第三行中的 f(y[k+1])。

因此你的错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-22
    • 1970-01-01
    • 2021-06-07
    • 1970-01-01
    • 1970-01-01
    • 2022-11-02
    • 2021-09-13
    • 1970-01-01
    相关资源
    最近更新 更多