mathcad tutorial 1

1
INTRODUCTION TO MATHCAD Mathcad is a "numerical scratchpad" which allows you to solve complex systems of equations, find roots of polynomials and plot results. It can evaluate integrals and derivatives, both numerically and symbolically. Units are easily incorporated, and numerical answers cannot be found if the units are inconsistent. Mathcad is a powerful tool, but easy to use. These notes are a brief introduction. They are meant to be used while working in Mathcad. If you enter Mathcad, you will notice there are several menus that appear on the top line (File, Edit, Text, etc.). There is a drop-down "Math" palette containing icons for calculator, graphs, matrices, etc. If you move the cursor to an icon, there is a brief message explaining it. If you click on an icon, a palette with various operations appears. Click on the calculator palette and select the symbol. Type = and you get π 3.142 = Some symbols, such as , g (acceleration of gravity) and R (degrees Rankine), have a specific meaning unless you redefine them. Now, choose the nth root symbol and put in some numbers, such as: 6 458247 8.78 = To enter a number, just click on the appropriate placeholder (the black box). These examples show how to use the "=" sign, which means evaluate or calculate. Mathcad can be used as a calculator in this way, although it is a bit expensive and not particularly portable (unless you have a lap top). If you choose the Evaluation and Boolean icon, you will see there are several kinds of equals, in addition to inequalities. The ":=" sign means "is defined by." Type x followed by this sign, and assign a number such as: x 4.75 := In addition to using the Evaluation palette, this ":=" sign can be inserted simply by typing a colon (:). Once x is defined, we could then use it in an expression such as: ln x () x 2 + To get the natural log, simply type "ln". There are a number of built in functions available as you can see by clicking of the "f(x)" icon and scrolling through the list. It includes error functions and Bessel functions, in addition to the more mundane ones. Each comes with a brief explanation. There are also functions for numerical integration using the Runge-Kutta method. We will use the simple ones here. Return to the present problem. If you now type a regular equals sign, from the palette or the keyboard, you get: ln x () x 2 + 24.121 = You could also define a dependent variable, f, as this function of x, for use in later calculations: f ln x () x 2 + := If you want the numerical value of f, simply type f 24.121 = This type of function may be called explicit: you can write f equals some expression which contains only the independent variable, x. Suppose, however, we turned the equation around and wanted to find the x for which: 24.121 ln x () x 2 + This is an example of an implicit equation. You cannot rearrange this to get it into the form x = (something containing no x). You can, however, solve this equation in Mathcad. This is accomplished in a "given - find" block. Mathcad does this numerically and needs initial guesses for the unknowns, in this case x. Enter a guess: x 1 := Then type "given" with or without a capital G. (In most cases, Mathcad is not case sensitive.) Given Next, enter the equation. In these blocks you must use a third type of "equals" which means "is constrained by" and is used in these "given-find" blocks. (In the Evaluation palette, this is called a "Boolean equals.") This is the bold faced "=" from the evaluation palette (or CTRL = from the keyboard). 24.121 ln x () x 2 + To get an answer, type "find(x)" followed by an = Find x () 4.75 = If you next type x=, you will not see the solution (4.75), but the initial guess. This is because you have not redefined the value of x. If you want the value of x to be the solution, you can proceed as follows: x 1 := (Initial guess) Given 24.121 ln x () x 2 + x Find x () := Now x has been replaced by the solution of the equation. To see the value, just type: x 4.75 = The major restriction on the given-find statements is that you cannot use general subscripted variables (i.e., i or j) in them. You can, however, solve some interesting problems. In the analysis of flow of a power law fluid in an annulus, you need to find the value of in the following equation: κ λ ξ λ 2 ξ ξ - 1 n d λ 1 ξ ξ λ 2 ξ - 1 n d where is the ratio of the inner to the outer radius, n is the power law index and is the radial coordinate. In order to get the integral sign, go to the calculus palette and choose the definite integral. We can solve this equation using "given - find" as above. First define the and n and guess a value for : κ 0.4 := n 0.3 := λ 1 := Given κ λ ξ λ 2 ξ ξ - 1 n d λ 1 ξ ξ λ 2 ξ - 1 n d Find λ ( ) 0.657 = We can also include units in Mathcad calculations. Many units are already defined in Mathcad; others can be assigned. If you click on the measuring cup, you can see a list of the units available. You can change the default system of units by choosing Math then Units then Change System of Units. However, whatever system of units you are in, you can also always go from one to another. For example, take the acceleration of gravity, g. Type g= to see its value in SI (presuming the default system is set on MKS): g 9.807 m s 2 - = You notice a "placeholder" or a black square after the units. Click on this value and insert another set of units: g 5.004 10 9 × in hr 2 - = As an example of a problem with units, suppose we want investigate the P-V-T behavior of methane using the Redlich-Kwong equation of state. First, we enter the critical values for methane: Tc 190.6 K := Pc 45.99 bar := When you do this, the pressure units, bar, are highlighted. Mathcad doesn't recognize that unit. We can define this as a new unit using the third type of equal sign: bar 10 5 Pa Here is yet another type of equals. You can get this symbol from the Evaluation Palette, or by typing a tilde (~). Once you define the units bar in this fashion, the highlight disappears. Notice, however, what happens if you use the ":=" to define the bar unit. In this case, the bar in the Pc equation still remains highlighted. This "triple equals" is a "global assignment;" it's good throughout the Mathcad document. The other equals (:=) means the definition is good from this point on, and can be re-defined. Now, introduce the gas constant and the two constants associated with the Redlich-Kwong equation: R 1.987 cal mole K := b 0.08664 R Tc Pc := a 0.42748 R 2 Tc 5 2 Pc := We can define the moles in terms of grams for methane: (See what happens later if you do not use this global assignment.) mole 16 gm Suppose we were given a volume and a temperature and wanted to find the pressure. The specified temperature and volume are: T 165 K := v 53.29 cm 3 mole := According to the Redlich - Kwong equation, pressure is given by the expression: P RT v b - a v v b + ( ) T 1 2 - := Therefore: P 19.12 atm = To find the volume at 165K and 19.39 bar, we have to solve an implicit equation using Given - Find: P 19.39 bar := v 1.0 m 3 mole := (Initial guess) Given P RT v b - a v v b + ( ) T 1 2 - Find v () 0.032 m 3 kg = The default units of kg and m can be changed to moles and cm, or anything else you desire. In this problem, as in many others, the solution is sensitive to the initial guess. Try using a different value for the guess. Sometimes, the numerical procedure will not converge, or will converge on a different root. One of the other Mathcad features is root finding. Given a polynomial, we can find all the roots, real and imaginary, using the "polyroots" function. To use this feature in this problem, first, write the equation in polynomial form. The Redlich - Kwong equation is cubic in the volume (it is an example of a "cubic equation of state"): V 3 RT P V 2 - b 2 bR T P + a PT 0.5 - V - ab PT 0.5 - 0 Next, form a vector, call it V, of the coefficients in this equation. The first element is the coefficient of V 0 , the next, V 1 , etc: Notice that the elements of this vector, as with any vector, must have the same units. To use the root finding function, however, the elements must all be dimensionless. The three roots are then found simply by using the "polyroots" function: V a b PT .5 kg 3 m 9 - b 2 bR T P + a PT .5 - - kg 2 m 6 R T P - kg m 3 1 := polyroots V ( ) 3.33 10 3 - × 8.845 10 3 - × 0.032 = To decide on which root means what, we have to know a little about the physics of the situation. The vapor volume is the maximum value, so define: Vg max polyroots V ( ) ( ) m 3 kg := Since we took out the units in the vector V, we have to add them back again. We also have to put back the same ones we took out, that is, if we multiplied the elements of V by kg/m 3 , we must use m 3 /kga s the units for V. Vg 0.032 m 3 kg 1 - = The liquid volume is the minimum: Vf min polyroots V ( ) ( ) m 3 kg := Vf 3.33 10 3 - × m 3 kg 1 - = The middle value has no physical significance. (It might be interesting to some, at least, to note that the actual vapor volume of methane at these conditions is 0.032, as calculated. The liquid volume is 3.095 10 -3 .) Try using a different T and P, say 300K and 1 atm, and see what the roots are. Now, let's plot the compressibility factor for methane as a function of pressure for a temperature of 300K. We need to find v for a number of values of P. However, only one given-find statement is needed. We can proceed as before: T 300 K := (We already have an initial guess for v above.) MPa 10 6 Pa Given P RT v b - a v v b + ( ) T 1 2 - v P () Find v () := The v(P) means that v is a function of P; for each value of P, a different v is found. If we specify a particular value of P we get a numerical answer: v 4 MPa ( ) 0.036 m 3 kg 1 - = Now, introduce a range of pressures. This could have been done before the given - find statement if desired: P 0.1 MPa 0.2 MPa , 10 MPa .. := This means P takes on the values 0.1, 0.2, 0.3, ... all the way to 10 MPa. The two dots appear when you type a semicolon (;). Note that units must be included. Define the compressibility factor: zP () PvP () RT := Notice that z is also a function of P. Now create an x-y plot using the Graphics menu, the Graphing palette, or simply SHFT @. Put the cursor on the placeholders for the x and y axes and put in P and z(P), respectively: 0 2 10 6 × 4 10 6 × 6 10 6 × 8 10 6 × 1 10 7 × 0.85 0.9 0.95 1 zP ( ) P Notice the values of P are much larger than you defined. That's because this P is in Pa (the default units for pressure), not MPa. To change the scale, you can plot P/MPa rather than P (or P/atm, P/psi, or anything you want). If you double click on the graph, or if you choose "Format" with the right mouse buton, you can customize your graphs. By clicking the mouse in the region near the graph (or anything else), you can get a line enclosing it. You can move the graph, text or equation anywhere on the page using the cursor. When the cursor is at the boundary of the line, an arrow appears. This can be used to re-size the graph. As an example of a somewhat more complicated problem, consider the velocity of a freely falling drop of water in air. The diameter of the drop is: D 1.0 mm := Density of the water: ρs 62.4 lb ft 3 := Density and kinematic viscosity of air: ρ 0.0735 lb ft 3 := ν 0.169 10 3 - ft 2 sec := The drop velocity v depends on the friction factor f, which also depends on the Reynolds number, NRe, vD/. We can write this as 2 equations with 2 unknowns, v and f. In addition, if we are above the creeping flow regime, f is given by one of two expressions, depending on the value of the Reynolds number. If NRe is greater than 500, f is 0.44. If NRe is less than 500, f depends on NRe. The "if" statement in Mathcad provides an easy way to handle this. The syntax of this statement is: if (condition, true, false). In this example, f equals 0.44 if vD/ is greater than 500. Initial guesses: v 1 ft sec := f 0.44 := Given Momentum balance: f 4 3 g D v 2 ρ s ρ - ρ f if vD ν 500 < 18.5 vD ν 3 5 , 0.44 , Friction factor: The solution to this given - find statement will be a two component vector, v and f. To form this vector, go to the Vectors and Matrices Palette, choose the symbol for a matrix, enter the number of rows and columns (2 and 1 in this case), and hit "Create". You will then see a general 2 component vector. Define the elements as the unknowns and define this vector as the "find": v f Find v f , ( ) := v 13.501 ft sec = f 0.655 = The value of the Reynolds number is: NRe vD ν := NRe 262.094 = There is often an easier way to set a problem up, and so make the solution easier. Very often, with multiple unknowns in a given - solve block, the solution is very sensitive to the initial guesses. Therefore, it is always easier to have only one unknown. In the present problem, we are interested in the velocity, and not really the Reynolds number and friction factor. We could, therefore, define these two functions outside of the given - find statement. NRe vel ( ) vel D ν := f vel ( ) if NRe vel ( ) 500 < 18.5 NRe vel ( ) ( ) 3 5 , 0.44 , := To avoid any possible confusion between these 2 examples, we'll use a different symbol for velocity. Initial guess for vel: vel 1 ft sec := Given f vel ( ) 4 3 g D vel 2 ρ s ρ - ρ vel Find vel ( ) := vel 13.501 ft sec = If we wanted the Reynolds number and the friction factor, then we could evaluate them: NRe vel ( ) 262.094 = f vel ( ) 0.655 = While it is convenient to have only one unknown in a given - find block, it is not always possible to do so. However, the fewer the unknowns, the easier it is to find a solution. The number can usually be reduced to a number smaller than you might think. It just requires a little "up-front" thought about the nature of the problem and what are the knowns. Consider this complex vapor - liquid equilibrium problem. A solution of 2-propanol and water is at a pressure of 101.33 bar and the mole fraction of propanol in the vapor phase is 0.4. We want to find the temperature and the mole fraction of propanol in the liquid. The liquid phase is non-ideal, and we have to use activity coefficients for the liquid. These depend on temperature and liquid composition. We'll assume the Wilson model can be employed. First, introduce some constants for this system of components: a 12 437.98 cal mole K := a 21 1238 cal mole K := V 1 76.92 := V 2 18.07 := Two temperature-dependent functions which appear in the activity coefficient model are: Λ12 T ( ) V 2 V 1 exp a 12 RT - := Λ21 T ( ) V 1 V 2 exp a 21 RT - := We also have the Antoine equations for the vapor pressures: P1sat T ( ) exp 16.678 3640.2 T 53.54 - - := P2sat T ( ) exp 16.2887 3816.44 T 46.13 - - := The mole fraction of component 2 is: x2 x1 ( ) 1 x1 - := The activity coefficients are: γ1Tx1 , ( ) exp ln x1 x2 x1 ( ) Λ12 T ( ) + ( ) - x2 x1 ( ) Λ12 T ( ) x1 x2 x1 ( ) Λ12 T ( ) + Λ21 T ( ) x2 x1 ( ) x1 Λ21 T ( ) + - + := γ2Tx1 , ( ) exp ln x2 x1 ( ) x1 Λ21 T ( ) + ( ) - x1 Λ12 T ( ) x1 x2 x1 ( ) Λ12 T ( ) + Λ21 T ( ) x2 x1 ( ) x1 Λ21 T ( ) + - - := The total pressure is: PTx1 , ( ) x1 γ 1Tx1 , ( ) P1sat T ( ) x2 x1 ( ) γ 2Tx1 , ( ) P2sat T ( ) + := Notice that this differs from the case of Raoult's law becuase of the presence of the activity coefficients, 1 and The vapor phase composition is given by: y1 T x1 , ( ) x1 γ1Tx1 , ( ) P1sat T ( ) PTx1 , ( ) := There are a large number of unknowns in this problem: the vapor pressures, the activity coefficients, the liquid phase compositions, the temperature. However, there are only two independent variables, x1 and T. Once these are known, all the others can be found. The given - find statement is just: Initial guess: T 200 := x1 0.5 := Given PTx1 , ( ) 101.33 y1 T x1 , ( ) 0.4 T x1 Find T x1 , ( ) := T 360.617 = x1 0.064 = If desired, we could find all the other dependent variables: P1sat T ( ) 124.417 = P2sat T ( ) 63.641 = γ 1Tx1 , ( ) 5.099 = γ 2Tx1 , ( ) 1.021 = Mathcad cannot, apparently, handle functions which have general subscripts. If, for example, you want to evaluate the vapor pressure of a set of components as a function of temperature (as in the previous problem), you might first introduce the Antoine constants. To place a subscript on a variable, use the left bracket [ . After you enter the first number, type a comma. Mathcad will then start to form a vector. i 1 2 .. := kPa 10 3 Pa A i 13.8594 14.0045 := B1 i 2773.78 3279.47 := C1 i 220.07 213.2 := You can either enter the constants times K, or redefine the values as done here. B B1 K := C C1 K := You would like to define the vapor pressure with an equation like: P sat T ( ) i exp A i B i T C i + - kPa You can define variables with subscripts as we did here by typing "P." (P with a period). This is sometimes convenient. There is a fundamental difference between the subscript formed in this way and that formed with the left bracket. The former ("sat" above) is simply part of the name of a variable. The latter ("i") makes the variable a vector with components i. In addition, the variable i must be defined separately. Returning to the problem at hand, note that if you define the vapor pressure in this way (using the := sign to define the function) Mathcad tells you this is not a proper name. One way around this difficulty is to employ the "vectorize" option: treat P sat as a vector make the right side of the equation a vector. Notice that we did this sort of thing above when we said C:=C1*K. Instead of redefining each of the components of C, we redefined the whole vector. Write the Antoine equation without subscripts, but place an overbar (from the Vectors and Matrices Palette) over the entire right side: P sat T ( ) exp A B C T + - kPa := Without the vectorize operation, Mathcad will want to divide one vector by another: an illegal array operation. There is one problem with this approach. Choose a temperature and evaluate the vapor pressures (in this equation, temperature must be in Celsius, but Mathcad, unfortunately, cannot go from C to K. If you want to employ Celsius degrees, you must also use the symbol K and be careful): P sat 90 K ( ) 1 136.148 24.247 kPa How did we wind up with 3 pressures when we only defined 2 components? Mathcad always assumes that the first element of a vector is numbered 0, the next 1, and so forth. Since we did not define the constants A 0 , etc., Mathcad took them to be zero. Hence the first vapor pressure is exp(0) or 1. We can get around this problem by redefining the first element using the ORIGIN command. (Case is important here.) ORIGIN 1 Now calculate the pressures: P sat 90 K ( ) 136.148 24.247 kPa = The first element of the vector is now numbered 1, and we have only 2 components. BE CAREFUL WHEN YOU USE THIS "ORIGIN" STATEMENT! As a final class of examples, consider curve fitting. The first example is straightforward. The following set of data gives dimensionless temperature as a function of dimensionless distance in a fiber spinning experiment. Each set forms a vector. One way to represent the vector is to use subscripts to denote each element of its elements. Since, by default, the numbering of elements in a Mathcad vector begins with 0, we will return to that default setting: Here we used the regular definition :=, rather than the global, so that this numbering system takes effect from here on. ORIGIN 0 := i 0 8 .. := Mathcad assumes the increment is one unless you tell it otherwise. Next, type in the data: θ i 1 0.91 0.78 0.72 0.7 0.65 0.5 0.31 0.2 := ζ i 0 0.9 1.2 1.8 2.3 2.7 5.5 11 15.3 := We expect the temperature to follow the equation: θ exp 0.1625 - Nu ζ ( ) where Nu is the Nusselt number, which we must find. The constant, 0.1625, comes about from the way and Nu are defined. We can take the natural log of this equation to put it in linear form: ln θ () αζ where is the slope (-0.1625 Nu). Finding the slope of a straight line is easy in Mathcad. First, we need to define a new dependent variable as the log of : Y i ln θ i ( 29 := The slope of a straight line through these points is found by using the "slope(x,y)" function, where x is the vector of independent variables : α slope ζ Y , ( ) := α 0.1 - = Therefore, the Nu is: Nu α 0.1625 - := Nu 0.616 = To verify, we can plot the data (, ) and the prediction, p: θp i exp 0.1625 - Nu ζ i ( 29 := After you create the x-y plot, you can customize it to display the experimental data as points, rather than a line. Double click on the graph, go to Format and Traces. For the first trace (in this case the measured temperatures) choose a symbol, and "points" for Type. To plot 2 variables on the same axis, put a box around the first one, here i , then type a comma. A new place holder will appear on the axis. Unless you tell it otherwise, Mathcad will plot each of the dependent variables against the same independent variable. In this case, the fit is not all that good, perhaps because the straight line fit does not have an intercept of zero. We can check that with the "intercept" function: intercept ζ Y , ( ) 0.095 - = exp intercept ζ Y , ( ) ( ) 0.909 = Mathcad 2000 contains a series of curve fitting routines which can be found with the function key. One is simply "line." If we employ the line function here we get: line ζ Y , ( ) 0.095 - 0.1 - = The intercept and the slope are given directly. The predicted best fit using a straight line model with non-unity intercept is: θp2 i exp intercept ζ Y , ( ) ( ) exp 0.1625 - Nu ζ i ( 29 := 0 5 10 15 20 0 0.2 0.4 0.6 0.8 1 θ i θp i θp2 i ζ i The fit is improved with θp2 i , but now the initial temperature is not 1. You might also try changing the y-axis to a log scale using X-Y Plot, Format, X-Y Axes. There is one more fit we can try, this time forcing the intercept to be unity. Define a new function, SSE(Nu), as the sum of the square error between the measured and predicted temperature: SSE Nu ( ) i θ i exp 0.1625 - Nu ζ i ( 29 - ( 29 2 := We want to find the value of Nu which makes this function a minimum. Notice it will not be zero unless the fit were exact. To find this minimum, we can use a procedure related to the Given - Find method: Given Notice we already have a guess for Nu defined above. Instead of "Find," we use "Minerr". The value of Nu which gives the minimum error in the equation is: SSE Nu ( ) 0 Nu3 Minerr Nu ( ) := Nu3 0.797 = This is quite different from the previous value. We can define another predicted temperature: θ3p i exp 0.1625 - Nu3 ζ i ( 29 := And plot this function to compare with the data and the other two predictions: 0 5 10 15 20 0.1 1 θ i θp i θp2 i θ3p i ζ i Mathcad 2000 also provides an exponential fit function. The form is a*(e^b*x)+c. There are a few problems with this, not the least of which is that we do not want a fit of this form. (It has the extra constant, c. There does not appear to be any way to use this function without the constant. Also, the results seem very sensitive to the initial guesses. We will not pursue this method. For those who prefer a more rigorous way of finding the best value of Nu, we can find the value which forces the derivative of the SSE equation with respect to the Nusselt number to be to zero: Given i Nu θ i exp 0.1625 - Nu ζ i ( 29 - ( 29 2 d d 0 Nu4 Find Nu ( ) := Nu4 0.797 = This is quite close to what we just found using Minerr. In general, the procedure using the derivative seems to give better results (in the sense of a smaller value of the sum of the square errors) than Minerr procedure. However, the derivative method is more sensitive to the initial guess. There are two aditional Mathcad functions which can be used for data fitting, linfit and genfit. Suppose we have the following set of x-y data. We want to fit these data to an equation of the form: y a bx + cx 2 + We could use either of the methods discussed above. In addition, we could use the linfit function. This is useful whenever we can write the dependent variable as series of any functions, not just powers as in this example. First, define the variables: i 0 11 .. := x i 0.045 0.094 0.183 0.291 0.398 0.507 0.546 0.595 0.721 0.814 0.897 0.957 := y i 1.121 1.107 1.048 1.021 1.01 1.012 1.014 1.018 1.04 1.064 1.103 1.148 := Then define a vector, called F here, which gives the various functions which appear in the expression for the dependentvariable. In the present case: F x () 1 x x 2 := The linfit function returns a vector, called S here, which contains the coefficients used to create a linear combination of the functions in F which give the best fit to the data in x and y: S linfit x y , F , ( ) := S 1.146 0.579 - 0.6 = The best fit to the data is then easily written in matrix notation: y best x () Fx ()S := Another way of representing this series is: F2 x () x 1 x - x1 x - ( ) := S2 linfit x y , F2 , ( ) := S2 1.167 1.146 0.6 - = You can easily verify the two results are the same. Very often, we cannot write the dependent variable as a linear combination of functions. Suppose we wanted to fit the above y values to an equation of the form: yx () ln x 1 x - ( )u 0 + - 1 x - ln 1 x - xu 1 + ( 29 x - and want to find the best values of u 0 and u 1 . In this case, we can use the genfit function. We first define a vector G(x,u). The first element is the desired function; the second, the derivative of the function with respect to the first constant; and the third, the derivative with respect to the second: Then we give a vector of guesses for the constants: G xu , ( ) ln x 1 x - ( )u 0 + - 1 x - ln 1 x - xu 1 + ( 29 x - 1 - x 1 x - ( )u 0 + 1 - 1 x - xu 1 + ( 29 := g 1 1 := The genfit function returns the best values of the constants: u genfit x y , g , G , ( ) := u 0.55 0.527 = The best fit is then: y best2 x () ln x 1 x - ( )u 0 + - 1 x - ln 1 x - xu 1 + ( 29 x - := z 0 0.05 , 1 .. := 0 0.2 0.4 0.6 0.8 1 0.4 0.6 0.8 1 1.2 y i y best z () y best2 z () x i z , z , Clearly, the second function does not give a very good fit in this case. The polynomial is clearly superior.

Upload: paragbhole

Post on 21-May-2017

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Mathcad Tutorial 1

INTRODUCTION TO MATHCAD

Mathcad is a "numerical scratchpad" which allows you to solve complex systems of equations, find roots of polynomials and plot results. It can evaluate integrals and derivatives, both numerically and symbolically. Units are easily incorporated, and numerical answers cannot be found if the units are inconsistent. Mathcad is a powerful tool, but easy to use. These notes are a brief introduction. They are meant to be used while working in Mathcad.

If you enter Mathcad, you will notice there are several menus that appear on the top line (File, Edit, Text, etc.). There is a drop-down "Math" palette containing icons for calculator, graphs, matrices, etc. If you move the cursor to an icon, there is a brief message explaining it. If you click on an icon, a palette with various operations appears. Click on the calculator palette and select the symbol. Type = and you get π 3.142=

Some symbols, such as , g (acceleration of gravity) and R (degrees Rankine), have a specific meaning unless you redefine them.

Now, choose the nth root symbol and put in some numbers, such as:

6458247 8.78=

To enter a number, just click on the appropriate placeholder (the black box).

These examples show how to use the "=" sign, which means evaluate or calculate. Mathcad can be used as a calculator in this way, although it is a bit expensive and not particularly portable (unless you have a lap top). If you choose the Evaluation and Boolean icon, you will see there are several kinds of equals, in addition to inequalities. The ":=" sign means "is defined by." Type x followed by this sign, and assign a number such as:

x 4.75:=

In addition to using the Evaluation palette, this ":=" sign can be inserted simply by typing a colon (:). Once x is defined, we could then use it in an expression such as:

ln x( ) x2+

To get the natural log, simply type "ln". There are a number of built in functions available as you can see by clicking of the "f(x)" icon and scrolling through the list. It includes error functions and Bessel functions, in addition to the more mundane ones. Each comes with a brief explanation. There are also functions for numerical integration using the Runge-Kutta method. We will use the simple ones here.

Return to the present problem. If you now type a regular equals sign, from the palette or the keyboard, you get:

ln x( ) x2+ 24.121=

You could also define a dependent variable, f, as this function of x, for use in later calculations:

f ln x( ) x2+:=

If you want the numerical value of f, simply type f 24.121=

This type of function may be called explicit: you can write f equals some expression which contains only the independent variable, x. Suppose, however, we turned the equation around and wanted to find the x for which:

24.121 ln x( ) x2+

This is an example of an implicit equation. You cannot rearrange this to get it into the form x = (something containing no x). You can, however, solve this equation in Mathcad. This is accomplished in a "given - find" block. Mathcad does this numerically and needs initial guesses for the unknowns, in this case x. Enter a guess:x 1:=

Then type "given" with or without a capital G. (In most cases, Mathcad is not case sensitive.)

Given

Next, enter the equation. In these blocks you must use a third type of "equals" which means "is constrained by" and is used in these "given-find" blocks. (In the Evaluation palette, this is called a "Boolean equals.") This is the bold faced "=" from the evaluation palette (or CTRL = from the keyboard).

24.121 ln x( ) x2+

To get an answer, type "find(x)" followed by an =

Find x( ) 4.75=

If you next type x=, you will not see the solution (4.75), but the initial guess. This is because you have not redefined the value of x. If you want the value of x to be the solution, you can proceed as follows:

x 1:= (Initial guess)Given

24.121 ln x( ) x2+

x Find x( ):=

Now x has been replaced by the solution of the equation. To see the value, just type:

x 4.75=

The major restriction on the given-find statements is that you cannot use general subscripted variables (i.e., i or j) in them. You can, however, solve some interesting problems. In the analysis of flow of a power law fluid in an annulus, you need to find the value of in the following equation:

κ

λ

ξλ2

ξξ−

1

n

⌠⌡

d

λ

1

ξξλ2

ξ−

1

n

⌠⌡

d

where is the ratio of the inner to the outer radius, n is the power law index and is the radial coordinate. In order to get the integral sign, go to the calculus palette and choose the definite integral. We can solve this equation using "given - find" as above. First define the and n and guess a value for :

κ 0.4:= n 0.3:= λ 1:=

Given

κ

λ

ξλ2

ξξ−

1

n

⌠⌡

d

λ

1

ξξλ2

ξ−

1

n

⌠⌡

d

Find λ( ) 0.657=

We can also include units in Mathcad calculations. Many units are already defined in Mathcad; others can be assigned. If you click on the measuring cup, you can see a list of the units available. You can change the default system of units by choosing Math then Units then Change System of Units. However, whatever system of units you are in, you can also always go from one to another. For example, take the acceleration of gravity, g. Type g= to see its value in SI (presuming the default system is set on MKS):

g 9.807m s2−⋅=

You notice a "placeholder" or a black square after the units. Click on this value and insert another set of units:

g 5.004 109× in hr

2−⋅⋅=

As an example of a problem with units, suppose we want investigate the P-V-T behavior of methane using the Redlich-Kwong equation of state. First, we enter the critical values for methane:

Tc 190.6 K⋅:= Pc 45.99 bar⋅:=

When you do this, the pressure units, bar, are highlighted. Mathcad doesn't recognize that unit. We can define this as a new unit using the third type of equal sign:

bar 105Pa⋅≡

Here is yet another type of equals. You can get this symbol from the Evaluation Palette, or by typing a tilde (~). Once you define the units bar in this fashion, the highlight disappears. Notice, however, what happens if you use the ":=" to define the bar unit. In this case, the bar in the Pc equation still remains highlighted. This "triple equals" is a "global assignment;" it's good throughout the Mathcad document. The other equals (:=) means the definition is good from this point on, and can be re-defined.

Now, introduce the gas constant and the two constants associated with the Redlich-Kwong equation:

R 1.987cal

mole K⋅⋅:=

b 0.08664 R⋅Tc

Pc⋅:=

a 0.42748 R2⋅Tc

5

2

Pc⋅:=

We can define the moles in terms of grams for methane:(See what happens later if you do not use this global assignment.) mole 16 gm⋅≡

Suppose we were given a volume and a temperature and wanted to find the pressure. The specified temperature and volume are:

T 165 K⋅:= v 53.29cm

3

mole⋅:=

According to the Redlich - Kwong equation, pressure is given by the expression:

PR T⋅v b−

a

v v b+( )⋅ T

1

2⋅

−:=

Therefore: P 19.12 atm⋅=

To find the volume at 165K and 19.39 bar, we have to solve an implicit equation using Given - Find:

P 19.39 bar⋅:= v 1.0m3

mole⋅:= (Initial guess)

Given

PR T⋅v b−

a

v v b+( )⋅ T

1

2⋅

Find v( ) 0.032m3

kg⋅=

The default units of kg and m can be changed to moles and cm, or anything else you desire.

In this problem, as in many others, the solution is sensitive to the initial guess. Try using a different value for the guess. Sometimes, the numerical procedure will not converge, or will converge on a different root.

One of the other Mathcad features is root finding. Given a polynomial, we can find all the roots, real and imaginary, using the "polyroots" function. To use this feature in this problem, first, write the equation in polynomial form. The Redlich - Kwong equation is cubic in the volume (it is an example of a "cubic equation of state"):

V3 R T⋅

PV2⋅− b

2 b R⋅ T⋅P

+a

P T0.5⋅

V⋅−

a b⋅

P T0.5⋅

− 0

Next, form a vector, call it V, of the coefficients in this equation. The first element is the

coefficient of V0, the next, V

1, etc:

Notice that the elements of this vector, as with any vector, must have the same units. To use the root finding function, however, the elements must all be dimensionless. The three roots are then found simply by using the "polyroots" function:V

ab

P T.5⋅

⋅kg3

m9

b2

b R⋅T

P⋅+

a

P T.5⋅

−kg2

m6

RT

P⋅

kg

m3

1

:=

polyroots V( )

3.33 103−×

8.845 103−×

0.032

=

To decide on which root means what, we have to know a little about the physics of the situation.

The vapor volume is the maximum value, so define:Vg max polyroots V( )( )

m3

kg⋅:=

Since we took out the units in the vector V, we have to add them back again. We also have to put back the same ones we took out, that is, if we multiplied the

elements of V by kg/m3, we must use m

3/kga s the units for V.

Vg 0.032m3kg

1−⋅=

The liquid volume is the minimum:Vf min polyroots V( )( )

m3

kg⋅:=

Vf 3.33 103−× m

3kg

1−⋅=

The middle value has no physical significance. (It might be interesting to some, at least, to note that the actual vapor volume of methane at these conditions is 0.032, as calculated. The liquid

volume is 3.095 10-3

.)

Try using a different T and P, say 300K and 1 atm, and see what the roots are.Now, let's plot the compressibility factor for methane as a function of pressure for a temperature of 300K. We need to find v for a number of values of P. However, only one given-find statement is needed. We can proceed as before:

T 300 K⋅:= (We already have an initial guess for v above.) MPa 106Pa⋅≡

Given

PR T⋅v b−

a

v v b+( )⋅ T

1

2⋅

v P( ) Find v( ):=

The v(P) means that v is a function of P; for each value of P, a different v is found. If we specify a particular value of P we get a numerical answer:

v 4 MPa⋅( ) 0.036m3kg

1−⋅=

Now, introduce a range of pressures. This could have been done before the given - find statement if desired:

P 0.1 MPa⋅ 0.2 MPa⋅, 10 MPa⋅..:=

This means P takes on the values 0.1, 0.2, 0.3, ... all the way to 10 MPa. The two dots appear when you type a semicolon (;). Note that units must be included.

Define the compressibility factor:

z P( )P v P( )⋅R T⋅

:=

Notice that z is also a function of P. Now create an x-y plot using the Graphics menu, the Graphing palette, or simply SHFT @. Put the cursor on the placeholders for the x and y axes and put in P and z(P), respectively:

0 2 106× 4 10

6× 6 106× 8 10

6× 1 107×

0.85

0.9

0.95

1

z P( )

P

Notice the values of P are much larger than you defined. That's because this P is in Pa (the default units for pressure), not MPa. To change the scale, you can plot P/MPa rather than P (or P/atm, P/psi, or anything you want). If you double click on the graph, or if you choose "Format" with the right mouse buton, you can customize your graphs. By clicking the mouse in the region near the graph (or anything else), you can get a line enclosing it. You can move the graph, text or equation anywhere on the page using the cursor. When the cursor is at the boundary of the line, an arrow appears. This can be used to re-size the graph.

As an example of a somewhat more complicated problem, consider the velocity of a freely falling drop of water in air.

The diameter of the drop is: D 1.0 mm⋅:=

Density of the water: ρs 62.4lb

ft3

⋅:=

Density and kinematic viscosity of air: ρ 0.0735lb

ft3

⋅:= ν 0.169 103−⋅ft2

sec⋅:=

The drop velocity v depends on the friction factor f, which also depends on the Reynolds number, NRe, vD/. We can write this as 2 equations with 2 unknowns, v and f. In addition, if we are above the creeping flow regime, f is given by one of two expressions, depending on the value of the Reynolds number. If NRe is greater than 500, f is 0.44. If NRe is less than 500, f depends on NRe. The "if" statement in Mathcad provides an easy way to handle this. The syntax of this statement is: if (condition, true, false). In this example, f equals 0.44 if vD/ is greater than 500.

Initial guesses:v 1

ft

sec⋅:= f 0.44:=

Given

Momentum balance: f4

3g⋅

D

v2

⋅ρs ρ−

ρ

f ifv D⋅ν

500<18.5

v D⋅ν

3

5

, 0.44,

Friction factor:

The solution to this given - find statement will be a two component vector, v and f. To form this vector, go to the Vectors and Matrices Palette, choose the symbol for a matrix, enter the number of rows and columns (2 and 1 in this case), and hit "Create". You will then see a general 2 component vector. Define the elements as the unknowns and define this vector as the "find":

v

f

Find v f, ( ):= v 13.501ft

sec⋅=

f 0.655=

The value of the Reynolds number is: NRev D⋅ν

:= NRe 262.094=

There is often an easier way to set a problem up, and so make the solution easier. Very often, with multiple unknowns in a given - solve block, the solution is very sensitive to the initial guesses. Therefore, it is always easier to have only one unknown. In the present problem, we are interested in the velocity, and not really the Reynolds number and friction factor. We could, therefore, define these two functions outside of the given - find statement.

NRe vel( ) velD

ν⋅:= f vel( ) if NRe vel( ) 500<

18.5

NRe vel( )( )

3

5

, 0.44,

:=

To avoid any possible confusion between these 2 examples, we'll use a different symbol for velocity.

Initial guess for vel: vel 1ft

sec⋅:=

Given

f vel( )4

3g⋅

D

vel2

⋅ρs ρ−

ρ

vel Find vel( ):= vel 13.501ft

sec⋅=

If we wanted the Reynolds number and the friction factor, then wecould evaluate them:

NRe vel( ) 262.094=

f vel( ) 0.655=

