You are on page 1of 6

Mathcad Basics

chung Page 16 12/27/2005


Lecture 2 Mathcad Basics
There are three items that are crucial to step up to the next level of programming such as;
Operators & Order of operations
Range variables and vectors
Functions

Lets talk about these.
Operators
+ Addition
- Subtraction
* Multiplication
/ Division
^ Power
( ) Specify evaluation order


Order of operations: Operator Precedence

1. Built-in constant
2. ( ) parentheses
3. Built-in function
4. Unary operator (Negation) 1+(-1), -sqrt(x)
5. Binary operators: Exponentiation ^
6. Binary operators: Multiplication and Division * /
7. Binary operators: Addition and Subtraction + -

y := 2 x := 3 * y^2 x = 12

y := 2 x := (3*y)^2 x = 36

y := 2 x := 3 * y + 2 x = 8

z := 3*6+6*2/4 z = 21

x := 5^2/2 x = 12.5

Range Variables and Vectors

A range variable is a variable that contains a series of numbers rather than a single number
(but its not a vector). A range variable is different than a vector (e.g., it cannot be
referenced for specific values). All the values in a range variable must be used at the same
time. If a range variable appears in an equation, it implies that the equation should be
executed for each value in the range variable. We can also use range variables as another
way to create vectors.


Mathcad Basics
chung Page 17 12/27/2005
In general, vectors are much more flexible than range variables in terms of what you can do
with them in Mathcad. We will learn to use range variables as a tool to create vectors, then
use the vectors to carry out calculations and create graphs.

Range variables
Range variables are single variables that take on multiple values. They are used to perform
many of the operations which would require loops in a traditional programming language.
Range variables are declared in Mathcad as:

var := start, next; end

Note that instead of specifying an increment directly, the increment is specified indirectly by
specifying the next value in the sequence. If the value of next is omitted, the default
increment is 1 or -1 depending on the values of start and end.

i 1 2 , 5 .. := j 4 2 , 4 .. := x 0 0.1 , 0.4 .. := y 1 5 .. := z 5 1 .. :=
i
1
2
3
4
5
= j
-4
-2
0
2
4
= x
0
0.1
0.2
0.3
0.4
= y
1
2
3
4
5
= z
5
4
3
2
1
=

Remember that this range variable should not be treated as a vector (e.g., one
dimensional array). That is, you should be aware of creating a vector (an array) by using a
range variable. If you employed a range variable instead of a vector where you intended (or
wanted) to use a vector, then you would be using a scalar variable that contains merely a
set of numbers varying in a range. In other words, a range variable is nothing but a scalar.
However, a vector is an indexed variable or one-dimesional array that contains multiple
scalar quantities. You might ask, "How do I create a vector? Thats a good question. Lets
extend our understanding to how to create a vector and where we use it.


Vectors (one-dimensionally indexed variables)

