ValueError: 对象太深,无法得到所需的数组 odepack.error: 函数调用结果不是浮点数数组。
我已经阅读了以下问题:
- scipy curve_fit error: Result from function call is not a proper array of floats - 如何使用scipy odeint解决这个微分方程? - Object Too Deep for Desired Array - scipy.integrate.odeint
这些似乎应该是有帮助的,但我不确定如何将它们应用到我的问题上。由于我对Python还比较新,请耐心等待,如果我错过了一些显而易见的东西或者做了一些特别愚蠢的事情,请您谅解。我在绘制方面没有任何问题,所以一旦我弄清楚如何让它真正起作用,我想我就可以解决问题了。
import numpy as np
import scipy.integrate as integrate
##Parameters
kp=.5 #proportional gain
ki=.1 #integral gain
vr=30 #desired velocity in m/s
Tm=190 #Max Torque in Nm
wm=420 #engine speed
B=0.4 #Beta
an=12 #at gear 4
p=1.3 #air density
Cd=0.32 #Drag coefficient
Cr=.01 #Coefficient of rolling friction
A=2.4 #frontal area
##Variables
m=18000 #weight
v=20 #starting velocity
time=np.linspace(0,10,50) #time
theta=np.radians(4) #Theta
def vderivs(state,t):
v = state
vel=[]
ti=0
while ti < t:
v1 = an*controller(ti,vr,v)*torque(v)
v2 = m*Cr*np.sign(v)
v3 = 0.5*p*Cd*A*v**2
v4 = m*np.sin(theta)
if t < 10:
vtot = v1+v2+v3
vfin = np.divide(vtot,m)
else:
vtot = v1+v2+v3+v4
vfin = np.divide(vtot,m)
vel.append(vfin)
ti+=1
trueVel = np.array(vel, float)
return trueVel
def uderivs(state,t):
v = state
deltax = vr - v
return deltax
def controller(time,desired,currentV):
z = integrate.odeint(uderivs, currentV, time)
u = kp*(vr-currentV)+ki*z
return u.flatten()
def torque(v):
return Tm*(1-B*(np.divide(an*v,wm)-1)**2)
def velocity(mass,desired,theta,t):
v = integrate.odeint(vderivs, desired, t)
return v.flatten()
test = velocity(m,vr,theta,time)
print(test)
请让我知道您需要我提供的其他任何信息!
controller
取一个t
的值,但是controller
只是odeint
的包装器,它期望t
是要集成的所有时间值的数组,但是您在vderivs
中使用标量来调用它。您可能只需将vderivs
向量化即可解决此问题,但我不确定。 - askewchan