Texas A&M University Artie McFerrin Department of Chemical Engineering Using Mathcad to solve non-linear equations CHEN 354: Fall 2008 Prof. Perla B. Balbuena Mathcad is a mathematical software easy to use. Among the capabilities of Mathcad are: Solving non-linear equations, graphing functions in two or three dimensions, solving differential equations, performing symbolic calculations, etc. The objective of this quick guide is to provide you the basic knowledge to use Mathcad in order to solve systems of non-linear equations. First, we go through the basic operations in Mathcad, such as defining variables and evaluating functions. Then, we solve a vapor-liquid equilibrium problem involving a system of non-linear equations. Mathcad basics The Mathcad window is shown in the following figure. All the expressions and objects are inserted in the workspace T.A. Rafael Callejas Tovar | jrc2267@chemail.tamu.edu Page | 1 Prof. Perla B. Balbuena The “Math toolbar” is useful to get quick access to the different capabilities of Mathcad. Defining variables and functions To define a variable, just place the cursor in the workspace and type its name followed by the “Definition symbol”, located in the “Evaluation toolbar” or typing the “:” key. For example, define P equal to 101.33. If you want to know the numerical value of a previously defined variable, place the cursor below its definition, type the name of the variable followed by the “Evaluate numerically symbol” whose shortcut is the “=” key. Defining functions is simple. Type the name of the function followed by the name of its independent variables enclosed in parenthesis. Then use the “Definition symbol” and create the correct expression aided by “Space bar”. You can evaluate the functions for given values of their independent variables. T.A. Rafael Callejas-Tovar jrc2267@chemail.tamu.edu Prof. Perla B. Balbuena Useful tips If you want to use Greek symbols to name the variables, use the “Greek symbol toolbar”. If you know the equivalent letter in the Latin alphabet, type the letter then “CTRL+g”. For example, to obtain gamma type “g” then “CTRL+g” To insert a textbox just type the “double quote key” and the region will become a text region. All the characters inside the textbox will be treated as text. When selecting a math region the cursor is blue, in a text region the cursor is red. The Given… Find solution block The “Given… Find block” is the tool that allows us to solve non-linear equations. The following is an example of its implementation. It is required to provide initial approximations for the unknowns. The block starts with the word “Given” written as a math expression; below it we place all the equations we want to solve. These equations may contain previously defined functions, is important to use the “Boolean equal symbol” to write the equations (“Ctrl+=”). T.A. Rafael Callejas-Tovar jrc2267@chemail.tamu.edu Prof. Perla B. Balbuena The “Given… Find block” ends with the “Find” command. Its arguments are the unknowns that we want to calculate. The result of the “Find” command may be assigned to a variable or function. In the example shown above the results are stored in the function “Solution(y1)”. This is useful when we want to solve the system for several values of y1. In the example shown above, we have a two-equations/two-unknonws system. Therefore, the solution porvided by the “Find” command will be a two-rows/one-column vector. We can split assign each vector element to separate functions as follows. To make reference to the elements of a vector use the subscript, “[“ key. In Mathcad the array subscripts begin at zero. Making plots To make plots, first define a “range variable”, in general, it will be in the x-axis of the 2Dplot. A range variable takes an initial value and add it a constant increment until a final value. To define it, name the variable, use the definition symbol, put the initial value, type a comma “,”, put the increment and type a semicolon “;”, then type the final value T.A. Rafael Callejas-Tovar jrc2267@chemail.tamu.edu Insert a 2D-plot template (“Shift+2”), fill the blanks accordingly to the functions you want to graph. You can format the plot by selecting it and right click > Format… Example: Create T-x-y and x-y diagrams Solve the following problem using Mathcad. For the system methanol(1)/methyl acetate(2), the following equations provide a reasonable correlation for the activity coefficients: γ ) 1 = exp where ( 2 2 = exp A =14.25326 − Ax )γ = 2 2 exp ( Ax 2 1 2.771 − 0.00523 T In addition, the following Antoine equations provide vapor pressures: Sat Sat P ⎛ 3643.310 ⎞ P ⎛ 2665.54 ⎞ 1 = exp 16.59158 − ⎜⎟⎜ ⎟ kPa TK − 33.424 kPa TK − 53.424 ⎝⎠⎝ ⎠ Assuming the validity of yP = x γ P (i = 1,2, …, N ) Sat i iii Calculate T and { x } i diagrams at this pressure. , for P = 101.33 kPa and y = 1 0.40 . Also, prepare T-x-y and x-y T.A. Rafael Callejas-Tovar jrc2267@chemail.tamu.edu Using Mathcad to create T-x-y and x-y diagrams Methanol(1)/Methyl acetate(2) Input data and expressions Pressure, kPa P := 101.33 (T) := 2.771 −⋅ Temperature dependence of the activity coefficient parameter, T in K 0.00523 T The activity coefficients depend on temperature and composition, T in K γ1(x1 , T) := exp ⎡⎣ A(T) ⋅ (1 − x1) 2 ⎤⎦ ( 2) γ2(x1 , T) := exp A(T) ⋅ x1 I n i t i a l a p p r o x i m a t Antoine equations, T in K ⎛⎜⎝ ⎞⎟⎠ := 300 ⎞⎟⎠ 3643.310 Psat1 (T) 16.59158 −:= exp T − 33.424 2665.54 T − 53.424 ⎛⎜⎝ Psat2 (T) 14.25326 −:= exp ions for the unknown s x1 := 0.5 Mathcad solution block Given x1⋅γ1(x1 , T) ⋅ Psat1(T) = y1 P ⋅ (1 − x1) ⋅γ2(x1 , T) ⋅ Psat2(T) = (1 − y1) ⋅ P Solution(y1) := Find(x1 , T) The solution of the system of equations is written as a function of y1. This is useful if you want to solve the system for several values of y1. ) : Solution(y1)0 = Since we have 2 unknows in the system of equations, the solution vector is formed by 2 rows and 1column. The order of the unknows in this vector corresponds to the definition of the "Find" command. : Solution(y1)1 = Suppose that we want to know theIndew-temperature and composition Mathcad the array origin subscriptof is 0 by default. T(y1) the first condensed drop for y1 = 0.4. We just have to evaluate the previously defined functions,T(y1) and x1(y1), at y1 = 0.4 x1(0.4) = 0.46 T(0.4) = 326.697 If we want to create T-x-y and x-y diagrams, we have to evaluate T(y1) and x1(y1) at several points of y1. Therefore, we define y1 as a range variable. y1 := 0 , 0.01 .. 1 y1 take values from 0 to 1 using a constant increment of 0.01 First values of y1 y1 = 0 . 0 5 0 . 0Ins 1ert the 02D.plot 0and 2fill the 0bla .nks 0as 3follo w 0 . 0340 4 0 335 T( y1 ) T( y1 ) 330 325 00.20.40.60.8 y1 , x1 ( y1 ) 1 y1 0.5 y1 0 x1 ( y1 ), y1 You can use the previously defined funtions T(y1) and x1(y1) to build other functions, for example, you can calculate You can use the previously defined funtions T(y1) and x1(y1) to build other functions, for example, you can calculate ln(γ1) and ln(γ2) 00.20.40.60.8 1 ln(γ1 ( y1 , T( y1 ))) ln(γ2 ( y1 , T( y1 ))) 0.5 0 x1 ( y1 ) 00.20.40.60.8 ⸀㈀ ⸀㐀 ⸀㘀 ⸀㠀 ⸀㈀ ⸀㠀 ⸀㐀 ⸀㘀 ⸀㈀ ⸀㐀 ⸀㘀 ⸀㠀