100% found this document useful (1 vote)
352 views162 pages

Adidharma - Mathcad For Engineers

Libro sobre el uso de mathcad aplicado a la ingenieria quimica

Uploaded by

Omar Dario Rozas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
100% found this document useful (1 vote)
352 views162 pages

Adidharma - Mathcad For Engineers

Libro sobre el uso de mathcad aplicado a la ingenieria quimica

Uploaded by

Omar Dario Rozas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 162
UNIVERSIDAD NACIONAL DE JUJUY BIBLIOTECA invent. ING.6445 Top. 66.0:004.4 Mathcad for Chemical Engineers Second Edition Hertanto Adidharma University of Wyoming Valery Temyanko University of Arizona TOP: 66.0:004.4 ADI 2.e. - N° Inventario: ING.6445 IVACU Contents Preface to the Second Edition Chapter 1. Introduction Chapter 2. Getting Started with Mathcad 2.1. Mathcad Worksheet and User Interface 2.2. Variables and Functions 2.3. Arrays: Vector and Matrix 2.4. Working with Units 2.5. Graphics Features 2.6. Symbolic Math Capabilities Example set 2.1 Problems Chapter 3, Non-Linear Equation 3.1. Polynomial Example set 3.1 3.2. Non-Polynomial Example set 3.2 Problems Chapter 4. System of Equations 4.1. System of Linear Equations Example set 4.1 4.2, System of Non-Linear Equations Example set 4.2 4.3, System of Equations with Constraints Example set 4.3 Contents Problems Chapter 5. Curve Fitting 5.1. Interpolation 5.1.1. Linear Interpolation 5.1.2. Cubic Spline Example set 5.1 5.2. Regression 5.2.1. Linear Regression 5.2.2. Polynomial Regression 5.2.3, Non-Linear Regression Example set 5.2 Problems Chapter 6, Differentiation and Integration 6.1. Differentiation Example set 6.1 6.2. Integration Example set 6.2 Problems Chapter 7. Optimization 7.1. Extreme Value Problem 7.1.1. Unconstrained Function 7.1.2. Constrained Function Example set 7.1 7.2. Linear Programming Example set 7.2 7.3. Non-Linear Programming Example set 7.3 Problems 52 61 61 61 62 65 67 70 71 7a 79 79 82 91 91 91 94 96 97 98 101 102 104 Contents Chapter 8. Differential Equation 8.1. Ordinary Differential Equation 8.1.1. First Order ODE 8.1.2. System of First Order ODEs 8.1.3. Higher Order ODE — Initial Value Problem 8.1.4. Higher Order ODE ~ Boundary Value Problem 8.1.5. Second Order ODE ~ Cylindrical/ Spherical Coordinate 8.1.6. System of Higher Order ODEs Example set 8.1 8.2. Partial Differential Equation 8.2.1. Parabolic PDE 8.2.2. System of Parabolic PDEs 8.2.3. Elliptic PDE Example set 8.2 Problems Chapter 9. Miscellaneous 9.1. Data Handling 9.2. Data Exchange with Excel 93. Introduction to Mathcad Programming 9.3.1. Conditional (ifStatement) 9.3.2. Loops (fox/ while Statement) Example set 9.1 Problems Index. 109 109 109 113 114 116 118 119 121 124 125 127 128 129 131 141 141 141 144 145 147 154 Chapter 1 + Introduction 1 hapter 1 troduction The profession of chemical engineering officially started as ear- ly as 1888, when Professor Lewis Norton of the Massachusetts Institute of Technology introduced “Course X” and united chemical engineers with a formal degree. Soon after that, Tulane University and the Univer sity of Pennsylvania followed the suit by adding four-year program to their curriculum. The first programs were the combination of mechani- cal engineering and chemistry with an emphasis on the industrial needs of that time. Indeed, the challenges at that time required very different logics compared to what modern chemical engineers are facing today. ‘The complexity of the logics grew as the process requirement became more demanding due to economical, environmental, and/or technologi- cal considerations. Nowadays, in performing their tasks, such as operation, equip- ment design, process design, process control, and process analysis, chem- ical engineers utilize more sophisticated mathematical frameworks. As shown in Figure 1.1, the problem solving in chemical engineering in- volves several steps. First, the physical problem with all of the con- straints must be clearly defined. At this stage, it is also very important to properly state the final goal. Second, a mathematical model of the sys- tem of interest has to be formulated using fundamental theories such as force balance, momentum, heat, and mass balance, thermodynamics, and rate laws (momentum, heat, and mass transfer, kinetics). This is a critical step in problem solving. If a model is incorrectly formulated, one will obtain erroneous results although the mathematical solution of the mod- elis perfect. fo Third, after a mathematical model has been correctly formulated, the task is reduced to how to solve it fast and reliably. Before the com- puter era, mathematical models had to be simple enough so that analyti- cal or numerical solutions can be obtained in a reasonable period of time. Since numerical solutions in the past were very time consuming, if achievable at all, chemical engineers sacrificed the accuracy of the solu- tions by oversimplifying the problems. Fortunately, modern computing allows us to perform such tasks much faster and deal with more and more complex mathematical formulations. This means that unnecessary 2 Chapter 1 + Introduction assumptions in developing mathematical models could be removed, op- timization of an integrated system could be performed, and what-if anal- ysis could be easily done, When a numerical method or procedure is de- veloped, it must be carefully and rigorously checked for errors and tested on some problems of known solutions. Due to the advanced improvement on the mathematical software packages, many basic numerical methods, such as routines (functions) for solving non-linear equations and ordinary differential equations, have been at our fingertips, Thus, the errors originated from numerical me- thods used can be minimized. Fourth, after a mathematical solution has been obtained, result verification must be performed to ensure the correctness of the problem solution. This can be done by critically analyzing the results and/or comparing with the available experimental data. Of course, in analyzing the results, logical thinking, good sense, and deep understanding of the fundamental theories underlying the physical problem are needed. If the results violate theories, do not make sense, or do not agree with experi- mental data, a revision of the model or numerical approach should be considered. Figure 1.1, Problem solving in chemical engineering The success of problem solving in chemical engineering is clearly determined by the accuracy of the model, the versatility of the computa~ tion, and the capacity of analysis. The first factor calls for a thorough understanding of the fundamental theories underlying the physical problem, the second calls for a versatile and powerful computational tool, and the third calls for both understanding theories and computa- tional tool. Therefore, learning a computational tool is critical in chemi- cal engineering. We need a computational tool for a certain problem not only to solve the mathematical model obtained, but also to learn the behavior of the system, for which what-if analysis is usually performed. We intui- Chapter 1 + Introduction 3 tively like to experiment, by varying parameters and variables and ana- lyzing their effects, to obtain a better understanding of the system beha- vior. Learning by doing, experiencing, and observing is usually very ef- fective. ‘The desired computational tool should therefore provide us with not only robust numerical tools, but also easy ways to interpret and ana- lyze the results of different scenarios. Mathcad is one of such tools that we will learn in this book. Mathcad Worksheet and User Interface Chapter 2 * Getting Started with Mathcad apter 2 tting Started with Mathcad As in learning other software packages, in learning Mathcad, before we can develop more complicated procedures to solve mathemat- ical problems, we need to understand Mathcad’s basic features, such as definition and syntax. The effort needed for this purpose is minimal and much less than that needed for learning other software packages because Mathead’s basic features, as we will find later, are very intuitive. Howev- er, one should not overlook the details to avoid unnecessary mistakes. Keep in mind that bad input or instruction produces bad output or result (garbage in, garbage out). If we are familiar with other program packages run on Windows, such as Microsoft Word or Excel, we will find the Mathcad user inter- face very familiar. As shown in Figure 2.1, Mathcad has Menu Bar, Stan dard Toolbar, Formatting Toolbar, and Math Toolbar. A tool bar that is different from that in many Windows applications is the Math Toolbar. This Math Toolbar makes mathematical calculations very easy and con- venient to perform. The Math Toolbar allows a user to access all mathe- matical symbols and operations. If we click on any button on this bar we will see an expanded toolbar under the specified category on the screen. The following buttons are available from the Math toolbar: Calculator Toolbar Graph Toolbar Vector and Matrix Toolbar Evaluation Toolbar Calculus Toolbar Boolean Toolbar Programming Toolbar Greek Symbol Toolbar ye PN any es wp yo Symbolic Keyword Toolbar 6 Chapter 2 « Getting Started with Mathcad eau Bat 6 1 Star Toot ‘= Farting Totter Plug Flow Reactor Design “hie werksheet i an example how ocesign a plug lew rector using Matnese Fate constant: ogois #*! Concenraton ef Alnthe reed: Ca ene rt eprint) =k Gy (1-Xa) omonratecreacanthaterennes: ig, <2 on sutton for ug How etre dered rommal balance: Ygsaltne| ya For Me08) yaaa oar? “0 Ma=O8: VotCos) = 643807 Figure 2.1, Mathcad Worksheet and Liser Interface We can find out the name of each toolbar by letting the mouse pointer rest on a button, The function of each toolbar is pretty much self-explanatory. For example, the Calculator Toolbar contains all of the basic arithmetic operators we usually find in a calculator. Some of them can also be directly typed through the computer keyboard, for example “for multiplication, ‘/* for division, “*” for power, etc. We will learn and use most of the toolbars as needed in the subsequent sections and chapters, Mathcad is essentially a worksheet for calculations. If we look at Figure 2.1 once again, one of the nicest features of Mathcad becomes ob- vious ~ we could develop a solution procedure just as we would do it on paper. We type the problem statement and variable definitions, then va- riable assignments, function definitions, and mathematical equations of the solution, and finally, graphical representation of the solution. Note that we can type math equations just as we would write them on paper. This is a natural and intuitive way to develop a problem solution, making it very easy to read and debug if necessary. Text, tables, equations, and graphs can be mixed on the same worksheet. To insert a text region, for example, type a double quotation mark (") (or click Insert, Text Region from the Menu Bar) and type the text we want. Thus, we can use Math- cad not only to obtain the results but also to elegantly present them. .2 Variables and Functions Chapter 2+ Getting Started with Mathcad The power and versatility of Mathcad become evident from the use of variables and functions. Mathcad has several built-in variables, such as x (3.14159), e (2.71828), and g (9.807 m/s*), which can be directly used once we open a worksheet. However, most of the time we will use our own variables, which are called user-defined variables. A variable is defined by assigning a quantity to a variable name, for example: xis3 ys2x+1 z= 112.5 Note that by default any assignment in Mathcad is done by typing a co- Jon (“:”), not an equal sign (when we type a colon, Mathcad will show a colon and an equal sign). This assignment is sometimes called the “define as equal to” operator. In fact, we can override the default operator dis- play in the Worksheet Options in the Tools menu, but this is not rec- ommended for beginners. ‘As we can see, a number, an expression of other previously de- fined variables, or a range of values might be assigned to a variable. In the next section, we will learn that a matrix/vector can also be assigned toa variable and in the last chapter, we will learn that a component can also be assigned to a variable. Thus, the type of a variable is directly de- termined by the type of the value assigned. An equal sign (“=”) is used when we want Mathcad to display the value of a defined variable or evaluate an expression (or a function) and display the result. From the example above, if we want to display the value of variable y, then we type “y” and “= y=7 The following examples show the use of equal sign for evaluating an ex- pression and a defined variable: 3-y-1=20 Wisxt+ 5=8 ‘The variable z in the example is defined as a range variable, which has a range of values. In this assignment, the initial value is typed first, fol- lowed by a comma (“,”) and the second value, then a semi-colon (“;”), 8 Chapter 2 » Getting Started with Mathcad and the last value. If the second value is omitted, the default interval is 1 (interval = the difference between the second and the first values). Names in Mathcad can be a combination of alphanumeric, Greek letters, prime symbol, subscript (literal subscript), and underscore, but it should start with an alphabet or a Greek letter, which can be accessed from the Greek Toolbar. Mathcad distinguishes uppercase and lowercase letters, This name convention holds for any names including names for functions, matrices, and vectors. Variable names may also contain operator symbols, as shown [eal This can be done by pressing [Ctrl][Shift]] to insert a pair of brackets Thus, the term on the left side is in fact the name of the variable, which has been defined to have a value of 1. below Mathcad has a huge array of built-in functions readily available once we open a worksheet. Click on the 43) icon on the Standard Tool- bar to see all of the built-in functions. We can also define our own func tions (user-defined functions), for example: 2.4 f0) = 3X + =~ 5 + sin(x) x (iy x wn-(" 2 i h(x, ) = x+ £09 -1 (34 P(M):=M + ( ; ;) The variables in the bracket on the left side are called arguments. The choice of argument(s) for a function in a certain problem could be the Key to problem solving. The argument(s) can be of any type, ie, scalar, vector, matrix, or function. In the example above, f and g have scalar arguments; h has scalar and function arguments, while p has a matrix argument, Similar to variable assignment, the type of a function is also determined by the type of the assigned value resulted from the expres- sion defining the function. Thus, fand hare scalar functions while g and P are matrix functions. See the next section for information on vec- tor/matrix, Chapter 2 = Getting Started with Mathcad Mathcad always performs the calculations from left to right and from top to bottom, which is intuitive. This means that before a user- defined variable/function can be used for a certain mathematical opera- tion, it should have been defined, i.e. given a value or an expression, somewhere above or on the left side of the operation. There is an excep- tion for this general rule of the calculation flow, as described in the later section. There are two other things that should be remembered when de- fining a variable/function. First, if the user-defined name is the same as one of the built-in names, then the user-defined variable/function will override the built-in variable/function. Second, variable names and func- tion names in a worksheet are not distinguishable. It means that if we define a function y(x) and later we define a variable y, then we will not be able to use y(x) anywhere below the definition of the variable y. When we redefine a variable or a function, Mathcad will warn us by flagging it with a green wavy underscore, It is always a good idea to avoid this green wavy underscore by just changing the name of the vari- able/function, Arrays: Vector and Matrix Mathcad can also handle arrays. Thus, variables can also be de- fined as array variables, such as vectors and matrices. A vector is a matrix with a single row or column. The initialization (assignment) of an array can be done in three different ways: (1) Using the Vector/Matrix icon from the Vector/Matrix Toolbar The assignment is done by typing the name of the array and a colon, and then followed by choosing Matrix from the Matrix Toolbar or pressing [Ctrl-M]. Examples of this initialization are shown below: 140 2 fe) & 1 8 B=] 1 “102 5 If the number of elements of an array is more than 600, the assign- ment must be done using other ways described below. (2) Using a range variable as an array subscript (index variable) This type of initialization is shown in the following examples: 10 Chapter 2 = Getting Started with Mathcad 2 Bs 0.4 G psiti 2 2 o1234 B=|1| C=/12345 L 5 23456 Here, i and j are range variables. The index of B, shown as a sub- script, is typed using the left square bracket (“[*) key followed by the name of the range variable, which is “i” in this case. After typing a colon, the values of elements of B, i.e., Bu, Bi, and Bz, are then en- tered, From one element to the next, we type a comma (",”) and Mathcad automatically creates an empty placeholder. In this exam- ple, we define Bo = 2, Bi = 1, and Br =5 This initialization is handy when the elements of an array have a certain pattern, such as those of matrix C above, and the number of elements is large. The index of C is also typed using the left square bracket key followed by the name of the first range variable, a com- ma, and the name of the second range variable. (3) Using data table If we use data table, click Insert, Data, Table from the Menu Bar, and . then type the name of the array. An example of this initialization is shown below: 0 1 2 0 1 & 0 2 1 3 2 “i 0 2 Note that the default starting index in Mathcad is in fact 0, not 1 Thus, for example, the first element of A is a) To set the starting index of arrays to 1, click Tools, Worksheet Options in the Menu Bar and change the Array Origin to 1, or by redefining the built-in variable ORIGIN on the worksheet: ORIGIN= | where symbol “=” means that the redefinition is globally applied throughout the worksheet (this symbol can be accessed in the Evaluation Chapter 2 « Getting Started with Mathcad 1 Toolbar). This global assignment clearly overrides the rule of calculation process (from left to right and from top to bottom). Please be aware that if we use a range variable to initiate an array and we still use the default starting index, the range variable must start from 0, otherwise the initialization will be incorrect, as shown below: 0 2 7 1 5 which means we created a 4x1 vector (Bo will automatically be set to 0 when it is not defined)! In general, an index of an array can be a variable, a number, or an arithmetic expression. The following examples are legitimate indexed variables (again the subscripts are created using the left square bracket key): By Ses Pr.3-1) This index should not be confused with a literal subscript that is used as a part of the variable name. A literal subscript can be typed using a pe- tiod (“.”) key. The following are examples of variables with literal sub- script: Psat % ‘We cannot have an arithmetic expression for this subscript because liter- al subscript is not a variable/number. Note that r2 here is a variable name, not an element of an array r. Many array operations can be done easily in Mathead, such as array addition and subtraction, dot product, cross product, matrix mul- tiplication, matrix inversion and transposition, and calculation of deter- minant. For example: 3,2 51 As Be 14 02 0.09 ae c=(aBy ! ( Det = |C| = 0.01 12 Chapter 2 * Getting Started with Mathcad -1 usvxwe| 13 9 ‘There is a unique type of array operator, i.e., vectorize operator, which is very handy for calculations involving a lot of data. This vector- ize operator applies any arithmetic operator and/or function element- wise (clement by element). To apply vectorize operator, we use an arrow sign above the operation or function, which can be accessed from the Matrix Toolbox. For example: 4 ———> (0.082 wis] 3 sale ou 2 0.139, 19 yi — 0) ex +3 few) =| 12 2 ‘Although many functions and operators, when applied to vectors, auto- matically perform operations on the elements of those vectors (arrays), the use of vectorize operator is recommended to avoid unnecessary mis- takes, An example below shows a potential problem: 16 (w-w) =| 9 wew=29 4 The second operation (without the vectorize operator) is in fact a dot product of two vectors. 2.4 Working with Units: Built-in and User-defined —_—— Ss In Mathcad, although there are some restrictions, physical units can be incorporated into most of calculations. Built-in and user-defined units can be considered as built-in and user-defined variables. When we want to incorporate a physical unit with a value, we just multiply the value with the name of the unit, such as shown in the example of area calculation below: D=2.Sin A==D 4 Chapter 2 « Getting Started with Mathcad 13 If the unit isa built-in unit, the name of the unit can be typed directly or inserted by clicking Insert, Unit from the Menu Bar. Knowing the exact names of the built-in units we will be using is important to avoid unne- cessary error. One example of common mistake: the name of unit ‘gram’ in Mathcad is incorrectly thought as ‘g’, but the correct one is in fact ‘gm’. If we forget about this, Mathcad will not warn us because ‘g’ is a built-in variable (gravitational acceleration). Therefore, use Insert, Unit from the Menu Bar if we are not sure about the unit names. If the unit is a user-defined unit, the unit should have been previously defined in term of built-in units and the name of the unit is also typed directly when it is used. The following is an example how we define and use a user-defined unit: kbar := 1000bar P kbar Mathcad always stores values and displays all results in SI units (the default unit system is SI system). Changing the default unit system is possible but not recommended. For the example above, if we evaluate A, we get A=3.167x 103m? a Note that an empty unit placeholder appeared on the right side (a small black solid box). We can easily change any unit displayed to the desired unit by typing the unit name in the empty unit placeholder. For exam- ple, if area in ft? is desired, we type “ft” in the unit placeholder: A= 0.0348 If another area unit is then desired, for example mm®, just replace “fe” with “mm”. There is no unit placeholder anymore once the unit place- holder has been filled. Calculations that incorporate °C and °F as the temperature units can also be performed. For example: t= 25°C = 298.15K_ t=77°F t= 30°C cal op = AH :=ep-(tl ay a2 gm A°C ° em Note that there are two types of Celsius and Fahrenheit built-in units, ie., temperature unit (°C or °F) and temperature difference unit (AeC or ASE), which should be distinguished from each other. These temperature units can be inserted by clicking Insert, Unit from the Menu Bar. The temperature unit °C or °F is in fact a postfix operator/function, for which, unlike the temperature difference units and other units, a multiplication 14 Chapter 2 * Getting Started with Mathcad operator (“dot”) must not be used. If we use absolute temperature units, both temperature and temperature difference units are the same, i.e., K orR. Remember that units are treated like variables. Consequently, if we define a variable named m, this will confuse ourselves with the unit “meter”. Thus, if units are incorporated in our calculations, we need to avoid using unit names as the names of our user-defined va- riables/functions, particularly when the unit will be used. If this should happen, i.e., if a unit name needs to be used as a user defined varia~ ble/function, we need to insert a namespace operator to differentiate the unit name from the user-defined variable. For example: m3 = 0.5- Meni The namespace operator [unit] is created by pressing [Ctrl][Shift]N and typing the word “unit”. The m in the definition of r above is the unit “meter”, not the variable m, which has been defined to have a value of 3. Since some Mathcad built-in functions do not support or fully support units, as we will find in the subsequent chapters, the unit incor- poration in the calculations is in fact recommended for advanced users. For beginners, the units of all variables can be made consistent before calculations, which can be easily done in Mathcad, and then the calcula~ tions could be performed without incorporating any units 2.5 Graphics Features: x-y Plot of Data and Functions Mathcad has many graphic features, including 3-D plot, contour plot, and animation. In this section, we discuss only x-y plot because it is the most widely used graphic representation. We will learn other plots in later chapters as needed. Data and functions can be plotted easily in Mathcad after inputting the data or defining the functions. Data pairs should be inputted in the forms of vectors of the same size. For example, if we want to plot two quantities obtained from an experiment, where x is the independent variable and y is the dependent variable, we need to create vectors x and y first. A graph frame is created by clicking the Graph Toolbar from the Math Toolbar and choosing x-y plot (scattered plot). To let Mathcad know what we want to plot, we have to type the variable/function names in the empty central placeholders of the abscissa and ordinate (x- and y-axes). The other empty placeholders are for scale adjustment. Mathcad will automatically plot the data once we hit [enter] Chapter 2 « Getting Started with Mathcad 15 or click any region outside the graph region. If we want to plot a second experimental data z measured at the same set of independent variable x, we need to create the vector z before the graph region (remember that Mathcad processes any calculations from left to right and from top to bottom). To incorporate this second dependent variable in the existing graph, we click on the name of the first dependent variable on that graph and type a comma (“,”) immediately after the last letter of the name of the first dependent variable. Then we type “z” in the second placeholder that just appeared. If the second set of data is measured at a different set of independent variables, say xl, the vector x1 has to be created first along with the vector z and remember that x1 and z must be vectors of the same size. Besides inserting z at the y-axis on the graph, we also insert x1 at the x-axis by typing a comma (“,”) immediately after the last letter of the name of the first independent variable. Below is an example of how to create x-y plot: 0 0 1 1 1 5 t=] 2 His] 4 x=] 2 y:=| 8 3 9 4 14 4 16 T 1s a d00 lor 1 9% x S Aaa yf 6 I 2 1 L 0 1 2 3 4 OF course, the vectors can also be created using other methods as de- scribed in Section 2.3. Sometimes we need to format our graph to obtain a better pres- entation. For example, we can change the symbols from circles to squares or change the symbol plot to line plot. We can also type the plot title, axis labels, and legends. To format a graph, double click the graph and a new window for formatting pops up. The formatting features are straightforward and the readers are encouraged to try by themselves. If

You might also like