Chapter 6 - Solving Ordinary Differential Equations System: Profa. Liliane - em Revisão
Chapter 6 - Solving Ordinary Differential Equations System: Profa. Liliane - em Revisão
Liliane – Em revisão
Usually, systems in chemical plants are represented by more than one equation, because most
of them is non-isothermal and multicomponent, demanding simultaneous solution of a set of
many interdependent equations. When this is the case, numerical solution is easier and faster
than analytical one.
The numerical methods for integration can be classified as explicit and implicit. In the explicit
methods, the state of a system at a condition (i+1) is calculated based only on the previous
condition (i) (sequential solution). In the implicit methods, the solution in i+1 depends on the
state of both i and i+1 (simultaneous solution), being more difficult to implement. Implicit
methods are very useful for stiff systems (in which a depend variable changes very rapidly and
abruptly with an independent variable), because they are numerically more stable. However,
most of systems in chemical plants are not stiff if operated at planned conditions, therefore
explicit methods are usually adopted in real problems of chemical engineering. In this way, in
this book only the explicit methods will be presented. The reader can see details about implicit
numerical methods in specific literature (Davis, 1984, Chapra and Canale, 2005, Rao, 2002).
6.1 – Motivation
Imagine the derivative shown in Equation (6.1) with the correspondent initial condition:
𝑑𝑦
= cos(𝑥) + 1 (6.1)
𝑑𝑥
In this case, if one wants to know the value of y when x is equal to 10, for example, the equation
(6.1) can be analytically integrate, to generate:
In this case, the Equation (6.1) is very easy to integrate, however, when systems with many
complex ODEs must be solved simultaneously, numerical solution can be an interesting
alternative. Before present the numerical methods, let us take a look in the expression that
represents the expansion of Taylor Series (Equation 6.3)
In which:
Profa. Liliane – Em revisão
y = function which depends on x
x = independent variable
The solution of Equation (6.1) could also be obtained using expansion of Taylor Series. The more
terms considered, the more precise the solution is. Let us solve Equation (6.1) considering n
(number of terms in Taylor series) equal to 1, 2, 3 and 4 and compare the results. We can assume
h = xi+1 – xi = Δx = 1. Since the initial condition (y(0)) is known, we can calculate y(1), y(2), and so
on until discover the value of y (10). The disadvantage in using Expansion of Taylor series is the
calculation of higher order derivatives when using more than one term (n > 1), which, in some
cases, can be a hard task. In this example, the higher order derivatives for Equation (6.1) are
easily obtained, as follow:
𝑑𝑦
= cos(𝑥) + 1 (6.1)
𝑑𝑥
𝑑2 𝑦
= −sin(x) (6.4)
𝑑𝑥 2
𝑑3 𝑦
= −cos(x) (6.5)
𝑑𝑥 3
𝑑4 𝑦
= sin(x) (6.6)
𝑑𝑥 4
1 2
yi1 yi h[cos(xi ) 1] h [ sin( xi )] (6.8)
2!
1 2 1
yi1 yi h[cos(xi ) 1] h [ sin( xi )] h3[ cos(xi )] (6.9)
2! 3!
1 2 1 1
yi1 yi h[cos(xi ) 1] h [ sin( xi )] h3[ cos(xi )] h 4 [sin( xi )] (6.10)
2! 3! 4!
Starting from the initial condition (at x = 0, y(0) = 0) and considering h =1, it is possible to
calculate y(1). From the y(1), we calculate y(2) and so on until arrives in y(10). Figure 6.1
comparers the analytical solution of Equation (6.1) with results obtained from Taylor series using
different number of terms.
We can indeed verify from Figure 6.1 that the increase in the number of terms generates more
accurate results, however, it is interesting to point out that if lower increment h were
considered, the curves obtained from Taylor series would be closer to the analytical solution.
Profa. Liliane – Em revisão
If, from one hand, more precise results are obtained for bigger n values, form the other hand
the more terms in the series outcomes in more difficult calculation, because additional
derivatives of higher order are need. In this way, sometimes, the Taylor series does not
represent a real advantage compared to the analytical solution, which demands the calculation
of just one integral to generate the exact solution.
Figure 6.1: Comparison among analytical solution and the ones obtained by expanded Taylor
series truncated after 1, 2 and 4 terms
Most researchers consider Taylor series methods too expensive for most practical problems. In
this way, there is another class of numerical methods which imitate the Taylor series methods,
without the necessity to calculate the higher order derivatives. These are called Runge-Kutta
methods, and will be presented in the section 6.2.
yi 1 yi j K j (6.11)
j 1
In which:
j 1
K j hf ( xi c j h, yi a jl K l ) (6.12)
l 1
c1 0
In the RK formulas, the term f represents the derivative of the dependent variable, in this way:
dy
f ( xi , yi ) ( )i (6.13)
dx
Profa. Liliane – Em revisão
The order of the Runge-Kutta method will depend on the number of terms considered in the
summation on Equation (6.11), as follow:
To find out the formula of Runge-Kutta of first order (Euler method), ν = 1 is considered in the
Equation (6.11). If we assume 1 =1, the following formula for the Euler method is obtain:
yi 1 yi K1 (6.14)
RK1 - Euler
K1 hf ( xi , yi ) (6.15)
yi 1 yi hf ( xi , yi ) (6.16)
Equation (6.16) is exactly the expansion of Taylor series presented in Equation (6.3) considering
n = 1. The Euler method can be easily visualized as shown Figure 6.2.
If an initial condition (x0,y0) is given and if x is sufficiently small (y1 ~ v1), the value of the
function y at x1=x0+ x can be obtained considering the definition of tangent and derivative.
Profa. Liliane – Em revisão
y1 y0 dy
tan ( )0 (6.17)
x1 x0 dx
dy
so y1 y0 x( )0 (6.18)
dx
dy
or generalizing: yi1 yi x( )i (6.19)
dx
To obtain the formula for the Runge-Kutta of second order (RK2), ν = 2 is adopted in Equation
(6.11) to yield:
yi 1 yi 1K1 2 K2 (6.20)
K1 hf ( xi , yi ) (6.21)
K2 hf ( xi c2 h, yi a21K1 ) (6.22)
The terms w1, w2, c2 and a21 in Equation (6.20) and (6.22) must be found. If we assume c2h and
a21K1 sufficiently small, f ( xi c2 h, yi a21K1 ) can be represented by an expansion of Taylor
series (we will adopt n = 1), and the equation (6.22) can be written as:
K 2 h[ f ( xi , yi ) c2 hf ´x ( xi , yi ) a21K1 f ´y ( xi , yi )] (6.23)
In which:
dy
f ( xi , yi ) ( )i (6.13)
dx
f
f ´x ( xi , yi ) ( )i (6.24)
x
f
f ´ y ( xi , yi ) ( )i (6.25)
y
On the other hand, if we represent yi+1 using the expansion of Taylor series with n =2, we get
(compare with Equation 6.3):
dy 1 d2y
yi 1 yi h( ) i h 2 ( 2 ) i R2 (6.27)
dx 2! dx
Profa. Liliane – Em revisão
Taken into account Equation (6.13) and considering the error R2 very small, Equation (6.27) can
be written as:
1 2
yi 1 yi hf ( xi , yi ) h f ´( xi , yi ) (6.28)
2!
By definition,
df f f dy (6.29)
f ´( xi , yi ) ( )i ( )i ( )i ( )i
dx x y dx
1 2 f f dy (6.30)
yi 1 yi hf ( xi , yi ) h [( ) i ( ) i ( ) i ]
2! x y dx
Using Equations (6.13), (6.24) and (6.25), Equation (6.30) can also be written as:
1
yi 1 yi hf ( xi , yi ) h 2 [ f ´x ( xi , yi ) f ´ y ( xi , yi ) f ( xi , yi )] (6.31)
2
Observe the similarity between Equations (6.26) and (6.31). The terms w1, w2, c2 and a21 of RK2
can be found comparing Equation (6.26) (generated from the application of RK2) with the
Equation (6.31) (obtained from the application of Taylor series truncated in the second term,
n=2). We conclude that:
1 2 1 (6.32)
2 c2 1 / 2 (6.33)
2 a21 1 / 2 (6.34)
Since there as four unknown variables (w1, w2, c2 and a21) and only three equations (6.32-6.34),
one of the variables is fixed, to allow the calculation of the others. Two examples are shown:
Case 1: Fix c2 = 0.5, so w2 = 1, w1 = 0 and a21 = 0.5, and the Equations (6.20) to (6.22) that
represent RK2 can be expressed as follow:
yi 1 yi K2 (6.35)
With K1 hf ( xi , yi ) (6.15)
Case 2: Fix c2 = 1.0, so w2 = 0.5, w1 = 0.5 and a21 = 1.0, and the Equations for RK2 will be:
yi 1 yi 0.5( K1 K2 ) (6.37)
K2 hf ( xi h, yi K1 ) RK2 (6.38)
Profa. Liliane – Em revisão
With K1 hf ( xi , yi ) (6.15)
For the cases 1 and 2 of RK2, only first order derivatives are needed This is an advantage compare
to the Taylor series truncated in the second term, which demands also the second order
derivative. In cases 1 and 2, K1 represents the first order derivative applied at the initial condition
(xi,yi), as occurred in Euler method. In case 1 and 2, K2 represents the first order derivatives at
condition (xi+0.5h,yi+0.5K1) and (xi+h,yi+K1) respectively, so K2 can only be obtained after K1 is
calculated.
In a similar manner, Runge-Kutta methods of higher order can be developed, however, these
developments will not be presented herein, because it is not the scope of this book.
Observe the RK methods are based on the Taylor series. So, we can conclude the higher the
Runge-Kutta order the more accurate the method is, because more terms of Taylor series are
considered. However, from the practical point of view, the most used type of RK family is the
Runge-Kutta of fourth order (RK4), presented in section 6.2.4. Runge-Kutta methods for other
orders will not be detailed herein, because they are not usually applied in Chemical Engineering.
The readers can find information about this subject in specific literature (Davis, 1984, Chapra
and Canale, 2005, Varma, Morbidelli, 1997, Rao, 2002). Appendix C of this book also shows a set
of expressions for some RK methods.
The expression for the Fourth Order Runge-Kutta (RK4) can be seen bellow:
h
yi 1 yi ( K1 2 K 2 2 K 3 K 4 ) (6.39)
6
K1 f ( xi , yi ) (6.40)
1 1 RK4
K 2 f ( xi h, yi hK1 ) (6.41)
2 2
1 1
K3 f ( xi h, yi hK2 ) (6.42)
2 2
K 4 f ( xi h, yi hK3 ) (6.43)
The RK4 demands the calculation of four first derivatives (K1, K2, K3 and K4), at four different
points for independent and depended variables (x,y). Figure 6.3 compares results obtained from
the solution of Equation (6.1) using RK of different orders with the analytical solution.
Profa. Liliane – Em revisão
Figure 6.3: Comparison among analytical solution and the solution obtained by RK of first,
second and fourth order considering h = 1.
We can conclude the higher the order of RK method, the more precise the results are. This is
more evident for non-smooth functions. Observe the RK1 method is closer to the analytical
solution in the regions in which y varies in a more linear way with x (from 0 to 1 and from 6 to
7). If the function is not very nonlinear, Euler method can be used successfully, if a low value of
h (step) is adopt.
The choice of the ideal step is of crucial importance to obtain reliable results. But how to pick
the best step if we do not know the analytical solution? The idea is to use different steps and
compare the results. Figure 6.4 solve Equation (6.1) using Euler method for different values of
h. Observe the curves are practically coincident for h = 0.05 and h = 0.1, so there is no advantage
in using steps lower than 0.05.
Figure 6.4: Comparison among different values of steps for the solution of Equation (6.1) using
Euler method
Profa. Liliane – Em revisão
Observe that to obtain almost the same accuracy in the results, RK4 used a step around ten
times bigger than the one used in Euler method (compare Figures 6.3 and 6.4)
Section 6.3 and 6.4 will show how solve ODE and ODEs system using Excel spreadsheet and VBA
in Excel respectively. The procedure adopted in section 6.3 can be tedious and laborious,
however, it can be very useful in order to clearly understand how the calculus is done in RK
method, especially for system of equations solved simultaneously.
In order to understand how to solve an ODE using spreadsheet Excel, let us start with a very
simple example presented in Example 4.1. The equation below represents the axial increase of
temperature, in steady state, of a liquid flowing in a cylindrical tube of length 60 m with heat
exchange with a jacket.
𝑑𝑇 2𝜋𝑅U
= (300– T) , with the initial condition: at x = 0, T = 200C
𝑑𝑥 𝑄𝜌𝐶𝑝
In which:
2𝜋𝑅U
𝑀 = = constant
𝑄𝜌𝐶𝑝
For didactical point of view, let us start solving this ODE using Euler method. A suggestion of
spreadsheet can be seen in Figure 6.5, where in the first lines the ODE and the initial condition
are printed just as a comment. Lines 10 to 12 show the parameter of the model, needed to
calculate the constant M. To calculate M, select a cell (in our case, the cell C14), go to the
function space (at the top of your spreadsheet), type the equal sign followed by the expression
that represents the constant M and press “Enter” (in grey it is written, as a comment, the
expression you have to type). Then choose a cell to insert the step (in our case, the cell C15 was
used and h = 1 was chosen). After that, write down the initial conditions (see cells C18 and D18
for x0 and T0 respectively).
Profa. Liliane – Em revisão
According to Euler method, the independent and dependent variables at x1 can be calculated
based on the initial condition (at x0) (see also Equation 6.16), as follow:
x1 x0 h (6.45)
T1 T0 K1 (6.46)
K1 hf ( x0 , T0 ) (6.47)
or
T1 T0 hf ( x0 , T0 ) (6.48)
To calculate x1, select cell C19, type in the function space the equal signal followed by the
corresponding equations (Equation 6.45) and press “Enter”. In the same way, to calculate T1,
select cell D19, type in the function space the equal signal followed by the corresponding
equations (Equation 6.48) and press “Enter”. In Figure 6.5, these two expressions are also
written in grey as comment.
Figure 6.5: Suggestion of an Excel spreadsheet to calculate a single ODE using Euler method
Profa. Liliane – Em revisão
After that, select together the two cells (C19 and D19) containing the calculus you have just done
for x1 and T1 and hover the cursor over the small box at the bottom right corner of the selected
cells. This small box is called the "Fill Handle". When the mouse cursor is directly above the Fill
Handle, the cursor will change to a crosshair symbol. Drag the Fill Handle down to obtain values
of x and T for the next steps.
When dragging the Fill Handle down, automatically x2 and T2 (in cells C20 and D20) are calculated
using values of x1 and T1 (cells C19 and D19), x3 and T3 (in cells C21 and D21) are calculated using
values of x2 and T2 (cells C20 and D20), and so on. To avoid using the following cells for the
constant M and h (that must be fixed), we use $C$14 and $C$15 instead of C14 and C15. The
symbol “$” can also be easily added by pressing the key “F4” just after clicking the cell.
Figure 6.6a shows another way to produce the Excel spreadsheet, in which K1 is calculated
separately (equations 6.45, 6.46 and 6.47 are used). Figure 6.6a also depicts other values for x
and T after the dragging.
Figure 6.6: Proposed Excel spreadsheet to solve equation 6.44 using a) Euler (left) and b) RK4
(right) methods
Solving ODE by RK4 method using Excel spreadsheet is more laborious, but it is worth to see
how to do it to better understand the method. Figure 6.6b shows a suggestion for the Excel
spreadsheet to solve Equation (6.44) using RK4. Comparing with Figure 6.6a, we observe that
another three first derivatives are needed (K2, K3 and K4).
Knowing the initial condition (x0, T0), the condition at the first step (x1, T1) can be calculated,
according RK4 method, as follow:
Profa. Liliane – Em revisão
h
T1 T0 ( K1 2 K 2 2 K 3 K 4 ) (6.49)
6
K1 f ( x0 ,T0 ) (6.50)
1 1
K 2 f ( x0 h, T0 hK1 ) (6.51)
2 2
1 1
K 3 f ( x0 h, T0 hK 2 ) (6.52)
2 2
x1 x0 h (6.45)
The first derivatives K1, K2, K3 and K4 for the initial condition are calculated in cells E18, F18, G18
and H18, respectively. Figure 6.6b shows a detail for the expression used to calculate K 2 and K4
at x = 0. After that, x1 is calculated in cell C19 as done previously, and T1 is calculated in cell D19
using the Equation 6.49 (see detail in the function space of Figure 6.6b). Then select (individually
or in groups) the cells E18, F18, G18, H18, C19 and D19 and drag the Fill Handle down to obtain
all values shown in Figure 6.6b.
The temperature profiles obtained by the integration of Equation (6.44) revels an almost linear
dependence between temperature and length for this specific system (Figure 6.7), so, in this
case, the difference between Euler and RK4 method is not very expressive, even for high values
of integration steps (h).
Figure 6.7: Comparison between Euler and RK4 for different values of integration steps when
solving Equation (6.44)
In order to better understand how apply RK methods to solve a system of ODEs, let us revisit the
Example 4.3, which modeled a bitubular heat transfer operating in a concurrent way. The
Equations that represent this system are rewritten below:
Profa. Liliane – Em revisão
𝑑𝑇𝑏𝑒𝑛
Benzene: 𝑊𝑏𝑒𝑛 𝐶𝑝𝑏𝑒𝑛 = 𝑈1.25𝜋(𝑇𝑡𝑜𝑙 − 𝑇𝑏𝑒𝑛 ), initial condition: at x = 0, Tben = 600F
𝑑𝑥
𝑑𝑇𝑡𝑜𝑙
Toluene: 𝑊𝑡𝑜𝑙 𝐶𝑝𝑡𝑜𝑙 = 𝑈1.25𝜋(𝑇𝑏𝑒𝑧 − 𝑇𝑡𝑜𝑙 ), initial condition: at x = 0, Ttol = 1700F
𝑑𝑥
𝑑𝑇𝑡
Toluene: 𝑔(𝑥, 𝑇𝑏, 𝑇𝑡) = = 𝑀𝑡(𝑇𝑏 − 𝑇𝑡), initial condition: at x = 0, Tt= 1700F (6.55)
𝑑𝑥
In which:
𝑈×1.25×𝜋
𝑀𝑏 = = constant
𝑊𝑏𝑒𝑛 ×𝐶𝑝𝑏𝑒𝑛
𝑈×1.25×𝜋
𝑀𝑡 = = constant
𝑊𝑡𝑜𝑙 ×𝐶𝑝𝑡𝑜𝑙
Let us first solve simultaneously Equations (6.54) and (6.55) using Euler method. Knowing the
initial condition (x0, Tb0 and Tt0), the condition at the first step (x1, Tb1 and Tt1) can be calculated
as follow:
x1 x0 h (6.45)
K1 f ( x0 , Tb0 , Tt 0 ) (6.58)
L1 g ( x0 , Tb0 , Tt 0 ) (6.59)
Observe there is a first derivative for the dependent variable Tb (K1) and another for Tt (here
named L1). Note L1 and K1 are interdependent (see equation 6.54 and 6.55), and must be solved
together. Figure 6.8 shows a suggestion for an Excel spreadsheet to solve this system with two
ODEs (Equations 6.54 and 6.55).
Profa. Liliane – Em revisão
The constants Mb and Mt in cells C15 and C16 are calculated based on the parameters written
in lines 11 to 13. Figure 6.8 highlights the first calculations made for K1, L1, Tb and Tt. After the
first calculations are done, the Fill Handle must be dragged down to obtain how Tb and Tt vary
along position, which, if plotted, generate the curves shown in Figure 4.5.
Figure 6.8: Suggestion of an Excel spreadsheet to solve a system with two ODEs using Euler
method
If the RK4 method is used, the condition at the first step (x1, Tb1 and Tt1) is obtained based on
x0, Tb0 and Tt0 (initial condition), as follow:
h
Tb1 Tb1 ( K1 2 K 2 2 K 3 K 4 ) (6.60)
6
h
Tt1 Tt1 ( L1 2 L2 2 L3 L4 ) (6.61)
6
In which the first derivatives for the functions Tb and Tt at the initial condition are:
K1 f ( x0 , Tb0 , Tt 0 ) (6.58)
h h h
K 2 f ( x0 , Tb0 K1 , Tt 0 L1 ) (6.62)
2 2 2
Profa. Liliane – Em revisão
h h h
K 3 f ( x0 , Tb0 K 2 , Tt 0 L2 ) (6.63)
2 2 2
L1 g ( x0 , Tb0 , Tt 0 ) (6.59)
h h h
L2 g ( x0 , Tb0 K1 , Tt 0 L1 ) (6.65)
2 2 2
h h h
L3 g ( x0 , Tb0 K 2 , Tt 0 L2 ) (6.66)
2 2 2
Figure 6.9 presents a suggestion of how solve the system of Equations (6.54) and (6.55) using
RK4 method in an Excel spreadsheet. The first calculations for Tb and Tt, as well as for some first
derivatives are highlighted.
Figure 6.9: Suggestion of an Excel spreadsheet to solve equations (6.54) and (6.55) using RK4
method
Excel spreadsheet can be very useful to solve systems with very few equations by Euler method,
or to solve just one equation by RK4. For bigger systems, Excel spreadsheet is impracticable, but
Profa. Liliane – Em revisão
it was presented herein mainly for didactical reasons. Next section will show the procedure
usually adopted to solve ODEs system.
The idea of this book is not to show the VBA language in detail, because this can be saw in many
books (Walkenbach, 2013a, 2013b, Billo, 2007) and in sites of internet. Herein it will be
presented examples of a few computational program to solve ODEs using Euler and RK4
methods. These codes will be generic enough to allow the students to use them as a starting
point to develop their own program for different systems of ODEs.
The first thing to do to develop a computational program in Excel is to check if your computer is
enabled to use VBA. To do that, check if your Excel has “developer” tab, as shown in Figure 6.10.
If not, click the “file” tab, shown by the arrow in Figure 6.10, which will open a window shown
in Figure 6.11. By clicking “option” (see arrow in Figure 6.11), a window depicted in Figure 6.12
will show up. Choose “Customize Ribbon” button and, in the list of main tabs, select the
“Developer” check box. Click the “ok” button to close the “option” dialog box.
At this point your Excel is enabled to use Visual Basic program. Now can choose the “Developer”
tab, and then click in “Visual Basic” icon (see dashed arrow in Figure 6.10), to open a space in
which you can write your program. The space shown in Figure 6.13 is originally gray, but it
becomes white (and available to type the code) after clicking the “view code” icon, indicated in
Figure 6.13. After typing your code in this space, you can run the program, by clicking the green
triangle indicated by a dashed arrow in Figure 6.13. If needed, the reader can see detail in how
to debug the program in the ‘help” option in the Excel. However, if the code is correct, the click
in the green triangle will be enough to provide the simulation results.
Figure 6.10: Detail of “Developer” tab and of “Visual Basic” icon in the Excel spreadsheet
Profa. Liliane – Em revisão
Figure 6.12: Detail of “Customize Ribbon” option, to activate the “Develop” tab.
As occur in all programming languages, VBA provides may different possibilities of command
that can be used to develop a code. We will use herein just a few. Reader can learn more about
the different commands in VBA in specific literature (Walkenbach, 2013a, 2013b, Billo, 2007).
To understand how to develop a program code, let us start with a very simple problem, that can
be represented by just one ordinary differential equation; and let us adopt Euler method as
numerical method to solve this ODE. The Proposed Problem 3.1 studies the concentration of
HCl inside a tank over time, which modeling generates the following ODE:
𝑑𝐶𝐻𝐶𝑙
= 0.01 − 0.4×𝐶𝐻𝐶𝑙 , (6.66)
𝑑𝑡
A suggestion of a simple code to solve Equation (6.66) using Euler method is depicted in Figure
6.14a. This code is written in the “Space to develop your VBA code” shown in Figure 6.13. The
name of the program is “Euler” and the word “Sub” is used to start the code. When “Sub Euler
()” is written and the “Enter” key is pressed, automaticity the expression “End Sub” will appear
(see the last line of the code) and the words “Sub” and “End Sub” will turn blue. The code must
be written between these two commands.
In the first three lines of the code it is attributed values for the initial and final times e for the
initial concentration of HCl respectively (ti, tf and CHCl). We choose integrate the Equation (6.66)
until 15 hours, and we assume a step size (h) equal to 0.1 (see line 4).
The VBA prints the results in the Excel spreadsheet, so to define the cells where results are
printed, we use the command “Cells (k,j) =” , in which k indicate the line and j the column of the
spreadsheet. If we want print a word, we must use quotation marks, as indicated in lines 6 and
7 of the code, which generates cells A1 and B1 of Figure 6.14c.
Euler method demands a loop, and in Figure 6.14a it was done by the command “Do While”,
which must be close by the command “Loop” (see both in blue). Between these two commands
the calculus of Euler method is done, step by step, until reaches tf equal to 15m and results for
each step are printed. There is a counter (i) used to print the results, which started from 2 (see
line 8 of the code), because line 1 of the spreadsheet is dedicated to the title of the table of
results.
Another option to a VBA code to solve Equation (6.66) using Euler is presented in Figure 6.14b.
Observe the code is basically the same, but the command to build the loop is the “For…Next”.
The variable “nSteps” is the number of steps. The calculus in the loop is done 150 times, but it
is used “nSteps + 2” in the “For” command because the counter i starts from 2. The three lines
highlighted in the codes of Figures 6.15a and 6.15b denote the calculus demanded by Euler
method.
These two codes could be written in a more general way if we create a “Function”. Figure 6.16a
shows an alternative for the code of Figure 6.15a.
Profa. Liliane – Em revisão
a
b c
Figure 6.15: Suggestion of a code to solve Equation (6.66) using a) “Do While” command; b)
“For…Next” command; and c) Results obtained from the VBA code.
Figure 6.16: Using the command “Function” when solving an ODE by Euler method
Profa. Liliane – Em revisão
Observe the main program of Figure 6.16a is very similar to the one shown in Figure 6.15a,
except by the fact the calculus demanded by the Euler method is done in the Function
“RungeKutta1” (compare highlighted command in Figures 6.15a and 6.16a).
The Function RungeKutta1 needs the step (h) and the previous values of t and CHCl, to provide
the next ones, so these three variables (t, CHCl, h) are passed as arguments to the Function (see
variable names, separated by commas, between the brackets). The main program receives the
updated values of t and CHCl. So, the variables t, CHCl and h are common between the main
program and the Function RungeKutta1.
The Function RungeKutta1 could calculate k1 inside it, however, we decide to call another
Function to calculate the derivative (see Function Derivative), which needs the CHCl value to
return k1, so these two variables are also arguments.
Figure 6.16b repeats the code presented in Figure 6.16a, but uses a more generic nomenclature.
From now on, the independent and dependent variables will be called x and y respectively (t
could be mistaken with temperature and CHCl is very specific). Also, k1 and h will be replaced
by dydx and dx respectively. Besides, the independent variable x is being pass as argument in
the Function Derivative (see arrows), because differently from Equation (6.66), usually the ODEs
depends also on the independent variable.
The code of Figure 6.16b could be used to solve all systems represented by one ODE, just by
changing the ODE in the Function "Derivative" and the initial conditions in the main program.
Do not forget to save your program often. It is done clicking in “File” as shown by the arrow in
Figure 6.15c. However, most importantly, do not forget to save your workbook as a macro
enabled workbook by VBA. Choose the option “Excel Macro-Enabled Workbook (*.xlsm)” when
saving.
If we want to solve Equation (6.66) using Runge-Kutta of Forth Order (RK4), we can base on the
generic code shown in Figure 6.16b and exchange the Function “Rungekutta1” by the Function
“RungeKutta4”, presented in Figure 6.17. The RK4 method calculates the derivative (K1, K2, K3
and K4) four times, at different points of x and y, as presented in Equations (6.40) to (6.43).
Because of this, the Function “RungeKutta4“calls the Function “Derivative” four times.
In Figure 6.17, k1 is calculated as done in Figure 6.16b, however, k2 must be calculate at (xi+
0.5h, yi+0.5hk1) (see Equation 6.41), so just after the calculus of k1, we define the points x,y in
with k2 must be calculated. These two values x and y are transitory (just to call k2), so they were
called xtran and ytran respectively. To calculate k2, the Function “RungeKutta4” passes as
argument xtran and ytran, however, the Function “Derivative” keeps the same shown in Figure
6.16b.
After obtaining k2, we need to find out xtran and ytran to calculate k3. We can see by Equation
(6.42), that xtran will be the same used to get k2, so it is not updated, but ytrans will be yi+0.5hk2
(see Equation 6.42). The same procedure is followed until obtain k4. After that, dependent and
independent variables are updating (see Equation 6.39 to obtain yi+1).
Profa. Liliane – Em revisão
6.4.3 – Developing an algorithm to solve a system of ODEs using Euler and RK4
Methods
In this section, let us see how to develop a code in VBA to solve a system of ODEs. We will use
the example 3.8, which models a CSTR with a cooling jacket operating in a transient state.
The Equations 3.17 to 3.21 that represents the mass and energy balance of the reactor
and the energy balance for the jacket can be rewritten as follow:
𝑑𝐶𝐴 𝑄 𝐸𝐴
Mass balance for reactant A (mol): = (𝐶𝐴𝑖𝑛 −𝐶𝐴 ) − [𝑘0 exp(− )]𝐶𝐴 𝐶𝐵 (6.67)
𝑑𝑡 𝑉 𝑅𝑇
𝑑𝐶𝐵 𝑄 𝐸𝐴
Mass balance for reactant B (mol): = (𝐶𝐵𝑖𝑛 −𝐶𝐵 ) − [𝑘0 exp(− )]𝐶𝐴 𝐶𝐵 (6.68)
𝑑𝑡 𝑉 𝑅𝑇
𝑑𝐶𝐶 𝑄 𝐸𝐴
Mass balance for product C (mol): = (𝐶𝐶𝑖𝑛 −𝐶𝐶 ) + [𝑘0 exp(− )]𝐶𝐴 𝐶𝐵 (6.69)
𝑑𝑡 𝑉 𝑅𝑇
𝐸
𝑑𝑇 𝑄 𝑈𝐴 [𝑘0 exp(− 𝐴 )]𝐶𝐴 𝐶𝐵 (−∆𝐻𝑅 )
𝑅𝑇
Energy balance reactor (J): = (𝑇𝑖𝑛 − 𝑇) + (𝑇𝑗 − 𝑇) + (6.70)
𝑑𝑡 𝑉 𝑉𝜌𝐶𝑝 𝜌𝐶𝑝
𝑑𝑇𝑗 𝑄𝐽 𝑈𝐴
Energy for the cooling fluid (J): = (𝑇𝑗𝑖𝑛 − 𝑇𝑗) + (𝑇 − 𝑇𝑗) (6.71)
𝑑𝑡 𝑉𝐽 𝑉𝐽 𝜌𝐽 𝐶𝑝𝐽
Profa. Liliane – Em revisão
The parameters of the model and the initial conditions are the one presented in Tables 3.1, 3.2
and 3.3, but assuming the transient regime started because the flow rate (Q) the was 3 m3/min
(see Table 3.1), now changed to 4 m3/min. It is also said in Example 3.8 that the volume for the
jacket is (VJ) is 0.032 m3.
To solve this system of ODEs (Equations 6.67 to 6.71), the code in the Figure 6.16b has to be
changed, to include the system with five ODEs. Here we introduce the concept of “array”,
because there are five dependent variables (y) in our equation system. Using an array, we can
use the same name for the dependent variable (y) and use a number (index) to tell them apart.
The individual values are called the elements of the array. In our case we can consider: y(1) = CA;
y(2) = CB, y(3) = CC, y(4) = T and y(5) = Tj. In the same way, we will also assume the variable dydx
as an array. So, we our case it yields: dydx(1) = dCA/dt; dydx(2) = dCB/dt; dydx(3) = dCC/dt;
dydx(4) = dT/dt; dydx(5) = dTj/dt.
An example of how the Function “Derivative” could be written to represent the Equation (6.67)
to (6.71) is presented in Figure 6.18. This code could be applied for Euler and for RK4 methods.
Figure 6.18: Function derivative representing the system of ODEs (Equations 6.67-6.71)
In the first lines of the Function appears the parameters of Table 3.1, 3.2 and 3.2. These
parameters could be read from the spreadsheet, but herein we will not explain how to do it,
because this is not the focus of this book. Reader can find this information in books about VBA
and in the online help of excel.
Equation (6.70) is very long, so observe the command for the dydx(4) was broken in two lines.
In order to do that, we used the symbol underscore ( _ ).
The main programs to solve Equations (6.67) - (6.71) for Euler and RK4 methods are shown in
Figure 6.19a and 6.19b respectively. They are the same, just the name of the Function called is
Profa. Liliane – Em revisão
different (RungeKutta1 or RungeKutta4). Compare them to the main program of Figure 6.16b.
The first difference is we must declare the dimension (Dim) of the array for y and dydx (in our
case, the size is 5). You can choose to store any number, or array of numbers, as integers, single-
or double-precision. In our case, we choose double-precision. Observe also we need a loop to
print all dependent variable y (from 1 to 5).
Figure 6.19: Examples of main program to solve a system of ODEs using Euler and RK4.
Finally we need to develop a Funtion for Runge-Kutta method. Figure 6.20 and 6.21 present
examples of how could be a Function for Euler and RK4 method respectivelly to solve a system
of ODEs. Compare Figure 6.20 and 6.16b. For a system of equation, the derivative k1 has
dimension 5 (for equations 6.67 to 6.71) and it is declared in the beginning of the code as double
precision. Observe also the loop needed to calculate k1 and y for the five ODEs.
Figure 6.21 can be compared with Figure 6.17. The difference between them is the variables k1,
k2, k3, k4 and ytran are arrays with dimention equal to five, to account all ODEs (Equations 6.67
to 6.71). For the same reason a loop is needed to calculate them, going from 1 until ny (number
of ODEs), which is passed as argument in the Function “RungeKutta4”. Observe also that dydx
is argument in the Function “RungeKutta4” and “RungeKutta1”, because its dimension is defined
in the main program.
Profa. Liliane – Em revisão
Figure 6.20: Example of a “Function” for Euler method to solve a system of ODEs
Figure 6.21: Example of a “Function” for Runge Kutta of 4th order to solve a system of ODEs
Profa. Liliane – Em revisão
No matter which system of ODEs you need to solve, Functions presented in Figure 6.20 and 6.21
can be used as they are for Euler and Runge-Kutta of forth order methods. The only thing to
change is to adjust the dimension for k1, k2, k3, k4 and ytran if your system has more than 5
equations.
All codes presented in this chapter are just suggestions of how a program could be develop to
solve ODEs by Runge-Kutta methods using Visual Basic. The reader can find your own style of
programing. Besides, a more elegant code could be shown herein, however, much more details
about visual basic would be needed, and present these details is not the scope of this book.
Proposed Problems
6.1) Imagine the three interconnected tanks studied in Example 5.1. Assume the volume of liquid
in the three tanks is the same and remains constant and equal to V (m3). The volumetric flow
rate for all tanks are the ones presented in Table 5.1. In the beginning the three tanks contain
pure water, but at certain point, the streams Q01 and Q02 start feeding tanks 1 and 2 with a NaOH
solution with concentrations 10 mol/m3 (C01) and 1 mol/m3 (C02) respectively, instead of pure
water, at the same flow rates. The system of ODEs that represents the variation of concentration
of NaOH over time in the three tanks is presented below:
𝑑𝐶1 1
= (50 − 7𝐶1 + 2𝐶3 ), at t = 0, C1 = 0
𝑑𝑡 𝑉
𝑑𝐶2 1
= (7𝐶1 + 1 − 8𝐶2 ), at t = 0, C2 = 0
𝑑𝑡 𝑉
𝑑𝐶3 1
= (8𝐶2 − 8𝐶3 ), at t = 0, C3 = 0
𝑑𝑡 𝑉
a) Solve the ODEs system using Euler method and Excel spreadsheet, as per section 6.3.
Assume initially V = 5m3 and an increment (step) equal to 0.2 min. Plot curves of
concentration of NaOH for each tank over time and compare with the concentrations in
steady obtained in Example 5.1. Obtain the ideal step for this condition and numerical
method.
b) Change the volume of the three tanks from 5m3 to 1m3 and check what occur with the three
curves. Keep the volume for the three tanks equal to 1 m3 but use an increment (step) for
the Euler method equal to 0.1 min and observe the curves. What can you conclude?
c) Change the volume of the three tanks to 8m3 and after to 2m3 and observe the time needed
to achieve the steady state. The concentrations in the steady state are the same? Why?
d) Alter the initial concentrations for all tanks to 2 mol/m3. Are the concentrations in steady
state the same? Why?
e) Double the flow rate Q01 and Q02 and analyze the results in terms of concentration in steady
state and time to reach this steady state.
f) Develop a code in Visual Basic and solve the system of the three ODEs using Euler method.
Assume V = 5m3 for the three tanks and increment (step) equal to 0.2 min.
Profa. Liliane – Em revisão
g) Develop a code in Visual Basic and solve the system of the three ODEs using RK4 method.
Assume also V = 5m3 for the three tanks and increment (step) equal to 0.2 min. Compare
the results with the one obtained in item f.
h) Use the code developed in item g and run your program considering V = m3 and step equal
to 0.2 min (as done in item b). Compare the results with the ones you obtained using Euler
method (item b). What can you conclude?
6.2) Consider the four isolated CSRTs presented in the Proposed Problem 5.2. Find the system
of four ODEs that represent this reaction system. Assume that initially all reactors have
concentration of reactant A equal to 1mol/l.
a) Develop a code in Visual Basic using Euler method and find the profiles of concentration of
A over time and compare the results with the one obtained in Proposed Problem 5.2 for
steady state.
b) Repeat the item a, but this time use RK-4 method
c) Double the concentration of A at the initial condition for the four reactors and observe the
concentration of A at the steady state. Make comments.
d) Imagine that, after reach the steady state, the concentration of A fed in reactor A (C Ain) be
doubled. Find the new steady state. Make comments.
e) This time, after reaching the steady state in items a and b, double the flow rate fed in reactor
1 and find the new steady state. Make comments.
f) Repeat item a or b but this time double all kinetic constant and check what happens.
6.3) Tubular chemical reactors are widely used in the chemical industry. To develop
mathematical models to describe them, it is common to assume their operations with no radial
gradients of temperature, velocity, or concentration. In this case, we have the plug-flow
reactors. Consider the following irreversible reaction in the plug-flow tubular reactor:
𝐴+𝐵 →𝐶
The rate law is elementary and the reaction is carried out isothermally at 300𝐾 in a plug-flow
reactor in steady state. The feed stream has a volumetric flow rate of 𝑄 = 10𝐿/𝑚𝑖𝑛 and has
both reactants, A and B, with concentrations of 𝐶𝐴0 = 1𝑀 and 𝐶𝐵0 = 2𝑀, respectively. Note
that 𝑘 = 0.07𝐿/𝑚𝑜𝑙𝑚𝑖𝑛 at 300𝐾.
a) Write the Ordinary Differential Equations to represent the concentration of A, B and C along
the reactor length.
b) Solve the EDO system using VBA. Plot the concentration profile of all species in the reactor
along its length. Determine at which length we are able to determine 90% of conversion in
a tubular reactor (Hint: conversion is always calculated using the limiting reactant as the
reference).
c) Choose two different values for the radius of the PFR. Plot the curves and compare the
results.
Profa. Liliane – Em revisão
6.4) Consider a Continuous Stirred Tank Reactor (CSRT) in steady state in which the irreversible
and isothermal reaction takes place, with a rate constant (k) equal to 10 min-1. The
reactor is fed with a solution of reactant A in a flowrate (Q) of 5 m3/min and concentration of A
(CAin) of 1400 mol/m3. The same flowrate leaves the reactor and the density of the solution does
not change, so the reaction volume is constant over the reaction.
a) Develop mathematical models to represent the mass balance of A in four different situations:
i) Assume just one CSRT with reaction volume equal to 10 m3; ii) Assume the reaction system is
composed by two CSTRs of 5 m3 each in series; iii) Assume five CSTRs of 2 m3 each in series; iv)
Assume ten CSTRs of 1 m3 each in series.
b) Solve the algebraic equations obtained in item a using a numerical method presented in
chapter 5 and compare the final concentration of reactant A leaving the reaction system. What
can you conclude?
c) Imagine there is available a Plug Flow Reactor (PFR) with cross section area and length equal
to 1m2 and 10m respectively, making the reaction volume also equal to 10m3. Assume steady
state and the same flow rate (Q = 5 m3/min) and reactant concentration (CAin = 1400 mol/m3)
feeding the system. Develop a mathematical model to represent the concentration of A along
the PFR.
d) Solve the mathematical model generated in item c using a numerical method presented in
this Chapter and obtain the profile of reactant concentration along the PFR length.
e) Plot in the same graph the concentration of A along the reactor volume obtained in items b
and d. What can you conclude?
6.5) A very long cylindrical metal bar with diameter D, length L and thermal conductivity khas
one end maintained at Tw by constant contact with a hot wall (see Figure PP6.1). The surface of
this cylinder is exposed to ambient air at a constant temperature of T∞ with a convection heat
transfer coefficient of ℎ. The system was left for a long time until it became completely stable.
Figure PP6.1: Schematics of the metallic bar. (Adapted from Incropera et al., 2006)
a) Write the ordinary differential equation that describes the temperature profile as well as
the two boundary conditions. Assume that there is no radial temperature profile inside the
bar. Make assumptions if needed to simplify the mathematical solution of this problem.
Profa. Liliane – Em revisão
b) Solve the mathematical model using VBA and RK4. Hint: In order to integrate numerically
second-order ordinary differential equations more easily, the following substitution can be
very handy:
𝑑𝑇
= 𝑦 = 𝑓(𝑥)
𝑑𝑥
𝑑 2 𝑇 𝑑𝑦
= = 𝑔(𝑥)
𝑑𝑥 2 𝑑𝑥
Instead of solving directly one single second order differential equation, it is possible to break it
into two first order differential equations to be solved independently. Observe also that in this
problem one of the boundary conditions must suffer this change in variable too.
Determine and plot the temperature profiles along the bar length when it is manufactured from
pure copper, aluminum and stainless steel.
- D = 5𝑚𝑚
- 𝑇𝑤 = 100℃
- 𝑇∞ = 25℃
- ℎ = 100𝑊/𝑚2 𝐾
𝑊
- Copper: 𝑘 = 398
𝑚𝐾
𝑊
- Aluminum: 𝑘 = 180
𝑚𝐾
𝑊
- Stainless steel: 𝑘 = 14
𝑚𝐾
c) Determine for each metallic material the minimum length that the bar must have for the
bar temperature profile reach a minimum plateau. Using the minimum lengths for each one
of the metals, determine the heat loss for each material.
6.6) Imagine the two concentric cylinders modeled in Example 4.8 and assume the system had
reached the steady state, so it can be represented by:
d 2T dT
r 0
dr 2 dr
r = R1 , T = T0
r = R2, dT h (T Tenv )
dr k
Profa. Liliane – Em revisão
Assume the following numerical values: R1 = 0.5cm, R2 = 3cm, T0 = 1000C, Tenv = 250C, 𝑘 =
𝑊
180 and ℎ = 100𝑊/𝑚2 𝐾. Use the same hint suggested in Proposed Problem 6.5 and solve
𝑚𝐾
this problem using VBA and RK4. Plote the radial profile of temperature.
REFERENCES
Billo, E.J., Excel® for Scientists and Engineers Numerical Methods, John Wiley & Sons, Inc. (2007)
Davis, M. E., Numerical Methods and Modeling for Chemical Engineers, John Wiley & Sons, 1984.
Chapra, C.C, Canale, R.P., Numerical Methods for Engineers, 5th ed., Mc Graw Hill (2005)
Incropera, F.P., DeWitt, D.P., Bergman, T.L., Lavine, A.S., Introduction to Heat Transfer, 5th ed.,
Wiley (2006)
Varma, A., Morbidelli, M., Mathematical Methods in Chemical Engineering, Oxford University
Press, Inc. (1997)
Walkenbach, J., Excel VBA Programing for Dummies, 3rd ed., John Wiley & Sons, Inc. (2013a)
Walkenbach, J., Excel ® Bible, John Wiley & Sons, Inc. (2013b)
Rao, S.S., Applied Numerical Methods for Engineers and Scientists, Prentice Hall, (2002)