Mathcad Lecture #3 In-Class Worksheet Functions - Solutions: Practice
Mathcad Lecture #3 In-Class Worksheet Functions - Solutions: Practice
Functions - Solutions
At the end of this lecture, you should be able to:
find and use intrinsic Mathcad Functions for fundamental, trigonometric, and if statements
construct compound if functions using AND, OR, XOR, NOT operators
create user-defined functions
construct use-defined functions with appropriate units
decide when a user-defined function is needed
Practice
1. Calculate the following: floor of π, ceil of π, the remainder of dividing π by 3.
round ( π 5 ) 3.14159 Note: you must change the display precision
3. Let x = 0.9 and calculate the following: ex, log10 (x), log2 (x), ln(x)
x
x 0.9 e 2.46 log( x ) 0.046 log( x 2 ) 0.152 ln( x ) 0.105
Trigonometric Functions
All trigonometric function are available and are obtained by typing the name followed by ()'s.
x is assumed to be in radians unless specified otherwise.
2. User-Defined Functions
One of the powerful features of Mathcad is the ability to define your own functions. This is very helpful when
doing engineering calculations and will be used often.
Syntax
To create a function:
1. Type the desired function name
2. Type (x) where x is the variable of the function. Note: you can use more then one variable in a function,
3. Type : to give you := simply separate each with a comma.
4. Define the expression in terms of x.
Demonstration
Nmoles Rg Temp
J
Rg 8.314 v ig Nmoles Temp Press v ig( 1mol 273.15K 1bar ) 22.71 L
mol K Press
Key Points
1. Rg does not appear in the parameter list of the function so it must be defined previously.
2. Parameters names (selected when a function if defined) correspond to the variable names from some governing
equation. (e.g. For V=NRT/P, N is given the name Nmoles)
3. To obtain a numerical answer, the function is given numbers as inputs.
Important Point 1
The parameter names in the argument list of the function are really just place holders. When I evaluate the
function, I can use other variables which contain the numerical values I want to use in evaluation.
lbmol 453.59237mol
n 2lbmol
T1 700R P1 50psi
v ig n T1 P1 8.508 m
3
T2 800R P2 70psi
v ig n T2 P2 6.946 m
3
Notice that I can use the name function over and over to give me the volume of an ideal gas given any
temperature and pressure.
The temperature and pressure can be numbers (as done above) or variable names.
The variable names do not have to be the same name as when the function was defined.
Important Point 2
If you pass a function variables or numbers with incorrect units, the function will either not evaluate or return
incorrect units.
3
m
T3 900 P3 80psi v ig n T3 P3 12.307
K
In engineering, we often need physical properties, such as heat capacities, to do design calculations. We usually obtain
these by looking up an equation in a handbook. For example, the heat capacity of liquid water is dependent upon the
temperature and can be found using the following equation.
Cp ( T F )
18.4613 0.013584T 7.24045 105 T 2 1.87335 10 7 T 3 2.14815 10 10 T 4
BTU
lbmol F
The T/°F of the equation means the temperature must be in units of °F. The BTU/(lbmol*°F) means the heat
capacity returned by the equation is in those units. Our challenge is to write a Mathcad function that always
returns the correct heat capacity based upon the equation given above.
Before creating the function lets look at how Mathcad handles units and unit conversions.
300K 540 R ASK
300K
=? "If you believe the top expression, what will this give you?"
R
300K
459.67 80.33 "What does 80.33 represent?"
R Ans: The numerical value of the temperature in units of °F.
Demonstration
5 7 10
A 18.4613 B 0.01358 C 7.24045 10 D 1.87335 10 E 2.14815 10
2 3 4
Cp( t) A B t C t D t E t
Cp( 300K) Cp( 300 ) 17.586 ASK: "What is the correct value of the heat
capacity?"
Cp( 540R) Cp( 540 ) 21.009
If you don't place units in, the function doesn't
Cp[ ( 80.33 459.67) R] Cp( 80.33 ) 17.749 know what units the temperature is in.
2 3 4
Cp( t) A B 459.67 C 459.67 D 459.67 E 459.67
t t t t
R R R R
ASK: "What is the term in ()'s doing?"
Cp( 300K) 17.749 Cp( 300 )
2 3 4
Cp( t) A B 459.67 C 459.67 D 459.67 E 459.67
t t t t BTU
R R R R lbmol R
2
m kg
Cp( 300K) 74.314 BTU By adding the units to the entire
2 Cp( 300K) 17.749
mol K s lbmol R expression, we have made full use of
Mathcad's unit capability. We can now
2
m kg BTU use the Cp function in later calculations
Cp( 540R) 74.314 Cp( 540R) 17.749
2 lbmol R and all units will be correct.
mol K s
2
m kg BTU
Cp[ ( 80.33 459.67) R] 74.314 Cp[ ( 80.33 459.67) R] 17.749
2 lbmol R
mol K s
Key Points:
The term in ()'s forces Mathcad to convert any temperature passed to the function into °F as required by the original
expression.
Multiplying the entire expression by the correct units allows us to use the function in subsequent calculations and let
Mathcad do the unit conversions.
Practice
The Reynold’s number is a dimensionless quantity used to characterize fluid flow. For a circular pipe, the
Dv
Reynold’s number is defined as Re , where and are the density and viscosity of the fluid, D is
the diameter of the pipe, and v is the velocity of the fluid through the pipe. Calculate the Reynold’s
number if water, at 2 degrees Celsius, travels down a 2 in diameter pipe at 3 m/s. The density and viscosity
of water can be found from the expressions below. The molecular weight of water is 18.02 gm/mol.
T
K exp 52.843 3703.6 5.8660 ln(T ) 5.879 10 29 T 10.0000
Pa s
T
T
C 7.98187 0.00138771T 2.87476 10 6 T 2 1.94845 10 9 T 3 3.72428 10 13 T 4
lbmol ft 3
29
A1 52.843 B1 3703.6 C1 5.8660 D1 5.879 10
6 9 13
A2 7.98187 B2 0.00138771 C2 2.87476 10 D2 1.94845 10 E2 3.72428 10
gm m
MW 18.02 v 3 D 2in
mol s
D1 t Pa s
10
C1 ln
B1 t
μ( t) exp A1
t K K
K
2 3 4
t t t t
ρ( t) A2 B2 273.15 C2 273.15 D2 273.15 E2 273.15
lbmol
K K K K ft3
T ( 2 273.15) K
ρ( T) D v
Re MW 5
μ( T) Re 2.182 10
4. The if function
Syntax
if(condition, true, false) True and false can be functions, text, letters, numbers, etc.
Different conditions can be found on the Boolean tool palette.
Demonstration
Demonstration
Imagine that the temperature of the reactor must be kept above 170 °C to keep the reaction going and below 200 °C
to prevent excessive pressure from forming in the vessel. Write a simple logical function to determine if the reaction
temperature is in the correct range.
Write a logical function to turn on a drainage pump if the liquid height in a holding tank exceeds 3.1 meters and
inlet flow rate exceeds 24.5 gal/min. If both of these conditions are met, the output from the function should equ
1.0, otherwise the output should equal 0 (zero).
drain( h f ) if h 3.1m f 24.5 1 0 drain 5m 25 1 drain 3m 25 0 drain 5m 24 gal 0
gal gal gal
min min min min
n
xi
ln(1 x) (1) i 1
i 1 i
(calculus toolbar)
f (i) , summation
i 1
n
f (i ) , product
(calculus toolbar) i1