简体   繁体   English

为什么我的函数说 np.ndarray 对象不可调用?

[英]Why does my function say np.ndarray object not callable?

Attempting to solve a state space for an inverted pendulum using a trapezoidal collocation function with the initial condition 'x0'.尝试使用初始条件为“x0”的梯形搭配函数求解倒立摆的状态空间。 I do not quite understand the error I am getting.我不太明白我得到的错误。

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))

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

statespace returns an numpy.ndarray wich is an object containing your values. statespace 返回一个 numpy.ndarray ,它是一个包含您的值的对象。 It is not callable meaning you can't do : object() For example an int is not callable either.它不是可调用的,意味着你不能这样做: object() 例如一个 int 也不是可调用的。

But trap treats second argument : f in that way : f(y[k+1]) in the third line of the function declaration.但是 trap 在函数声明的第三行以这种方式处理第二个参数 : f : f(y[k+1]) 。

Hence your error.因此你的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM