You are on page 1of 11

LIST of MATLAB PROGRAMS (m-FILES) NUMERICAL TECHNIQUES AND MATLAB CODES for CHEMICAL and BIOLOGICAL ENGINEERS S.

Elnashaie, F. Uhlig, C. Affane

Chapter 1: MATLAB ========= 1.2 Numerical Analysis and MATLAB 1.2 Numerical Analysis and MATLAB Numerical polynomial root finder trial m files: fzerotry1.m Computes the zero of the polynomial f(x) = x^3 - 2*x^2 + 4 near x = -2 first and then within the interval [-2,-1], using MATLABs fzero root finder. Computes the zero of the polynomial f(x) = x^9 - 18*x^8 + 144*x^7 - 672*x^6 + 2016*x^5 ... - 4032*x^4 + 5376*x^3 - 4608*x^2 + 2304*x - 512 near x = 1.5 first and then within the interval [1.91, 2.1], using MATLANs fzero root finder. Tries to find the roots of p(x) = (x-2)^9 given in extended form p(x) = x^9 - 18*x^8 + 144*x^7 - 672*x^6 + 2016*x^5 ... - 4032*x^4 + 5376*x^3 - 4608*x^2 + 2304*x - 512 via Newton's method starting at xstart, and performing k iterations Plots the graph of p(t) = (t-2)^9 = t.^9 - 18*t.^8 + 144*t.^7 - 672*t.^6.. + 2016*t.^5 - 4032 *t.^4 + 5376*t.^3 - ... 4608*t.^2 + 2304*t - 512 in one subplot, both in closed form (curve) and in extended form (o) near t = 2. And repeats the same for the derivative p'(t) = 9*(t-2)^8 = 9*t^8 + ... + 9*256 of p in both forms, closed in a curve, and extended (o) in a second subplot. uses euclidean algorithm for a polynomial of and its derivative to represent the polynomial by the eigenvalues of a tridiagonal matrix T. The eigenvalues of the matrix T are found via the DQR algorithm that keeps the tridiagonal structure of T intact. Very efficient [only O(n^2) operations]; and reliable with repeated roots. xxxxxxxxxxxxxxxxxx END Chapter 1 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

fzerotry2.m

newtonpoly.m

poly.m

pzero.m degree n

Chapter 3: Some Scalar Equations Models, with and without Bifurcation ========= 3.1 Continuous Stirred Tank Reactor; the adiabatic case 3.2 Continuous Stirred Tank Reactor; the non-adiabatic case 3.4 Scalar Static Equations without Bifurcation 3.1 Continuous Stirred Tank Reactor; the adiabatic case Adiabatic case: ============== Simple plots of adiabatic graphs: Numercial steady state finders: solveadiabxy.m uses 'fzero' to find solutions to the adiabatic equation (3.2); peters out for alpha near the bifurcation limits. runsolveadiabxy.m plots the solution branches for equation (3.2) taking the output from solveadiabxy for increasing values of alpha. plots adiabatic function for al, bt, ga. [Looking for zero crossings.] This needs adiabNiso.m the adiabatic the origin for alpha. multiplicity

adiabNisoplot.m

Solving for bounds of the multiplicity region: adiabNisoauxf1.m plots the linear and nonlinear parts of equation and the extremal tangents from determining the multiplicity region in [looking for intersections, solving for region]. Surface and contour plots: adiabNisosurf.m plots the surface and zero level curve. This m-function uses adiabNiso.m

adiabNisocolorcontour.m plots color contour map of the specified region and level zero contour curve. This m-function uses adiabNiso.m Compound plots of the multiplicity region: runadiabNiso.m plots same curve/surface plot as above (adiabNisoauxf1), followed by surface of the adiabatic function, as well as the contour = 0 plot with bifurcation limits according to adiabnisoauxf1 and thereby giving the multiplicity region. This m-function relies on adiabNiso3dplot. Further surface and contour plots: adiabNisocontourcurve.m plots only the level zero contour curve. This m-function calls adiabNiso and adiabNisoauxf1. adiabNisographsol.m interpolates the zero level graph to find all steady state values; computes well for all alpha values. This m-function calls adiabNiso and adiabNisoauxf1.

Auxiliary functions:

