Development of A Simulink-based Matlab Tool for Equilibrium Calculation through Gibbs Free Energy
Minimization
By
M G Toufik Ahmed and
Tareque Ahmed
A thesis
Submitted to the Department of Chemical Engineering for partial fulfillment of the requirements for the degree of
BSc in Chemical Engineering
DEPARTMENT OF CHEMICAL ENGINEERING
BANGLADESH UNIVERSITY OF ENGINEERING AND TECHNOLOGY, DHAKA
ii
DECLARATION
We hereby declare that, the theoretical knowledge and thoughts reflected in this thesis work from other sources are properly referenced and any theories or ideas from other research paper are not directly copied in this thesis. Moreover, the rules and regulations of the university has been followed properly in this work.
Supervised by
Dr. Kawnish Kirtania Assistant Professor
Department of Chemical Engineering
Bangladesh University of Engineering and Technology, Dhaka.
Signature of the Candidates
M. G. Toufik Ahmed
Student ID: 1402025
Tareque Ahmed
Student ID: 1402028
iii
ABSTRACT
Study and analysis of chemical equilibrium is of great importance for industrial processes involving chemical reactions, hence, a topic of interest for chemical engineers. The present study aims to develop one such tool, using Simulink interface in Matlab platform. The equilibrium composition in a reactive system can be determined through minimization of Gibbs Free Energy of the system. Hence, the algorithm has been based on direct Gibbs free minimization method to simulate water gas shift and methane steam reforming: two industrially important simultaneous reactions. Built-in Matlab functions were used to solve the non-linear equations formulated.
Among the functions, lsqnonlin function gave best results. The trend observed in the result obtained from the developed tool was found to be consistent with literature. A Hysys simulation was also performed under similar condition for comparison. Some discrepancy was observed due to considering standard enthalpy change as independent of temperature. The tool developed in this work is very much generalized and can be extended to any reactive system. Use of simulink has provided the tool with a graphical interface, making it more convenient to use compared to scripting programming, which is a key feature of the tool. Future development of the tool can be achieved by including the variation of enthalpy change with temperature.
iv
ACKNOWLEDGEMENT
To begin with we want to express our gratitude to the Almighty for giving us the strength to complete all the activities of the thesis work.
We would like to express gratitude to our supervisor, Dr. Kawnish Kirtania, Assistant Professor, Department of Chemical Engineering, BUET, for proposing such an interesting and exciting topic to study and work on. It would not have been possible to accomplish the tasks without his sound guidance and instructions.
We are also grateful to our family members for their support in the progress of the work.
M. G. Toufik Ahmed Tareque Ahmed 20 April, 2019
v
CONTENTS
DECLARATION ... ii
ABSTRACT ... iii
ACKNOWLEDGEMENT ... iv
CONTENTS ...v
LIST OF FIGURES ... vii
LIST OF TABLES ... viii
SYMBOLS ... ix
1 Introduction...1
1.1 Background ...1
1.2 Objective and Scope of the Thesis...2
1.3 Organization of the Thesis ...3
2 Literature Review ...4
2.1 Chemical-reaction Equilibrium ...4
2.2 Methods for Solving Reaction Equilibrium ...4
2.3 Direct Gibbs Free Energy Minimization Method ...5
2.4 Gasification ...7
2.5 Computer Tools for Solving Reaction Equilibrium ...8
2.6 MATLAB Algorithm for Solving Simultaneous Non-linear Equations ...9
3 Methodology ... 13
3.1 Formulation of Constraints ... 13
3.2 Formulation of Code ... 16
vi
3.3 Simulation ... 17
3.4 Application on Gasification Process ... 21
4 Results and Discussion ... 25
4.1 Results ... 25
4.2 Comparisons ... 27
5 Conclusion and Recommendations ... 31
5.1 Conclusion ... 31
5.2 Recommendations for future work ... 32
REFERENCES ... 33
APPENDIX ... 35
vii
LIST OF FIGURES
Figure 3.1: Flow-chart summarizing the coding………. .16
Figure 3.2: The main Simulink Interface ……….18
Figure 3.3: Unasked view of the subsystem in simulink………19
Figure 3.4: Input of parameters in simulink interface………. 20
Figure 3.5: Main interface of the ‘application’ model..……… 22
Figure 3.6: Unmasked view of Gasifier Subsystem……… .23
Figure 3.7: Unmasked view of ‘thermodynamic simulator’ subsystem……… 24
Figure 4.1: Molar composition vs Temperature for fsolve ……….. 25
Figure 4.2: Molar composition vs Temperature forfmincon ...……… 26
Figure 4.3: Molar composition vs Temperature forlsqnonlin .……… 26
Figure 4.4: Plot of Composition vs Temperature obtained in the developed tool……… 27
Figure 4.5:Plot of Composition vs Temperature presented by Smith et al.(1984).(Adapted)…. 28 Figure 4.6:Plot of Composition vs Temperature obtained using Hysys……….. 29
viii
LIST OF TABLES
Table 3.1 List of values of aik of the species in the reactive system……….14 Table 3.2 Standard Gibbs Free Energy of formation and Standard Enthalpy Change of formation of the reacting species………14 Table 4.1: Molar composition obtained using tool developed in this work and that presented by Smith et al. for 1000 K and an initial CH4/H2O ratio of 3:2. ……….……30
ix
SYMBOLS
aik number of atoms of the kth element present in each molecule of chemical species i
Ak total number of atomic masses of the kth element in the system ḟi fugacity of species i
fio fugacity for pure species i in its standard state Gt total Gibbs free energy of a single-phase system
Ji Jacobian Matrix
R universal gas constant w total number of elements
λ Damping factor
λk, Lagrange multiplier фi Fugacity coefficient
фi the standard enthalpy change of formation for species i µi chemical potential
1
Chapter 1
Introduction
1.1 Background
Many industrial processes often involve chemical reactions. Analysis of equilibrium of these reactions can, to a large extent, facilitate efficient design of the processes. Knowledge of equilibrium can be utilized in reactor design, selection of proper temperature and pressure, so that desired product composition is achieved. Hence, study of chemical reaction equilibrium is of great importance for chemical engineers.
In the past few decades, there have been significant attempts to develop algorithm which can solve reaction equilibrium. The principle of calculation of reaction equilibrium underlies in thermodynamics. Whenever any reaction reaches equilibrium, the Gibbs free energy of the system is a minimum. So the task is to solve for the composition involving minimized Gibbs free energy.
For simple reactions, calculation of equilibrium is straight-forward. But often, the equations governing chemical reaction equilibrium assume non-linear form, which are difficult to solve analytically, necessitating use of numerical method.
Based on fundamental thermodynamic principles, different algorithms have been developed to make computer tools that can solve chemical equilibrium numerically. Newton-Raphson method, Levenberg-Marquardt Algorithm, Trust Region Algorithm, Lagrangian Method of steepest descent, Interior Point Algorithm etc. are some of the methods developed to solve problems constrained by non-linear equations. Codes have been developed for Python, Mathcad and MATLAB platforms.
2 1.2 Objective and Scope of the Thesis
The prime focus of the present study is to develop a simulink-based Matlab tool to solve for multi- reaction equilibrium at varying temperature. The reactions considered here are methane steam reforming and water gas shift, which occur inside a gasification reactor
The algorithm of choice in this work is Direct Gibbs Free Energy Minimization method. This method requires knowledge of standard Gibbs Free Energy of formation of the reacting species.
Since the purpose is finding equilibrium composition at varying temperatures, the Gibbs Free Energies of formation need to be known at different temperature. Standard Gibbs Free Energy of formation of different compounds at 298 K are readily available. Gibbs Free Energy of formation at other discrete temperatures are available in literature. However, it would be more efficacious if Gibbs Free Energy of formation at any temperature can be expressed as a function of temperature.
Gibbs-Helmhotz equation has been resorted to in the present study for this purpose. However, the Gibbs-Helmhotz equation involves Standard Enthalpy Change of Formation of the species.
Enthalpy change of Formation is a function of temperature, but it has been assumed constant in the Gibbs-Helmhotz equation. This is a limitation of the work.
The governing non-linear equations have been solved numerically using Matlab functions. There are several built-in Matlab functions which follow different algorithms for calculation. Choosing the right Matlab function and algorithm considering characteristics of the problem is a key step in this work. The present work used fsolve, fmincon and lsqnonlin with their default algorithms. The result of the present work depends on the accuracy of solution in these algorithms.
The code has been developed incorporating thermochemical data of the species in the reactive system considered. The algorithm being a generalized one, the tool can be extended to solve any reactive system with any set of reaction, provided required thermochemical data is incorporated.
Use of Simulink can facilitate this extension.
3 1.3 Organization of the Thesis
Chapter 1 includes introduction of the thesis, along with some background and also thesis organization.
Chapter 2 presents a review of the literature regarding the topics addressed in the thesis. Along with an insight in to the fundamental thermodynamic principles that form the basis of the calculation, development of equilibrium-solving algorithms and tools have been discussed.
Chapter 3 describes the methodology followed in the present work.
Chapter 4 presents and discusses the results obtained from the work along with comparison of the obtained results with reference to standard literature.
Chapter 5 provides the conclusions drawn from the work while recommending suggestions for future works.
4
Chapter 2
Literature Review
'
2.1 Chemical-reaction Equilibrium
In a chemical reaction, chemical equilibrium is a state when the reactants and products are present in such concentration that there is no tendency for further change of any property of the system. Usually, this state results when the forward reaction proceeds at the same rate as the reverse reaction. [Atkins et al., 2006].
From a thermodynamic point of view, in an irreversible process at constant temperature and pressure, total Gibbs free energy of any closed system decreases. Hence, a reactive system will reach equilibrium when total Gibbs free energy of the system is a minimum [Smith et al., 2001].
2.2 Methods for Solving Reaction Equilibrium
The equations governing Chemical Equilibrium are mostly non-linear. It is difficult to solve multi-species chemical equilibrium analytically, except for some simple cases. This necessitates development of numerical methods [Paz Garcia et al., 2013].
Numerical method for solving chemical equilibrium may be either stoichiometric or non- stoichiometric (van Baten and Szczepanski, 2011). While stoichiometric algorithms converge on the solution of a set of mass balance and mass action equations, non-stoichiometric algorithms involves direct minimization of Gibbs Free Energy of Formation of chemical species, the functions being constrained by mass balance equations [Paz Garcia et al., 2013].
The first algorithm to solve numerically for the equilibrium state of a multicomponent system was published by Brinkley (1947). The method was based on stoichiometric strategy i.e.
evaluating equations for equilibrium constants of the reactions White et al. (1958) first put forward the non-stoichiometric strategy: direct Gibbs free energy minimization method for solving Reaction Equilibrium [Bethke, 2008].
5
Bethke (2008) opines that actually the two methods are computationally and conceptually equivalent, since the equilibrium constant of a reaction is a function of standard Gibbs free energy change of the reaction.
2.3 Direct Gibbs Free Energy Minimization Method
Smith et al.(2001) describes the governing equations for Gibbs Free Energy Minimization.
The total Gibbs free energy of a single-phase system is given as:
(Gt)T,P = g(n1, n2, n3,...nN) (2.1) The problem is to find the set {ni} which minimizesGt, for specified T and P, subject to the material balance constraints. The standard solution to this problem is based on the method of Lagrange’s undetermined multipliers.
The fact that the total number of atoms of each element is constant yields the constraining material balance equations.
If Ak is the total number of atomic masses of the kth element in the system, as determined by the
initial constitution of the system and aik is the number of atoms of the kth element present in each molecule of chemical species i, the material balance on each element k is:
∑ 𝑛𝑖 𝑖𝑎𝑖𝑘− 𝐴𝑘 = 0 (k= 1, 2, 3 …,w) (2.2) w being the total number of elements in the system.
Introducing the Lagrange multipliers λk,
λk (∑ 𝑛𝑖 𝑖𝑎𝑖𝑘− 𝐴𝑘) = 0 ( k= 1, 2, 3 …,w) (2.3) These equations are summed over k:
∑ 𝑘 λk (∑ 𝑛𝑖 𝑖𝑎𝑖𝑘− 𝐴𝑘) = 0 ( k= 1, 2, 3 …,w) (2.4)
A function, F is formed by adding the last sum to Gt.
F = Gt+ ∑ 𝑘 λk (∑ 𝑛𝑖 𝑖𝑎𝑖𝑘− 𝐴𝑘) = 0 (k= 1, 2, 3 …,w) (2.5)
6
This new function is identical with Gt. However, function F incorporating the constraints of the material balances, the partial derivatives of F and G' with respect to ni are not same.
The minimum value of F and G' is obtained when all of the partial derivatives (δF/δni)T,p,n are zero. Differentiating the preceding equation and setting the resulting derivative to zero:
(𝛿𝐹
𝛿𝑛𝑖)T, P, ni = (𝛿𝐺𝑡
𝛿𝑛𝑖)T, P, ni + ∑ 𝜆𝑘 𝑘𝑎𝑖𝑘 = 0 (2.6) The first term of the right side defines chemical potential, µi. Hence,
µi + ∑ 𝜆𝑘 𝑘𝑎𝑖𝑘= 0 (2.7) The chemical potential is also given by:
µi = Gi0 + RTln(ḟi/fio) (2.8) where, ḟi is the fugacity of species i and fio is that for pure species i in its standard state.
For gas-phase reactions and standard states as the pure ideal gases at 1 bar:
µi = Gi0 + RTln(ḟi/Po) (2.9) If Gi0 is arbitrarily set equal to zero for all elements in their standard states, then for any compound,
Gi0 = ∆G0fi, the standard Gibbs-energy change of formation for species i.
Expressing fugacity in terms of the fugacity coefficient, фi (ḟi = yiфiP), the equation for µi
becomes:
µi = Gi0 + RTln(yiфiP/Po) (2.10)
Combining equations,
∆G0fi + RTln(yiфiP/Po) + ∑ 𝜆𝑘 𝑘𝑎𝑖𝑘 = 0 (i= I, 2 ,3 ,…..,N) (2.11)
Equation (2.11) represents N equilibrium equations for each chemical species, ni's being unknown. Eq. (2.5) represents w material-balance equations for each element, λk's being unknown. So there is a total of (N + w) equations with same number of unknowns. Thus the number of equations is sufficient for the determination of all unknowns.
7
If temperature varies, Gibbs Free Energy of formation of any compound at any temperature can be expressed using Gibbs-Helmholtz Equation:
∆𝐺𝑓𝑖(𝑇)
𝑇 - ∆𝐺𝑓𝑖(𝑇0)
𝑇0 = ∆H0,i (1
𝑇 - 1
𝑇𝑜) (2.12) Where ∆H0,i is the standard enthalpy change of formation for the individual components.
2.4 Gasification
Gasification is an incomplete combustion of coal or another solid feedstock to convert the entire non-ash fraction of the feed to gases that preserve, as much as possible, the heat of combustion value of the feedstock. The resulting gases are called syngas, which include methane, carbon monoxide, carbon-di-oxide, hydrogen and steam [Bell et al. 2010].
Alongside power generation, syngas can be used to produce chemicals like liquid fuels, hydrocarbons, ammonia and methanol. If the end product is a chemical, it is important that the equilibrium compositions of the gases maintain proper ratio. For example, to produce methanol, the ratio of carbon monoxide and hydrogen should be around 2. Hence, it is important to study and analyze equilibrium.
Bell et al. (2010) discusses fundamentals of gasification and related technologies from a process engineering point of view. The whole gasification process involves 13 simultaneous and series reactions, as reported by Bell et al. (2010).
The reactions occurring in the gasifier can be summed up here:
Fuel/Biomass + O2 CO2 + CO + H2O + CH4 + H2 + H2S + SO2
The composition of product gases varies depending on the composition of the fuel and the gasifier temperature.
To adjust the ratio of carbon monoxide and hydrogen, water gas shift reaction is often used in the downstream. The temperature in the gasifier being very high, water gas shift reaction and also, methane steam reforming reaction occur spontaneously.
CO (g) + H2O (g) CO2 (g) + H2 (g)
8
CH4 (g) + H2O (g) CO (g) + 3 H2 (g)
A stoichiometric approach for solving equilibrium of the reactions: Methane Steam Reforming and Water Gas Shift is presented by Bell et al. (2010). The equations were solved using Solver in Microsoft Excel. The solution required knowledge of equilibrium constant of the reactions as a function of temperature and pressure.
2.5 Computer Tools for Solving Reaction Equilibrium
In recent years there have been many attempts to develop tools to solve reaction equilibrium problems. Excel, MATLAB, FEMLAB and Mathcad are some of the software that have been used to perform the calculations.
A code named SOLGAS was developed by Eriksson (1971) following the methodology of White et al. for solving system of ideal gas and pure condensed phases. The code was subsequently modified by Eriksson & Rosen (1973), Eriksson (1975), and Besmann (1977).
Gordon and McBride (1994) described a thermochemical equilibrium code: Chemical Equilibrium with Applications developed by Nasa Glenn Research Centre. This code uses Newton-Raphson method for numerical solution.
Blecic J et al. (2010) have developed an open-source python code: Thermochemical Equilibrium Abundances (TEA) based on the methodology of White et al. (1958) and Eriksson (1971). TEA can be used to calculate equilibrium composition of gaseous molecules for any using 84 elemental species and the thermodynamic data for more than 600 gaseous molecular species; for any temperature and pressure, adopting any initial elemental abundances. As numerical solution method, the code uses Lagrangian Method of steepest descent.
As Paz-Garcia et al. (2013) list, some other computer programs released for solving multi- species chemical equilibrium are PHREEQC (Parkhurst & Appelo, 1999), PHREEQCi (Charlton et al., 1997), WATEQ4F (Ball & Nordstrom, 1991), MINTEQ (Petersonet al., 1987), EQ3/6 (Wolery, 1992) and GEMS (Remy, 2004). Combination of these programs with more general codes for different purposes requires significant computational time. This makes simulations slow or, sometimes, unfeasible.
9
A Mathcad code for solving equilibrium composition for Methane-Steam Reforming and Water Gas Shift reaction by Gibbs Free Energy Minimization method is presented by Smith et al.
(2001).
Ye Lwin (2000) described use of Solver function in Microsoft Excel Spreadsheet package in computing Chemical Equilibrium in either ideal or non-ideal gas phase employing Gibbs Free Energy Minimization Method. The Solver function uses Generalized Reduced Gradient (GRG) method to solve non-linear function.
Another approach by using Goal-seek in Microsoft Excel Spreadsheet for solving equilibrium composition for water-gas shift reaction was presented by Finalyson (2006). Equilibrium constant was known in the specific example provided there.
Aspen Plus can also be used to solve reaction equilibrium problem (Finalyson, 2006). The process simulator can calculate Gibbs Free Energy of Formation of different compounds at different temperature.
Another approach designed for Matlab, based on extent of reaction, numerical solution being quassi-Newton-Raphson method, is presented by Paz-Garcia et al. (2013).
Ericsson (2004) describes a MATLAB package: CHEPP (Chemical Equilibrium Program Package) for solving combustion reaction equilibrium. Along with equilibrium calculating programme, CHEPP also includes a database containing thermochemical information and a set of functions to access the database. The calculation is based on Gibbs Free Energy minimization, following Newton method for minimization.
2.6 MATLAB Algorithm for Solving Simultaneous Non-linear Equations
There are several packages for solving simultaneous non-linear equations in Matlab. Some of them are: fsolve, fmincon, lsqnonlin, fminunc, lsqcurvefit and lsqnonlin. The appropriate package is to be selected with careful consideration of the characteristics of the problem.
fmincon
fmincon is a solver that finds minimum of constrained nonlinear multivariable function.
It can find the minimum of a problem specified by
10
(2.13)
Here, b and beq are vectors, A and Aeq are matrices, c(x) and ceq(x) are functions that return vectors, and f(x) is a function that returns a scalar. f(x), c(x), and ceq(x) can be nonlinear functions.
x, lb, and ub can be passed as vectors or matrices.
Fmincon has five algorithm options:
interior-point (default)
trust-region-reflective
sqp
sqp-legacy
active-set
'interior-point' handles large, sparse problems and small dense problems too. This algorithm satisfies bounds at all iterations. sqp and sqp-legacy are similar, both of which satisfies bounds at all iterations. The active-set algorithm is effective for problems with non-smooth constraints.
The trust-region-reflective can use special technique like Hessian Multiplier to save memory usage.
Constrained minimization finds vector x that is a local minimum to a scalar function f(x) subject to constraints.
As Byrd et al.(2006) describes, interior point algorithm, also known as barrier methods, solve linear and non-linear convex optimization problems. Mathematically stated:
Min f(x), subject to h(x) = 0 and g(x) ≤ 0 (2.13) For each μ > 0, the approximate problem is:
Min fµ(x,s)=minf(x) - µ∑ 𝑙𝑛(𝑠𝑖 𝑖) , subject to h(x) = 0 and g(x) + s = 0 (2.14)
11
As μ decreases to zero, the minimum of fμ should approach the minimum of f. The added logarithmic term is called a barrier function.
The approximate problem in the second equation is a sequence of equality constrained problems. These are easier to solve than the original inequality-constrained problem in the previous equation. The approximate problem can be solved by either direct step or conjugate gradient step. The algorithm first attempts to solve by direct step. Conjugate Gradient step is taken in case of failure of the first step.
lsqnonlin
lsqnonlin solves nonlinear least-squares curve fitting problems of the form:
min || f(x)||22 = min (f1(x)2 + f2(x)2 +…+ fn(x)2 (2.15) lsqnonlin follows two algorithms:
trust-region-reflective (default)
levenberg-marquardt
As More (1983) describes, in case of Trust-Region-Reflective algorithm, the function f(x) to be minimized takes vector arguments and return scalars. The function f is approximated with a simpler function q, which reflects the behavior of function f in a neighborhood N (which is called trust region) around the point x. A trial step s is computed by minimizing over N.
if f(x + s) < f(x), the current point is updated to be (x + s); otherwise, the current point remains unaltered. The trust region is thus shrunk and the trial step is repeated.
In the standard trust-region method, the quadratic approximation q is defined by the first two terms of the Taylor approximation to F at x. The trust region is usually spherical or ellipsoidal in shape. Mathematical statement is as follows:
Min {1
2sTHs + sTg such that ||Ds|| ≤∆} (2.16) Where, g is the gradient of f at the current point x, H is the Hessian matrix, D is a diagonal scaling matrix, Δ is a positive scalar, and ∥ . ∥ is the 2-norm. Algorithms for solving this equation typically involve the computation of all eigenvalues of H and a Newton process applied to the secular equation
12
1
∆− 1
||𝑠|| = 0 (2.17) First published in 1944 by Kenneth Levenberg and rediscovered in 1963 by Donald Marquardt, the Levenberg-Marquardt Algorithm (LMA) searches for a solution in a direction that interlinks between Gauss-Newton Method and Steepest Descent Direction Method. The LMA iteratively solves for the parameters B of a model curve f(x, B) so that sum of squares of deviations S(B) is a minimum. In each iteration the parameter vector B is replaced by (B + δ).
Δ is obtained from the linearized approximation:
f(xi, B+δ) ≈ f(xi, B+δ) + Ji δ (2.18) Where Ji is the gradient of f with respect to B.
The sum S(B) of square deviations is minimized at a zero gradient with respect to B. First order approximation of f(xi, B+ δ) yields:
S(B+ δ) ≈ ∑ [𝑦𝑚1 𝑖− 𝑓(𝑥𝑖, 𝐵) − 𝐽𝑖𝛿]2 (2.19) Setting the derivative of S(B+ δ) with respect to δ, equal to zero,
(JTJ+λI) δ = JT[y-f(B)] (2.20) Where J is Jacobian Matrix. λ is the damping factor. When length of the calculated step δ or the reduction of sum of squares from (B+𝛿) becomes smaller than predefined limit, iteration ends. The last value of is B is the solution.
fsolve Algorithms
fsolve has three algorithms:
trust region dogleg (default)
'trust-region'
'levenberg-marquardt'
trust-region-dogleg is the only algorithm that is specially designed to solve nonlinear equations. The other algorithms attempt to minimize the sum of squares of the function. This algorithm is effective on sparse problems. It can use special techniques such as a Jacobian multiply function for large-scale problems.
13
Chapter 3
Methodology
3.1 Formulation of Constraints
As derived in the previous chapter, the equation governing chemical equilibrium at constant pressure is:
∆𝐺𝑓𝑖
𝑅𝑇 + ln 𝑛𝑖
∑ 𝑛𝑖 𝑖 + ∑ 𝜆𝑘𝑎𝑖𝑘
𝑘 𝑅𝑇 = 0 (2.11) Where,
∆𝐺𝑓𝑖 =T{𝐺𝑓𝑖
0
𝑇𝑜 + ∆Ho,i (1
𝑇 - 1
𝑇0)} (Gibbs-Helmhotz Equation) (2.12) To = 298 K
∆Ho,i = the standard enthalpy change of formation for the individual components. Hence,
𝑇{
𝐺𝑓𝑖0
𝑇𝑜 + ∆Ho,i (𝑇1 − 𝑇01)}
𝑅𝑇 + ln 𝑛𝑖
∑ 𝑛𝑖 𝑖 + ∑ 𝜆𝑘𝑎𝑖𝑘
𝑘 𝑅𝑇 = 0 (3.1)
The species that are present in the reactive system are: CH4, H2O, CO, CO2 and H2. The values of aik for the species are listed in Table 3.1
14
Table 3.1 List of values of aik of the species in the reactive system.
Species, i Element, k
Carbon Oxygen Hydrogen
aik = no. of atoms of k per molecule of i
CH4 1 0 4
H2O 0 1 2
CO 1 1 0
CO2 1 2 0
H2 0 0 2
The values of standard Gibbs Free Energy of formation and Standard Enthalpy Change of formation of the reacting species are listed in Table 3.2
Table 3.2 Standard Gibbs Free Energy of formation and Standard Enthalpy Change of formation of the reacting species (at 298K) [Smith et al., Table C.4, p. 637].
Species 𝑮𝒇𝒊𝟎 (kJ/kgK-1) ∆Ho,I (kJ/kgK-1)
CH4 -50800 -74900
H2O -228610 -241818
CO -137180 -110525
CO2 -394390 -393509
H2 0 0
15
So, the governing equations for reactive species are as follows:
CH4: 𝑇{
−50800
298 +(−74900)(𝑇1 − 2981 )}
8.314×𝑇 + ln𝑛𝐶𝐻4
∑ 𝑛𝑖 𝑖 + 𝜆𝐶
𝑅𝑇 + 4𝜆𝐻
𝑅𝑇= 0 (3.2) H2O: 𝑇{
−228610
298 +(−241818)(𝑇1 − 2981 )}
8.314×𝑇 + ln𝑛𝐻2𝑂
∑ 𝑛𝑖 𝑖 + 2𝜆𝐻
𝑅𝑇 + 𝜆𝑜
𝑅𝑇= 0 (3.3) CO: 𝑇{
−137180
298 +(−110525)(1𝑇 − 2981 )}
8.314×𝑇 + ln𝑛𝐶𝑂
∑ 𝑛𝑖 𝑖 + 𝜆𝐶
𝑅𝑇 + 𝜆𝑜
𝑅𝑇= 0 (3.4) CO2: 𝑇{
−394390
298 +(−393509)(𝑇1 − 2981 )}
8.314×𝑇 + ln𝑛𝐶𝑂2
∑ 𝑛𝑖 𝑖 + 2𝜆0
𝑅𝑇 + 𝜆𝑐
𝑅𝑇= 0 (3.5) H2: ln𝑛𝐻2
∑ 𝑛𝑖 𝑖 + 2𝜆𝐻
𝑅𝑇 = 0 (3.6)
For the reactive system containing CH4, H2O, CO, CO2 and H2, the atomic balance equations are:
C balance: nCH4 + nCO + nCO2 – nCH4,i - nCO,i - nCO2,i = 0 (3.7) H balance: 4nCH4 + 2nH2O + 2nH2 – 4nCH4,I – 2nH2O,i -2nH2,i= 0 (3.8) O balance: nH2O + nCO + 2nCO2 -nH2O,i - nCO,i - 2nCO2,i = 0 (3.9)
∑ 𝑛𝑖 = nCH4 + nCO + nCO2 + nH2O + nH2 (3.10) Now, there are nine non-linear equations with nine unknown variables (nCH4, nCO, nCO2, nH2O, nH2,
∑ 𝑛𝑖, 𝜆𝑐, 𝜆𝐻,𝜆𝑜). These equations need to be solved.
16 3.2 Formulation of Code
Figure 3.1 provides a flow diagram demonstrating the steps followed in development of the Matlab code. Three built-in Matlab functions: fsolve, fmincon and lsqnonlin were used for the numerical solution. The codes are included in the appendix.
Figure 3.1: Flow-chart summarizing the coding.
17 3.3 Simulation
Simulink, developed by MathWorks, is a graphical programming environment for modeling, simulating and analyzing multi-domain dynamical systems. Its primary interface is a graphical block diagramming tool and a customizable set of block libraries. It offers tight integration with the rest of the MATLAB environment and can either drive MATLAB or be scripted from it.
Simulink is widely used in automatic control and digital signal processing for multidomain simulation and Model-Based Design. As the present problem is a dynamic one, using Simulink to solve this is always a better choice. Simulink also has feature to incorporate Matlab script file within it. As a result, traditional coding can be encoded in the Simulink model.
An m file was built up based on the developed equations. Now the task was minimization of Gibbs free energy, requiring solution of the equations encoded in the m-file.
Block diagram of the problem in the Simulink were built. The block diagram contained all the variables. Any value inside the blocks can be changed by masking, allowing the user to input value of the variables.
In the present case, masking gives the freedom to change the variables: nCH4, nCO, nCO2, nH2O, nH2, 𝜆𝑐, 𝜆𝐻,𝜆𝑜, temperature and pressure. MATLAB interpreted function was added to link the m- file with the Simulink. A display screen was set up to view the results on screen at the final time.
A workspace was included to collect data from the Simulink and use it for manipulation or plotting graph in the MATLAB. A scope can be used to show the graph on screen in Simulink.
Temperature or Pressure or both can be varied simultaneously. But varying both simultaneously will not give the user any desirable solution. So it is preferable to vary one of these two variables for a distinct range while the other is kept constant. One can get a result for a particular temperature and pressure for a given molar ratios of the components just by putting those values in the masked screen. If one wants to get a result for a given range of Temperature or Pressure, the range can be set by giving a desired run time on the Simulink window. Once all the parameters are input, clicking run button will start calculation. When simulation is complete, the results can be obtained from screen display or graph, or it can be collected from Simulink workspace for further manipulation in the Matlab environment.
18
No matter which code or algorithm is used, the procedure for each case in the Simulink is same.
The block diagram for the Simulink in each case is also same. For any modification, just the m- file needs to be changed. The Simulink model is left unaltered. It is very much user friendly and more convenient than traditional scripting programming.
Figure 3.2 shows the main interface of Simulink. The final results are displayed in the display box.
Scope shows the graph of composition vs temperature. ‘Workspace’ is used to collect and store data for further analysis and interpretation in Matlab. The subsystem box contains the heart of the Simulink model.
Figure 3.2: The main Simulink Interface
19
Figure 3.3 provides the unmasked interface of the subsystem. This is the core part of the model.
Here the initial guesses for molar composition of CH4, H2O, CO, CO2, CO and H2, gamma1(𝜆𝑐), gamma2 (𝜆𝐻 ) and gamma3 (𝜆𝑜) are given along with Temperature & Pressure. They are processed through Interpreted Matlab function by a MUX. Then results are given by a DEMUX. Another MUX is used after that to show the result in one display.
Figure 3.3: Unasked view of the subsystem in simulink
20
By clicking on the masked subsystem, initial assumption of composition, temperature and pressure can be input. Figure 3.4 demonstrates this.
Figure 3.4: Input of parameters in simulink interface
21 3.4 Application on Gasification Process
The core model has been extended for application as thermodynamic simulator of a gasifier reaction.
The initial composition of the reacting species in the two reactions that have been dealt with were required to solve the equations. That composition results from the gasification reactions occurring inside the reactor prior to methane steam reforming and water gas shift reactions. This composition may vary with the fuel composition and gasifier temperature.
The present work assumed that the fuel is methane (CH4). This methane has been gasified in presence of oxygen in an amount less than that required stoichiometrically for complete combustion, so that CO is produced. The fuel has been assumed to have no Sulphur in it. The composition of the product gases has been selected arbitrarily, satisfying atomic balance of elements.
For x mole of methane, y moles of oxygen has been considered. Assuming that the resulting gas contains: a moles of CH4, b moles of H2O. c moles of CO, d moles of CO2 and e moles of H2. Elementary balance yields,
C: a + c + d = x H: 4a + 2b + 2e = 4x O: b + c + 2d = 2y
Arbitrarily taking, d = x/6, e = x/2, y = 1/3;
The balance equations yield: a = 2/3, b= 1/6, c= 1/6
So, this work considers the initial composition is (when y is 1/3):
2/3 moles of CH4 1/6 moles of H2O 1/6 moles of CO
1/6 moles of CO2 1/2 moles of H2
22
The values are subject to change with the change of x and y values.
These initial molar compositions were input in the thermodynamic simulator. Hence, there was no need to assume initial composition. By putting temperature and pressure, equilibrium composition of syngas were obtained at that condition.
Figure 3.5 provides the main interface of the ‘application’ model. Here in the masked subsystem, the user can enter molar composition of Carbon & Oxygen which yield the initial composition of CH4, H2O, CO, CO2 and H2 by arbitrary atomic balance. In the masked thermodynamic simulator subsystem, user can input temperature and pressure to get equilibrium composition at that condition. The data can be stored and used for further analysis in Matlab through ‘to workspace’
block.
Figure 3.5: Main interface of the ‘application’ model
23
Figure 3.6 provides unmasked view of the Gasifier subsystem showing the block model for solving an arbitrary atomic balance. Here x and y values (from input) produce moles of atom of C, H and O, followed by processing through Matlab interpreted function by a MUX. Consequently, it yields the result of initial molar composition for the thermodynamic simulator.
Figure 3.6: Unmasked view of Gasifier Subsystem
24
Unmasked subsystem of thermodynamic simulator with block models is provided in Figure 3.7.
Here initial values of molar composition come from the Gasifier Block. Temperature and Pressure have to be input from the masked subsystem. The values are processed through Interpreted Matlab Function, yielding the results consequently.
Figure 3.7: Unmasked view of ‘thermodynamic simulator’ subsystem.
25
Chapter 4
Results and Discussion
4.1 Results
Graphs showing molar composition of reacting species against temperature were obtained for the Matlab functions: fsolve, fmincon and lsqnonlin. Figure 4.1, 4.2 and 4.3 present the graphs for fsolve, fmincon and lsqnonlin respectively. The temperature range was 100 0C to 700 0C for each case.
Figure 4.1: Molar composition vs Temperature for fsolve
26
Figure 4.2: Molar composition vs Temperature forfmincon
Figure 4.3: Molar composition vs Temperature forlsqnonlin
27
As evident from the graphs, the one for fsolve was not at all satisfactory. When fmincon algorithm was used, the results seemed more reasonable, but there were fluctuations. The graph obtained by using lsqnonlin was comparatively smoother.
4.2 Comparisons
To analyze the accuracy of the tool developed in the present work, graph of composition vs temperature obtained by the developed tool has been compared with those found in literature and another simulation tool: Hysys, for a temperature range of 100 0C to 1000 0C, at 1 atm pressure and an initial CH4/H2O molar ratio of 1:1.
Figure 4.4 presents the graph obtained by using the tool following lsqnonlin algorithm.
Figure 4.4: Plot of Composition vs Temperature obtained in the developed tool.
Smith et al.(1984) presents a graph for same temperature range, pressure and initial molar ratio.
The graph is shown in Figure 4.4. A simulation for the reactions for the same conditions were performed in Hysys. The results obtained in Hysys is presented in figure 4.6.
Molar Composition
28
Figure 4.5:Plot of Composition vs Temperature presented by Smith et al.(1984).(Adapted)
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8
0 200 400 600 800 1000
Molar Composition
Temperature (0C)
CH4
H2O
CO
CO2
H2
29
Figure 4.6:Plot of Composition vs Temperature obtained using Hysys.
As evident from figures 4.4, 4.5 and 4.6, the trend observed in the graph obtained using the developed tool is similar to that found in literature and that obtained using Hysys.
To compare numerically, the molar composition was calculated for a definite temperature: 1000 k, at 1 atm pressure for a CH4/H2O molar ratio of 2:3. A Mathcad solution for a similar problem in similar conditions is provided by Smith et al.(2001). Table 4.1 presents the compositions obtained in the two cases.
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8
0 500 1000
Molar Composition
Temperature (oC)
CH4
H2O
CO
CO2
H2
30
Table 4.1: Molar composition obtained using tool developed in this work and that presented by Smith et al. for 1000 K and an initial CH4/H2O ratio of 3:2.
Species Molar composition obtained using the developed tool
Molar composition obtained by Smith et al. using
Mathcad
CH4 0.06886 0.0196
H2O 0.1594 0.0980
CO 0.1481 0.1743
CO2 0.03588 0.0371
H2 0.5878 0.6710
As evident, the result obtained deviate from literature value. The reason behind this discrepancy lies in the fact that, Smith et al. directly used standard Gibbs Free Energy of Formation at 1000 K for the calculation, while in the equation used in this tool, the Gibbs Free Energy of Formation at 1000 K was calculated from that of 298 K using Gibbs-Helmhotz equation. The standard Enthalpy change of formation used in that equation was for 298 K, no temperature correction being made.
Farther the temperature from 298 K, larger will be the error induced.
31
Chapter 5
Conclusion and Recommendations
5.1 Conclusion
A computational tool for equilibrium calculation has been developed through this work. The tool can solve equilibrium composition of a multi-reactive system comprising of methane steam reforming and gasification reaction for varying temperature. The developed tool can be applied for finding final syngas composition or determining the temperature which provides desired syngas composition resulting from gasification. One such problem has been solved using a specific application part added to the core part. For a variable feed of oxygen, product gas from gasification of methane has been simulated for demonstration. The tool developed being a generalized one, can be used for many any process involving water-gas shift and methane steam reforming.
Based on direct Gibbs free energy minimization method, the tool numerically solves the governing non-linear equations using lsqnonlin function in Matlab. Use of simulink block diagram is an exclusive feature of the developed tool. This has provided the tool with a graphical interface where variables can be input easily. The tool readily provides results graphically, making it easier to interpret. The data stored in workspace can be utilized for further analysis.
Although results obtained from the tool are consistent with standard literature, some error was induced to the simulation due to the limitation in considering the enthalpy change with temperate.
32 5.2 Recommendations for future work
The constraint equations governing equilibrium should be modified by expressing standard enthalpy change of formation of the compounds as functions of temperature. This will improve the results.
Attempts can be made to develop the tool such that it can solve equilibrium of product gas resulting from gasification of more variety of fuel, allowing input of different elemental composition of the fuel.
The tool can be extended by formulating equations for more compounds and incorporating thermochemical data for them so that it can be used for any set of reaction.
Thermochemical data of many compounds at different temperature can be stored in a text file which can be readily accessed by the algorithm, eliminating necessity of inputting the thermochemical data in the code manually for each case.
Attempts can be made to improve GUI (Graphic User Interface) for better user experience.
33
REFERENCES
[1] Aspen Technology Inc, 2012. Aspen Hysys 2012. [computer program]. Available at:<https://www.aspentech.com/en/products/engineering/aspen-hysys>
[2] Bell D., Towler B., Fan M., 2010, Coal Gasification and Its Application. 1st Edition, Elsevier.
[3] Besmann, T. M., 1977. SOLGASMIX-PV, A Computer Program to Calculate Equilibrium Relationships in Complex Chemical System. Tech. rep., Oak Ridge National Lab, TN (USA).
[4] Bion, Nicolas & Epron, Florence & Duprez, Daniel, 2015. Bioethanol reforming for H2 production: A comparison with hydrocarbon reforming. Catalysis 22.
10.1039/9781847559630-00001.
[5] Brinkley, SR., Jr. Discussion of ‘A brief survey of past and curent methods of solution for equilibrium composition’ by H. E. Brandmaier and J. J. Harnett”. G. S. Bahn and E. E.
Zukoski (eds.), 1960. Kinetics, Equilibria and Performance.
[6] Byrd, R.H., Gilbert J. C., and Nocedal J., 2000. A Trust Region Method Based on Interior Point Techniques for Nonlinear Programming. Mathematical Programming, Vol 89, No. 1, pp. 149–185.
[7] Eriksson, G., 1975. Chemica Scripta. 8, p 100.
[8] Finalyson B. A.,2006. Introduction to Chemical Engineering Computing. Wiley-Interscience.
Jasmina B, Harrington, Joseph & Bowman M. Oliver.Tea:A Code For Calculating Thermochemical Equilibrium Abundances. ApJ Supplement Series.
[9] K.-H. Li, Van Ness H. C., and Abbott M. M.,1984. Reaction Kinetics, Reactor Design and Thermodynamics (Section 4) in Perry’s Chemical Engineering Handbook. Mc Graw Hill.
[10] Levenberg, Kenneth, 1944. A Method for the Solution of Certain Non-Linear Problems in Least Square. Quarterly of Applied Mathematics. 2 (2):pp 164–168.
[11] Lwin, Ye., 2000. Chemical Equilibrium by Gibbs Energy Minimization on Spreadsheets.
International Journal of Engineering Education.p 16.
[12] Marquardt, Donald., 1963. An Algorithm for Least-Squares Estimation of Nonlinear Parameters”. SIAM Journal on Applied Mathematics. 11 (2):pp 431–441.
[13] McBride J, Bonnie & Gordon, Sanford, 1996. Computer Program for Calculation of Complex Chemical Equilibrium Compositions and Applications. I: Analysis. NASA-RP-1311.
[14] Moré, J.J. and Sorensen D.C., 1983. Computing a Trust Region Step. SIAM Journal on Scientific and Statistical Computing, Vol. 3, pp 553–572.
34
[15] Smith, J. M., & Van Ness, H. C.,1959. Introduction to chemical engineering thermodynamics.
New York, McGraw-Hill.
[16] White, W.B., Johnson S.M., and Dantzig G.B., 1958. Chemical equilibrium in complex mixture. Journal of Chemical Physics 28, 751–755.
35
Appendix
Code for Lsqnonlin function =lsqlatest(x,T,P) fun = @dualfun;
n1=x(1);
n2=x(2);
n3=x(3);
n4=x(4);
n5=x(5);
n6=x(6);
n7=x(7);
n8=x(8);
n9=x(1)+x(2)+x(3)+x(4)+x(5);
T=x(9);
P=x(10);
C=x(1)+x(3)+x(4);
H=4*x(1)+2*x(2)+2*x(5);
O=x(2)+x(3)+2*x(4);
x0 = [n1,n2,n3,n4,n5,n6,n7,n8,n9];
LB=[0.001 0.001 0.001 0.001 0.001 0.01 0.01 0.01 .1];
UB=[C O C C H/2 100 100 100 20];
% options=optimoptions(@fmincon,'Algorithm','interior-point','StepTolerance',1e- 12,'MaxFunctionEvaluations',5000);
36
options=optimoptions(@fmincon,'Algorithm','interior- point','MaxFunctionEvaluations',5000);
z=fmincon(fun,x0,[],[],[],[],LB,UB,[],options);
% z = lsqnonlin(fun,x0,LB,UB)
%for mole frac
methane=z(1,1)/sum(z(1:5));
water=z(1,2)/sum(z(1:5));
carbonmono=z(1,3)/sum(z(1:5));
carbondi=z(1,4)/sum(z(1:5));
hydro=z(1,5)/sum(z(1:5));
n=[methane water carbonmono carbondi hydro z(1,6) z(1,7) z(1,8) z(1,9)];
% n=[z(1,1) z(1,2) z(1,3) z(1,4) z(1,5) z(1,6) z(1,7) z(1,8) z(1,9)];
function [ff] = dualfun(n)
%DUALFUN Summary of this function goes here % Detailed explanation goes here
% n(1)=nch4,n(2)=nh20,n(3)=nco,n(4)=nco2 ,n(5)=nh2;
% n(6)=lambdaC/RT, n(7) = lambdaH/RT, n(8)=lambdaO/RT R=8.314;
RT=R*T;
%CH4 F(1)=(T*(-50800/298+(-74900)*(1/T- 1/298))/(RT))+log(n(1)/n(9))+n(6)+4*n(7)+log(P);
% H2O
F(2)=T*(-228610/298+(-241818)*(1/T-
1/298))/(RT)+log(n(2)/n(9))+2*n(7)+n(8)+log(P);
% CO
37 F(3)=T*(-137160/298+(-110525)*(1/T-
1/298))/(RT)+log(n(3)/n(9))+n(6)+n(8)+log(P);
% CO2
F(4)=T*(-394390/298+(-393509)*(1/T-
1/298))/(RT)+log(n(4)/n(9))+n(6)+2*n(8)+log(P);
% H2
F(5)=log(n(5)/n(9))+2*n(7)+log(P);
Code for fmincon
function n=fminlatest(x,T,P) fun = @dualfun;
n1=x(1);
n2=x(2);
n3=x(3);
n4=x(4);
n5=x(5);
n6=x(6);
n7=x(7);
n8=x(8);
n9=x(1)+x(2)+x(3)+x(4)+x(5);
T=x(9);
P=x(10);
C=x(1)+x(3)+x(4);
H=4*x(1)+2*x(2)+2*x(5);
O=x(2)+x(3)+2*x(4);
38 x0 = [n1,n2,n3,n4,n5,n6,n7,n8,n9];
z = fsolve(fun,x0);
z=real(z);
methane=z(1,1)/sum(z(1:5));
water=z(1,2)/sum(z(1:5));
carbonmono=z(1,3)/sum(z(1:5));
carbondi=z(1,4)/sum(z(1:5));
hydro=z(1,5)/sum(z(1:5));
n=[methane water carbonmono carbondi hydro z(1,6) z(1,7) z(1,8) z(1,9)];
function F = dualfun(n)
%DUALFUN Summary of this function goes here % Detailed explanation goes here
% n(1)=nch4,n(2)=nh20,n(3)=nco,n(4)=nco2 ,n(5)=nh2;
% n(6)=lambdaC/RT, n(7) = lambdaH/RT, n(8)=lambdaO/RT R=8.314;
RT=R*T;
%CH4
F(1)=(T*(-50800/298+(-74900)*(1/T-
1/298))/(RT))+log(n(1)/n(9))+n(6)+4*n(7)+log(P);
% H2O
F(2)=T*(-228610/298+(-241818)*(1/T-
1/298))/(RT)+log(n(2)/n(9))+2*n(7)+n(8)+log(P);
% CO
F(3)=T*(-137160/298+(-110525)*(1/T-
1/298))/(RT)+log(n(3)/n(9))+n(6)+n(8)+log(P);
39 % CO2
F(4)=T*(-394390/298+(-393509)*(1/T-
1/298))/(RT)+log(n(4)/n(9))+n(6)+2*n(8)+log(P);
% H2
F(5)=log(n(5)/n(9))+2*n(7)+log(P);
% C balance
F(6)=n(1)+n(3)+n(4)-C;
% H balance Code for fsolve
function n= fsollatest(x,T,P) fun = @dualfun;
n1=x(1);
n2=x(2);
n3=x(3);
n4=x(4);
n5=x(5);
n6=x(6);
n7=x(7);
n8=x(8);
n9=x(1)+x(2)+x(3)+x(4)+x(5);
T=x(9);
P=x(10);
C=x(1)+x(3)+x(4);
H=4*x(1)+2*x(2)+2*x(5);
O=x(2)+x(3)+2*x(4);
40 x0 = [n1,n2,n3,n4,n5,n6,n7,n8,n9];
LB=[0.001 0.001 0.001 0.001 0.001 0.01 0.01 0.01 .1];
UB=[C O C C H/2 100 100 100 20];
% options=optimoptions(@fmincon,'Algorithm','interior-point','StepTolerance',1e- 12,'MaxFunctionEvaluations',5000);
% options=optimoptions(@fmincon,'Algorithm','interior-
point','MaxFunctionEvaluations',5000);
% z=fmincon(fun,x0,[],[],[],[],LB,UB,[],options);
z = lsqnonlin(fun,x0,LB,UB)
%for mole frac
methane=z(1,1)/sum(z(1:5));
water=z(1,2)/sum(z(1:5));
carbonmono=z(1,3)/sum(z(1:5));
carbondi=z(1,4)/sum(z(1:5));
hydro=z(1,5)/sum(z(1:5));
n=[methane water carbonmono carbondi hydro z(1,6) z(1,7) z(1,8) z(1,9)];
% n=[z(1,1) z(1,2) z(1,3) z(1,4) z(1,5) z(1,6) z(1,7) z(1,8) z(1,9)];
function F = dualfun(n)
%DUALFUN Summary of this function goes here % Detailed explanation goes here
% n(1)=nch4,n(2)=nh20,n(3)=nco,n(4)=nco2 ,n(5)=nh2;
% n(6)=lambdaC/RT, n(7) = lambdaH/RT, n(8)=lambdaO/RT R=8.314;
RT=R*T;
%CH4
41 F(1)=(T*(-50800/298+(-74900)*(1/T-
1/298))/(RT))+log(n(1)/n(9))+n(6)+4*n(7)+log(P);
% H2O
F(2)=T*(-228610/298+(-241818)*(1/T-
1/298))/(RT)+log(n(2)/n(9))+2*n(7)+n(8)+log(P);
% CO
F(3)=T*(-137160/298+(-110525)*(1/T-
1/298))/(RT)+log(n(3)/n(9))+n(6)+n(8)+log(P);
% CO2
F(4)=T*(-394390/298+(-393509)*(1/T-
1/298))/(RT)+log(n(4)/n(9))+n(6)+2*n(8)+log(P);
% H2
F(5)=log(n(5)/n(9))+2*n(7)+log(P);
% C balance
F(6)=n(1)+n(3)+n(4)-C;
% H balance
F(7) = 4*n(1)+2*n(2)+2*n(5)-H;
% O balance
F(8)=n(2)+n(3)+2*n(4)-O;
F(9)=1-((n(1)+n(2)+n(3)+n(4)+n(5))/n(9));%
ff1=(abs(F(1))+abs(F(2))+abs(F(3))+abs(F(4))+abs(F(5))+abs(F(6))+abs(F(7))+abs(F (8))+abs(F(9)));
% ff=ff1*2500/(log(P+1));
End
42 Code for Gassifier Block
function n=practical(x,T,P) fun = @dualfun;
n1=x(1);
n2=x(2);
n3=x(3);
n4=x(4);
n5=x(5);
n6=x(6);
n7=x(7);
n8=x(8);
n9=x(1)+x(2)+x(3)+x(4)+x(5);
T=x(9);
P=x(10);
C=x(1)+x(3)+x(4);
H=4*x(1)+2*x(2)+2*x(5);
O=x(2)+x(3)+2*x(4);
x0 = [n1,n2,n3,n4,n5,n6,n7,n8,n9];
LB=[0.001 0.001 0.001 0.001 0.001 0.01 0.01 0.01 .1];
UB=[C O C C H/2 100 100 100 20];
% options=optimoptions(@fmincon,'Algorithm','interior-point','StepTolerance',1e- 12,'MaxFunctionEvaluations',5000);%
options=optimoptions(@fmincon,'Algorithm','interior- point','MaxFunctionEvaluations',5000);
% z=fmincon(fun,x0,[],[],[],[],LB,UB,[],options);
43 z = lsqnonlin(fun,x0,LB,UB)
%for mole frac
methane=z(1,1)/sum(z(1:5));
water=z(1,2)/sum(z(1:5));
carbonmono=z(1,3)/sum(z(1:5));
carbondi=z(1,4)/sum(z(1:5));
hydro=z(1,5)/sum(z(1:5));
n=[methane water carbonmono carbondi hydro z(1,6) z(1,7) z(1,8) z(1,9)];
% n=[z(1,1) z(1,2) z(1,3) z(1,4) z(1,5) z(1,6) z(1,7) z(1,8) z(1,9)];
function F = dualfun(n)
%DUALFUN Summary of this function goes here % Detailed explanation goes here
% n(1)=nch4,n(2)=nh20,n(3)=nco,n(4)=nco2 ,n(5)=nh2;
% n(6)=lambdaC/RT, n(7) = lambdaH/RT, n(8)=lambdaO/RT R=8.314;
RT=R*T;
%CH4
F(1)=(T*(-50800/298+(-74900)*(1/T-
1/298))/(RT))+log(n(1)/n(9))+n(6)+4*n(7)+log(P);
% H2O
F(2)=T*(-228610/298+(-241818)*(1/T-
1/298))/(RT)+log(n(2)/n(9))+2*n(7)+n(8)+log(P); % CO F(3)=T*(-137160/298+(-110525)*(1/T-
1/298))/(RT)+log(n(3)/n(9))+n(6)+n(8)+log(P);
% CO2
44 F(4)=T*(-394390/298+(-393509)*(1/T-
1/298))/(RT)+log(n(4)/n(9))+n(6)+2*n(8)+log(P);
% H2
F(5)=log(n(5)/n(9))+2*n(7)+log(P);
% C balance
F(6)=n(1)+n(3)+n(4)-C;
% H balance
F(7) = 4*n(1)+2*n(2)+2*n(5)-H;
% O balance
F(8)=n(2)+n(3)+2*n(4)-O;
F(9)=1-((n(1)+n(2)+n(3)+n(4)+n(5))/n(9));
%
ff1=(abs(F(1))+abs(F(2))+abs(F(3))+abs(F(4))+abs(F(5))+abs(F(6))+abs(F(7))+abs(F (8))+abs(F(9)));
% ff=ff1*2500/(log(P+1));
end
Code for Thermodynamic Simulator Block function z= lsqpractical(x)
%n1,n2,n3===mole of C,H,O2 n1=x(1); % this is x
n2=x(2);
n3=x(3); % this is y
%found an arbitary equation,put it in matrix format.The equations are given
%here
45
%output: CH4=a, H2O=b, CO=c, CO2=d, H2=e
%assume, e=x/2; d=x/6;
% C balance => a+c=x-x/6
% H balance => 4a+2b=4x-x
% O balance => b+ c = 2*y-x/3 A=[1 0 1;4 2 0;0 1 1];
b=[n1*(1-1/6);3*n1;2*n3-n1/3 C=A\b;
%CH4,H2O,CO,CO2,H2
z=[C(1,1) C(2,1) C(3,1) n1/6 n1/2]
end