【问题标题】:positional arguments are being asked for even though I have included them in my args instead?即使我已将它们包含在我的参数中,但仍要求位置参数?
【发布时间】:2022-12-01 06:56:44
【问题描述】:

您好我正在编写一个程序,该程序旨在为分配的不同 ODE 执行 RK4 方法。我们必须使用的东西之一是 *args。当我调用包含 *args(rk4 一个)的函数时,我在最后列出了额外的参数。当我尝试运行它时,它说我的函数(在本例中为 f2a)缺少 3 个必需的位置参数,即使我已将它们包含在我认为是 RK4 函数参数列表的 args 部分的末尾。这是否意味着我没有指出这些是正确的额外参数?或者我是否需要将它们添加到我正在使用 RK4 的功能中?我对编码真的很陌生,所以非常感谢任何帮助。这是我的整个代码:

import numpy as np
import math
import matplotlib.pyplot as plt

#defining functions
H0=7 #initial height, meters
def f2a(t,H,k,Vin,D):
    dhdt=4/(math.pi*D**2)*(Vin-k*np.sqrt(H))
    return(dhdt)

def fb2(J,t):
    x=J[0]
    y=J[1]
    dxdt=0.25*y-x
    dydt=3*x-y
    #X0,Y0=1,1 initial conditions
    return([dxdt,dydt])



#x0 and y0 are initial conditions
def odeRK4(function,tspan,R,h,*args):
 
    #R is vector of inital conditions
    x0=R[0]
    y0=R[1]


    #writing statement for what to do if h isnt given/other thing
    if h==None:
        h=.01*(tspan[1]-tspan[0])
    elif h> tspan[1]-tspan[0]:
        h=.01*(tspan[1]-tspan[0])
    else:
        h=h
    #defining the 2-element array (i hope)
    #pretty sure tspan is range of t values
    x0=tspan[0] #probably 0 if this is meant for time
    xn=tspan[1] #whatever time we want it to end at?
    #xn is final x value-t
    #x0 is initial

    t_values=np.arange(x0,21,1) #0-20 
    N=len(t_values)
    y_val=np.zeros(N)
    y_val[0]=y0
    #I am trying to print all the Y values into this array
    for i in range(1,N):
        #rk4 method
        #k1
        t1=t_values[i-1] #started range @ 1, n-1 starts at 0
        y1=y_val[i-1]
        k1=function(t1,y1)
    
        #k2
        t2=t_values[i-1]+0.5*h
        y2=y_val[i-1]+0.5*k1*h
        k2=function(t2,y2)
    
      
        #k3
        t3=t_values[i-1]+0.5*h
        y3=y_val[i-1]+0.5*k2*h
        k3=function(t3,y3)
    
        #k4
        t4=t_values[i-1]+h
        y4=y_val[i-1]+h*k3
        k4=function(t4,y4)
    
        y_val[i]=y_val[i-1]+(1/6)*h*(k1+2*k2+2*k3+k4)
        #this fills the t_val array and keeps the loop going

    a=np.column_stack(t_values,y_val)
    print('At time T, Y= (t on left,Y on right)')
    print(a)
    plt.plot(t_values,y_val)


print('For 3A:')
#k=10, told by professor bc not included in instructions
odeRK4(f2a, [0,20],[0,7], None, 10,150,7)

【问题讨论】:

  • 是的,您需要将 *args 从 RK4 传递给您正在集成的功能。否则它会如何工作?现在您只将两个参数传递给传递给 RK4 的 function

标签: python


【解决方案1】:

在运行此命令时,您正确传递了将存储在函数odeRK4args 参数中的额外参数:

# at the end of you snippet ^^
odeRK4(f2a, [0,20],[0,7], None, 10,150,7)

但是,正如您提到的 f2a 需要 3 个参数。查看odeRK4的定义,我们看到在传递f2a之后,它现在在odeRK4中被称为function

def odeRK4(function,tspan,R,h,*args):

在函数odeRK4中,你像这样使用function,除了odeRK4的参数之外,在任何地方都看不到args的用途:

t1=t_values[i-1] #started range @ 1, n-1 starts at 0
y1=y_val[i-1]
k1=function(t1,y1) # <--- 2 instead of 3 for f2a
    
#k2
t2=t_values[i-1]+0.5*h
y2=y_val[i-1]+0.5*k1*h
k2=function(t2,y2)  # <--- 2 instead of 3 for f2a

# more uses...

正如您所见,这是两个参数。为了将args 传递给function。你必须这样称呼他们:

t1=t_values[i-1] #started range @ 1, n-1 starts at 0
y1=y_val[i-1]
k1=function(*args) # <--- fixed
    
#k2
t2=t_values[i-1]+0.5*h
y2=y_val[i-1]+0.5*k1*h
k2=function(*args)  # <--- fixed

# more uses...

这解决了您使用 3 arguments wanted 的问题,但这会使您的计算不使用您的 t1t2 等。但是您有自己的修复程序并使用 args 以便它满足您的需要。

祝你好运!

【讨论】:

    猜你喜欢
    • 2022-12-15
    • 1970-01-01
    • 1970-01-01
    • 2019-10-23
    • 2021-01-02
    • 1970-01-01
    • 2021-11-02
    • 2015-07-25
    • 1970-01-01
    相关资源
    最近更新 更多