Working with 1st-order rates:
let’s learn a little bit of Python
while we learn chemical kinetics
By the way... this may useful for n-th
order reactions too
#ck2526
Syllabus
You may find a commented Syllabus in your Moodle course
Kinetics
Differential rate law
Integrated rate law
Rate constant
Reaction order
Molecularity
Experiment-first or postulate-first
Plot of concentrations vs time
Plot of rates vs time
Numerical methods
Python in Chemical Kinetics
Working with 1st order
kinetics
Question A: If we guess a
differential law, v=v(t). May we
derive an integrated rate law?
Question B: If we have some data
of concetrations vs time. May we
derive an integrated reaction law?
(A) From guess to plot (1)
●
Let’s guess a 1st order rate law, for
reaction A ->P
●
d[A]/dt = - k [A]
●
Now we can either...
– (1.1)Try to solve it analytically by hand
– (1.2) Try to solve it analytically using
Python
(A) From guess to plot (2)
●
Let’s guess a 1st order rate law, for reaction A ->P
●
d[A]/dt = - k [A]
●
Now we can also...
– (2.1) Solve it numerically using a calculator or Python
– (2.2) Solve it numerically using routines for differential
equations in Python
– (2.3) Simulate it in Tenua (you computer may complain)
– (2.4) Simulate it stochastically (each run yiels different results)
– (2.5) Use chemistry-related packages in Python: ChemPy
Solving analytically - by hand (1.1)
Solving by hand (1.1)
Solving analytically/symbolically
in Python (1.2)
# Analytical solution to 1st order kinetics A->P
from sympy import *
A = symbols ('A', cls=Function)
k,t, A0 = symbols ('k t A0')
diffeq = Eq(A(t).diff(t),-k*A(t))
pprint(diffeq)
expr=dsolve (diffeq,A(t))
pprint(expr)
#determine C1
C1 = Symbol('C1')
C1_ic = solve([Link]({t:0})-[Link]({t:0}),C1)[0]
pprint (C1_ic)
equation = [Link]({C1:C1_ic})
pprint(equation)
(see [Link]
[analytical-1st file] in [Link]
Plotting analytical solutions via Python
... Use the lambdify function
# code to generate a plottable function - i.e., numerical values of [A](t)
# set initial concentration, i.e., [A](0)
equa2 = [Link]({A(0):1})
pprint(equa2)
# set the value of the rate constant
equa3 = [Link]({k:0.5})
pprint (equa3)
# transform sympy function into numerical (numpy) function
f = lambdify(t,[Link])
print (f)
import numpy
import [Link] as plt
# for values to t in t0...[Link]
l1=[Link](0,10,0.3)
print(l1)
# Evaluate function [A](t) for that range
print(f(l1))
[Link](l1,f(l1),'bo')
[analytical-1st file] in [Link]
A, P = 1.00, 0.00 # molarity, M
k = 0.05 # 1/s for a first-order reaction
length = 100 # length of simulation in seconds
Solving
time = range(length + 1)
# create arrays to hold calculated concentrations
A_conc = [Link](length + 1)
P_conc = [Link](length + 1)
# simulation
numerically in
for sec in time:
# record concentration
A_conc[sec] = A
Python
P_conc[sec] = P
# recalculate rate
rate = k * A
(simple) (2.1)
# recalculate new concentration
A -= rate
P += rate
s = 5 # step size
[Link](time, A_conc, label='A Simulated')
[Link](time, P_conc, label='P Simulated')
[Link]('Time, s')
[Link]('Concentration, M')
[Link]();
May be found in Section 9.1.2 of Weiss’ book.
Solving numerically in
Python (i.e., well) (2.2)
import numpy as np
import [Link] as plt
from [Link] import odeint
def rate_1st(A, t):
return -k * A
t = [Link](0,50,4) # time(seconds)
A0 = 1 # starting concentration (molarity)
k = 0.1 # rate constant in 1/s
A_t = odeint(rate_1st, A0, t)
P_t = A0 - A_t # concentration of product
[Link](t, A_t, 'o', label='A')
[Link](t, P_t, 'p', label='P')
[Link]('Time, s')
[Link]('[X], M')
[Link]()
(see Weiss’ book, section 9.1.2) [ode-1st file] in [Link]
Simulate using Tenua (2.3)
a <-> b; // the reaction
k(+): 2; // initialization statements
K(-): 0; // reaction only proceeds to the right A → P
a: 10;
b: 0;
startTime: 0;
endTime: 10;
epsilon: 1e-6;
timeStep: 0;
*output // output expressions
a; b;
TENUA will be considered later in this course, not now
Stochastic solution (2.4)
●
Not developped in this course
ChemPy Python package (2.5)
●
ChempPy must be installed first with
"pip install ChemPy"
●
See examples at
– [Link]
– [Link]
31893/chemical-kinetics—system-of-
ordinary-differential-equations-
(B) From data to rate law
●
Using Python / regression
●
Using TENUA (it requires a close
numerical guess for rate constants)
... 2nd part of this course -
problems
TENUA will be considered later in this course, not now
Deriving rate laws from lab data
●
See the example in folder problems, problem
3.
●
# Reaction A->P let's check if it is first-
order, thus [A]=[A]0 exp(-k*t)
– # x=time
– x=[0,184,319,526,867,1168,1877,2315,3144]
– # ya=concentration of reactant A, in reaction A->P
– ya=[2.33,2.08,1.91,1.67,1.36,1.11,0.72,0.55,0.34]
Let’s use AI tools: [Link]
One may ask questions about chemical
kinetics
●
"Show me how to integrate a first-order
rate law in chemical kinetics"
●
"Write a python code de plot the time
evolution of a first-order chemical kinetics"
●
"Can you provide an example of fitting
experimental data to a first-order reaction
model in Python"
As for Problem 3... using AI
At times
0,184,319,526,867,1168,1877,2315,3144 (in
seconds), we find concentration of a
chemical species to be
2.33,2.08,1.91,1.67,1.36,1.11,0.72,0.55,0.34
mol/liter. Does this reaction follow a first-
order kinetics?