CHBE 230 Computational Methods
Case Study &BDIIPNFXPSLTVCNJTTJPOTIPVMEIBWFEPDVNFOUTBTJOHMF+VQZUFSOPUFCPPLBOEB1%'
a single
filebe
output
from
Jupyter
FYQPSUFEGSPNUIFOPUFCPPL*ODMVEFZPVSMBTUOBNFBOETUVEFOUOVNCFSJOUIFGJMFOBNFT
FH
EachSubmit
homework
mustPDF
be
handed
as a Jupyter
andnotebook
a PDF
printout.
Each
homework
must
handed
as your
a notebook
single
Jupyter
notebook
/BNF@@)8JQZOCBOE/BNF@@)8QEG
In this case study we build upon the example given on page 144 in the textbook. The author
calls this ”the greenhouse gases and rainwater case study”. Here, we consider the equilibrium
ofCO2i nt heair,witht hecarbonateCO3 2andbicarbonateHCO3 i nt hewater.Thes ystemof
equationst oconsideraret hef ollowingequilibriumr elationships
CO2(g) ⌦ HCO3 + H +
HCO3 ⌦ CO3 + H
2
K1 =
[H + ][HCO3 ]
[CO2(g) ]
[H + ][CO3 2 ]
K2 =
[HCO3 ]
Kw = [H + ][OH ]
+
H2 O ⌦ OH + H +
(1)
(2)
(3)
In addition to this, there must be charge neutrality in the solution, i.e.,
[H + ] = [OH ] + 2[CO3 2 ] + [HCO3 ]
(4)
It should be noted that the notations given in the textbook are slightly changed. [CO2(g) ] is a
known function given by the expression
[CO2(g) ] =
KH
0.012226(t
106
1983)2 + 1.418542(t
1983) + 342.38309
(5)
where t is the year. In this case the question the textbook is asking is to estimate the pH in
the rainwater (pH = log10 [H + ]) as a function of time (year). The constants K1 = 10 6.3 ,
K2 = 10 10.3 , Kw = 10 14 and KH = 10 1.46 are also known. The learning objectives of this
case study are three fold:
1. to understand how this system of equations can be transformed into a root finding problem,
2. to understand how to solve for the root in Python,
3. to understand how to use a root finding method in a real life engineering problem.
Creating the Problem f (x) = 0
In this subsection, we will reduce Equations (1)-(4) to one single equation. To recap we have four
equations, with four unknowns: [H + ], [CO3 2 ], [HCO3 ], and [OH ]. The goal is to eliminate
1
[CO3 2 ], [HCO3 ], and [OH ] and form one equation in [H + ]. We begin by examining Equations
(1)-(3):
K1 [CO2(g) ]
[H + ]
K1 K2 [CO2(g) ]
K2 [HCO3 ]
(2) ) [CO3 2 ] =
=
[H + ]
[H + ]2
Kw
(3) ) [OH ] =
[H + ]
(1) ) [HCO3 ] =
(6)
(7)
(8)
Upon substitution of these three relationships in Equation (4), we find
[H + ] =
K1 K2 [CO2(g) ] K1 [CO2(g) ]
Kw
+
2
+
[H + ]
[H + ]2
[H + ]
(9)
Upon rearrangement and redefining x = [H + ], the problem to be solved reduces to
f (x) = x3
Kw x
(2K2 + x)K1 [CO2(g) ] = 0
(10)
Questions
The goal is to write a Python script that calls the Bisection method from Lecture 2 to determine
the evolution of the pH in the rainwater (pH = log10 [H + ]) for each year from 1958 to 2008.
Please insert comment on every line to explain what your script does.
1. Create a new Jupyter notebook, define the constants K1 = 10 6.3 , K2 = 10 10.3 , Kw =
10 14 and KH = 10 1.46 and plot function [CO2(g) ] as a function of time (i.e., for each
year from 1958 to 2008).
2. Add a new part to your Python script in the following way: Define function f (x) for the
year 1958, i.e., t = 1958, and plot it in the interval [0, 10 5 ].
3. Add a new part to your Python script in the following way: Solve the root problem
f (x) = 0 for the year 1958 using the Bisection method with initial interval [10 14 , 1].
Explain why this initial interval is adequate. Ask your script to print the pH in the
rainwater (recall that if the root of f (x) is xroot , then pH = log10 (xroot )) using print.
4. Add a new part to your Python script in the following way: Create a loop from 1958 to
2008 (51 years) and solve the root problem f (x) = 0 for each year. Store the results in
term of pH in the rainwater in an array. Note that only [CO2(g) ] changes from one year
to another.
A pseudo-code looks like:
for year=1958:2008
compute [CO2(g) ](year) using (5)
2
define f with [CO2(g) ](year)
solve f (x) = 0 using Bisection, this gives xroot
store pH[year 1958] = log10 (xroot )
end of loop
5. Add the following final part to your Python script: Plot the pH in the rainwater from
1958 to 2008.
For all plots produced by your Jupyter notebook, write a title and give names to the axis
labels.
3