TypeError: model() takes exactly 3 arguments (5 given)
TypeError: model() takes exactly 3 arguments (5 given)
odeint 以下设置工作正常;
import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import odeint
v0 = 10.0
k1 = 0.5
k2 = 0.35
def model(x,t):
dx0 = v0 - k1*x[0]
dx1 = k1*x[0] - k2*x[1]
return [dx0, dx1]
time = linspace(0.0,20.0,100)
xinit = array([0.0,0.0])
x = odeint(model,xinit,time)
plt.plot(time, x[:,0], time, x[:,1])
但是当我想定义一个参数化模型然后将参数传递给 model() 函数时,当 odeint 调用它时我遇到了这个错误:TypeError: model( ) 恰好接受 3 个参数(给定 5 个)
.这里出了什么问题?传递参数的正确设置是什么?
import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt
def model (x,t,p):
dot_x = np.zeros(2)
v0 = p[0]
k1 = p[1]
k2 = p[2]
dot_x[0] = v0 - k1*x[0]
dot_x[1] = k1*x[0] - k2*x[1]
return dot_x
p = (10,0.5,.35)
xinit = [0.0,0.0]
time = linspace(0.0,20.0,100)
x = odeint(model,xinit,time,p)
plt.plot(time, x[:,0], time, x[:,1])
感谢 Thiru 的评论,这是解决方案:
import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt
def model (x,t,*p):
dot_x = np.zeros(2)
v0 = p[0]
k1 = p[1]
k2 = p[2]
dot_x[0] = v0 - k1*x[0]
dot_x[1] = k1*x[0] - k2*x[1]
return dot_x
p = (10,0.5,.35)
xinit = [0.0,0.0]
time = linspace(0.0,20.0,100)
x = odeint(model,xinit,time,p)
plt.plot(time, x[:,0], time, x[:,1])
odeint 以下设置工作正常;
import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import odeint
v0 = 10.0
k1 = 0.5
k2 = 0.35
def model(x,t):
dx0 = v0 - k1*x[0]
dx1 = k1*x[0] - k2*x[1]
return [dx0, dx1]
time = linspace(0.0,20.0,100)
xinit = array([0.0,0.0])
x = odeint(model,xinit,time)
plt.plot(time, x[:,0], time, x[:,1])
但是当我想定义一个参数化模型然后将参数传递给 model() 函数时,当 odeint 调用它时我遇到了这个错误:TypeError: model( ) 恰好接受 3 个参数(给定 5 个) .这里出了什么问题?传递参数的正确设置是什么?
import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt
def model (x,t,p):
dot_x = np.zeros(2)
v0 = p[0]
k1 = p[1]
k2 = p[2]
dot_x[0] = v0 - k1*x[0]
dot_x[1] = k1*x[0] - k2*x[1]
return dot_x
p = (10,0.5,.35)
xinit = [0.0,0.0]
time = linspace(0.0,20.0,100)
x = odeint(model,xinit,time,p)
plt.plot(time, x[:,0], time, x[:,1])
感谢 Thiru 的评论,这是解决方案:
import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt
def model (x,t,*p):
dot_x = np.zeros(2)
v0 = p[0]
k1 = p[1]
k2 = p[2]
dot_x[0] = v0 - k1*x[0]
dot_x[1] = k1*x[0] - k2*x[1]
return dot_x
p = (10,0.5,.35)
xinit = [0.0,0.0]
time = linspace(0.0,20.0,100)
x = odeint(model,xinit,time,p)
plt.plot(time, x[:,0], time, x[:,1])