================== adiabNiso.m adiabNiso3dplot.m adiabNisoauxf1.m evaluate adiabatic function. plots two of the pictures in runadiabNiso.m plots the linear and nonlinear parts of equation and the extremal tangents from determining the multiplicity region in [looking for intersections, solving for region]. the adiabatic the origin for alpha. multiplicity

3.2 Continuous Stirred Tank Reactor; the non-adiabatic case Non-adiabatic case: ================== Solving the Non-adiab equation: solveNadiabxy.m uses fzero (built in root finder) to solve the Non-adiab equation; (often imprecise near multiplicity region boundaries).

Simple plots of the transcendental functions: kctriple.m Plots F1 in (3.16) for three values of feedback kc. Exhibits zero crossings of the non-adiabatic function F1 = 0 for varying feedback gains kc to illustrate the occurrence of bifurcation depending on the feedback gain. NadiabNisoplotfgxy.m NadiabNisoplotfgxA.m plots the linear and nonlinear parts of the equation wrt y; (looking for intersection points). plots the linear and nonlinear parts of the equation wrt xA and y; (looking for intersection points). plots the same as above, and also the complete Non-adiab function equation wrt xA and wrt y; (looking for zeros now). creates a plot of the Non-adiab equation surface, and a contour graph for at height zero: shows multiplicity regions for kc; adjust kc limits for more precise results. [More accurate than solveNadiabxy.m] This uses the external subroutines: NadiabNisoauxfkcxA and NadiabNiso

NadiabNisoplotfgxAy.m

runNadiabNisokc.m

runNadiabNisokccurve.m determines first which range of kc1 < kc2 insures bifurcations, then plots the y versus kc multiplicity region. runNadiabNisoal.m creates a plot of the Non-adiab equation surface and a contour graph at height zero: shows multiplicity regions for alpha; adjust alpha limits for more precise results. [More accurate than solveNadiabxy.m]

runNadiabNisoalcurve.m determines first which range of alpha (al) values insures bifurcations, then plots the y versus al multiplicity region. Plotting bifurcation regions for both kc and yc, and alpha: NadiabNisoalkcplot.m plots the region of alpha and kc values with bifurcation for one value of yc; [best if repeated with improved limits for kc from a general, unlimited kc plot.]

NadiabNisoalkcmultiplot.m repeats NadiabNisoalkcplot.m for several values of yc, all in one plot. (Keep kc >= -1 for best results). NadiabNisoalycplot.m plots the region of alpha and yc values with bifurcation for one value of kc; [best if repeated with improved limits for yc from a general, unlimited yc plot.] NadiabNisoalycmultiplot.m repeats NadiabNisoalycplot.m for several values of kc, all in one plot. NadiabNisoalgraphsol.m Plots the (3.12) Nadiab equation zero level Curve as a 2D plot, then the program decides on bifurcation and computes the values of y at the steady states of the CSTR system for the specified a0 value of alpha. Auxiliary m files: ================== NadiabNiso.m NadiabNisoauxfalxA.m NadiabfkcxA.m NadiabNisoauxfkcxA.m

evaluate non-adiabatic function wrt y. searches for values of al (given kc) with bifurcation (done wrt xA). evaluate non-adiabatic function wrt xA. searches for values of kc (given al) with bifurcation (done wrt xA).

3.4: Scalar Static Equations without Bifurcation The static equations: Colebrook equation and real gas volume formula Colebrook equation: ================== colebrookplotsolve.m Real gas volume: =============== idealgas3dplot.m realgas3dplot.m Adaptive algorithm that solves the Colebrook pipe friction equation and graphs it.

3D plot of volume of an ideal gas depending on temperature T and pressure P. 3D plot of volume of a real gas depending on temperature T and pressure P; with the gas'critical values Pc and Tc marked on the axes. 3D plot of relative volume ratio of an ideal and real

idealrealgascompare.m

