1
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. x_lc,v_lc is being called from another 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.

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?

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

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

File "C:\ProgramData\Anaconda3\lib\site-packages\scipy\integrate_ivp\ivp.py", line 502, in 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()

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)

File "C:\ProgramData\Anaconda3\lib\site-packages\scipy\integrate_ode.py", line 1343, in run 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)

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)

TypeError: dxfc_dt() missing 5 required positional arguments: 'x_lc0', 'v_lc0', 'da_lc', 'k', and 'ts'

qhumphrey
  • 23
  • 4

1 Answers1

0

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.

solve_ivp Error: "missing 2 required positional arguments:" mentioned that this problem is solved in newer version of scipy.

yoonghm
  • 4,198
  • 1
  • 32
  • 48