我试图计算一些导数,以完成一些方程。但是当我定义我想要容纳6个不同变量的函数时。它给了我这个错误。
File "C:\Users\Leona\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/Leona/.matplotlib/Python_Dyn_Opdracht6_tweede.py", line 94, in <module>
afgeleiden = derivatives_circ([theta[n], omega[n]], x[n], vx[n], y[n], vy[n], t[n])
TypeError: derivatives_circ() takes 2 positional arguments but 6 were given
这是我用来创建函数的代码
def derivatives_circ(state, t):
theta = state[0]
omega = state[1]
Moment=F*D
alpha = Moment/Ig
x = state[2]
vx = state[3]
ax = F*np.cos(theta)/m
y = state[4]
vy = state[5]
ay = F*np.sin(theta)/m
return [omega, alpha, vx, vy, ax, ay]
resultaat = odeint(derivatives_circ,[theta0, omega0, x0, vx0, y0, vy0], t)
theta = resultaat[:,0]
omega = resultaat[:,1]
x = resultaat[:,2]
vx = resultaat[:,3]
y = resultaat[:,4]
vy = resultaat[:,5]
for n in range(len(t)-1):
if theta[n]<0.5*np.pi:
afgeleiden = derivatives_circ([theta[n], omega[n]], x[n], vx[n], y[n], vy[n], t[n])
omega[n+1] = omega[n] + afgeleiden[1]*dt
theta[n+1] = theta[n] + afgeleiden[0]*dt
vx[n+1]=vx[n]+afgeleiden[3]*dt
x[n+1]=x[n]+afgeleiden[2]*dt
vy[n+1]=vy[n]+afgeleiden[5]*dt
y[n+1]=y[n]+afgeleiden[4]*dt
else:
afgeleiden_circ = derivatives_circ([theta[n], omega[n]], x[n], vx[n], y[n], vy[n], t[n])
omega[n+1] = omega[n] + afgeleiden_circ[1]*dt
theta[n+1] = theta[n] + afgeleiden_circ[0]*dt
afgeleiden_lin = derivatives_circ([x[n],vx[n]],t[n])
vx[n+1]=vx[n]+afgeleiden[3]*dt
x[n+1]=x[n]+afgeleiden[2]*dt
afgeleiden = derivatives_circ([y[n],vy[n]],t[n])
vy[n+1]=vy[n]+afgeleiden_circ[5]*dt
y[n+1]=y[n]+afgeleiden[4]*dt
真的很感激任何帮助!
这一行在这里:
afgeleiden = derivatives_circ([theta[n], omega[n]], x[n], vx[n], y[n], vy[n], t[n])
应该是:
afgeleiden = derivatives_circ([theta[n], omega[n], x[n], vx[n], y[n], vy[n]], t[n])
基于您之前对derivatives_circ
的调用
在这一行:
def derivatives_circ(state, t):
你定义了这个函数,它需要2个参数,但是在这一行:
afgeleiden = derivatives_circ([theta[n], omega[n]], x[n], vx[n], y[n], vy[n], t[n])
你给它6个参数!这就是它给你错误的原因。我不知道你的代码背后的心态,但用这一行代替它:
afgeleiden = derivatives_circ([theta[n], omega[n], x[n], vx[n], y[n], vy[n]], t[n])
应该管用!