我拼命尝试使用scipy ODE集成器,但我不断收到以下错误消息:
Y[0] = (1/I3) * T_z(INP[0], INP[1], INP[2], INP[3], INP[4])
TypeError: 'float' object is not subscriptable
我的代码如下:
import scipy.integrate as spi
import numpy as np
import pylab as pl
from time import time
#Constants
I3 = 0.00396
lamb = 1
L = 5*10**-1
mu = 1
m = 0.1
Cz = 0.5
rho = 1.2
S = 0.03*0.4
K_z = 1/2*rho*S*Cz
g = 9.81
#Initial conditions
omega0 = 10*2*np.pi
V0 = 25
theta0 =np.pi/2
phi0 = 0
psi0 = -np.pi/9
X0 = 0
Y0 = 0
Z0 = 1.8
#for integration
t_start = 0.0
t_end = 5
t_step = 0.1
t_range = np.arange(t_start, t_end+t_step, t_step)
INPUT = omega0, V0, theta0, phi0, psi0, X0, Y0, Z0 #initial conditions
def diff_eqs(INP,t):
def M(v_G, w_z):
return L*K_z*(v_G**2 + v_G*L*w_z*np.sin(w_z*t_step)+(L*w_z)**2)
def F_x(w_z, v_G, theta, phi, psi):
return K_z*(v_G**2+(L*w_z)**2)*np.sin(theta)*np.sin(phi) + lamb*v_G*(np.cos(psi)*np.cos(phi) - np.cos(theta)*np.sin(phi)*np.sin(psi))
def F_y(w_z, v_G, theta, phi, psi):
return -K_z*(v_G**2+(L*w_z)**2)*np.sin(theta)*np.cos(phi) + lamb*v_G*(np.cos(psi)*np.sin(phi) + np.cos(theta)*np.cos(phi)*np.sin(psi))
def F_z(w_z, v_G, theta, phi, psi):
return -K_z*(v_G**2+(L*w_z)**2)*np.cos(theta) + lamb*v_G*np.sin(theta)*np.sin(psi) - m*g
def T_x(w_z, v_G, theta, phi, psi):
return M(v_G, w_z)*(-np.sin(w_z*t_step)*(np.cos(psi)*np.cos(phi) - np.cos(theta)*np.sin(phi)*np.sin(psi)) \
+ np.cos(w_z*t_step)*(-np.sin(psi)*np.cos(phi) - np.cos(theta)*np.sin(phi)*np.cos(psi))) \
- mu * w_z * (np.sin(theta)*np.sin(phi))
def T_y(w_z, v_G, theta, phi, psi):
return M(v_G, w_z)*(-np.sin(w_z*t_step)*(np.cos(psi)*np.sin(phi) + np.cos(theta)*np.cos(phi)*np.sin(psi)) \
+ np.cos(w_z*t_step)*(-np.sin(psi)*np.sin(phi) - np.cos(theta)*np.cos(phi)*np.cos(psi)))
- mu * w_z * (np.sin(theta)*np.cos(phi))
def T_z(w_z, v_G, theta, phi, psi):
return M(v_G, w_z)*(-np.sin(w_z*t_step)*np.sin(theta)*np.sin(psi) + np.cos(w_z*t_step)*np.sin(theta)*np.cos(psi)) \
- mu * w_z * np.cos(theta)
Y = np.zeros(8)
Y[0] = (1/I3) * T_z(INP[0], INP[1], INP[2], INP[3], INP[4])
Y[1] = -(lamb/m)*F_x(INP[0], INP[1], INP[2], INP[3], INP[4])
Y[2] = (1/(I3*INP[0]))*(-T_y(INP[0], INP[1], INP[2], INP[3], INP[4])*np.cos(INP[4]) - T_x(INP[0], INP[1], INP[2], INP[3], INP[4])*np.sin(INP[4]))
Y[3] = (1/(I3*INP[0]*np.cos(INP[3]))) * (-T_y(INP[0], INP[1], INP[2], INP[3], INP[4])*np.sin(INP[4]) + T_x(INP[0], INP[1], INP[2], INP[3], INP[4])*np.cos(INP[4]))
Y[4] = -(1/(m*INP[1]))*F_y(INP[0], INP[1], INP[2], INP[3], INP[4])
Y[5] = INP[1]*(-np.cos(INP[4])*np.cos(INP[3]) + np.sin(INP[4])*np.sin(INP[3])*np.cos(INP[2]))
Y[6] = INP[1]*(-np.cos(INP[4])*np.sin(INP[3]) - np.sin(INP[4])*np.cos(INP[3])*np.cos(INP[2]))
Y[7] = INP[1]*(-np.sin(INP[4])*np.sin(INP[2]))
return Y
ode = spi.ode(diff_eqs)
# BDF method suited to stiff systems of ODEs
ode.set_integrator('vode',nsteps=500,method='bdf')
ode.set_initial_value(INPUT,t_start)
ts = []
ys = []
while ode.successful() and ode.t < t_end:
ode.integrate(ode.t + t_step)
ts.append(ode.t)
ys.append(ode.y)
t = np.vstack(ts)
我有一组要求解的8个微分方程。因此,我有8个初始值存储在“输入”中。但是,当我在ode.set_initial_value(INPUT,t_start)中使用此变量时,它会不断重复该变量是float!这已经困扰了我好几个小时,答案也许很明显,但是我看不到我在哪里犯了错误。而且我不认为方程本身,即使它们很混乱,也没有涉及到这里。
预先感谢您的帮助。
答案 0 :(得分:0)
您的参数顺序是odeint
的ODE函数所要求的。对于ode
,您需要订购(t, INP)
。
尝试使用最新的solve_ivp
接口,它具有与ode
类相同的功能,并且具有与odeint
相同的紧凑调用结构。