You are on page 1of 6

Visual Basic for Design Dr. H.

Bock

Assignment 2
Chemical Equilibrium

Newton’s method with boundaries

In this assignment we want to employ Newton’s method to find the roots of functions to
calculate the equilibrium composition of chemical reactions. This usually involves to solve
algebraic equations with large exponents which is conveniently done numerically.

Here we want to design VBA functions that can do this for six arbitrary components on an
Excel spreadsheet.

Submission

Your submission consists of two files, one document and one Excel spreadsheet. The
name of both files should be your username (the bit before the @ in your email) followed
by the respective suffix, e.g. .xlsm.

To solve all Tasks 4 use the template that is provided on VISION. The template contains
only two sheets and most of it is protected. You are only allowed to alter the blue cells.
Do not alter any of the function calls. We will test your functions by changing some of
the parameters given in the template.

You are allowed to add new sheets to be used during code development, but we will
delete them before marking. So, you should better delete them yourself to make sure
everything is still working.

It may be convenient to develop your code in a different workbook to avoid problems


with the function calls on the marking sheet.

The General Concept

Consider the generic chemical equilibrium reaction


all components
0  C
i 1
i i , (1)

where i is the stoichiometric coefficient of component C i . An example would be

H2 + I2  2HI (2)

The (gas phase) reaction equilibrium, i.e. the composition of the reaction mixture at
equilibrium, is given by the reaction quotient.
all components
  i
 P 
all components

 i
X i  K (T ) 0   K (T , P)
i 1
(3)
i 1 P 
Here we have introduced K (T , P) to simplify notation.

The usual problem is that we have calculated K (T , P) and now want to determine the
composition of the equilibrium system. This is done by solving
all components

 X
i 1
i
i
 K (T , P) (4)

The mole fractions in eq. (4) are given as

ni
Xi  all components
(5)
n
i 1
i

where ni stands for the current number of moles of component i in the reaction
mixture.

To solve eq. (4) we need to take the stoichiometry into account via

n1 n2 ni


   const.  n * (6)
1 2 i
or equivalently via

ni   i n * (7)

where n * indicates the “progress” or “extent” of the reaction and ni the change in the
number of moles of component i due to the reaction. This allows us to express the ni in
terms of n*.
ni  ni0  ni  ni0   i n * (8)

Thus, together with eqs. (5) and (8), eq. (4) can now be solved. We could substitute the
mole fractions in eq. (4) using eqs. (5) and (8). But we can also do it differently:

1) We guess a value for n * and indicate it by n0 * .


2) We use eq. (8) to calculate the ni n0 * .  
3) We use eq. (5) to calculate the mole fractions X i n0 * .  
4) Then we test if our guessed n0 * is actually correct. If it is, then

 X n *  K (T , P)  0
all components

i
i
0 (9)
i 1

must be true.
Two comments are in place: (i) we see that solving eq. (4) (or equivalently solving eq. (9))
means to find the n * at equilibrium, and (ii) the initial guess n0 * is most likely incorrect,
i.e. it won’t solve eq. (9).

The goal now is to find a better value n1 * . This can be done using Newton’s method to
find roots. The idea (as discussed previously) is to view the right side of
all components

 X n *  K (T , P)  0
i 1
i
i
(10)

as a function of n*
all components
f (n*)   X n *  K (T , P)
i 1
i
i
(11)

The value n~ * for which f (n~*)  0 solves eq. (10).


Graphically this could be represented as

f
f (n*)

n*

~*)  0
f (n
The Boundaries

While mathematically n * in eq. (10) can take any value, we know that none of the ni
can be negative. This imposes limits (boundaries) on n * that need to be observed while
solving eq. (10). Graphically this may look like this:

f
f (n*)

limit
allowed values

n L* n *

~*)  0
f (n

The problem is that during the iterations Newton’s method may suggest a value that lies
beyond the limit n L* .

f
limit

allowed values
ni * n*
n L*

Thus, it must be checked if the suggested new value lies beyond a limit. Should that be
the case, then an alternative value must be constructed. It is clear (from the sign of the
derivative) that the new point ni 1* should lie between ni * and the limit n L * . This
suggest that to construct the new point we may use something similar to

ni 1* 
1
2

ni *  n L*  (12)
Task 1

Explain (in your document) which boundaries (or limits) exist in the problem and how you
would identify them.

Task 2

To get started we need a “good” starting value n0 * . For chemical equilibria Newton’s
method is reasonably robust, but there can be (numerical) problems at low reactant mole
fractions.

Naturally, the first step should lead in the right direction. Thus n0 * should have the right
sign. The choice depends on whether the initial composition lies too far on the reactant
or too far on the product side.

Explain (in your document) how you would determine the sign of n0 * .

Task 3

As a “safety feature” design a method that stops the iteration after 100 iterations and
sends a message to the screen. For the former the EXIT statement may be useful and for
the latter you should test the following code:
MsgBox "Reached maximum number of iterations (100)."

Provide your solution (in the document) by adding your code to the generic while loop
given below.

Do
<<code in loop>>
Loop While (<<condition>>)
Hint: It is a good idea to implement this feature in the next task right from the beginning
to avoid infinite looping.
Task 4

Use the provided spreadsheet template to design and implement a function that
calculates n~ * .
As Newton’s method is an iterative method n k * will get closer and closer to n~ * as k
increases but it will never reach it. Thus, the iteration has to be stopped at some point. A
good condition to stop the iteration is

 X n *  K (T , P)  10
all components
4
i
i
0 (13)
i 1

Hints:

1) In Task 2 we have determined the sign of the starting solution. You will find that
for positive n0 * it is advisable to choose a value close to the upper limit of n* ,
e.g. n0 *  0.99999  upper limit of n * . The lower limit is less critical, but you
could use the same approach.
6
2) You may find it useful to employ a numerical derivative. Use a distance of 10
between the two points.
3) It may be useful to start with a simple version of the code that can handle simple
reactions with K (T , P)  2 and an equimolar initial system.

Please note

1) Your function must be able to handle up to 6 components that could be given in


any order.
2) The function call has already been included in the spreadsheet and must not be
changed.
3) The spreadsheet recalculates K (T , P) based on your value for n~ * . Use this as a
quality check while you are developing code. For non-extreme values
10 5
 K (T , P)  10 the method should be very accurate.
5

You might also like