Python Forum

Full Version: I tried to solve basic reaction kinetics ode but i cant run the simulation. Plz help
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt

def rxn(Z, t):
    k1 = 1
    k2 = 1.5
    
    r1 = k1*Z[0]*Z[1]
    r2 = k2*Z[1]*Z[2]
    
    dAdt = -r1
    dBdt = -r1 -r2
    dCdt = r1 -r2
    dDdt = r2
    return[dAdt,dBdt,dCdt,dDdt]

t = np.arange(0,3.01,0.2)
Z0 = [1,1,0,0]
Conc = odeint(rxn(Z0,t))

cA = Conc[:,0]
cB = Conc[:,1]
cC = Conc[:,2]
cD = Conc[:,3]

S = np.empty(len(cC))
for i in range(len(cC)):
    if abs(cC[i]+cD[i]>1e-10):
        S[i] = cC/(cC+cD)
    else:
        S[i] = 1.0

plt.plot(t,cA)
plt.plot(t,cB)
plt.plot(t,cC)
plt.plot(t,cD)
plt.xlabel(time)
plt.ylabel(Concentration)
plt.legend('Ca','Cb','Cc','Cd')
Tried running, you are missing arguments:
Error:
Traceback (most recent call last): File "/media/larz/Projects/projects/QRST/T/TryStuffNew/src/Feb_8_2022_1.py", line 20, in <module> Conc = odeint(rxn(Z0,t)) TypeError: odeint() missing 2 required positional arguments: 'y0' and 't'