我试图使用curve_fit
估计一个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() missing 1 required positional argument: 'B'.
我大致了解发生了什么:my model()函数在开始时接受了4个参数:I、t、A和B。然而,在代码中的某个地方,代码只识别出前3个参数,而忽略了B。我不确定如何修复这个问题
我试过几件事:
curve_fit
行中有3个参数,这给了我一个新的错误Improper input: N=3 must not exceed M=1
这让我觉得,initialGuess条目不是问题所在
将错误行中的model
更改为model()
,这给了我错误
model() missing 4 required positional arguments: 'I', 't', 'A', and 'B'
为了解决这个问题,我把model
改为model(I,t,A,B)
,结果给了我name 'A' is not defined
现在我迷路了
所有这些错误都发生在同一行中,因此我尝试在其中更改内容,但可能我遗漏了其他内容。大多数涉及此错误的在线来源都提到必须实例化一个类实例,但我不确定这在本文中意味着什么,我还没有在代码中定义一个类
我希望我已经澄清了我的困惑,希望您能给予指导
使用
model
函数从scipy.optimize执行曲线拟合(请参见https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.curve_fit.html):返回:
接下来,使用来自
scipy.integrate
的odeint
执行集成:显示绘图(请参见https://docs.scipy.org/doc/scipy/reference/generated/scipy.integrate.odeint.html):
相关问题 更多 >
编程相关推荐