[Go to previous section: 1.2| Go
to next section: 1.4]
[ Go to CENG301 Homepage
| CENG301 Notes Table of Contents]
![]()
Part 3
The function mix simulates the behavior of a
mixer. Two arguments must be given to mix:
in and out, give the indices of the inlet streams
and the index of the outlet stream, respectively.
The help notes for a mixer show:
mix - Mass balance mixer module
function mix(in,out)
Simulates the behavior of a mixer.
Argument Gives
in the indices of the inlet streams.
out the index of the exit stream.
mix mixes all the inlet
streams to make the outlet stream.
For additional help and picture, see PICMIX
OKB, TYLC
Example: >> mix(1:2,3)
Section 5.3 gives a
full listing and diagram of a mixer.
Note how simple this operation is in MATLAB. The sum
function operates over the first index to produce just what we
want.
>> help sum SUM For vectors, SUM(X) is the sum of the elements of X. For matrices, SUM(X) is a row vector with the sum over each column. SUM(DIAG(X)) is the trace of X.
To illustrate the operation of mix, consider the following problem. One hundred pounds of a 20 percent (by weight) NaOH solution is mixed with fifty pounds of 10 percent NaOH solution and 200 pounds of water. Find the composition of the resulting mixture.
There are 3 inlet streams and one outlet stream so the in vector is:
[1 2 3] or 1:3
Note that the mix module may be used for compound flows in either molar or mass units. All flows must be in the same units, but that is the only requirement. In this problem the flows are given in mass units so we will find it easiest to specify all flows as pounds. We put the names: sodium hydroxide and water in cnms with start301 and initialize ns with the given flows in the inlet streams (NaOH is component 1 and water is component 2).
>> start301
1: Click (1) to start a new session
2: Click (2) to enter the name of a datafile from
a previous session, in the current directory
3: Click (3) to use compound names not in the CENG 301 database
WARNING...if you hit (3) no compound data will be available

<-- Choose (3) Other
You have chosen to not use any compound data
If you wish to enter data for a compound, exit (control-C)
then execute "create", then run a "new session", choice (1)
Enter the number of compounds: 2
Compound names must be less than 25 characters long!
Give the name of compound # 1: sodium hydroxide
Compound names must be less than 25 characters long!
Give the name of compound # 2: water
Enter the number of streams: 4
>> ns(1:3,:)=[20 80;5 45;0 200]
ns =
20 80
5 45
0 200
0 0
Execute mix.
>> mix(1:3,4)
Let's check ns.
>> showm(1:3,4,10,1)
Compound Inlet | Outlet
Stream 1 2 3 Total | 4
sodium hydroxide 20.0 5.0 0.0 25.0 | 25.0
water 80.0 45.0 200.0 325.0 | 325.0
Total 100.0 50.0 200.0 350.0 | 350.0
The outlet stream is ns(4,:). Its weight fraction of NaOH is:
25/350 or 0.07142857143
Therefore, the mixture is 7.14 percent NaOH. We can put our
results in a typical flow table involving a mixer.
|
Comp No. |
1 |
2 |
Total |
||
|---|---|---|---|---|---|
|
Stream |
From Unit* |
To Unit* |
|||
|
1 |
|
Mixer |
20 |
80 |
100 |
|
2 |
Mixer |
|
5 |
45 |
50 |
|
3 |
Mixer |
|
0 |
200 |
200 |
|
4 |
Mixer |
|
25 |
325 |
350 |
The function react simulates the behavior of a reactor. Three arguments must be given react:
1) r, is the vector of reaction rates. It has one element for systems with one reaction, and N elements for systems with N reactions;
2) in, gives the index of the inlet stream;
3) out, gives the index of the outlet stream.
Here are the help notes for react:
React: mass-balance module for simulating a reactor
function react(r,in,out)
Simulates the behavior of a reactor.
Argument Gives
r the vector of reaction rates
in the index of the inlet stream(s)
out the index of the outlet stream
The global variable: stoic is a matrix giving the
coefficient of the ith compound in the jth
reaction as: stoic(i,j).
For additional help and picture, see PICREACT
OKB, TYLC
Example: >> react(10,1,2)
would treat stream 1 as the inlet and stream 2
as the exit. There must be only 1 reaction
and its rate is 10.
The diagram and full listing of react is in section 5.4.
>> start301
Copyright 1998 Rice University
All rights reserved
1: Click (1) to start a new session
2: Click (2) to enter the name of a datafile from
a previous session, in the current directory
3: Click (3) to use compound names not in the CENG 301 database
WARNING...if you hit (3) no compound data will be available
<-- Choose (3) Other
You have chosen to not use any compound data
If you wish to enter data for a compound, exit (control-C)
then execute "create", then run a "new session", choice (1)
Enter the number of compounds: 5
Compound names must be less than 25 characters long!
Give the name of compound # 1: A
Compound names must be less than 25 characters long!
Give the name of compound # 2: B
Compound names must be less than 25 characters long!
Give the name of compound # 3: P
Compound names must be less than 25 characters long!
Give the name of compound # 4: Q
Compound names must be less than 25 characters long!
Give the name of compound # 5: X
Enter the number of streams: 2
>> global stoic
>> stoic=[-1 -2 1 1 0;-2 0 0 0 1]
stoic =
-1 -2 1 1 0
-2 0 0 0 1
>> chemeqs(stoic,cnms)
ans =
A + 2B --> P + Q
2A --> X
>> ns(1,:)=[10 25 0 0 0];
>> react([7 1],1,2)
>> showm(1,2,10,1)
Compound Inlet | Outlet
Stream 1 | 2
A 10.0 | 1.0
B 25.0 | 11.0
P 0.0 | 7.0
Q 0.0 | 7.0
X 0.0 | 1.0
Total 35.0 | 27.0
Nearly all of the code in react is there to make
sure that the user knows what is wrong if the data given it is
inconsistent. A simple (in MATLAB at least) matrix multiply in the
last line of the program determines the amount of each compound that
is produced by each reaction. It also sums this to determine the net
effect of all the reactions on each compound.
Before executing react, the global matrix, stoic,
must be specified. The matrix: stoic gives the
stoichiometric coefficients for the reactions. Its shape is C columns
by R rows, where C is the number of components and R is the number of
reactions.
First we will look at a reactor in which ammonia is produced by the
single reaction:
Our compounds are numbered as:
Component Name Formula
1 Nitrogen N2
2 Hydrogen H2
3 Ammonia NH3
We can create a data file with the compound and reaction data by:
>> start301
Copyright 1998 Rice University
All rights reserved
Welcome to CENG301's start301!!
You have three choices, Please read them carefully
Then click on the appropriate choice in the menu bar
1: Click (1) to start a new session
2: Click (2) to enter the name of a datafile from
a previous session, in the current directory
3: Click (3) to use compound names not in the CENG 301 database
WARNING...if you hit (3) no compound data will be available
<-- Choose (1) New Session
Now choose from the above menu

