Académique Documents
Professionnel Documents
Culture Documents
Range Variables
The aim of this worksheet is to introduce you to more things you can do with vectors and
matrices in MathCAD.
By the end of the sheet you will know how to:
Create vectors and matrices
Perform simple matrix and vector mathematics
Access individual elements of arrays
Access columns and submatrices within arrays
Create and use range variables
Use range variables to create vectors and matrices
Read in data to analyse from a file.
This worksheet takes the form of a number of examples which illustrate and introduce
these principles. Work through them all.
VA
MA
VB
MB
3
4
5 6
7 8
DPL 2002,3,5
1/14
VA
VB
MA
MB
10 12
VA
VB
MB
MA
4 4
4 4
MathCAD enforces rules of matrix maths and will not allow addition of matrices or
vectors of different sizes.
MA
VA
MathCAD will allow addition or multiplication of an array by a scalar. In this case the
scalar is added or multiplied by each element of the matrix in turn:
VA
VA 3
MA
3
3
MA 3
2 3
4 5
3 6
9 12
Addition of constant
Multiplication by scalar
MathCAD will also calculate the dot products of vectors and matrices, providing the
inner dimensions are compatible
VA VB 11
MA MB
MA VA
19 22
43 50
5
11
The dot operator for matrix multiplication is the same * as is used for conventional
multiplication, alternatively you can select the dot product operator from the matrix
toolbar.
For practice, create the following arrays and matrices:
DPL 2002,3,5
2/14
3
Vec3DA
Vec3DB
Vec3DC
11
1.2
3.4
5.6
7.8
9.0
1.2
MC
MD
2
3
0 1
5 0
ME
9 11 13
1 1
2 4
3 9 27
4 16 64
MA
MC
VB
1 2
MB
3 4
1.2
3.4
5.6
7.8
9.0
1.2
MD
3
4
5 6
7 8
0 1
5 0
9 11 13
ME
1 1
2 4
3 9 27
4 16 64
Using these arrays, we will first use subscripts to extract values from the arrays and then
use subscripts to set individual elements of the array.
DPL 2002,3,5
3/14
On the worksheet you have been using so far, position the cursor below the last
matrix definition.
Type VA[0=
MathCAD responds by giving the value of the zeroth element of the vector VA
In the same way, query the value of VA1 by typing VA[1=
The two values should be displayed as shown.
VA 0 1
VA 1 2
For two dimensional arrays, (matrices), mathCAD uses 2 indices separated by a comma,
in the order Row,Column
MD0 1 1
MD1 0 5
ME1 2 8
ME2 1 9
We can use the same subscript operator to set individual elements of arrays. To do this,
simply follow the expression which references the array with the assignment operator
<:>
For example, to set value of the zeroth element of VB to 7, type VB[0:7
VB0
VB
7
7
4
DPL 2002,3,5
4/14
PH15720
Column
Operator
Matrix to
operate
on
Column
Index
1
1
ME
4
9
16
To insert the row operator into your worksheet, you can either select it from the matrix
toolbar or type <ctrl-6> from the keyboard. The ability to extract a column from a matrix
is particularly useful when reading experimental data from a file into mathCAD, as we
will see later.
MathCAD also has a function, submatrix(), which can extract a number of rows and
columns from an array
First Column
Last Column
Matrix to
operate on
submatrix( ME 0 2 1 2 )
9 27
First Row
Last Row
DPL 2002,3,5
5/14
PH15720
As an exercise, use the functions and operators to print out the following parts of the
arrays:
The zeroth column of array MA
The first column of array MB
The zeroth and first row of matrix MC
The 2x2 submatrix at the bottom right hand corner of matrix MD
The submatrix with 2 columns and 3 rows at the bottom right hand corner of ME
1 1 1
ME
2 4 8
1 2 3 4
3 9 27
T
ME 1 4 9 16
4 16 64
1 8 27 64
DPL 2002,3,5
6/14
T
ME
2 T
( 3 9 27 )
PH15720
Experiment with the using these functions on your example vectors and matrices to solve
the following problems:
DPL 2002,3,5
7/14
PH15720
7 7
Suppose that instead of wanting to find the determinant of a matrix we wish to find the
absolute value of all the elements in the matrix. In order to do this we use the vectorise
operator from the matrix toolbar. This is shown as an arrow over f(M).
1
1 2
3 4
In the example on the left, the determinant operator is applied to the 2x2 matrix and
calculates the determinant as normal. In the example on the right, the vectorise operator
has been applied to the determinant operator, causing it to be applied to each element of
the matrix in turn.
The vectorise operator also works with matrix multiplication, as shown below:
1
2 5 32
2 5
10
18
In the left hand case, two vectors are multiplied together using the normal rules of matrix
multiplication, giving a scalar product. In the right hand case the vectorise operator forces
the multiplication to take place on an element by element basis.
The vectorise operator works for all operations that normally have a special meaning for
matrices.
DPL 2002,3,5
8/14
PH15720
will highlight the offending expression in red and the error message will refer to Illegal
Context.
Range values correspond to FOR loops in conventional programming languages, and are
very useful when used with arrays to identify elements of the array.
To start with, we will create a range variable, i, and do some experiments with it.
0 3
i
i
0
1
2
3
Notice how the display of the values of I is subtly different from the way in which
mathCAD displays a matrix or a vector.
You can create an expression involving the range variable and mathCAD will evaluate
the expression for each of the values taken by the range variable. In the example below I
have asked mathCAD to do evaluate a number of expressions involving the range
variable.
0 3
i
i
i 1
0
1
2
3
1
2
3
4
0
1
4
9
1
2
4
8
Again, you should note that mathCAD is simply displaying the result of a calculation
repeated a number of times, changing the value of i each time.
DPL 2002,3,5
9/14
PH15720
Range variables find their greatest use when filling an array with a set of values.
Suppose that we were performing an experiment where we took a reading every 10
minutes. We could create an array and type the times into it by hand, like this:
0
time
10
min
20
30
But this is tedious and prone to mistakes, especially if we had 100 times to type in instead
of the 4 in this case.
Looking at the series of numbers in the array, we can say that the i'th element is i times 10
minutes, which we express to mathCAD like this:
i
0 3
timei
i 10 min
0
600
time
1.2 10
1.8 10
time
10
20
min
30
Note how the range variable on the left hand side of the assignment symbol is used to
index through the array elements while the same variable on the right hand side is used to
create their values. For practice, after you have created the time array, have a go at
creating the following arrays using range variables:
When you have finished, your arrays should look like this:
0
evens 4
odds 5
squares
4
9
16
25
DPL 2002,3,5
10/14
PH15720
2.53
2.56
2.62
2.28
2.44
2.41
2.49
The mean and stdev functions may be selected from the f(x) dialog box, under the
function category statistics.
Note that you can add units to a vector. If all elements of a vector have the same units, the
easiest way to do this is to select the entire matrix and multiply it by the appropriate unit.
The units will then propagate through to the answers as shown below:
DPL 2002,3,5
11/14
PH15720
Diameters
2.53
2.56
mm
2.62
2.28
2.44
2.41
2.49
3
mean( Diameters) 2.476 10 m
DPL 2002,3,5
4
stdev( Diameters) 1.029 10 m
12/14
PH15720
Now, using the techniques we started to explore with the resistor experiment and the
photoelectric effect, carry out the following analyses on the data. I have put some hints at
the bottom of the page which may help if you get stuck.
From the matrix of data, create a vector to hold the Detector number and another
to hold the counts.
Plot the counts against detector number.
DPL 2002,3,5
13/14
PH15720
Create a plot of count rate vs detector, given that the sample period was 10
seconds.
Each detector is 20um x 2mm. Produce another graph with the plot rate scaled in
terms of counts per second per cm2.
There are 2 clear peaks in the data. Find the height of the largest peak in terms of
counts per second.
Create an expression which will give the number of the detector with the highest
counts.
Use the submatrix function to create a matrix containing the data from the highest
counting channel together with the 6 channels on either side.
Calculate the total number of counts in the highest peak and its adjacent channels
that you have just created above.
Create a function called PeakArea which takes as its arguments the channel
number and the width of the peak in channels. It is to return the total number of
counts of the given channel together with those up to 1 width away. For example,
PeakArea(42,3) will give the total counts from detectors 39,40,41,42,43,44 and
45.
Use the PeakArea function to re-calculate the area of the highest peak and the 6
channels either side.
Create a graph which will show the largest peak and a specified number of
adjacent channels on either side.
Go back to the file read component and right click on it, select Choose File
from the pop-up menu and pick the file DetectorData2.txt. Click out of the file
read component and check that all of your answers and graphs change to reflect
the new data file.
Hints
The following hints may be useful to you. Use the help system and conduct some
experiments with MathCAD to see how.
You may want to create a unit for the micron (1 um is 10-6 m)
The max() function will tell you the largest number in a vector
The csort() function can be used to sort a matrix so that a particular column is in
ascending order.
You can reverse() the results of a sort.
You can use variables in the placeholders which determine the axis scaling of a graph.
The submatrix() function can be used to pick off part of a matrix.
The matrix toolbar has one way of summing the elements of a vector.
The calculus toolbar has two more ways of doing summations.
END
DPL 2002,3,5
14/14