【问题标题】:Function missing required positional arguments缺少所需位置参数的函数
【发布时间】:2022-01-09 13:42:24
【问题描述】:
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]

我为二阶 ODE IVP 编写了代码。 x_lc,v_lc 正在从另一个函数调用。当我运行程序时,它告诉我参数 'x_lc0'、'v_lc0'、'da_lc'、'k' 和 'ts' 丢失了,但我在外部定义了它们。

我相信这与我的这部分代码有关,如下所示,因为控制台显示了这一点,但我想知道我可能做错了什么。我需要以不同的方式定义这些参数吗?

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(最近一次通话最后):---------

文件“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))

文件“C:\ProgramData\Anaconda3\lib\site-packages\scipy\integrate_ivp\ivp.py”,第 502 行,在solve_ivp message = solver.step()

文件“C:\ProgramData\Anaconda3\lib\site-packages\scipy\integrate_ivp\base.py”,第 182 行,步骤 成功,消息 = self._step_impl()

文件“C:\ProgramData\Anaconda3\lib\site-packages\scipy\integrate_ivp\lsoda.py”,第 150 行,在 _step_impl self.t_bound、solver.f_params、solver.jac_params)

文件“C:\ProgramData\Anaconda3\lib\site-packages\scipy\integrate_ode.py”,第 1343 行,运行中 y1, t, istate = self.runner(*args)

文件“C:\ProgramData\Anaconda3\lib\site-packages\scipy\integrate_ivp\base.py”,第 139 行,有趣 return self.fun_single(t, y)

文件“C:\ProgramData\Anaconda3\lib\site-packages\scipy\integrate_ivp\base.py”,第 21 行,在 fun_wrapped return np.asarray(fun(t, y), dtype=dtype)

TypeError:dxfc_dt() 缺少 5 个必需的位置参数:“x_lc0”、“v_lc0”、“da_lc”、“k”和“ts”

【问题讨论】:

  • 您能否提供一个更完整的示例(定义了 dxlc_dt,并给出了起始参数)?因此更容易重现您的问题。
  • 这是我的实际代码以获取更多信息
  • 请提供您收到的实际错误消息 - 目前尚不清楚问题到底是什么。
  • @Grismar 刚刚添加了它
  • 如果您还不知道,您可以通过import scipy; print(scipy.__version__) 查看您使用的 SciPy 版本。您至少需要 1.4 版本才能使用 solve_ivpargs 参数。

标签: python scipy ode


【解决方案1】:

我测试了你的代码,除了除以零之外它可以正常工作

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()

我正在使用 Python 3.8.11 和 scipy 1.7.1。

solve_ivp Error: "missing 2 required positional arguments:" 提到这个问题在新版本的 scipy 中得到了解决。

【讨论】:

  • 有趣.. 我仍然遇到同样的错误
  • 另外,上面的 x_lc,v_lc 的定义不应该阻止它吗?
  • 我已经更新了回复。您可能需要更新到较新的 scipy。
  • 谢谢你,我必须更新 scipy,它现在可以工作了!
猜你喜欢
  • 2021-03-19
  • 2018-06-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-17
  • 2021-04-07
  • 1970-01-01
相关资源
最近更新 更多