Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.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 Monod生长/降解方程与实验数据的曲线拟合_Python_Scipy_Curve Fitting_Differential Equations - Fatal编程技术网

Python Monod生长/降解方程与实验数据的曲线拟合

Python Monod生长/降解方程与实验数据的曲线拟合,python,scipy,curve-fitting,differential-equations,Python,Scipy,Curve Fitting,Differential Equations,所以这里面临的问题是Monod方程与实验数据的曲线拟合。细菌生长和有机碳降解的模型如下所示: dX/dt=(u*S*X)/(K+S) dS/dt=(-1/Y)*u*S*X)/(K+S) 使用scipy odeint函数求解这些方程。整合后的结果存储到两个载体中,一个用于生长,另一个用于降解。下一步是将该模型与实验观测数据进行曲线拟合,并估计模型参数u、k和y。一旦代码运行,就会产生以下误差: File "C:\ProgramData\Anaconda3\lib\site-packages\sci

所以这里面临的问题是Monod方程与实验数据的曲线拟合。细菌生长和有机碳降解的模型如下所示:

dX/dt=(u*S*X)/(K+S)

dS/dt=(-1/Y)*u*S*X)/(K+S)

使用scipy odeint函数求解这些方程。整合后的结果存储到两个载体中,一个用于生长,另一个用于降解。下一步是将该模型与实验观测数据进行曲线拟合,并估计模型参数u、k和y。一旦代码运行,就会产生以下误差:

File "C:\ProgramData\Anaconda3\lib\site-packages\scipy\optimize\minpack.py", line 392, in leastsq
    raise TypeError('Improper input: N=%s must not exceed M=%s' % (n, m))

TypeError: Improper input: N=3 must not exceed M=2"
为了方便起见,曲线拟合部分被注释掉,因此可以生成预期结果的绘图。下面是代码示例:

import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import odeint
from scipy.optimize import curve_fit

"""Experimental data!"""
t_exp = np.array([0, 8, 24, 32, 48, 96, 168])
S_exp = np.array([5.5, 4.7, 3.7, 2.5, 1.5, 0.7, 0.5])
X_exp = np.array([10000, 17000, 30000, 40000, 60000, 76000, 80000])

"Model of the microbial growth and the TOC degradation"
# SETTING UP THE MODEL
def f(t, u, K, Y):
     'Function that returns mutually dependent variables X and S'
     def growth(x, t):
         X = x[0]
         S = x[1]
         "Now differential equations are defined!"
         dXdt = (u * S * X )/(K + S)
         dSdt = ((-1/Y) * u * S * X )/(K + S)
         return [dXdt, dSdt]
     # INTEGRATING THE DIFFERENTIAL EQUATIONS
     "initial Conditions"
     init = [10000, 5]
     results = odeint(growth, init, t)
     "Taking out desired column vectors from results array"
     return results[:,0], results[:,1]

# CURVE FITTING AND PARAMETER ESTIMATION
"""k, kcov = curve_fit(f, t_exp, [X_exp, S_exp], p0=(1, 2, 2))
u = k[0]
K = k[1]
Y = k[2]"""

# RESULTS OF THE MODEL WITH THE ESTIMATED MODEL PARAMETERS
t_mod = np.linspace(0, 168, 100)
compute = f(t_mod, 0.8, 75, 13700)# these fit quite well, but estimated manually
X_mod = compute[0]
S_mod = compute[1]

# PLOT OF THE MODEL AND THE OBSERVED DATA
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.plot(t_exp, X_exp, "yo")
ax1.plot(t_mod, X_mod, "g--", linewidth=3)
ax1.set_ylabel("X")

ax2 = ax1.twinx()
ax2.plot(t_exp, S_exp, "mo", )
ax2.plot(t_mod, S_mod, "r--", linewidth=3)
ax2.set_ylabel("S", color="r")
for tl in ax2.get_yticklabels():
    tl.set_color("r")
plt.show()
如能提供任何关于如何处理此问题并继续进行的建议,我们将不胜感激。提前感谢。

f()的结果需要与作为第三个参数输入到
曲线拟合中的实验数据具有相同的形状。在
f()
的最后一行中,您只需获取两个ODE的解的t=0s值并返回该值,但您应该返回完整的解。使用
curve\u fit
一次拟合多组数据时,只需将它们集中(水平堆叠),即

并称之为曲线拟合

k, kcov = curve_fit(f, t_exp, np.hstack([X_exp, S_exp]), p0=(1, 2, 2))
您还必须调整脚本的绘图部分:

compute = f(t_mod, u, K, Y)
compute = compute.reshape((2,-1))

错误消息是scipy抱怨你有3个变量和2个值。谢谢你的回答@PéterLeéh,我仍然在试图找出如何摆脱它,我应该以什么方式调整我的脚本,以便它可以完成工作。亲爱的克里斯蒂安,非常感谢你的回答!这很有帮助。
compute = f(t_mod, u, K, Y)
compute = compute.reshape((2,-1))