Indexed variables are closely related to range variables and are analogous to array element
references in traditional programming languages. Think of the variable name as the name of
the array and the subscript as the index of the element to be assigned or accessed. Index
subscripts are actually functional as opposed to the appearance-only subscripts which are
actually parts of a variable name. Apperance-only subscripts are typed as var.sub whereas
index subscripts are typed as var[sub.

Note that arrays (indexed variables) in Mathcad normally begin with index (0) zero rather
than (1) one. It is recommended that you change (as the first thing to declare in your
program) by setting the ORIGIN variable equal to 1 using a global assignment equal sign. It
is very important to understand the origin of an array. Many students have had troubles
before simply because they forgot defining the origin as "1 while they thought the array
starting from "1. This can lead to a tedious dimensional error later on and make you really
sick of programming. My point is that you should be always aware of one simple fact:


Mathcad Basics
chung Page 18 12/27/2005
ORIGIN 1


(In Mathcad, type ORIGIN. Then hit the tilde in the keyboard and type "1. This is called a
global definition as you see a triple equal sign)

Creating Vectors using Range Variables
The general format to create a range variable is: i:= start, next ; end

Again, this creates a range variable (NOT a vector) starting at the value `start, and ending
at the number `end. `next determines the increment by stating the next number after
`start, and the same increment between start and next is used to build the entire range
variable. Therefore a range variable must consist of a number of equally spaced values. If
you do not specify the `next value, Mathcad uses a default stepping value of 1.

Lets use a range variable to create a vector (examples are following).
i 1 5 .. :=
create a range variable i
vec
i
1 0.5 i + :=
use the range variable as an index to a vector we are creating
use the range variable in the equation for the vector definition
view the vector we just created
vec
1.5
2
2.5
3
3.5

=
index a specific value inside vec
vec
3
2.5 =
i
3
= i
3
=
cant view a specific value within a range vector


Note: the range variable `i is used to specify the index to the vector `vec being created.
This only works if the contents of the range variable are integers.
The range variable `i also appears in the equation


Mathcad Basics
chung Page 19 12/27/2005
i 1 5 .. := vec
i
1 0.5 i + :=
value
i
vec
i
( )
3
:=
value
i
1.837
2.828
3.953
5.196
6.548
=
OR
value
1.837
2.828
3.953
5.196
6.548

=

Note difference in printout appearances when using (typing) "value = and "value[i =
Generalizing end points of an evenly spaced vector:
We can generalize the previous operation to arbitrary starting and ending points. We want
to create a vector that spans from -2 to 5 in even steps of 10.

First enter scalars and calculate increment
begin := -2 end := 5 NP := 10 inc := (end-begin)/(NP-1)

Now create the range variable `i and use it to create the vector `x

i := 1;10 x
i
:= begin + (i-1)*inc

inc 0.778 =
x
T 1 2 3 4 5 6 7 8 9 10
1 -2 -1.222 -0.444 0.333 1.111 1.889 2.667 3.444 4.222 5
=

Creating 2-D arrays using range variables:

We can extend the idea to arrays of 2 dimensions. Suppose I want a 3 by 5 matrix in which
each element is the sum of its indices...

i :=1;3 j :=1;5 z
i,j
:= i + j

z
2
3
4
3
4
5
4
5
6
5
6
7
6
7
8

=


Note that the operation on the right hand side of the equal sign is calculated for every
combination of the values in the range variables i and j.
Functions: A first look
A single or multiple commands can be assigned to a single word (or letter) that represents
those commands, followed by a list of inputs (i.e., dummy arguments) within parentheses.

Mathcad Basics
chung Page 20 12/27/2005
Once this assignment is made, you can re-issue all of those commands again and again by
using the single word. A simple example is the creation of a long equation that you intend to
use more than once. Why re-type over and over?

As shown below, a function called `y, which is a function of `t, is created. To use it, just
`type y(number)= to view the result. Note that we can send in scalar values or a range
variable `i:=1,1.5..2.5 which produces 4 answers.


y t ( ) 5 0.6t + .2 t
2
log sin t ( ) 2 + ( ) + :=
y .1 ( ) 5.38 = y 30 ( ) 156.995 =
x 6 y 4 ( )
2
+ := x 24.443 =
i 1 1.5 , 2.5 .. := y i ( )
5.854
5.927
5.864
5.665
=


A function with more than one line of commands has a distinct look by the use of a vertical
line to the right of the name we give the function, and to the left of the statements in the
function. Lets say we want to create a function to solve the quadratic equation, and we
break down the solution into parts. The way to create the vertical line is either the right
square bracket, or go to `add line from the `programming toolbar (under view => toolbars)

QuadEq a b , c , ( ) radical b
2
4 a c
ans
1
b radical +
2 a

ans
2
b radical
2 a

ans
:=
QuadEq solves the quadratic equation
of the form a x
2
b x + c + 0 =
QuadEq 2 3 , 1 , ( )
0.5
1

= QuadEq 4 0 , 16 , ( )
2
2

= radical :=



Note that the solution requires 3 pieces of input information, and the output has two pieces
of information. We can now call this function from wherever we like in this worksheet (after
the function definition). Of course I validated the answers I got to make sure they are
correct.

IMPORTANT: None of the variables created in the function are recognized outside the
function, except those listed at the end. In this case, `ans is listed at the end, while `radical

Mathcad Basics
chung Page 21 12/27/2005
is not. Therefore, the contents of `ans is transferred out to become the answers, while
`radical is not recognized outside the function. You can see this above. I tried to type
`radical = `, but Mathcad did not recognize the variable name, and is trying to force me to
define it as a new variable name. `ans also does not exist outside the function, but its
contents have been transferred as the result of `QuadEq(2,3,1) =

We will spend a good deal of time working with functions. This will be the main tool for
developing our own programs. Once we learn the three basic control structures (decisions,
counted loops, and conditional loops), the possibilities are almost endless with what we can
do.



Example
How can I create a vector that contains the expression:
2
y x = for [ 3, 4] x = in steps of
0.25 ??

step 1) define variables using the given input parameters
step 2) Compute a total number of data within the given interval
step 3) create a range variable `i from 1 to NP (the total number of data points that has
been computed using the start, end , and step size)
step 4) create a vector of "x using the range variable "i as to index and to create the value
of each component of the vector
step 5) create a graph and place `x in the box along the x-axis, and `y(x) in the box along
the y-axis

What would be seen on the screen
begin 3 := end 4 := inc 0.25 := NP
end begin
inc
1 + :=
i 1 NP .. := x
i
begin i 1 ( ) inc + := y
i
x
i
( )
2
:=
5 0 5
0
10
20
y
x

You might also like