gas with given parameters om (omega), Pc, and Tc, depending on temperature T and pressure P; Pc and Tc are marked on the axes and surface. xxxxxxxxxxxxxxxxxx END Chapter 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Chapter 4: Initial Value Problems ========= 4.1 An Example of a Non-Isothermal Distributed System 4.1.5 Numerical Solution of the Resulting Initial Value Problem 4.2 Anaerobic Digester for Treating Waste Sludge 4.2.4 Solutions of the Steady State Equations 4.2.5 Steady State Conversion in terms of the Feed Concentration of the Microorganisms 4.2.6 The Unsteady State Behavior of the Digester and the Solution of the Initial Value Differential Equations 4.3 Bubbling Fluidized Bed Catalytic Reactors (Heterogeneous Two-phase System) 4.3.6 Numerical Treatment of the Steady State and Dynamical Cases of the Bubbling Fluidized Bed Catalytic reactor with Consecutive Reactions 4.4 A Biomedical Example: The Neurocycle Enzyme System 4.4.6 Numerical Considerations 4.1: An Example of a Non-Isothermal Distributed System 4.1.5: Numerical Solution of the Resulting Initial Value Problem fixedbedreact.m plots the two part solution to equation (4.22), V versus XA and V versus T. qfrun.m Adprun.m Tjrun.m Tfrun.m plots two multiple line graphs of the fixed-bed-reactor xA and T curves wrt V. plots two multiple line graphs of the fixed-bed-reactor xA and T curves wrt V. plots two multiple line graphs of the fixed-bed-reactor xA and T curves wrt V. plots two multiple line graphs of the fixed-bed-reactor xA and T curves.

4.2: Anaerobic Digester for Treating Waste Sludge 4.2.4: Solutions of the Steady State Equations VversusCxf.m plots the graph V(Cxf) (in m^3) versus Cxf.

4.2.5: Steady State Conversion in terms of the Feed Concentration of the Microorganisms

XsssversusCxf.m steadystCsCx.m

plots the steady state conversion Xsss for different values of Cxf. gives two plots of CSss(V) and CXss(V).

4.2.6: The Unsteady State Behavior of the Digester and the Solution of the Initial Value Differential Equations anaerdigest.m plots the Cs and Cx curves from time = 0 to tend in hours.

4.3: Bubbling Fluidized Bed Catalytic Reactors (Heterogeneous Two-phase System) 4.3.6: Numerical Treatment of the Steady State and Dynamical Cases of the Bubbling Fluidized Bed Catalytic Reactor with Consecutive Reactions controlhetss.m xBversusy.m hetcontbifrange.m hetcontbifmultiK.m xAversusy.m fluidbed.m fluidbedprofiles.m plots the heterogeneous control equation surface and the zero contour curve. plots of xB(y) and marks max of xB on the plot. plots the graph of the line and exponential functions in equation (4.84). plots the graph of multiple lines and exponential function in (4.84). plots xA versus y. 3D plot of solution to the DEs in (4.74), (4.75), and (4.76). profile plots xA, xB, and y versus t of solution to the DEs in (4.74), (4.75), and (4.76).

4.4: A Biomedical Example: The Neurocycle Enzyme System 4.4.6: Numerical Considerations neurocycle.m Three different plots are obtainable by (un)commenting: First plotting block active: Graphs of all 8 profiles for 0 <= t <= Tend in an 8 window plot. Second plotting block active: Phase plot of acetylcholine concentrations in (I) versus (II), after limit cycle has been reached (for t >= 0.4 * Tend). Third plotting block active: s_12 profile and phase plot for 0 <= t <= Tend. Use the MATLAB text editor ("Text" help button) to (un)comment the three plotting blocks so that ONLY ONE is active.

xxxxxxxxxxxxxxxxxx END Chapter 4 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Chapter 5: Boundary Value Problems, with and without Bifurcation

