'Issue with odeint and class
I am trying to use a simple ordinary diff equation in a class and solve it with odeint. Without using classes everything works ok, but with a class I get the error:
---> 18 return self.u()/self.Vd - (self.Cl/self.Vd)*C
---> 12 if self.t % self.tau <= self.infusion:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
The class code:
class modelCmt1():
def __init__(self, Vd, Cl, infusion, dose, tau, t):
self.Vd = Vd
self.Cl = Cl
self.infusion = infusion
self.dose=dose
self.tau=tau
self.t = t
def u(self):
if self.t % self.tau <= self.infusion:
return self.dose/self.infusion
else:
return 0
def diffEq(self, C, t, Vd, Cl):
return self.u()/self.Vd - (self.Cl/self.Vd)*C
Use:
Cinitial = 0
Vd=22.4
Cl=6
dose=1000
tau=8
infusion=3
t = np.linspace(0,24,100)
model = modelCmt1(infusion,dose,tau,t)
solve = odeint(model.diffEq, Cinitial, t, args=(Vd,Cl))
Solution 1:[1]
Solved it by removing self.t, and passing directly t like this:
class modelCmt1():
def __init__(self, Vd, Cl, infusion, dose, tau, t):
self.Vd = Vd
self.Cl = Cl
self.infusion = infusion
self.dose=dose
self.tau=tau
def u(self, t):
if t % self.tau <= self.infusion:
return self.dose/self.infusion
else:
return 0
def diffEq(self, C, t, Vd, Cl):
return self.u(t)/self.Vd - (self.Cl/self.Vd)*C
Use:
Cinitial = 0
Vd=22.4
Cl=6
dose=1000
tau=8
infusion=3
t = np.linspace(0,24,100)
model = modelCmt1(Vd,Cl,infusion,dose,tau,t)
solve = odeint(model.diffEq, Cinitial, t, args=(Vd,Cl))
plt.plot(t,solve)
plt.show()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Adrian |



