我是Python的新手,正在尝试使用scipy.interpolate.odeint
来求解这个微分方程。但是,我一直得到TypeError
。我已经看过了,但无法找到如何修复这个问题以使odeint
模块工作。
下面是我的代码:
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from scipy.interpolate import CubicSpline
from scipy.integrate import odeint
from math import *
# Define parameters
alpha = 52.875
beta = 13.345
gamma = -1.44
delta = 2.29
# Define model
def model(h,t,om):
halflife = alpha - (beta(-delta+gamma*om))
k = log(2)/halflife
dherb_dt = -kh
return dherb_dt
# Initial condition
h0 = 4.271 # mg/kg, assuming a 2.67 pt/acre application of Dual II Magnum
# Time, in days, to interpolate over
t = np.linspace(0, 20)
# Solve ODE
om = 2.5
y1 = odeint(model, h0, t, args=(om,))
# Plot
plt.plot(t,y1)
plt.xlabel("Months")
plt.ylabel("Dose")
plt.show()
但是,会发生下列错误:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Input In [17], in <cell line: 22>()
20 # Solve ODE
21 om = 2.5
---> 22 y1 = odeint(model, h0, t, args=(om,))
24 # Plot
25 plt.plot(t,y1)
File ~\anaconda3\envs\agron893\lib\site-packages\scipy\integrate\_odepack_py.py:241, in odeint(func, y0, t, args, Dfun, col_deriv, full_output, ml, mu, rtol, atol, tcrit, h0, hmax, hmin, ixpr, mxstep, mxhnil, mxordn, mxords, printmessg, tfirst)
239 t = copy(t)
240 y0 = copy(y0)
--> 241 output = _odepack.odeint(func, y0, t, args, Dfun, col_deriv, ml, mu,
242 full_output, rtol, atol, tcrit, h0, hmax, hmin,
243 ixpr, mxstep, mxhnil, mxordn, mxords,
244 int(bool(tfirst)))
245 if output[-1] < 0:
246 warning_msg = _msgs[output[-1]] + " Run with full_output = 1 to get quantitative information."
Input In [17], in model(h, t, om)
8 def model(h,t,om):
----> 9 halflife = alpha - (beta(-delta+gamma*om))
10 k = log(2)/halflife
11 dherb_dt = -kh
TypeError: 'float' object is not callable
如何解决此问题以便解出方程?
1条答案
按热度按时间6tqwzwtp1#
你试图使用典型的数学符号
a(b)
来乘以a * b,但Python语法并不是这样工作的,你必须显式地使用符号*
来执行乘法。对于Python来说,
beta(-delta+gamma*om)
看起来像一个函数调用。请使用以下命令: