Using Arrays and Vectors to Make Graphs In

Using Arrays and Vectors to Make Graphs In Mathcad
Charles Nippert
This Quick Tour will lead you through the creation of vectors (one-dimensional arrays)
and matrices (two-dimensional arrays). After that, you will use these data structures to
make plots and graphs. Begin by opening Mathcad. As before move the cursor down
and to the right.
Getting Started-Planning
In this exercise, you will draw a three dimensional graph of a coil. You will use the
parametric form of the equation of a coil. When written in vector notation the equation
for a coil becomes: r (t ) = (R ⋅ sin (t ), R ⋅ cos(t ), t ) . To make a plot you will need the values
of this path function at a range of points. First, you will make an array that contains the
values of the parameter t. Then, you will make one array for the values of the X
coordinate,, a second array for the values of the y coordinates and a third array for the
values of the z coordinate. Then you will use these three arrays to create a threedimensional graph.
Arrays, vectors and matrices are groups of numbers in which each individual number or
element is identified by a set of integers, called subscripts. The number of integers used
to identify a particular element is called the dimension of the array. The arrays that you
will create in this project are one-dimensional arrays, or vectors. Matrices are created
similarly.
The first element in a one-dimensional array is identified by the integer value of zero.
The second element is identified by the integer one and so on. Therefore if the array
named "x" contains 10 elements, the first element will be identified as x0, and the last
element will be identified as “x9”. In this example, the "i-th" element of each of the three
arrays will contain the numerical values of the corresponding x, y and z “i-th” point in a
set of points that the graphing routine will use to end draw the plot.
1.
Begin by defining a variable “N” that contains the maximum value of a subscript
in an array. Create the variable definition by one of the following:
(1) Type “N:10”
(2) Type “N”, then choose "View/Toolbars/Evaluation" from the menu options.
Click the
button. Then type "10".
Press “Enter” after either of these two operations.
2.
Create a "Range Variable", also known as a “Running Variable". This is a
variable that will contain the integers for all the values of all the array elements.
In this example it will contain the integers zero through 10. Begin by creating a
variable "i" and set it equal to zero as you did in step one. Do not press "Enter".
Your screen should look something like figure 1.
Figure 1
After Step 2
3.
Choose "View/Toolbars/Matrix" from the menu to make the Matrix dialog box
appear. If the dialog box is already present, a small check will appear to the left
of the word "Matrix". Click the
button on the Matrix toolbar. In my screen,
this symbol was the last symbol in the second row. Your screen should now look
something like figure 2.
Figure 2
After Step 3
To dots and a solid
black rectangle and the
blue inverted L. cursor
appears in your
equation.
Use the "m..n”
button on the
matrix dialog
box to create a
range variable
4.
A "Range Variable" contains at least an upper and lower limit. Mathcad allows
more complex forms of range variables in which the increment between each
value is not one. For now we will limit ourselves to simple ranges in which the
increment is one. The two dots after the zero are Mathcad's way of indicating that
the variable "i” is now a Range variable. The lower limit of that Range variable is
zero. The solid black rectangle shows that Mathcad is waiting for the upper limit
of the Range variable. Type "N" and press "Enter". The limits on the Range
variable are now zero and the value contained in the variable "N" (the number 10
for now).
5.
Now create and array or vector named "t" that contains values ranging from zero
to 6π. Using these values, Mathcad will do calculations around three loops of the
coil. The mathematical equation that allows us to calculate the values of the
i
parameter at N equally spaced points between zero and 6π is t i = 6π where i=
N
an integer from zero to N. Type the letter "t” just below the definition of "i".
key in the matrix dialog box. In my computer, this was the
Then press the
second key in the first row. A small black rectangle will appear to the right and a
little below the "t”. This indicates that the program is ready to accept a subscript
for the array named “t”. Pressing the “[” key is a shortcut to creating subscripts.
Pressing the“[” key can also create a subscript. Your screen should now look like
figure 3.
Figure 3
After Step 5
This place holder
indicates a subscript.
Use the
button or press the
“[” key to create a subscript.
6.
Press the “i” key. The black rectangle will be replaced by the letter i. The
variable “ti” now refers to each variable in a one-dimensional array named “t”.
Notice that the right hand side of the equation given in step 5 contains the Greek
letter π. To create Greek letters, open the Greek dialog box by choosing
"View/Toolbars/Greek" menu option. A new dialog box showing Greek letters
will appear. Your screen will now look something like Figure 4.
Figure 4.
Figure 4 After Step 6
7.
Finish entering the arithmetic assignment statement. Either press the “Shift :” key
,“:=”, button on the Evaluation toolbar to create the
on the keyboard or press the
symbol,“:=”. Press the “6” and then the “*” keys on your keyboard. To enter the Greek
letter π, press the button labeled "π” on the dialog box. Finish entering the arithmetic
assignment statement by typing “*i/N”. Your screen should now look like figure 5. The
statement you have just entered will create an array containing N+1 elements numbered
from zero to N. The values in this array will be 11 equally spaced real numbers from 0 to
6π.
Figure 5
After Step 7
8.
Now defining a value for the variable “R”. Press "Enter". The box around the
last statement will disappear and the Red Cross cursor will appear just below that
statement. Type the characters “R:2” to create the defining equation for the
radius. Your screen should now look like figure 6.
Figure 6 After Step 8
9.
You will now enter the statements that create three arrays containing the values of
the x, y and z coordinates of the points you are going to plot. Fill each array by
creating a formula using the Range Variable to create statements that define each
variable in these arrays. Press "Enter". Then begin to enter the defining
statement for the x array. Type "x[i:R*”.This statement will use preprogrammed
Mathcad functions. Choose "Insert/Function" from the menu. The Insert
Function dialog box will appear. This dialog box will allow you to select among
the preprogrammed functions available in Mathcad. If you already know the
function, you can simply type it in. In the first box, labeled "Function Category",
select "Trigonometric". A list of trigonometric functions will appear in the right
hand box, labeled "Function Name", choose "sin". Your screen will now look
like figure 7.
Figure 7
After Step 9
10.
Press the "OK” button on the dialog box. The dialog box will disappear and the
sine function, "sin", will appear in the statement. The black rectangle will appear inside
the parenthesis. Type the characters “t[i” to finish the equation. Your screen should now
look like figure 8.
Figure 8
After Step 10
11.
Press "Enter" and create a statement for the y array: y i = R ⋅ cos(t i ) . Finally enter
the statement for the z array: z i = t i . Your screen should now look like figure 9.
Figure 9
After Step 11
12.
You are now ready to create the graph. Press "Enter". The red cross cursor will
appear just below the last equation. Choose "Insert/Graph/3D Plot Wizard" from
the menu. The graphical wizard will appear. A list of choices will appear at the
left of the dialog box. A sample graph will appear at the right. Choose "Scatter
Plot" from the list of plot types. This type of plot will display points in three
dimensions. Your screen will look like figure 10.
Figure 10
After Step 12
13.
Press the "Next" button on the wizard. The next portion of the dialog box, named
"Appearance" will appear. Choose "Connect with lines". The picture to the right
will show lines drawn between the points. Your screen will now look like figure
11.
Figure 11
After Step 13
14.
Press "Next". The last portion of this wizard allows you to set the coloring for
your image. For this drawing choose "Solid color". Your screen will now look
like figure 12.
Figure 12
After Step 14
15.
Press "Finish". The dialog box will close and a 3-D plot area will appear and
replace the red cross. The upper left-hand corner of the graph will be at the spot
of the red cross. The small black rectangle in the lower left-hand corner indicates
the area where the arrays containing the XYZ values will be entered. Your screen
will now look like figure 13. A 3-D graph will appear inside the shape of a
square. Small solid black rectangles will appear on the edges of the box. You can
grab these solid black rectangles with your mouse cursor to change the size of the
graph. The solid black rectangle in the left hell for he-hand corner indicates the
position where the XYZ values will be entered.
Figure 13
Portion of the Screen After Step 15
You will enter the
values to be plotted
in the lower left
rectangle.
16.
You can change the size
of the graph by grabbing
the black rectangles on
the edge with the mouse
cursor and dragging
them to a new location.
The inverted blue "L" in the lower left solid black rectangle in the graph indicates
that the program is waiting for the names of the arrays containing the coordinates.
Type "(x,y,z)” and press "Enter". Your screen should now look like figure 14.
Notice that the coil does not look very much like a coil. Instead it looks like a
series of jagged lines. That is because the program draws a straight line between
each set of points. In order to get a more believable image, you must increase the
number of points in the arrays. In the final step in this set of notes, you will learn
how to increase the number of elements and create a more realistic graph.
Figure 14
A Portion of the Screen at After Step 16
17.
Recall that for this sheet the number of points in each array is specified by the
upper limit of the counter “i”. This term is the variable “N”. Therefore to
increase the size of the array in Mathcad all you need to do is change the variable
called "N”. Mathcad will then recalculate all subsequent statements. You may
have to scroll the work area up to relocate the defining statement for the variable
“N”. Click on that statement and move the blue L-shaped cursor to the end of the
equation by pressing the right arrow key. Change the "10” in the defining
statement for“N” to "75” by pressing the backspace key twice. This action will
remove the“10” and restore the solid black rectangle. Finally, type“75” and press
"Enter". Your screen should now look do something like figure 15.
Figure 15
A Portion Of the Screen After Step 17