Mathcad Guerrilla Guide - Western Washington University

Notes on Mathcad
A Short Reference Guide
Hints, Tips and Tricks
Mathcad 2001 Professional
Max R. Knittel
Department of Physics/Astronomy
Western Washington University
January 17, 2003
2
Notes on Mathcad
Table of Contents
ACKNOWLEDGEMENTS ......................................................................................................................... 4
THE OFFICIAL MATHCAD USER’S GUIDE AND MATHCAD REFERENCE MANUAL ............ 4
NOTATION .................................................................................................................................................. 5
WHAT IS MATHCAD?............................................................................................................................... 5
SAVING ........................................................................................................................................................ 5
THE MATHCAD WORKSPACE .............................................................................................................. 5
ENTERING EQUATIONS IN A MATH REGION .................................................................................. 7
DEFINITIONS AND VARIABLES .................................................................................................................... 7
RANGE VARIABLES ..................................................................................................................................... 9
FUNCTIONS ............................................................................................................................................... 11
USE OF ARRAY INDICES ............................................................................................................................ 12
ORDER OF EVALUATION ........................................................................................................................... 15
ENTERING DATA .................................................................................................................................... 16
INPUT LIST ................................................................................................................................................ 16
INPUT TABLES........................................................................................................................................... 16
TABLE FORMATTING............................................................................................................................ 18
ENTERING TEXT..................................................................................................................................... 20
UNITS AND DIMENSIONS ..................................................................................................................... 22
PLOTTING................................................................................................................................................. 24
PLOTTING A FUNCTION ............................................................................................................................. 24
PLOTTING TABLES OF DATA ..................................................................................................................... 25
FORMATTING PLOTS ................................................................................................................................. 26
REGRESSION (FITTING A FUNCTION TO DATA) .......................................................................... 27
SYMBOLIC EVALUATION .................................................................................................................... 31
PRINTING .................................................................................................................................................. 33
Notes on Mathcad
3
Acknowledgements
Much of the material compiled into this set of notes has come from handouts and lab procedures
developed by Drs. Richard Vawter and Rob Quigley. They have made major contributions
toward the understanding and use of Mathcad in Physics labs.
The Official Mathcad User’s Guide
When logged into a Physics workstation, you can refer to the official Mathcad documentation by
selecting Mathcad 2001 User’s Guide from the Physics Applications folder.
4
Notes on Mathcad
Notation
Whenever you are to type something, the keystrokes will be displayed in bold courier
font. You will be asked to:
You type:
v.initial:a^2[space]+b^2
Mathcad shows on the screen:
vinitial := a2 + b2
What is Mathcad?
Mathcad is a calculation, graphing and presentation computer application. Mathcad displays
equations as you are used to seeing them in text books and lets you insert graphs and text as you
build your document. It is a powerful tool for writing laboratory reports, letting you write
introductions, conclusions, and enter (or import) experimental data, analyze the data, and then
graph your results and fit theoretical functions to the experimental points. You can also solve just
about any mathematical problem, symbolically or numerically.
Saving
You may think it odd to discuss saving your Mathcad worksheet in one of the first sections of this
reference guide, but Mathcad is not a forgiving application. Mathcad will not let you undo a
number of operations, as you may be used to in other applications. For example, if you
accidentally delete the wrong region, you cannot get it back! So make sure you save early and
often so you don’t have to manually redo much work if you perform some operation you wish
you hadn’t.
Additionally, Mathcad crashes on occasion (actually, quite often for a program as mature as
Mathcad). Before performing any radical operation such as moving a lot of regions or printing,
save your work. It doesn’t hurt to even have two copies of your work: one in your U:\ home
directory and one on a floppy diskette or Zip disk.
The Mathcad Workspace
When you start Mathcad, you’ll see a window with some familiar toolbars. The top line of the
window is the familiar Windows File Edit … menu headings. The next line is the Standard
toolbar with icons for File, Print, Print Preview, Cut and Paste, etc. The third line is the
Formatting toolbar for font type, size and attributes.
A floating Math toolbar accompanies the Mathcad window. Clicking on an icon in the Math
toolbar opens other toolbars:
•
Calculator—Common arithmetic operators
Notes on Mathcad
5
•
Graph—Various two- and three-dimensional plot types and graph tools.
•
Matrix—Matrix and vector operators
•
Evaluation—Equal signs for evaluation and definition
•
Calculus—Derivatives, integrals, limits, and iterated sums and products
•
Boolean—Comparative and logical operators for Boolean expression
•
Programming—Programming constructs
•
Greek—Greek letters
•
Symbolic—Symbolic keywords
Mathcad lets you enter equations and text anywhere on the page. Each equation, piece of text, or
other element is a region. Mathcad creates an invisible rectangle to hold each region. A
Mathcad worksheet is a collection of these regions.
To start a new region, click anywhere in a blank area of a page. You will see a small crosshair
cursor at this point; anything you now type appears at the crosshair. If you want to create a math
region at this point, just start typing. If you want to create a text region at this point, first select
Insert/Text Region from the top of the Mathcad workspace, and then start typing.
To select a single text region, simply click anywhere in it. Mathcad shows a rectangle around the
region. To select multiple regions, click and hold at one corner and drag the selection box to the
other corner until all desired regions are displayed inside dashed rectangles. You can also use the
standard windows techniques of holding down the <Ctrl> key while clicking to add regions oneat-a-time to the selected regions or holding down the <Shift> and clicking on the first and last
regions to select all regions in between. After selecting a region, you can drag, delete, cut, copy
or paste as with most Windows applications. You can even copy and paste math and plot regions
from Mathcad into Microsoft Word.
6
Notes on Mathcad
Entering Equations in a Math Region
Mathcad uses a few quirky keyboard sequences to enter equation notation. However, once you
learn and use them, they are not so difficult. The table below shows the common ones that you
will need to know.
Operation
Key
Example Keystrokes
Screen Display
Delete Region
<Ctrl>d
Multiplication
*
A*B
A⋅B
Division
/
A/B
A
B
Definition
:
g : 9.8
g := 9.8
Evaluate
=
v = gt
v = gt
Evaluate symbolically
<Ctrl>.
e^ln(a)<Ctrl>.
e ln(a) →
Convert to Greek Letter
<Ctrl>g
t <Ctrl>g
τ
Power of
^
x^2
x2
Literal Subscript
.
v.initial
vinitial
Matrix Index
<Ctrl>6
Data<Ctrl>6 3
Data<3>
Array Index
[
d[4
d4
Square Root
\
\4ac
4ac
Variable Range
;
1;10
1..10
Insert Parentheses
′
12+3[space]’
(12+3)
Global Definition
~
g~9.8
g≡9.8
Insert X-Y Plot
@
@
Definitions and Variables
In Mathcad, there is a difference between defining a variable and evaluating an expression.
When we want to give a value of 9.8 to the variable g, we define it by using the definition
operator :=; this is entered from the keyboard by entering the : (colon) character. We type the
variable name, then the colon, and finally the value. The value can be a single number or a more
complicated combination of numbers and previously defined variables. As you enter a definition
operator, Mathcad inserts a solid rectangle placeholder. This indicates that you need to complete
the definition by typing a value in place of the placeholder.
Variable names can be just about anything, with a few exceptions:
Notes on Mathcad
7
•
•
•
Names cannot start with a digit (0 – 9).
Names can contain uppercase and lowercase letters, digits, Greek letters, literal
subscripts that are entered starting with a . (period).
An uppercase character is interpreted as a different character from its lowercase
counterpart; e.g., DIAM is a different variable name than diam or diaM.
Greek letters can be selected from the Greek toolbar or by typing the the normal
keyboard equivalent of the Greek letter and then immediately typing <Ctrl>g. For
example, typing t <Ctrl>g yields τ, p<Ctrl>g yields π.
Names with literal subscripts (“literal” meaning that Mathcad is to take the subscript
just as the characters appear) are different than a name with an array index subscript.
The literal subscript is merely an extension of the variable name, while an array index
indicates which one of an array is to be used. The array index subscript is entered by
typing the variable name, then the character [ (left square bracket), and finally the
array index—which could be a number or another variable name (x1, vj).
•
Mathcad has a number of built in functions, constants and units such as sin, g, π, e,
etc. You can redefine these if you wish.
To evaluate an expression, you enter the “=” character. For example, if you have defined g to be
9.8, t to be 10, and d to be 1/2gt2, then to evaluate the distance d you would type:
g:9.8
Define the variable g
t:10
Define the variable t
d:1/2[space]*g*t^2
Define the variable d
d=
Evaluate d
The value for d is evaluated and displayed automatically after you type the “=” sign.
In the definition above for d, the keyboard strokes indicates that a [space] should be typed after
“1/2”. This is to move the insertion point from the denominator of the fraction back up to the
main line of the expression.
The use of the spacebar in a math region is to cycle through what portion of the math region is
currently selected. When entering characters into a math region, two editing lines replace the
crosshair cursor to show you where the insertion point will be. The vertical editing line shows
where the next character you type will appear, while the horizontal editing line indicates at what
level the next character will appear. Pressing the spacebar when in a math region cycles the
insertion point around to the various possible levels in the region.
For another example, you may want to set the first 0th element of a velocity table equal to the
initial velocity vinitial.
v.initial:9.8
8
Define the variable vinitial using a literal subscript
Notes on Mathcad
v[0:v.initial
Define the 0th element of the v array using an array
index to be vinitial using a literal subscript
If you want to insert parentheses to set the precedence of operations, make sure that the two
editing lines encompass the part of the expression you want to enclose in parentheses and then
press the “’” (single quote) key or select Parentheses from the Calculator toolbar.
For example, in the definition above for d, pressing the spacebar will in turn select the whole
expression, the numerator of the fraction, the denominator of the fraction, and then the whole ½.
This enables you to select what portion of the expression to edit. Using the mouse to move the
editing lines to the superscript 2 will then let you use the spacebar to cycle through selecting the
various points of that part of the expression.
The use of the mouse and the spacebar lets you place the editing lines at exactly the point you
want to edit.
Range Variables
To compute equations for a range of values, you first create a range variable. A range variable
is defined in the normal way any variable is defined: you type the name and a : (colon). In the
placeholder rectangle, instead of entering a single value, you enter the first value, a comma, the
second value, a ; (semi-colon)—which shows in the worksheet as two dots—and then the last
value. You may be more familiar with entering ranges with first value, last value and interval,
but Mathcad calculates the interval from the difference between the first and second values. If
the interval value is to be 1, then you can omit the second value and merely type the first value,
semicolon, and last value.
Once you define a range variable, it takes on its complete range of values every time you use it. If
you use a range variable in an equation, Mathcad evaluates that equation for each value of the
range variable.
For example, to create a range variable t that takes on the values from 1 to 21 with an interval of
2, you would type:
t:1,3;21
and Mathcad would display:
t := 1, 3 .. 21
To see these values, you want to create an output table. If you type t= Mathcad creates a single
column output table directly below. By default, the maximum length of the table is 16 rows; if
your range variable takes on more than 16 values, click on the output table; it will turn into a
scrolling table, and you can use the scroll bar to move down through the rest of the values.
You can use a range variable to create a series of values at which to evaluate an indexed variable.
For example, if you type f[t:10*t then you can display a vector of all the answers by typing
f[t= .
Notes on Mathcad
9
t := 1 , 3 .. 21
f := 10⋅ t
t
f =
t =
t
1
10
3
30
5
50
7
70
9
90
11
110
13
130
15
150
17
170
19
190
21
210
f = 30
3
Mathcad will calculate a value for f for each value of t. You can display the fourth element by
typing f[3= .
Note that using a range variable as an array index only works if all values of the range variable
are integers; e.g., you can’t generate the 3.5 element of an array.
If you are going to turn in a printed copy of your Mathcad worksheet, you should consider
expanding the length of the displayed table so that all of your values will print. If the table will
be far too long, you can show it in pieces by typing f[t= several times across the page and
setting the scroll bars for the tables to successively lower positions.
f =
f =
t
t
0
0
10
1
30
2
50
3
70
4
90
5
110
0
6
130
7
150
8
170
9
190
10 210
Note that to turn on table row and column numbering, right click on it and choose Properties.
Place a check mark in the box for Show column/row labels.
10
Notes on Mathcad
Functions
Mathcad has an extensive built-in function set. You can define additional functions in the same
way you define a variable, except that the function name contains an argument list. The
argument list are the variables enclosed in parentheses on the left side of the equals sign. For
example, to define the function dist(x,y), where (x,y) is the argument list, you would:
Type:
dist(x,y):\x^2[space]+y^2
Display:
dist(x, y) :=
x2 + y 2
Then evaluating dist(2,4) would show 4.472
dist(2,4) = 4.472
When using functions such as sin, cos, etc., you cannot enter the square of these functions as you
are used to seeing it: sin2(x) but rather as sin(x)2 (Type y(x):sin(x)^2). When in doubt, use
parentheses to ensure the correct order of operation precedence.
When you define a function , you do not need to have previously defined the variables in the
argument list: you are telling Mathcad what to do with the arguments, not what they are (they
could be scalars, vectors, matrices, etc.). However, any variable used on the right side of the
definition that is not in the function argument list must have been previously defined.
You can define a function f(t) in terms of a range variable t. Mathcad will perform a calculation
and displays the result for each value of t when asked to create an output table or plot.
t := 1 , 3 .. 21
f ( t) := 10⋅ t
t =
f ( t) =
1
10
3
30
5
50
7
70
9
90
11
110
13
130
15
150
17
170
19
190
21
210
f =
3
You may think that the two output tables above are vector matrices and you could display the
fourth element of f(t) by typing f[3= . But as you can see above, if you select the region for f3,
Mathcad will display the message that “This operation can only be performed on an array.” The
Notes on Mathcad
11
output table of a function is just a display and the individual elements cannot be addressed and
used elsewhere. You could evaluate f(3) and get 30.
For the same reason, you cannot omit a range variable from the function argument list, even
though it may seem you have already defined the values of t before you defined the function f.
Use of Array Indices
The use of array indices with variables is one of Mathcad’s most difficult concepts. It is often
difficult to understand when Mathcad requires the use of an array index, when it forbids the
use, and when it doesn’t care. Sometimes it is just a trial-and-error process.
Some basic rules for use of array indices are:
Input Table:
You can display or plot input tables h and T (if both axes use input tables) without using an index
or you can use an index if you have previously defined a range variable for the index.
h :=
T :=
1
1
1
0.050
1
1.620
2
0.075
2
1.384
3
0.100
3
1.270
4
0.125
4
1.214
5
0.150
5
1.188
6
0.175
6
1.182
7
0.200
7
1.189
8
0.225
8
1.204
9
0.250
9
1.223
10
0.275
10
1.245
1.8
1.6
T
1.4
1.2
1
0
0.1
0.2
0.3
h
If you are plotting an input table T on one axis and a range variable or a function that uses a range
variable h on the other axis, you must use an array index i for the input table.
i := 1 .. 10
h ( i) := 0.025⋅ ( 1 + i)
12
T :=
1
1
1.620
2
1.384
3
1.270
4
1.214
5
1.188
6
1.182
7
1.189
8
1.204
9
1.223
10
1.245
1.8
1.6
Ti
1.4
1.2
1
0
0.1
0.2
0.3
h( i)
Notes on Mathcad
Function with Input Table:
If you are using an input table h on the right side in a function definition Tsa, then that input table
name must appear in the list of arguments on the left side. Then to display or plot the function
you must use an array index i for the input table variable.
i := 1 .. 10
g := 9.8
2
h :=
Tsa( h ) := 2⋅ π⋅
1
1
0.050
2
0.075
3
0.100
4
0.125
1 1.618
5
0.150
2 1.383
6
0.175
3 1.269
7
0.200
4 1.213
8
0.225
5 1.187
9
0.250
6 1.181
10
k +h
2
g⋅ h
( i) =
Tsa h
1.8
1
1.6
T sa( hi) 1.4
1.2
1
0
0.1
0.2
0.3
hi
0.275
Function with Range Variable:
If you are using a range variable h on the right side in a function definition Tsa, then that range
variable name must appear in the list of arguments on the left side. To display or plot the
function, do not use an array index.
h := 0.04, 0.05.. 0.35
2
Tsa( h ) := 2⋅ π⋅
Tsa( h ) =
1
1
1.784
2
1.618
3
1.502
4
1.417
5
1.354
6
1.306
k +h
2
g⋅ h
2
T sa( h) 1.5
1
0
0.1
0.2
0.3
0.4
h
Notes on Mathcad
13
As another example as a function that uses a range variable as an argument, let’s look at a plot of
a sine function.
i := 1 .. 30
XX( i) :=
i
2
P( i) := sin ( XX( i) )
XX( i) =
P( i) =
0.5
0.479
1
0.841
1.5
0.997
2
0.909
2.5
0.598
3
0.141
1
P ( i)
0
1
0
5
10
15
XX ( i)
Variable with an Array Index:
We can graph the same sine function but using an index.
i := 1 .. 30
XX :=
i
i
2
P := sin ( XX)
1
1
1
0.5
1
0.479
2
1
2
0.841
XX = 3 1.5
P = 3 0.997
4
2
4
0.909
5
2.5
5
0.598
6
3
6
0.141
1
P
0
1
0
5
10
15
XX
Now XXi is calculated for each value of i, and stored in the XX table. Then P is calculated for
each value of XX and stored in its table. You can display or plot the functions with or without
the index i, but only because i is an integer. If i ranged in steps of 0.1 instead of 1, it could not
be used as an array index, and the method of plotting a function with a range variable would have
to be used.
14
Notes on Mathcad
Order of Evaluation
In the above examples, the order and location of the various math regions is very important.
Mathcad updates the results of all math regions as soon as you make a change and click outside of
the current math region. Mathcad starts at the top left of the document and goes from left to right
and top to bottom. Therefore, you must make all of your definitions above or to the left of when
they are used. If you try to use a variable before it is defined, it will be highlighted in red;
selecting that math region will pop up an error message. Particularly in plotting, what may look
like Mathcad telling you there is a serious error is nothing more than the plot is actually above
where your X- or Y-axis data is evaluated.
You can redefine a variable as often as you like in a worksheet, so be aware that the same name
may not have the same value everywhere.
It is possible to define a variable whose value is defined even above its location in the worksheet.
This is called a global definition. It is done by typing the “~” (tilde) instead of the “:” (colon) in
the definition. In the worksheet, it appears as the standard three-bar math definition symbol ≡. If
J is globally defined to be 7 (J ≡ 7) at the end of a worksheet, J will be 7 anywhere in the
worksheet.
Sometimes in editing a Mathcad document, you need to insert space in the middle in order to
enter additional definition or text. Use the mouse to position the crosshair cursor at the point you
want to insert space and press the <Enter> key as many times as needed.
Notes on Mathcad
15
Entering Data
There are a number of ways to enter data into Mathcad. For example, you can type in a list and
Mathcad will build a table, you can insert an input table and Mathcad creates a table that looks
like a spreadsheet, or you can import data files saved from other applications.
Input List
To enter lists of numbers—such as experimental data for analysis or plotting—you can create a
table. First define a range variable as an array index.
Type:
j:1;21
Next define your data variable such as dj and start entering your data values separated by
commas.
Type:
Mathcad displays:
d[j:2.3,5.6,7.9,12.4
d :=
j
2.3
5.6
7.9
12.4
As soon as you type the first comma, Mathcad changes the expression into a table, adding a new
row each time you press the comma. You can go back and insert rows by clicking at the end of a
number in the table, hitting the comma key and then typing the next value in the new row. The
display of this type of table stays just as you type in the numbers.
Input Tables
Alternatively, you can create an Input Table by placing the cursor where you want the table to
start and selecting Insert/Component/Input Table. Type a variable name in the placeholder at
the top and use the handles on the table to enlarge or shrink it in rows and columns to fit the size
of the data you want to enter. Values not entered into the input table are assumed to be zero by
Mathcad.
You can change the display of an input table by right clicking on a table entry and selecting
Properties. You can set the Displayed precision, whether or not to Show trailing zeros, and
whether or not to Show column/row labels. Although the global table properties set through
Format/Result/Number Format effects output tables, input tables are not effected and must be
set individually.
Mathcad always starts matrices, vectors and tables with the 0th element unless you have globally
defined the variable ORIGIN (in capital letters) to be something else. For example, at the top of a
16
Notes on Mathcad
worksheet you many want to enter ORGIN ≡ 1 so that all tables will start with the first row as
row 1.
Because an input table can be changed to any size (the initial size is 2×2), you can accidentally
cause Mathcad to believe you have data in a column when you really do not. Often an input table
is created to be nothing more than a single-column list of numbers for time or distance or angle.
If you accidentally get even one value entered in the second column, Mathcad immediately fills
the rest of the column with 0’s. Now Mathcad thinks your input table is two dimensional,
requiring that any operation performed with it have two indices: Xi,j.
If you have created this problem, to get rid of the second column, select any element of the
second column, right click on it and select Delete Cells and then Entire Column. This really just
clears all of the entries for that column, allowing you to work with your data as a single column
vector with one array index.
If you intend to use an input table as just a single column vector, before entering any data into the
table, you should shrink the width down to one column and stretch the length out to the number
of your data values.
Notes on Mathcad
17
Table Formatting
Setting the look of the various types of tables in Mathcad can be a confusing task. The following
list summarizes how to set table formatting.
Range Variable Table for Entering Data
The entries in this type of table appear just as you type them. You cannot turn on
column/row headings. You can add units, by multiplying each element by the unit name
or by redefining the variable (d := d⋅m).
Input Table
When an input table is created it has 2 decimals of displayed precision and does not show
trailing zeros. This format is not affected by the global table format. To set the table
format, for each input table, right click on one of the table entries and select Properties.
You can set the Displayed precision, whether or not to Show trailing zeros, and
whether or not to Show column/row labels.
If you want to add units to the values in an input table, the only way is to redefine the
variable name after you have created the table (Height:=Height⋅m).
Height :=
0
0
0
1
1
2
2
3
3
4
43
5
4
Height := Height ⋅ m
Output Table
To set a global format for output tables, select Format/Result/Number Format. You
can set the Displayed precision, whether or not to Show trailing zeros, and whether or
not to Show column/row labels for all subsequent output tables and the results of
evaluations.
You can set the format for an individual table or evaluation result by first selecting it and
then selecting Format/Result/Number Format. This table will not be affected by
subsequent changes to the global result format.
You can also set the output table Display Style to be either Matrix or Table or an
automatic mode that displays the table in a matrix style for small tables and then switches
to the table style if there are more than 10 rows or columns.
18
Notes on Mathcad
To add units to the values in an output table, type in the unit name in the placeholder to
the right of the table.
0
0
0
1
1
Height = 2
3
2 m
3
4 43
5
Notes on Mathcad
4
19
Entering Text
To enter text, click on a blank space, and you’ll see the small crosshair. Next, you can just start
typing text; when you hit the spacebar to start the second word, Mathcad will decide you’re
creating a text region. Alternatively to force a text region, before typing any text, either select
Insert/Text Region or start your text with the “ (double quote) character.
Mathcad changes the crosshair into a vertical line cursor and a box appears around it. Characters
you type appear to the left of this insertion point, and the text box grows as you enter text. If you
have started this text region in the middle of a worksheet, the text region may grow as you type
until it begins to overlap the following region. Mathcad does not automatically push regions
down as the current region grows, so you will have to do one of the following:
•
locate the crosshair cursor where you want to add blank space and repeatedly press the
<Enter> key,
•
after you have finished entering text, select the region that is overlapped, move the cursor
to the edge of the box until the cursor turns into a hand and then drag it our from under
the text region,
•
select Format/Separate Regions and all regions in the worksheet will be moved down
so that none overlap. Be careful when doing this as it could change the order of
evaluation.
If you want a text region that behaves as a word processor, first create a text region and then,
while it is selected, chose Format/Properties and click on the Text tab. Enter a check mark in
the “Occupy Page Width” box—this automatically will also turn on “Push Regions Down As
You Type”. Click on <OK>. CAUTION: For some reason, turning on “Occupy Page Width”
extends that text region slightly over the right margin, causing Mathcad to think there is
something on that new page to the right. Mathcad will print this new page as a blank page at the
end of your document. See the section on Printing for methods of dealing with printing
blank pages.
There are a number of special symbols that you may want to insert into a text region to help
document what you are doing in Mathcad. Greek letters can be entered the same way as in a
math region: using the Greek toolbar or by pressing the keyboard equivalent key and then
immediately <Ctrl>g. For example, to type an Ω in a text region, press “W” (capital w) and then
Çtrl>g.
Other symbols can be entered by using the <Alt> key in association with the numeric keypad—
make sure the <Num Lock> key is on. For example, to enter a ° (degree symbol) in a text
region, press and hold down the <Alt> and type “0176” on the numeric keypad. When you
release the <Alt> key, the degree symbol appears in your text.
If you want to enter an ∞ (infinity) in a text region, press and hold down the <Alt> and type
“0165” on the numeric keypad. When you release the <Alt> key, the ¥ symbol appears in your
text. Immediately pressing <Ctrl>g converts it to ∞.
20
Notes on Mathcad
The following table has some of the more common symbol codes for use in text regions.
Codes to Enter Symbols in Text Regions
Symbol
°
¼
½
¾
≤
≥
«
»
≈
≠
≡
≅
∼
∝
±
×
⋅
÷
∞
ƒ
Å
∑
⇔
⇐
⇑
⇒
⇓
↔
←
↑
→
↓
Notes on Mathcad
Code
<Alt>0176
<Alt>0188
<Alt>0189
<Alt>0190
<Alt>0163
<Alt>0179
<Alt>0171
<Alt>0187
<Alt>0187<Ctrl>g
<Alt>0185<Ctrl>g
<Alt>0186<Ctrl>g
@<Ctrl>g
~<Ctrl>g
<Alt>0181<Ctrl>g
<Alt>0177
<Alt>0180<Ctrl>g
<Alt>0183<Ctrl>g
<Alt>0184<Ctrl>g
<Alt>0165<Ctrl>g
<Alt>0166<Ctrl>g
<Alt>0197
<Alt>0229<Ctrl>g
<Alt>0219<Ctrl>g
<Alt>0220<Ctrl>g
<Alt>0221<Ctrl>g
<Alt>0222<Ctrl>g
<Alt>0223<Ctrl>g
<Alt>0171<Ctrl>g
<Alt>0172<Ctrl>g
<Alt>0173<Ctrl>g
<Alt>0174<Ctrl>g
<Alt>0175<Ctrl>g
21
Units and Dimensions
When you define variables, Mathcad assumes they are dimensionless. However, Mathcad
contains a set of units that are treated just like built-in variables. To assign units to a number or
expression, just multiply it by the name of the unit. Type d:27.4*m.
If you want to add units to each element of a range variable, you have to multiply each of the
three parts by the unit. Type t:0*s,1*s;24*s.
By default, Mathcad uses the SI system of units (m, kg, s). Almost any normally occurring
physics unit is already predefined. You can use the Insert/Insert Unit dialog box to find the
correct Mathcad unit. You can change the unit system by Math/Options/Unit System.
You can have Mathcad convert units for you. For example if you defined d := 34⋅m and then
evaluated it:
d = 32m
If you typed “ft” in the placeholder and clicked outside of the math region, the worksheet would
show:
d = 111.549⋅ft
Another example is that since Mathcad trigonometric functions work in radians, you can easily
convert your argument from degrees merely by attaching the degree units to it.
b g=1
sinb90 ⋅ degg = 1
sin
π
2
Sometimes Mathcad’s units manipulations leave odd units. For example, if you are performing
some calculations with moment of inertia and torque, you might have something that looks like:
M := 5.0⋅ kg
I :=
1
2
R := 0.2⋅ m
⋅ M⋅ R
2
2 I
κ := 4⋅ π ⋅
T
2
T := 2.0⋅ s
2
I = 0.1kg m
κ = 0.987J
The torsion constant κ is given the units of Joules, which although technically correct, is not the
standard units. To change the displayed units to the more familiar N-m, click on the last
evaluation for κ and type in an m in the units placeholder that follows the displayed J. You are
asking Mathcad to change the units to something that includes meters, and Mathcad decides that
it has to add the N to make the units correct.
22
Notes on Mathcad
Clicking on the evaluation looks like:
κ = 0.987J
and then typing in the m at the placeholder and clicking outside the region yields:
κ = 0.987N m
You will need to do this for each evaluation and output table to display more familiar units.
There is even a unit for percent. Normally, if you calculate percent difference, you would
multiply your decimal fraction answer to get percent. However, in the example below, we simply
multiplied the displayed answer for the variable %diff by “%” to automatically multiply by 100
(Type %diff= and then move the cursor to the units placeholder at the end and type %).
g accepted = 9.807
m
s
%diff :=
2
g experimental = 9.755
m
s
2
g accepted − g experimental
g accepted
%diff = 0.525%
Some functions such as ln or linfit require that there arguments have no dimensions or units.
Since you may have gone to a lot of trouble to include units in your calculations up to this point,
what can you do? Mathcad includes a special function called UnitsOf (the capital U and
capital O are required in the name) that returns the units of a variable. In the example moment of
inertia above, we can actually find the units that Mathcad is using by:
2
UnitsOf ( I) = 1 kg m
The UnitsOf function can be very useful in using the regression functions. Refer to the a later
section on REGRESSION for a further discussion.
If you have assigned units to your variables, then Mathcad will perform dimensional checking. If
you violate the principles of dimensional analysis, Mathcad will display an appropriate error
message. Sometimes it is a lot easier to not have Mathcad keep track of units; it’s more trouble
than it’s worth. However, this does not remove the need for you to go back and add units to your
worksheet as small text regions following the numerical display that Mathcad calculates. Units
are an important part of any answer and should always appear as part of your worksheet.
Notes on Mathcad
23
Plotting
To visually represent an expression or input data, Mathcad has a number of 2D and 3D plotting
capabilities. Initially, the most important type for us is the 2D X-Y plot. You can start a plot by
placing the crosshair where you want the plot, selecting Insert/Graph/X-Y Plot, selecting X-Y
Plot from the Graph toolbar or by pressing the “@” key.
Now fill in the X-axis and Y-axis placeholders with the names of the appropriate variables and
click outside the plot region. To change scales, ticks, colors, etc., right click on the plot and
select Format.
Plotting a Function
For example, to graph several waves of a sine function:
x := 0,0.1 .. 40
y(x) := sin(x)
1
1
0.5
y ( x)
0
0.5
−1
1
0
0
10
20
x
30
40
40
Next press “@” to start an X-Y Plot, enter “x” as the X-axis variable, “y(x)” for the Y-axis
variable, and click outside of the plot region.
If you want to graph a second function on the same plot, define a second variable:
z(x) := cos(x)
and place the editing lines at the end of the “y(x)” for the Y-axis variable, type a comma, the
second variable name “z(x)”, and click outside of the plot region.
24
Notes on Mathcad
1
1
0.5
y ( x)
0
z ( x)
0.5
−1
1
0
10
0
20
30
40
40
x
Plotting Tables of Data
To input data tables and then graph the results, first use Insert/Component/Input Table to create
tables for t and v experimental data.
t :=
v :=
0
0
0
1
0
1
1
2
1
6
2
3
2
15
3
4
3
22
4
5
4
39
5
6
5
51
6
7
6
67
7
8
7
81
8
9
8
104
9
10
9
133
Notes on Mathcad
25
Next, press the “@” key to start an X-Y plot and enter t for the X-axis variable and v for the Yaxis variable. You’re done!
133
150
100
v
50
1
0
0
2
4
1
6
8
10
t
10
Formatting Plots
The plots displayed above have used the default plot formatting; they are little more than a square
with automatic axis scales. Mathcad has many options for adding a title, axis labels, grid lines,
and trace symbols and lines. To make plot format changes, right click on the plot and select
Format. For example, the plot above can be formatted to look like:
Velocity vs. Time for My Car
133 150
Velocity (m/s)
125
100
v
75
50
25
1
0
0
1
26
1
2
3
4
5
6
t
Time (s)
7
8
9
10
10
Notes on Mathcad
Regression (Fitting a Function to Data)
Mathcad includes a number of functions for performing regression. Typically, these functions
generate a curve of a specified type that tries to minimize the error between itself and the data you
supply. Some of these functions are:
intercept(vx, vy)
The y-intercept of the least-squares regression line for the data
points in the vectors vx and vy.
slope(vx,vy)
The slope of the least-squares regression line for the data points
in the vectors vx and vy.
line(vx,vy)
Returns the y-intercept and slope of the line that best
approximates the data in vx and vy.
linfit(vx,vy,F)
Returns a vector containing the coefficients used to create a
linear combination of the functions in F that best approximate
the data in vx and vy.
expfit(vx,vy,vg)
Returns the parameters a, b and c for the exponential curve
a ⋅ e (b⋅x) + c that best approximates the data in vx and vy. Vector
vg specifies initial guesses for the three unknown parameters a,
b and c.
logfit(vx,vy,vg)
Returns the parameters a, b and c for the logarithmic curve
a ⋅ ln(x) b + c that best approximates the data in vx and vy.
Vector vg specifies initial guesses for the three unknown
parameters a, b and c.
pwrfit(vx,vy,vg)
Returns the parameters a, b and c for the power curve
a ⋅ x b + c that best approximates the data in vx and vy. Vector
vg specifies initial guesses for the three unknown parameters a,
b and c.
sinfit(vx,vy,vg)
Returns the parameters a, b and c for the sine curve
a ⋅ sin( x + b) + c that best approximates the data in vx and vy.
Vector vg specifies initial guesses for the three unknown
parameters a, b and c.
Except for the slope and intercept functions, Mathcad requires that the arguments to the
regression functions have no units.
For example, to fit a line to the data given above in t and v:
vtheory(t) := slope(t,v)⋅ t + intercept(t,v)
Notes on Mathcad
27
Now you can add the theoretical values of v as a second variable to plot on the Y-axis. You can
also change the plot of the experimental data to be discrete points by right clicking on the plot,
selecting Format/Traces and set Symbol to “x’s” and Type to “points” for Trace 1.
133
150
100
v
v theory( t )
50
0
− 12.218 50
0
2
4
6
1
8
10
t
12
12
You can plot theory and experiment with different ranges of X-axis values. For example, if you
had no experimental data value for an X-value of zero but still wanted to draw the theoretical line
to the intercept point, you could plot theory with a range that included zero.
This is a little more work. First, define a new range of time values we can call “tt”:
tt := 0,1 .. 12
Next, define the slope variable called “a” and the intercept value called “b”:
a := slope(t,v)
b := intercept(t,v)
Now you can define the new function vtheory.
vtheory(tt) := a⋅tt + b
Change the plot second Y-axis parameter and add a second X-axis parameter.
144.515
150
100
v
v theory( tt )
50
0
− 26.467 50
0
0
28
5
10
t , tt
12
Notes on Mathcad
Since a straight line does not fit the experimental data very well, let’s try adding a linear fit of
S0t2 + S1t + S2. This quadratic fit will use the Mathcad function linfit.
First, define the vector F of the functions we wish to combine as itself a function in some general
variable T (type “F(T):” and then Insert/Matrix 3 Rows by 1 Column):
FT I
F(T) := G T J
GG 1 JJ
H K
2
Next, define a general variable S, a three-row vector whose elements are the three fitting
constants, and define it to be the fitting function:
S := linfit(t, v, F)
If you have given units to t and v then you need to divide out the units by:
I
F
t v J
G
S := linfit G , , FJ
GH s ms JK
Instead of explicitly typing in the unit names to divide them out as above, you can use the
Mathcad function UnitsOf:
S: = linfit
FG t , v , FIJ
H UnitsOf(t) UnitsOf(v) K
We can display the fitting parameters by typing “S=”:
F 1.14 I
S = G 1.707 J
GH -1.383JK
Now we can define a quadratic function vvtheory:
vvtheory(tt) := S0⋅tt2 + S1⋅tt + S2
If you have given units to tt and v then you need to multiply each coefficient of S so that the units
for each term end up in “m/s”.
vv theory (tt) := S0 ⋅
Notes on Mathcad
m 2
m
m
⋅ tt + S1 ⋅ 2 ⋅ tt + S2 ⋅
3
s
s
s
29
Now you can add it to the plot.
183.28
200
100
v
v theory( tt )
vv theory( tt )
0
− 26.467 100
0
0
5
10
t , tt
12
You could also have equivalently defined vvtheory in a shorter vector multiplication notation:
vvtheory(tt) := F(tt) ⋅ S
30
Notes on Mathcad
Symbolic Evaluation
Up to this point, we have discussed using Mathcad for numeric calculations. We have developed
a function, assigned values to the variables, and then numerically evaluated the function. You
can also have Mathcad solve an expression symbolically. The result of a symbolic evaluation of
an expression is generally another expression.
For example, you could symbolically differentiate x3 – 2yx by:
(
)
2
d 3
x − 2⋅ y ⋅ x → 3⋅ x − 2⋅ y
dx
This example introduces two new Mathcad elements: the derivative operator from the Calculus
toolbar and the symbolic evaluation sign from the Evaluation toolbar.
Although you may think you can type d/dx[space] to enter the derivative—and it may look
the same—you actually must select the derivative operator from the Calculus toolbar or type the
keyboard shortcut <Shift>/ (forward slash).
To symbolically evaluate the expression, after typing the ending parenthesis, select the symbolic
evaluation sign → from the evaluation toolbar or type the keyboard shortcut <Ctrl>. (period).
This is analogous to typing the = (equal) sign to numerically evaluate the expression. Finally,
click outside of the math region to have Mathcad perform the symbolic evaluation.
The symbolic equal sign knows about previously defined functions and variables and substitutes
them into the evaluation whenever appropriate. For example:
b
⌠ 2
1 3 1 3
 x dx → ⋅ b − ⋅ a
⌡
3
3
a
a := 0
b := 10
b
⌠ 2
1000
 x dx →
⌡
3
a
In the first symbolic evaluation, the variables a and b are undefined, while in the second, the
variables are now previously defined, and after the symbolic evaluation, Mathcad makes the
substitutions. Mathcad will ignore a previous definition for a variable if the variable has since
been redefined recursively (a := a) or the variable has been defined as a range variable.
Mathcad can perform many other types of evaluations, using keywords that expand the meaning
of symbolic evaluation. For example:
3
3
2
2
( x + y ) expand → x + 3⋅ y ⋅ x + 3⋅ x⋅ y + y
Notes on Mathcad
3
31
uses the expand keyword evaluation from the Symbolic toolbar. Alternatively, you could select
the Keyword Evaluation symbol \→or type <Ctrl><Shift> . (period) and type the keword in
the placeholder.
The following list contains just a few of the types of symbolic evaluations. A full list with
explanations can be found in the on-line official Mathcad User’s Guide.
simplify
Simplifies an expression performing arithmetic, canceling common
factors, and using basic trigonometric and inverse function identities.
expand
Expands all powers and products of sums in an expression.
factor
Factors an expression into a product.
solve, var
Solves an equation for the variable var.
As another example, let’s find the determinant of a matrix:
 x 1 a 

2
6
3
2
2
 −b x −a  → x + x⋅ a⋅ b + b⋅ x − a⋅ b − a − a⋅ x

3
 1 b x 
This uses the Determinant and then Matrix commands from the Matrix toolbar.
This section on symbolic evaluation can only give you a taste of what Mathcad is capable. Please
see the on-line official Mathcad 2001 User’s Guide for more complete information.
32
Notes on Mathcad
Printing
Scroll through the entire document to make sure it looks good, with no regions overlapping page
breaks and no regions extending past the right margin. Make a note of the last page number N.
After saving your Mathcad worksheet for a final time, go to the Print dialog box by selecting
File/Print. Note where the Print dialog box says “Print range”. If the M in “All M pages” is the
value you just wrote down for N, click on <OK>.
If M is larger than the length of your worksheet, then there are some regions that extend beyond
the right margin. The easiest solution is to change the print range to Pages from 1 to N to make
sure you print only useful pages without extra blank pages at the end. Now click on <OK>.
Notes on Mathcad
33