Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 函数模型()缺少1个必需的位置参数_Python_Arguments_Ode - Fatal编程技术网

Python 函数模型()缺少1个必需的位置参数

Python 函数模型()缺少1个必需的位置参数,python,arguments,ode,Python,Arguments,Ode,我试图使用曲线拟合来估计一个ODE的两个参数值A和B,然后将该ODE的解决方案拟合到我的数据集,绘制结果 我的代码: def model(I,t,A,B): dIdt = A*(2000 - I) + B*(2000 - I)*(I/2000) return dIdt xData = # this is an np.array of my x values yData = # this is an np.array of my y values plt.plot(xData,

我试图使用
曲线拟合
来估计一个ODE的两个参数值
A
B
,然后将该ODE的解决方案拟合到我的数据集,绘制结果

我的代码:

def model(I,t,A,B):
    dIdt = A*(2000 - I) + B*(2000 - I)*(I/2000)
    return dIdt

xData = # this is an np.array of my x values
yData = # this is an np.array of my y values
plt.plot(xData, yData, 'r.-', label='experimental-data')   #This part of the code seems to work

initialGuess = [1.0,1.0]    
popt, pcov = curve_fit(model, xData, yData, initialGuess)  #This is where the error is
print(popt)
xFit = np.arange(0.0, 5.0, 0.01)
I0 = 0
t = np.linspace(0,60)
I = odeint(model,I0,t)                                     #This is where i integrate the ODE to obtain I(t).

plt.plot(xFit, I(xFit, *popt), 'r', label='fit params: a=%5.3f, b=%5.3f' % tuple(popt))
plt.xlabel('x')
plt.ylabel('y')
plt.legend()
plt.show()
我得到的错误是

model()缺少1个必需的位置参数:“B”

我大致了解发生了什么:my model()函数在开始时接受了4个参数:I、t、A和B。然而,在代码中的某个地方,代码只识别出前3个参数,而忽略了B。我不确定如何修复这个问题

我试过几件事:

  • 从错误行中去掉'initialGuess',这样在
    曲线拟合
    行中有3个参数,这给了我一个新的错误
  • 输入不正确:N=3不得超过M=1

    这让我觉得,initialGuess条目不是问题所在

  • 将错误行中的
    model
    更改为
    model()
    ,这导致了错误
    model()缺少4个必需的位置参数:“I”、“t”、“A”和“B”

  • 解决了这个问题,我将
    模型
    改为
    模型(I,t,A,B)
    ,结果给了我
    名称“A”没有定义

  • 现在我迷路了

    所有这些错误都发生在同一行中,因此我尝试在其中更改内容,但可能我遗漏了其他内容。大多数涉及此错误的在线来源都提到必须实例化一个类实例,但我不确定这在本文中意味着什么,我还没有在代码中定义一个类


    我希望我已经澄清了我的困惑,如有任何指导,我们将不胜感激。

    从scipy.optimize使用
    model
    函数执行曲线拟合(请参阅):

    返回:

    [1. 1.]
    
    接下来,使用
    scipy中的
    odeint
    执行集成。集成

    from scipy.integrate import odeint
    
    xFit = np.arange(0.0, 5.0, 0.1)
    I0 = 0
    t = np.linspace(0, 60)
    a, b = 1, 1
    
    
    def model(i, t, a, b):
        return a * (2_000 - i)\
               + b * (2_000 - i)\
               * (i / 2_000)
    
    
    I = odeint(model, I0, t, args=(a, b))
    plt.plot(xFit, I[:, 0], 'b', label= 'fit params: a=%5.3f, b=%5.3f' % tuple(popt))
    plt.xlabel('x')
    plt.ylabel('y')
    plt.legend()
    plt.show()
    
    显示绘图(请参见):

    为什么要在模型定义中输入“t”?它没有被使用,因此没有明显的必要,删除它似乎可以解决您的问题,尽管在进行曲线拟合的地方,您可能希望在扩展数据和yData之前使用initiaGuess。
    from scipy.integrate import odeint
    
    xFit = np.arange(0.0, 5.0, 0.1)
    I0 = 0
    t = np.linspace(0, 60)
    a, b = 1, 1
    
    
    def model(i, t, a, b):
        return a * (2_000 - i)\
               + b * (2_000 - i)\
               * (i / 2_000)
    
    
    I = odeint(model, I0, t, args=(a, b))
    plt.plot(xFit, I[:, 0], 'b', label= 'fit params: a=%5.3f, b=%5.3f' % tuple(popt))
    plt.xlabel('x')
    plt.ylabel('y')
    plt.legend()
    plt.show()