While it is convenient to have only one unknown in a given - find block, it is not always possible to do so. However, the fewer the unknowns, the easier it is to find a solution. The number can usually be reduced to a number smaller than you might think. It just requires a little "up-front" thought about the nature of the problem and what are the knowns. Consider this complex vapor - liquid equilibrium problem. A solution of 2-propanol and water is at a pressure of 101.33 bar and the mole fraction of propanol in the vapor phase is 0.4. We want to find the temperature and the mole fraction of propanol in the liquid. The liquid phase is non-ideal, and we have to use activity coefficients for the liquid. These depend on temperature and liquid composition. We'll assume the Wilson model can be employed.

First, introduce some constants for this system of components:

a12

437.98cal

mole K⋅⋅:= a

211238

cal

mole K⋅⋅:= V

176.92:= V

218.07:=

Two temperature-dependent functions which appear in the activity coefficient model are:

Λ12 T( )V2

V1

expa12

R T⋅−

⋅:= Λ21 T( )V1

V2

expa21

R T⋅−

⋅:=

We also have the Antoine equations for the vapor pressures:

P1sat T( ) exp 16.6783640.2

T 53.54−−

:= P2sat T( ) exp 16.2887

3816.44

T 46.13−−

:=

The mole fraction of component 2 is:

x2 x1( ) 1 x1−:=

The activity coefficients are:

γ1 T x1, ( ) exp ln x1 x2 x1( ) Λ12 T( )⋅+( )− x2 x1( )Λ12 T( )

x1 x2 x1( ) Λ12 T( )⋅+Λ21 T( )

x2 x1( ) x1 Λ21 T( )⋅+−

⋅+

:=

γ2 T x1, ( ) exp ln x2 x1( ) x1 Λ21 T( )⋅+( )− x1Λ12 T( )

x1 x2 x1( ) Λ12 T( )⋅+Λ21 T( )

x2 x1( ) x1 Λ21 T( )⋅+−

⋅−

:=

The total pressure is: P T x1, ( ) x1 γ1 T x1, ( )⋅ P1sat T( )⋅ x2 x1( ) γ2 T x1, ( )⋅ P2sat T( )⋅+:=

Notice that this differs from the case of Raoult's law becuase of the presence of the activity coefficients, 1 and

The vapor phase composition is given by:y1 T x1, ( )

x1 γ1 T x1, ( )⋅ P1sat T( )⋅P T x1, ( )

:=

There are a large number of unknowns in this problem: the vapor pressures, the activity coefficients, the liquid phase compositions, the temperature. However, there are only two independent variables, x1 and T. Once these are known, all the others can be found. The given - find statement is just:

Initial guess: T 200:= x1 0.5:=

GivenP T x1, ( ) 101.33

y1 T x1, ( ) 0.4

T

x1

Find T x1, ( ):=T 360.617= x1 0.064=

If desired, we could find all the other dependent variables:

P1sat T( ) 124.417= P2sat T( ) 63.641= γ1 T x1, ( ) 5.099= γ2 T x1, ( ) 1.021=

