简体   繁体   中英

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

statespace returns an numpy.ndarray wich is an object containing your values. It is not callable meaning you can't do : object() For example an int is not callable either.

But trap treats second argument : f in that way : f(y[k+1]) in the third line of the function declaration.

Hence your error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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