Sie sind auf Seite 1von 8

Range Variables and Arrays

Range Variables
"Array" is just a category; vectors, matrices, and nested arrays are all types of arrays. In Mathcad, a range -
variable is NOT considered an array.
Here is how you define a range variable:
n 1 3 , 8 .. :=
(keystrokes: n:1,3;8 or alternatively, you can use the range variable button on the vector/matrix toolbar)
where 1,3 and 8 define a set of real, evenly spaced numbers with number spacing 3 1 2 = up to maximum of 8:
n
1
3
5
7
|

\
|
|
|
|
|
.
=
NOTE: this looks just like an array! It IS NOT! It is a range variable.
Range variables are typically used for indexing arrays (more on that later), and as such usually do not carry units.
However you CAN attach units to a range variable like so:
i 0s 1ms , 1min .. :=
(keystrokes: i:0s,2ms;1min)
The only time I have ever used units on range variables is for graphing purposes (Mathcad's graphing capabilities
are COOL AS HECK by the way):
0 0.2 0.4 0.6 0.8 1
2
1
0
1
2
1 10
3

500
0
500
1 10
3

Position
Velocity
Time (min)
P
o
s
i
t
i
o
n

(
m
i
)
V
e
l
o
c
i
t
y

(
m
i
/
h
r
)
i
0
1
2
3
4
5
6
7
8
9
10
11
12
...
ms
=
Global definitions (~ key) of the functions so they can appear below the plot:
g 32.2
ft
s
2
ms
s
1000
v
init
250
m
s
d t ( ) v
init
t
1
2
g t
2
+ v
f
t ( ) v
init
g t +
(NOTE: the above example utlizes Mathcad functions; we'll cover functions another time. I just wanted to give an
example of utilizing units in range variables.)
Subscripts
There are two different kinds of subscripts in Mathcad: -
Nominal subscripts (nominal = naming) 1.
Ordinal subscripts (ordinal = numbered) 2.
Another name for an ordinal subscript is an index. This is what I will call them from now on.
Nominal subscripts are typed using the period key (.). An index is typed with the left bracket key ([). They -
look similar but they are not the same!:
ordinal subscript, or index
x
1
nominal subscript x
1
Variables can have mixtures of both subscripts: -
layer
1
1 :=
fill 2 :=
x
1
1
x
1
2

1
layer
1

2
fill
For a Matrix, the first index is the row and the second is the column: -
M
1
1
3
2
4
|

\
|
|
.
:= M
1
0 0 ,
1 = M
1
0 1 ,
2 = M
1
1 1 ,
4 =
You have already used nominal subscripts on your homework. You will now be using indeces as well (as part -
of working with arrays) on your next homework.
Arrays
Unlike range variables, arrays can be either vertical or horizontal (vectors), or both (in which case it is a -
matrix), or nested arrays:
v
1
2
3
|

\
|
|
|
.
:= v 1 2 3 ( ) := v
1
2
3
2
3
4
3
4
5
|

\
|
|
|
.
:=
Nested Array:
x
v
v
|

\
|
|
.
:= x
{3,3}
{3,3}
|

\
|
|
.
= x
1
1
2
3
2
3
4
3
4
5
|

\
|
|
|
.
=
By default, vectors are TOP TO BOTTOM, not left to right. You cannot change this. Sorry, bucko. -
Change the way arrays are displayed using the menu option Format -> Result -> Display Options: -
Expand nested arrays: x
1
2
3
2
3
4
3
4
5
|

\
|
|
|
.
1
2
3
2
3
4
3
4
5
|

\
|
|
|
.

(
(
(
(
(
(
(

=
Matrix Display Style - Table: v
0 1 2
0
1
2
1 2 3
2 3 4
3 4 5
=
You can get rid of the row/column labels of a table by right clicking the table, selecting properties, and -
unchecking the box.
If you display an array as a table (Format -> Result -> Display Options), you can right click on the table and -
alter the alignment. You can't do that with the matrix display setting (which is lame):
v
0 1 2
0
1
2
1 2 3
2 3 4
3 4 5
=
v
0 1 2
0
1
2
1 2 3
2 3 4
3 4 5
=
v
0 1 2
0
1
2
1 2 3
2 3 4
3 4 5
=
v
1
2
3
2
3
4
3
4
5
|

\
|
|
|
.
=
v
0 1 2
0
1
2
1 2 3
2 3 4
3 4 5
=
v
0 1 2
0
1
2
1 2 3
2 3 4
3 4 5
=
Like range variables, arrays can also carry units: v
1
2
3
2
3
4
3
4
5
|

\
|
|
|
.
s := -
ALL of the members of an array must carry the same unit dimension (so this array isn't valid):
v
"Time"
0
1s
5min
"Position"
1ft
1ft
2yd
"Velocity"
250
m
s
4
mi
hr
10
yd
s
"Acceleration"
g
g
0
|

\
|
|
|
|
|
|
|
|
.
:=
"Position"
Arrays are created several ways. -
The simplest is by just clicking the Matrix or Vector button, or pressing Ctl+m 1.
v
1
2
|

\
|
|
.
:=

You can refer to a previously defined range variable and use commas to enter separate values: 2.
(keystrokes: v shift+colon ctl+m <matrix dialog box> 1 tab 2 )
Range variable: j 1 2 , 4 .. :=
a
j
12s
1min
1hr
1day
:=
(note that you can enter different units of the same dimension)
a
0
0.2
1
60
1.44 10
3

\
|
|
|
|
|
|
|
.
min =
(Keystrokes:a[ j:12 s,1min,1hr,1day )
3. You can use the range variable in an expression to create the array:
b
j
j s :=
b
j
1
2
3
4
|

\
|
|
|
|
|
.
s =
IMPORTANT: Note that inputting an array with commas without referencing a range variable does NOT create a
valid array!! As a matter of fact, near as I can tell, it doesn't create anything! Be sure and let me know if any of you
ever figure out what comma delimited entry does.
h 1 2 , 3 , 4 , 5 , 6 , 7 , := <--invalid array (entered using commas)
h :=
<-- valid array (entered using commas):
h
j
1
2
3
4
:=
h
0
0
1
2
3
4
0
1
2
3
4
=
Considerations
j 1 2 .. := k 2 3 .. :=
j
j :=
k
k := 2ft :=
Here we have j and k as range variables, and as arrays/vectors, and (gamma) as a dimensioned scalar. -
When creating a new expression, all of the variables must match how they are used in the function itself. So:
y
j

j
:=
is valid, whereas: y

:=

is invalid. This is because k is being called as an array/vector but k is NOT an


array/vector, and likewise is being called as a range variable but NOT a range variable. Also notice that
although
j
is valid, the following is invalid:
R
j
:=
j
R is assumed by Mathcad to be a scalar until it is defined otherwise, and you cannot assign an array of values to a
scalar.
However you can do this:
R
j

j
:=
This is perfectly valid because R
j
is assumed to be an array.

j
j +
Let's look at another expression:
This makes things slightly different because j is now being treated as both a scalar and a range variable. But this
is not a problem because each member of the range variable IS a scalar, and we are only using one member at a
time:
R
j

j
j + := R
j
2
4
|

\
|
|
.
=
However, now let's see what happens when we include units in the expression (recall that has units of length):
R
j

j
j + := j R
j
= R
Now we have created a problem- j is dimensionless, but has units (of length).
We can fix this several ways. We could multiply j by a (length) unit:
R
j

j
j in + :=
R
j
0.635
1.27
m
=
Or we can divide the units out of the term:
Q
j

ft

j
j + := Q
j
3
6
=
You will have to use one or both of these two methods when working with empirical equations.
Here's an example using the Hardin and Drnevich equation for shear modulus of a sand:
OCR
j
1
1
:= e
j
0.7
1.1
:=
'
m
j
200psf
7kPa
:=
G
max
j
1230
2.973 e
j

( )
2
1 e
j
+
OCR
j

'
m
j
psi

(
(
(

psi :=
This allows you to input the '
m
term using any units you wish (psf, tsf, kPa, etc) and always get the right answer
even with an empirical equation:
G
max
j
317.191
149.068
tsf
= or G
max
j
3
4.40510
3
2.0710
psi
=
tsf
tonf
ft ft

Data Tables
Data Tables are just a second way to create arrays. Make an array with a Data Table by doing -
Insert-->Data-->Data Table.
Data Tables are useful because: -
you can more quicky add columns and rows to them than regular array entry 1.
you can import data into a data table from Excel or the web (see Mathcad Help) 2.
a data table displays as a table instead of as a matrix (easier to read) 3.
In Mathcad, data tables are also a type of object called "component"- search for them in the Help for more info. -
One technique I've used a lot- which can be done with both data tables and arrays/matrices- is to make a data -
table/matrix with NO units and then add the units later with an equation. The only difference is the way the
input array is displayed.
Data Table Data Matrix
DATA
1
0 1
1 1
300 6
:=
DATA
2
0
1
300
1
1
6
|

\
|
|
|
.
:=
Note that the results of the DATA1 and DATA2 are exactly the same because they're both arrays:
time
1
DATA
1
0
( )
s := time
2
DATA
2
0
( )
s :=
time
1
0
1
300
|

\
|
|
|
.
s = time
2
0
1
300
|

\
|
|
|
.
s =
distance
1
DATA
1
1
( )
ft := distance
2
DATA
2
1
( )
ft :=
distance
1
0.305
0.305
1.829
|

\
|
|
|
.
m = distance
2
0.305
0.305
1.829
|

\
|
|
|
.
m =
ORIGIN
In Mathcad, the default origin for arrays/vectors/matrices is ZERO. It is NOT 1. -
What does this mean? -
If I define a range variable k:
k 1 3 .. :=
And then define a vector array T:
T
k
2k m := ( T
k 1 =
is 2m, T
k 2 =
is 4m, etc)
The first member of T will not be at index 1! It will be at index zero, because ORIGIN=0:
T
0
2
4
6
|

\
|
|
|
|
|
.
m = T
k
2
4
6
|

\
|
|
|
.
m =
When you create a new array Mathcad automatically fills in any skipped indexes with a zero, so T
0
0 m = . -
Here's another example of this:
n
1
3
5
7
|

\
|
|
|
|
|
.
= (previously defined range variable) t
n
n := t
0
1
0
3
0
5
0
7
|

\
|
|
|
|
|
|
|
|
|
|
.
= t
n
1
3
5
7
|

\
|
|
|
|
|
.
=
(skipped indexes 0,2,4 and 6 are zero)
You can change the ORIGIN just like you change any other built-in Mathcad unit -
You can change the ORIGIN just like you change any other built in Mathcad unit. -
If you change ORIGIN to 1: ORIGIN 1 :=
This is what happens:
T
0
2
4
6
|

\
|
|
|
|
|
.
m = T
k
0
2
4
|

\
|
|
|
.
m =
Note that the contents of T did not change- the INDECES of T changed. The value T=2 used to be at INDEX 1.
When we changed ORIGIN, INDEX 1 changed to INDEX 2, so T=2 is at INDEX 2.
Why does this matter? Say you have a 3-layer soil profile and you do something like this: -
ORIGIN 0 :=
(default)
Soil profile:

wet
110
111
117
|

\
|
|
|
.
pcf := h
20
15
5
|

\
|
|
|
.
ft :=
You wish to find the total stress in the center of layer 1 (answer should be 1100 psf):

1
h
1
2

wet
1
832.5 psf = :=
<-- WRONG ANSWER
What happened? LAYER 1 IS LAYER 0. Because the ORIGIN is set to 0.

0
h
0
2

wet
0
1.1 ksf = :=
<-- CORRECT ANSWER
Get around this problem completely using one of three different ways. -
Always and consistently use range variable array index values. 1.
k
1
2
3
|

\
|
|
|
.
=
<--previously defined array index k
h
k
20ft
15ft
5ft
:=

wet
k
110pcf
111pcf
117pcf
:=

1
h
1
2

wet
1
1.1 10
3
psf = :=
<-- CORRECT ANSWER
2. Redefine your ORIGIN.
ORIGIN 1 :=

wet
110
111
117
|

\
|
|
|
.
pcf := h
20
15
5
|

\
|
|
|
.
ft :=
1
h
1
2

wet
1
1.1 10
3
psf = := <-- CORRECT ANSWER
3. Just remember that layer 1 is layer 0.
The problem with this method is that when you are creating project documents in the professional world with
Mathcad, it can be confusing for the reader. Remember: just because YOU know that Mathcad's indexes start at 0
doesn't mean the person trying to figure out what your documents say knows it!!

Das könnte Ihnen auch gefallen