<-- Select Mass Balances only, since we do not need
to use compound data in this example

<-- Select CENG 301's Database
Input the name of your new file: test
The output file name is: test
Input the number of compounds: 3
The number of compounds is: 3
Enter the name of compound # 1: N2
Enter the name of compound # 2: H2
Enter the name of compound # 3: NH3
Enter the number of reactions: 1
Enter the coefficients for each compound in the same order that
the compounds are listed. Coefficients for reactants should be
Negative, and coefficients for products should be positive
Enter the coefficients for each compound in reaction # 1
N2 H2 NH3
-1 -3 2
Do you want to check to see if the coefficients are correct?
type "y" for yes or simply press enter to move on: y
number of compounds = 3
number of reactions = 1
Here are your formulae...
N2
H2
NH3
Give the number of compounds and number of reactions
3, 1
Give the formulae for each of the compounds
N2
H2
NH3
Give the stoichiometric coefficients for reaction # 1
-1 -3 2 <-- One mol of N2 + 3 of H2 make 2 moles of NH3
-1.00000 -3.00000 2.00000
the equation is balanced <-- The equation is checked
A note on balancing equations:
By answering "yes" to checking the coefficients, a list of necessary data is outputted above the questions. For the number of compounds and coefficients, input the proper information. Our next suggestion is to cut and paste the list of formulae from above. This is done by highlighting the complete list and then pressing the middle mouse button in the appropriate area. The same cut and paste technique can be used to enter the stoichiometric coefficients; However, all data can still be inputted by hand. Also, when entering the coefficients, make sure to separate each entry with at least one space. If the equation is not balanced the first time, you will have only one additional chance to correct the coefficients.
Your reactions are currently as follows:
1) N2 + 3H2 --> 2NH3
Type "y" to change an equation, or press "enter" to continue:
<-- You should redo them if the equation is not balanced
<-- Since the equation was balanced we press enter
Here are your compounds' formulae and names:
No. Formula Name
----------------------------------------
1 N2 nitrogen
2 H2 hydrogen
3 NH3 ammonia
Here are your reactions:
----------------------------------------
1) N2 + 3H2 --> 2NH3
Enter the number of streams: 2
The variables for your compounds have now been created,
you may continue, or come back later and reload the same data.
Let's take a look at our variables:
>> who Your variables are: cnms mw ns stoic form nc nst
Now let's look at the functions used by start301
to list the reaction equations. They may be used outside that program
to see what you have put into the stoic array. The function
chemeqs transforms the coefficients in an array like
stoic into chemical equations. It uses the function
halfeq to produce each side of a chemical equation. Help
applied to the function halfeq gives:
>> help halfeq
halfeq - Used in setstc to set up stoic
function he=halfeq(st,fr)
Argument List:
Argument Gives
st a numeric vector of coefficients
fr a character array of formulae
halfeq is primarily used by chemeq.
Ex: halfeq([1 3],['NH3';'H2O'])
OKB
An example of its use:
>> halfeq([1 3.5],['CH4';'O2 ']) ans = CH4 + 3.5O2
Now we can look at the function chemeq that gives one whole equation from a vector of stoichiometric coefficients and list of chemical names. Here is a listing of chemeq:

Here is an example of its use:
>> chemeq([-1 2 0 -2 1],['CH4';'H2O';'N2 ';'O2 ';'CO2']) ans = CH4 + 2O2 --> 2H2O + CO2
Finally, here are the help notes for chemeqs. This
program gives a whole set of equations from a stoichiometric array
and a set of compound names.
>> help chemeqs
chemeqs - Shows the chemical equations defined by a stoichiometric array
function eqs=chemeqs(st,cn)
Argument List:
Argument Gives
st the matrix of stoichiometric coefficients
cn array of compound names
chemeqs runs chemeq once for
each column of st.
The output in eqs is a list
of the chemical equations.
Ex. stoic=[-1 -3 2];
cnms=cgrp('N2','H2','NH3');
chemeqs(stoic,cnms)
OKB
The function chemeqs may be
used with any stoichiometric array and set of names as seen in:
>> nms=cgrp('CH4','CO2','CO','H2','H2O');
>> disp(chemeqs([-1 -1 2 2 0;0 1 -1 1 -1],nms))
CH4 + CO2 --> 2CO + 2H2
CO + H2O --> CO2 + H2
A lot of work is done in constructing the equations, so be patient
when you call it!
We will return to the ammonia problem. If we assume that our feed
has an excess amount of hydrogen over what is required by
stoichiometry so that it is 22 mol % nitrogen and 78 mol % hydrogen,
we can set up ns for a basis of 100 mols of feed per time:
>> ns(1,:)=[22 78 0]
ns =
22 78 0
0 0 0
Remember that we said we would have two streams in our session with
start301. Here is what we get for a reaction rate of
10 mols per time:
>> react(10,1,2)
>> showm(1,2,12,1)
Compound Inlet | Outlet
Stream 1 | 2
nitrogen 22.0 | 12.0
hydrogen 78.0 | 48.0
ammonia 0.0 | 20.0
Total 100.0 | 80.0
Here is what we get if all the nitrogen reacts:
>> react(22,1,2)
>> showm(1,2,12,1)
Compound Inlet | Outlet
Stream 1 | 2
nitrogen 22.0 | 0.0
hydrogen 78.0 | 12.0
ammonia 0.0 | 44.0
Total 100.0 | 56.0
Now we will consider a more complicated reactor involving six
compounds and three reactions. Let's take the ammonia from the last
reactor (after purifying it) and mix it with air and pass this
mixture through a reactor in which we form nitrogen oxides and water.
The following reactions will be considered:
4NH3 + 7O2 --> 4NO2 + 6H2O
2NO2 --> 2NO + O2
2NO2 --> N2 + 2O2
Then we can set cnms and stoic by executing the
start301 program.
>> start301
Copyright 1998 Rice University
All rights reserved
Welcome to CENG301's start301!!
You have three choices, Please read them carefully
Then click on the appropriate choice in the menu bar
1: Click (1) to start a new session <-- Choose (1) New Session
2: Click (2) to enter the name of a datafile from
a previous session, in the current directory
3: Click (3) to use compound names not in the CENG 301 database
WARNING...if you hit (3) no compound data will be available
<-- Choose Mass Balances only
<-- Choose CENG 301's database
Now choose from the above menu
Input the name of your new file: test
The output file name is: test
Input the number of compounds: 6
The number of compounds is: 6
Enter the name of compound # 1: NH3
Enter the name of compound # 2: O2
Enter the name of compound # 3: N2
Enter the name of compound # 4: NO2
Enter the name of compound # 5: H2O
Enter the name of compound # 6: NO
Enter the number of reactions: 3
Enter the coefficients for each compound in the same order that
the compounds are listed. Coefficients for reactants should be
Negative, and coefficients for products should be positive
Enter the coefficients for each compound in reaction # 1
NH3 O2 N2 NO2 H2O NO
-4 -7 0 4 6 0
Enter the coefficients for each compound in the same order that
the compounds are listed. Coefficients for reactants should be
Negative, and coefficients for products should be positive
Enter the coefficients for each compound in reaction # 2
NH3 O2 N2 NO2 H2O NO
0 1 0 -2 0 2
Enter the coefficients for each compound in the same order that
the compounds are listed. Coefficients for reactants should be
Negative, and coefficients for products should be positive
Enter the coefficients for each compound in reaction # 3
NH3 O2 N2 NO2 H2O NO
0 2 1 -2 0 0
Do you want to check to see if the coefficients are correct?
type "y" for yes or simply press enter to move on:
<-- We press enter because we know our reactions to be balanced.
Your reactions are currently as follows:
1) 4NH3 + 7O2 --> 4NO2 + 6H2O
2) 2NO2 --> O2 + 2NO
3) 2NO2 --> 2O2 + N2 <--Allows user to view reaction to
check their validity.
Type "y" to change an equation, or press "enter" to continue:
<-- After seeing equations, the user is given an additional
opportunity to change an equation. We press enter.
Here are your compounds' formulae and names:
No. Formula Name
----------------------------------------
1 NH3 ammonia
2 O2 oxygen
3 N2 nitrogen
4 NO2 nitrogen dioxide
5 H2O water
6 NO nitric oxide
Here are your reactions:
----------------------------------------
1) 4NH3 + 7O2 --> 4NO2 + 6H2O
2) 2NO2 --> O2 + 2NO
3) 2NO2 --> 2O2 + N2
<-- Final set of reactions
Enter the number of streams: 4
The variables for your compounds have now been created,
you may continue, or come back later and reload the same data.
If we assume the air has the composition 21 mol % oxygen and the rest
nitrogen and add 25 mol % excess air over that required for
combustion of the ammonia to NO2 and water, we can set
ns with flows of our ammonia and air streams mixed to form the
feed to the reactor:
>> ns(1,1)=100;
>> o2=1.25*100*7/4;
>> ns(2,2:3)=o2*[1 79/21];
>> mix([1 2], 3)
>> ns
ns =
100.0000 0 0 0 0 0
0 218.7500 822.9167 0 0 0
100.0000 218.7500 822.9167 0 0 0
0 0 0 0 0 0
Now if we react all the ammonia to form NO2 and neglect the competing reactions, we get:
>> react([25 0 0],3, 4)
>> showm(3,4,12,4)
Compound Inlet | Outlet
Stream 3 | 4
ammonia 100.0000 | 0.0000
oxygen 218.7500 | 43.7500
nitrogen 822.9167 | 822.9167
nitrogen dioxide 0.0000 | 100.0000
water 0.0000 | 150.0000
Total 1141.6667 | 1116.6667
A partial conversion of the ammonia and appreciable amounts of the other reactions may be seen in:
>> react([20 5 1],3, 4)
>> showm(3,4,12,4)
Compound Inlet | Outlet
Stream 3 | 4
ammonia 100.0000 | 20.0000
oxygen 218.7500 | 85.7500
nitrogen 822.9167 | 823.9167
nitrogen dioxide 0.0000 | 68.0000
water 0.0000 | 120.0000
nitric oxide 0.0000 | 10.0000
Total 1141.6667 | 1127.6667
Let's enter this last example into our Flow and Reaction Rate
Tables:
|
Comp No. |
1 |
2 |
3 |
4 |
5 |
6 |
Total |
||||
|---|---|---|---|---|---|---|---|---|---|---|---|
|
Stream |
From Unit* |
To Unit* |
|||||||||
|
1 |
Feed |
Mixer |
100 |
0 |
0 |
0 |
0 |
0 |
100 |
||
|
2 |
Feed |
Mixer |
0 |
218.75 |
822.92 |
0 |
0 |
0 |
1041.97 |
||
|
3 |
Mixer |
Reactor |
100 |
218.75 |
822.92 |
0 |
0 |
0 |
1141.67 |
||
|
4 |
Reactor |
Product |
20 |
85.75 |
823.92 |
68 |
120 |
10 |
1127.67 |
|
Reaction Number |
Reaction |
Molar Rates |
|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|