Mathcad cannot, apparently, handle functions which have general subscripts. If, for example, you want to evaluate the vapor pressure of a set of components as a function of temperature (as in the previous problem), you might first introduce the Antoine constants. To place a subscript on a variable, use the left bracket [ . After you enter the first number, type a comma. Mathcad will then start to form a vector.

i 1 2..:= kPa 103Pa⋅≡

Ai

13.859414.0045

:= B1i

2773.783279.47

:= C1i

220.07213.2

:=You can either enter the constants times K, or redefine the values as done here.

B B1 K⋅:=

C C1 K⋅:=

You would like to define the vapor pressure with an equation like:

Psat T( )i

exp Ai

Bi

T Ci

+−

kPa⋅

You can define variables with subscripts as we did here by typing "P." (P with a period). This is sometimes convenient. There is a fundamental difference between the subscript formed in this way and that formed with the left bracket. The former ("sat" above) is simply part of the name of a variable. The latter ("i") makes the variable a vector with components i. In addition, the variable i must be defined separately.

Returning to the problem at hand, note that if you define the vapor pressure in this way (using the := sign to define the function) Mathcad tells you this is not a proper name. One way around this difficulty is to employ the "vectorize" option: treat P

sat as a vector make the right side of the

equation a vector. Notice that we did this sort of thing above when we said C:=C1*K. Instead of redefining each of the components of C, we redefined the whole vector.

Write the Antoine equation without subscripts, but place an overbar (from the Vectors and Matrices Palette) over the entire right side:

Psat T( ) exp AB

C T+−

kPa⋅

:=

Without the vectorize operation, Mathcad will want to divide one vector by another: an illegal array operation.

There is one problem with this approach. Choose a temperature and evaluate the vapor pressures (in this equation, temperature must be in Celsius, but Mathcad, unfortunately, cannot go from C to K. If you want to employ Celsius degrees, you must also use the symbol K and be careful):

Psat 90 K⋅( )

1

136.148

24.247

kPa⋅

How did we wind up with 3 pressures when we only defined 2 components? Mathcad always assumes that the first element of a vector is numbered 0, the next 1, and so forth. Since we did not define the constants A

0, etc., Mathcad took them to be zero. Hence the first vapor pressure is

exp(0) or 1. We can get around this problem by redefining the first element using the ORIGIN command. (Case is important here.)ORIGIN 1≡ Now calculate the pressures:

Psat 90 K⋅( )136.148

24.247

kPa⋅=

The first element of the vector is now numbered 1, and we have only 2 components.

BE CAREFUL WHEN YOU USE THIS "ORIGIN" STATEMENT!

As a final class of examples, consider curve fitting. The first example is straightforward. The following set of data gives dimensionless temperature as a function of dimensionless distance in a fiber spinning experiment. Each set forms a vector. One way to represent the vector is to use subscripts to denote each element of its elements. Since, by default, the numbering of elements in a Mathcad vector begins with 0, we will return to that default setting:

Here we used the regular definition :=, rather than the global, so that this numbering system takes effect from here on.ORIGIN 0:=

i 0 8..:= Mathcad assumes the increment is one unless you tell it otherwise.

Next, type in the data:

θi

10.910.780.720.70.650.5

0.310.2

:= ζi

00.91.21.82.32.75.5

1115.3

:=

We expect the temperature to follow the equation:

θ exp 0.1625− Nu⋅ ζ⋅( )

where Nu is the Nusselt number, which we must find. The constant, 0.1625, comes about from the way and Nu are defined.

We can take the natural log of this equation to put it in linear form:

ln θ( ) α ζ⋅

where is the slope (-0.1625 Nu). Finding the slope of a straight line is easy in Mathcad. First, we need to define a new dependent variable as the log of :

Yi

ln θi( ):=

The slope of a straight line through these points is found by using the "slope(x,y)" function, where x is the vector of independent variables :

α slope ζ Y, ( ):= α 0.1−=

Therefore, the Nu is: Nuα

0.1625−:= Nu 0.616=

To verify, we can plot the data (, ) and the prediction, p: θpi

exp 0.1625− Nu⋅ ζi

⋅( ):=

After you create the x-y plot, you can customize it to display the experimental data as points, rather than a line. Double click on the graph, go to Format and Traces. For the first trace (in this case the measured temperatures) choose a symbol, and "points" for Type. To plot 2 variables on the same axis, put a box around the first one, here

i, then type a comma. A new

place holder will appear on the axis. Unless you tell it otherwise, Mathcad will plot each of the dependent variables against the same independent variable.

In this case, the fit is not all that good, perhaps because the straight line fit does not have an intercept of zero. We can check that with the "intercept" function:

intercept ζ Y, ( ) 0.095−= exp intercept ζ Y, ( )( ) 0.909=

Mathcad 2000 contains a series of curve fitting routines which can be found with the function key. One is simply "line." If we employ the line function here we get:

line ζ Y, ( )0.095−

0.1−

= The intercept and the slope are given directly.

The predicted best fit using a straight line model with non-unity intercept is:

θp2i

exp intercept ζ Y, ( )( ) exp 0.1625− Nu⋅ ζi

⋅( )⋅:=

0 5 10 15 200

0.2

0.4

0.6

0.8

1

θi

θp i

θp2 i

ζi

The fit is improved with θp2

i, but now the initial temperature is not 1. You might also try changing the y-axis to a log scale using X-Y Plot, Format, X-Y Axes.

There is one more fit we can try, this time forcing the intercept to be unity.

Define a new function, SSE(Nu), as the sum of the square error between the measured and predicted temperature:

SSE Nu( )

i

θi

exp 0.1625− Nu⋅ ζi

⋅( )−( ) 2∑:=

We want to find the value of Nu which makes this function a minimum. Notice it will not be zero unless the fit were exact. To find this minimum, we can use a procedure related to the Given - Find method: Given

Notice we already have a guess for Nu defined above.

Instead of "Find," we use "Minerr". The value of Nu which gives the minimum error in the equation is:

SSE Nu( ) 0

Nu3 Minerr Nu( ):=

Nu3 0.797=

This is quite different from the previous value. We can define another predicted temperature:

θ3pi

exp 0.1625− Nu3⋅ ζi

⋅( ):=

And plot this function to compare with the data and the other two predictions:

0 5 10 15 200.1

1

θi

θp i

θp2 i

θ3p i

ζi

Mathcad 2000 also provides an exponential fit function. The form is a*(e^b*x)+c. There are a few problems with this, not the least of which is that we do not want a fit of this form. (It has the extra constant, c. There does not appear to be any way to use this function without the constant. Also, the results seem very sensitive to the initial guesses. We will not pursue this method.

For those who prefer a more rigorous way of finding the best value of Nu, we can find the value which forces the derivative of the SSE equation with respect to the Nusselt number to be to zero:

Given

iNu

θi

exp 0.1625− Nu⋅ ζi

⋅( )−( ) 2

d

d∑ 0

Nu4 Find Nu( ):= Nu4 0.797=

This is quite close to what we just found using Minerr. In general, the procedure using the derivative seems to give better results (in the sense of a smaller value of the sum of the square errors) than Minerr procedure. However, the derivative method is more sensitive to the initial guess.

There are two aditional Mathcad functions which can be used for data fitting, linfit and genfit.

Suppose we have the following set of x-y data. We want to fit these data to an equation of the form:

y a b x⋅+ c x2⋅+

We could use either of the methods discussed above. In addition, we could use the linfit function. This is useful whenever we can write the dependent variable as series of any functions, not just powers as in this example.

First, define the variables:

i 0 11..:=

xi

0.045

0.094

0.183

0.291

0.398

0.507

0.546

0.595

0.721

0.814

0.897

0.957

:=yi

1.121

1.107

1.048

1.021

1.01

1.0121.014

1.018

1.04

1.064

1.1031.148

:=

Then define a vector, called F here, which gives the various functions which appear in the expression for the dependentvariable. In the present case:

F x( )

1

x

x2

:=

The linfit function returns a vector, called S here, which contains the coefficients used to create a linear combination of the functions in F which give the best fit to the data in x and y:

S linfit x y, F, ( ):=S

1.146

0.579−

0.6

=

The best fit to the data is then easily written in matrix notation: ybest x( ) F x( ) S⋅:=

Another way of representing this series is:F2 x( )

x

1 x−

x 1 x−( )⋅

:=

S2 linfit x y, F2, ( ):= S2

1.167

1.146

0.6−

= You can easily verify the two results are the same.

Very often, we cannot write the dependent variable as a linear combination of functions. Suppose we wanted to fit the above y values to an equation of the form:

y x( )ln x 1 x−( ) u

0⋅+ −

1 x−

ln 1 x− x u1

⋅+( )x

and want to find the best values of u0 and u

1. In this case, we can use the genfit function. We first

define a vector G(x,u). The first element is the desired function; the second, the derivative of the function with respect to the first constant; and the third, the derivative with respect to the second:

Then we give a vector of guesses for the constants:

G x u, ( )

ln x 1 x−( ) u0

⋅+ −

1 x−

ln 1 x− x u1

⋅+( )x

1−x 1 x−( ) u

0⋅+

1−1 x− x u

1⋅+( )

:=

g1

1

:=

The genfit function returns the best values of the constants:

u genfit x y, g, G, ( ):=u

0.55

0.527

=

The best fit is then:

ybest2 x( )ln x 1 x−( ) u

0⋅+ −

1 x−

ln 1 x− x u1

⋅+( )x

−:=

z 0 0.05, 1..:=

0 0.2 0.4 0.6 0.8 10.4

0.6

0.8

1

1.2

y i

ybest z( )

ybest2 z( )

xi z, z,

Clearly, the second function does not give a very good fit in this case. The polynomial is clearly superior.