========= 5.1 The Axial Dispersion Model 5.1.2 Example of an Axial Dispersion Model. Linear and Non-linear Two-point Boundary Value Problems (BVPs) 5.1.3 Numerical Solution of Nonlinear BVPs. The Non-isothermal Case 5.2 The Porous Catalyst Pellet BVP 5.2.2 Numerical Solution for the Catalytic Pellet BVP 5.1: The Axial Dispersion Model 5.1.2: Example of an Axial Dispersion Model. Linear and Non-linear Two-point Boundary Value Problems (BVPs) conversionDa.m finds Da the desired value for the Damkoehler number and plots F(Da) curve near the root, if desired (plotting = 1). finds Da the desired vector of values for the Damkoehler number and plots Da versus xA(1). finds Pe the desired vector of values for the Peclet number and plots Pe versus xA(1) curve. Plots of the solutions (theoretical [if known] and numerical) to the 2 point BVP (5.24) for first and second order right hand sides, and their derivatives. variant of MATLAB bvp4c.m BVP solver, that allows for singular Jacobians and restricts their sizes to below 300 by 300 to avoid useless number crunching in case of spurious response from a suggested initial guess. [Uses Householder QR factorization to find the least squares solution in the search for the subsequent direction, rather than Gauss, as MATLAB's bvp4c.m does and then breaks down with every singular Jacobian.]

multiconversionDa.m multiconversionPe.m linquadbvp.m

bvp4cfsinghouseqr.m

5.1.3: Numerical Solution of Nonlinear BVPs. The Non-isothermal Case axialdisp4DaPerunbackw.m uses backwards integration and plots reversed function data; the graph of xA and y in terms of w. axialdisp4DaPerunfwd.m getting the same plots as in the previous m file. bvp4cfsinghouseqr.m variant of MATLAB bvp4c.m BVP solver, that allows for singular Jacobians and restricts their sizes to below 300 by 300 to avoid useless number crunching in case of spurious response from a suggested initial guess. [Uses Householder QR factorization to find the least squares solution in the search for the subsequent direction, rather than Gauss, as MATLAB's bvp4c.m does and then breaks down with every singular Jacobian.] gives two plots: the first one shows the solution profiles for the input variables, while the second one shows the corresponding end values of xA and y in

runaxialdispDa.m

dependence of Da, the Damkoehler number. This m-file relies on four Further programs: Damoveupbwd, Damovedownbwd, Damoveupfwd, and Damovedownfwd. plugflow.m plugflowxAy.m plots xA(1) and y(1) for the range of Dahmkoehler numbers. plots the profiles of xA and y for given Da and plug flow.

Auxiliary m files: ================== Damoveupfwd.m Damovedownfwd.m Damoveupbwd.m Damovedownbwd.m

these 4 functions integrate the BVP for specified ranges of Damkoehler numbers and given other parameters (beta, gamma, PeH, and PeM) and a suggested stepsize, either incresasing or decreasing in Da, and integrating forwards or backwards as indicated in their names. These functions decrease the step size adaptively in case of failure to integrate.

axialdisp4DaPecurvespots.m used implicitly inside Damoveupbwd, for example; plots in Figure(1) of the xA(1) and y(1) values versus the Damkler number (in a log10 scale). Gives implicit plot in Figure(2) of the solution profile for both xA and y from the internal call of axialdisp4DaPerunbackw.m etc. Also put out: the specific solution sol; sp = 0 if successful; the Da plot coordinates in P; and the error err of both DEs at the endpoints (10^-11 typical; 10^-2 for wrong stiffness applications; benign).M files used: axialdisp4DaPerunbackw.m or axialdisp4DaPerunfwd.m, depending on desired direction of integration. 5.2 The Porous Catalyst Pellet BVP 5.2.2 Numerical Solution for the Catalytic Pellet BVP For porous catalyst pellet: single second degree two point singular boundary value problem : plotting, analytic continuation of BVP solutions, and plotting and multiplotting of eta(phi) curves, that signify possible bifurcation. pelletrunfwd.m plots solution function y(x) of the BVP and computes pellet value eta.

multipelletrunfwdxAy.m graphs the solution profiles for concentration xA and temperature y. pelletetacurve.m constructs the plot of eta(phi) for a specified range of phi values; exhibits multiple states and bifurcation for the given data phi, beta, and gamma. constructs multiple curves for varying beta, or varying gamma values in one graph.

pelletetacurvemulti.m

For non-isothermal catalyst pellet reactions with external mass and heat transfer governed by Sherwood and Nusselt numbers Sh and Nu: Described by a system of two second degree two point singular boundary value problems: plotting

of BVP solution curves (multiple), and plotting of eta(phi) curves, that signify possible bifurcation. pellet4runfwd.m pellet4etacurve.m plots solution functions xA(omega) and y(omega) of the BVP and computes pellet value eta. constructs the plot of eta(phi) for a specified range of phi values; exhibits multiple states and bifurcation for the given data phi, beta, gamma, Sh and Nu.

Auxiliary m files: ================== Damoveupfwd.m Damovedownfwd.m Damoveupbwd.m Damovedownbwd.m these 4 functions integrate the BVP for specified ranges of Damkoehler numbers and given other parameters (beta, gamma, PeH, and PeM) and a suggested stepsize, either incresasing or decreasing in Da, and integrating forwards or backwards as indicated in their names. These functions decrease the step size adaptively in case of failure to integrate. these 2 functions integrate forward or backwards for the 4 DE model for specific Dahmkoehler etc parameters

pellet4DaPerunbackw.m pellet4DaPerunfwd.m

pellet4DaPecurvespots.m used implicitly inside Damoveupbwd, for example; plots in Figure(1) of the xA(1) and y(1) values versus the Damkler number (in a log10 scale). Gives implicit plot in Figure(2) of the solution profile for both xA and y from the internal call of axialdisp4DaPerunbackw.m etc. Also put out: the specific solution sol; sp = 0 if successful; the Da plot coordinates in P; and the error err of both DEs at the endpoints (10^-11 typical; 10^-2 for wrong stiffness applications; benign). M files used : axialdisp4DaPerunbackw.m or axialdisp4DaPerunfwd.m runpelletDanew.m gives two plots; DaPeH4PeM8profiles.fig and DaplotPeH4PeM8.fig. The first one shows the solution profiles for the input variables, while the second one shows the corresponding end values of xA and y in dependence of Da, the Damkoehler number. variant of MATLAB bvp4c.m BVP solver, that allows for singular Jacobians and restricts their sizes to below 300 by 300 to avoid useless number crunching in case of spurious response from a suggested initial guess. [Uses Householder QR factorization to find the least squares solution in the search for the subsequent direction, rather than Gauss, as MATLAB's bvp4c.m does and then breaks down with every singular Jacobian.]

bvp4cfsinghouseqr.m

xxxxxxxxxxxxxxxxxx END Chapter 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Chapter 6: Multistage Systems =========

6.2 Non-Reacting Multistage Isothermal Systems and High Dimensional Linear and Non-linear Systems 6.2.1 Absorption Columns or High Dimensional Lumped, Steady State and Equilibrium Stages Systems 6.2.2 Non-Equilibrium Multistages with Non-linear Equilibrium Relations 6.3 Isothermal Packed Bed Absorption Towers 6.3.3 Discussion and Results for both the Simulation and the Design Problem 6.4 The Non-Isothermal Case: a Battery of CSTRs 6.4.2 Numerical Solutions 6.2: Non-Reacting Multistage Isothermal Systems and High Dimensional Linear and Non-linear Systems 6.2.1: Absorption Columns or High Dimensional Lumped, Steady State and Equilibrium Stages Systems linearcolumn.m findNlinearcolumn.m finds X and Y, the molar fractions of component A in the liquid and gas phases, respectively. finds: N: number of trays for desired value of X_N, X and Y: the molar fractions of component A in the liquid and gas phases, respectively. finds X and Y, the molar fractions of component A in the liquid and gas phases, respectively,in trays 1 to N. finds X and Y, the molar fractions of component A in the liquid and gas phases, respectively.

quadcolumn.m linearnoneqicol.m

6.2.2: Non-Equilibrium Multistages with Non-linear Equilibrium Relations quadnonequicol.m gives X and Y, the molar fractions of component A in the liquid and gas phases, respectively,in trays 1 to N.

6.3: Isothermal Packed Bed Absorption Towers 6.3.3: Discussion and Results for both the Simulation and the Design Problem absorbtoweriterXe.m finds the output liquid concentration Xe and plots solution X and Y curves from l = 0 to l = Ht. (Xenew is the liquid output). plots solution X and Y curves from l = 0 to l = Ht. (Ht = necessary height of column)

absorbtoweriterHt.m

6.4: The Non-Isothermal Case: a Battery of CSTRs 6.4.2: Numerical Solutions threeCSTRrun.m 5 sets of phase plots using threeCSTR.m, overlaid in one figure. This m-file relies on threeCSTR.m to solve each IVP of the form (6.120). givwith annotations etc.

threeCSTR.m

xxxxxxxxxxxxxxxxxx END Chapter 6 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

You might also like