我正在尝试对具有2个反应物的Python中的两个耦合ODE进行参数优化。我想使用标准化残差作为我的目标函数。
反应是:
da/dt = - k1 * a + k2 * b
db/dt = k1 * a - k2 * b
初始值:a(0) = 1
和b(0) = 0
。
初始反应常数:k1 = 1
和k2 = 0.1
。反应常数在1e-3 < k < 10
范围内。这些是要优化的参数。
我设法编写了ODE和目标函数。
到目前为止,这是我的代码。
import numpy as np
from scipy.integrate import odeint
import operator
# My test data
data_b = [0.0244, 0.0842, 0.1208, 0.1724, 0.2315, 0.2634, 0.2831, 0.3084, 0.3079, 0.3097, 0.3324]
data_x = np.linspace(0, 10, 11) # Time points at which the data was measured
# Initial values
x = [1, 0]
k = [1, 0.1]
# My coupled ODE functions with constant reaction constants
def f(x, t):
k = [1, 0.1]
a = x[0]
b = x[1]
k1 = k[0]
k2 = k[1]
dadt = - k1 * a + k2 * b
dbdt = k2 * a - k2 * b
return(dadt, dbdt)
# Trying the function.
y = odeint(f, x, data_x)
print(y)
# My objective function. Only the concentration of `b` is taken into account.
def objective(sim, measured):
# The standard deviation of the data.
# The error is normal distributed with mean zero and `sd = 0.015`
sigma = np.repeat(0.015, len(sim))
j = list(map(operator.sub, sim, measured)) # Pairwise subtraction
j = list(map(operator.truediv, j, sigma)) # Division by sigma
j = list(map(operator.pow, j, np.repeat(2,len(j)))) # Squaring
j = sum(j)
return(j)
# Trying the objective function
j = objective(y[:,1], data_b)
print(j)
# The same ODEs but with reaction constants as parameters
def fb(x, k, t):
a = x[0]
b = x[1]
k1 = k[0]
k2 = k[1]
# The ODEs
dadt = - k1 * a + k2 * b
dbdt = k1 * a - k2 * b
return(dadt, dbdt)
此函数返回以下错误:
error: Extra arguments must be in a tuple
如何解决此错误以及如何进行?我知道我必须结合ODE函数和目标函数并使用函数scipy.optimize.curve_fit
进行优化。但是我不知道该怎么做。