** Time dependent concentration in a first order reversible reaction in a batch reactor
:PROPERTIES:
:categories: ODE
:tags: reaction engineering
:date: 2013/02/05 09:00:00
:updated: 2013/03/06 16:28:57
:END:
[[http://matlab.cheme.cmu.edu/2011/08/07/first-order-reversible-reaction-in-batch-reactor/][Matlab post]]
Given this reaction $A \rightleftharpoons B$, with these rate laws:
forward rate law: $-r_a = k_1 C_A$
backward rate law: $-r_b = k_{-1} C_B$
plot the concentration of A vs. time. This example illustrates a set of coupled first order ODES.
#+BEGIN_SRC python
from scipy.integrate import odeint
import numpy as np
def myode(C, t):
# ra = -k1*Ca
# rb = -k_1*Cb
# net rate for production of A: ra - rb
# net rate for production of B: -ra + rb
k1 = 1 # 1/min;
k_1 = 0.5 # 1/min;
Ca = C[0]
Cb = C[1]
ra = -k1 * Ca
rb = -k_1 * Cb
dCadt = ra - rb
dCbdt = -ra + rb
dCdt = [dCadt, dCbdt]
return dCdt
tspan = np.linspace(0, 5)
init = [1, 0] # mol/L
C = odeint(myode, init, tspan)
Ca = C[:,0]
Cb = C[:,1]
import matplotlib.pyplot as plt
plt.plot(tspan, Ca, tspan, Cb)
plt.xlabel('Time (min)')
plt.ylabel('C (mol/L)')
plt.legend(['$C_A$', '$C_B$'])
plt.savefig('images/reversible-batch.png')
#+END_SRC
#+RESULTS:
[[./images/reversible-batch.png]]
That is it. The main difference between this and Matlab is the order of arguments in odeint is different, and the ode function has differently ordered arguments.