【发布时间】: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