[英]Function missing required positional arguments
v_lc0 = 0.00833333 # initial velocity of lead car (lc) and following car (fc)
v_fc0 = 0.00833333
da_lc = 3 # deceleration of lead car (mi/s^2)
k = 0.00472222 # sensitivity constant (mi/sec)
d = 0.094697 # distance between cars (miles)
l = 0.00284 # length of average car (miles)
x_lc0 = d - l # distance from back of lc to fc
y0 = [0,0.00833333] # initial distance and velocity
def dxlc_dt(t,x_lc0, v_lc0, ts, da_lc):
if (t < ts):
v_lc = v_lc0 - t*da_lc
x_lc = v_lc0*t - 0.5*da_lc*t**2
else:
v_lc = 0
x_lc = v_lc0*ts - 0.5*da_lc*ts**2
return (x_lc,v_lc)
dxlc_dt = np.vectorize(dxlc_dt)
def dxfc_dt(t,y,x_lc0,v_lc0, da_lc,k,ts):
x_fc = y[0] # distance
v_fc = y[1] # velocity
x_lc,v_lc = dxlc_dt(t, x_lc0, v_lc0, ts,da_lc) # calling distance and velocity
dxfc = v_fc
dvfc = k*(v_lc - v_fc)/(x_lc - x_fc)
dxfc_dt = [dxfc, dvfc]
return (dxfc_dt)
t = np.arange(0,50.0001,0.0001) # time
ts = v_lc0/da_lc # time it takes for lc to stop
tspan = [0,50]
I made a code for second order ODE IVP.我为二阶 ODE IVP 编写了代码。 x_lc,v_lc is being called from another function.
x_lc,v_lc 被另一个 function 调用。 When I run the program it tells me that the arguments 'x_lc0', 'v_lc0', 'da_lc', 'k', and 'ts' are missing but I have them defined externally.
当我运行程序时,它告诉我 arguments 'x_lc0'、'v_lc0'、'da_lc'、'k' 和 'ts' 丢失了,但我在外部定义了它们。
I believe it has to do with this part of my code, below, as the console shows this but I'm wondering what I could be doing wrong.我相信它与下面我的代码的这一部分有关,因为控制台显示了这一点,但我想知道我可能做错了什么。 Do I need to define these arguments differently?
我需要以不同的方式定义这些 arguments 吗?
ys = solve_ivp(dxfc_dt,tspan,y0,method='LSODA',t_eval=t, args = (x_lc0,v_lc0,da_lc,k,ts))
x_lc,v_lc=dxlc_dt(t,v_lc0,x_lc0,ts,da_lc)
plt.plot(t,x_lc,label='Lead Car',color = 'black')
plt.plot(t,ys['y'][0],label='Following Car', color='yellow')
plt.xlabel('Time')
plt.ylabel('Distance (miles)')
plt.legend()
plt.show()
-----Traceback (most recent call last):--------- ----- Traceback(最近一次通话最后):------
File "C:\Users\qhumphre\OneDrive - Texas Tech University\Desktop\CE5310\Assignments\Assignment 5\assignment5_1.py", line 64, in ys = solve_ivp(dxfc_dt,tspan,y0,method='LSODA',t_eval=t,args=(x_lc0,v_lc0,da_lc,k,ts))文件“C:\Users\qhumphre\OneDrive - Texas Tech University\Desktop\CE5310\Assignments\Assignment 5\assignment5_1.py”,第 64 行,ys = solve_ivp(dxfc_dt,tspan,y0,method='LSODA',t_eval =t,args=(x_lc0,v_lc0,da_lc,k,ts))
File "C:\ProgramData\Anaconda3\lib\site-packages\scipy\integrate_ivp\ivp.py", line 502, in solve_ivp message = solver.step()文件“C:\ProgramData\Anaconda3\lib\site-packages\scipy\integrate_ivp\ivp.py”,第 502 行,在solve_ivp message = solver.step()
File "C:\ProgramData\Anaconda3\lib\site-packages\scipy\integrate_ivp\base.py", line 182, in step success, message = self._step_impl()文件“C:\ProgramData\Anaconda3\lib\site-packages\scipy\integrate_ivp\base.py”,第 182 行,步骤成功,message = self._step_impl()
File "C:\ProgramData\Anaconda3\lib\site-packages\scipy\integrate_ivp\lsoda.py", line 150, in _step_impl self.t_bound, solver.f_params, solver.jac_params)文件“C:\ProgramData\Anaconda3\lib\site-packages\scipy\integrate_ivp\lsoda.py”,第 150 行,在 _step_impl self.t_bound、solver.f_params、solver.jac_params)
File "C:\ProgramData\Anaconda3\lib\site-packages\scipy\integrate_ode.py", line 1343, in run y1, t, istate = self.runner(*args)文件“C:\ProgramData\Anaconda3\lib\site-packages\scipy\integrate_ode.py”,第 1343 行,运行 y1, t, istate = self.runner(*args)
File "C:\ProgramData\Anaconda3\lib\site-packages\scipy\integrate_ivp\base.py", line 139, in fun return self.fun_single(t, y)文件“C:\ProgramData\Anaconda3\lib\site-packages\scipy\integrate_ivp\base.py”,第 139 行,有趣的 return self.fun_single(t, y)
File "C:\ProgramData\Anaconda3\lib\site-packages\scipy\integrate_ivp\base.py", line 21, in fun_wrapped return np.asarray(fun(t, y), dtype=dtype)文件“C:\ProgramData\Anaconda3\lib\site-packages\scipy\integrate_ivp\base.py”,第 21 行,在 fun_wrapped 返回 np.asarray(fun(t, y), dtype=dtype)
TypeError: dxfc_dt() missing 5 required positional arguments: 'x_lc0', 'v_lc0', 'da_lc', 'k', and 'ts'类型错误:dxfc_dt() 缺少 5 个必需的位置 arguments:“x_lc0”、“v_lc0”、“da_lc”、“k”和“ts”
I tested your code, it is working except the it has a division by zero我测试了你的代码,它可以工作,除了它被零除
import numpy as np
import pandas as pd
from scipy.integrate import odeint, solve_ivp
import matplotlib.pyplot as plt
v_lc0 = 0.00833333 # initial velocity of lead car (lc) and following car (cl)
v_fc0 = 0.00833333
da_lc = 3 # deceleration of lead car (mi/s^2)
k = 0.00472222 # sensitivity constant (mi/sec)
d = 0.094697 # distance between cars (miles)
l = 0.00284 # length of average car (miles)
x_lc0 = d - l # distance from back of lc to fc
y0 = [0,0.00833333] # initial distance and velocity
def dxlc_dt(t,x_lc0, v_lc0, ts, da_lc):
if (t < ts):
v_lc = v_lc0 - t*da_lc
x_lc = v_lc0*t - 0.5*da_lc*t**2
else:
v_lc = 0
x_lc = v_lc0*ts - 0.5*da_lc*ts**2
return (x_lc,v_lc)
dxlc_dt = np.vectorize(dxlc_dt)
def dxfc_dt(t,y,x_lc0,v_lc0, da_lc,k,ts):
x_fc = y[0] # distance
v_fc = y[1] # velocity
x_lc,v_lc = dxlc_dt(t, x_lc0, v_lc0, ts,da_lc) # calling distance and velocity
dxfc = v_fc
print(x_lc - x_fc) ### Answer is 0 => DIVISION by ZERO
dvfc = k*(v_lc - v_fc)/(x_lc - x_fc)
dxfc_dt = [dxfc, dvfc]
return (dxfc_dt)
t = np.arange(0,50.0001,0.0001) # time
ts = v_lc0/da_lc # time it takes for lc to stop
tspan = [0,50]
ys = solve_ivp(dxfc_dt,tspan,y0,method='LSODA',t_eval=t, args = (x_lc0,v_lc0,da_lc,k,ts))
x_lc,v_lc=dxlc_dt(t,v_lc0,x_lc0,ts,da_lc)
plt.plot(t,x_lc,label='Lead Car',color = 'black')
plt.plot(t,ys['y'][0],label='Following Car', color='yellow')
plt.xlabel('Time')
plt.ylabel('Distance (miles)')
plt.legend()
plt.show()
I am using Python 3.8.11 with scipy 1.7.1.我正在使用 Python 3.8.11 和 scipy 1.7.1。
solve_ivp Error: "missing 2 required positional arguments:" mentioned that this problem is solved in newer version of scipy. solve_ivp 错误:“缺少 2 个所需的位置 arguments:”提到此问题已在较新版本的 scipy 中解决。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.