Matrix Math Using LabVIEW
1. The constant wired to the N input (called the count terminal) tells the For Loop to cycle 10 times
2. The i is the loop index (called the iteration terminal). The loop index is incre-mented each time the For Loop cycles. Indexing starts at 0, so the index will take on values of 0–9 in this VI.
3. The index value is displayed in an indicator on the front panel. When the VI is running, the values 0–9 are displayed one after another (see Figure 4.48).
4. The Wait function wired to a constant of 500 makes the For Loop wait half a second (500 ms) between cycles. This gives the user time to see the numbers change on the front panel.
Whereas While Loops are used when you want the loop to cycle until some condition is met (like the STOP button being clicked), For Loops are typically used when you know how many times the loop should cycle.
Some useful features of For Loops in LabVIEW:
• If you wire an array control to an edge of a For Loop, the loop will cycle once for every element in the array (this is termed auto-indexing). For example, in Figure 4.49, Array contains five elements. When Array is connected to the For Loop, the loop will cycle five times and the index will take on values from 0 to 4.
• The connection to the left edge of the For Loop is called a tunnel (indicated in Figure 4.50). Tunnels allow values to pass through, into the loop. When you wire an Array control to an input tunnel, the values of the array are passed into the loop and can be used inside the loop. In Figure 4.51 the array values are now being displayed one after another as the loop cycles. (The block diagram is shown in Figure 4.52.)
Note: Input tunnels are typically placed on the left side of the loop for readability, but actually an input tunnel can be on any For Loop boundary. LabVIEW knows values are coming into the loop because the values are being sent from a control output.
• Tunnels can pass information into the loop in two ways: with indexing enabled (as used here) and with indexing disabled. The “indexing” being referred to is array indexing. When indexing is enabled, one element of the array is passed into the loop with each iteration. When indexing is disabled, the entire array is passed into the loop when the loop initializes. Right-click on the tunnel to enable or disable indexing.
• When values are passed out of a loop through a tunnel (with indexing enabled), an array of values is created. The For Loop shown in Figure 4.53 cycles six times, and the index takes on values of 0–5. Those values are passed outside the loop Figure 4.50
The block diagram showing how Array is wired to the left edge of the For Loop.
Figure 4.51
Displaying Array values one after another using a For Loop.
Figure 4.52
Using Array values inside the For Loop.
Section 4.12 Programming Preview: For Loops 129
as an array when the loop terminates. The resulting output array is shown in Figure 4.54.
Note: Again, an output tunnel can be placed on any loop boundary. It is common to place them on the right boundary because information flow in LabVIEW programs tends to be from left to write.
Note: If indexing is disabled on an output tunnel, only the final element of the array is passed through the tunnel.
• A For Loop is an easy way to create an array of calculated values. In the VI shown in Figure 4.55, the loop index is used to create two arrays: x and sin(x). These are then plotted using an XY Graph as shown in Figure 4.56.
Figure 4.53
Creating an array using a For Loop.
Figure 4.54
The output array created using the For Loop.
Figure 4.55
For Loop used to create two calculated arrays for graphing.
Figure 4.56 The calculated arrays and XY graph.
PRACTICE
Using Probes
The creator of the VI shown in Figure 4.57 was attempting to graph a sine wave (one cycle). It didn’t work out that way.
When you look at their block diagram, you notice that the For Loop runs from 0 to 628. The creator of the VI intended to divide “i” by 100 to create x values ranging from 0 to 2π, but he or she forgot to include the Divide function.
Use the Probe tool to position a probe on the wire leaving the “i”. Then run the VI to demonstrate that the value being sent out of the For Loop as X is ranging from 0 to 627 instead of 0 to 6.27.
The value in the wire under the “20” probe (627) is shown in the probe window in Figure 4.58.
Figure 4.57
Failed attempt to plot one sine wave.
Figure 4.58
The VI with the probe in place (marked “20”).
P R O G R A M M I N G A P P L I C AT I O N
Section 4.12 Programming Preview: For Loops 131
Figure 4.59
VI for creating an array (subVI elements in dashed box).
Figure 4.60 Front panel showing successful array creation.
Figure 4.61
After creating the subVI.
Figure 4.62
The subVI block diagram.
Automatic Array Maker
A common programming task is creating an array of Nvalues values spread over a specified range (Xmin to Xmax). We will develop a VI that is capable of creating the array, and then create a subVI that can be used whenever such an array is required.
For development purposes, assume Nvalues = 5, Xmin = -20, Xmax = 20. With these values it is easy to predict that the final array will contain the values [-20, -10, 0, 10, 20]. We can use these values to test the VI.
The Nvalues output will be wired to the loop count terminal (the “N”) of the For Loop. The step size, DeltaX, is calculated as
The VI is shown in Figure 4.59, and we can see on the front panel in Figure 4.60 that it is generating the desired array values.
Next, we select the elements in the dashed box shown in Figure 4.59, and create a subVI using menu options: Edit / Create subVI. The result is shown in Figure 4.61. The subVI has been assigned a default icon and default number (“5” in this example, but it is arbitrary).
Double-click on the subVI icon to open the subVI for editing. The subVI (Figure 4.62) looks a lot like the original VI, except that the constants on the left side of Figure 4.59 have been replaced by controls in Figure 4.62.
The front panel of the subVI is shown in Figure 4.63. Double-click the icon in the upper right corner to open the icon editor.
DeltaX - Xmax - Xmin Nvalues - 1
Figure 4.63
The front panel of the new subVI.
Figure 4.64
LabVIEW 2009 Icon Editor.
The LabVIEW 2009 Icon Editor is shown in Figure 4.64; the default icon image has been replaced with one of the many glyphs now available in the Icon Editor. Click OK to close the Icon Editor and return to editing the subVI. The new icon is now used for the subVI.
Be sure to save the subVI from the LabVIEW editor (File / Save). The name you use to save the subVI will be used to identify the subVI whenever it is used. In this example it has been named Make Array.vi.
Now, if we want to create two arrays for plotting: an X array containing 20 elements ranging from 80 to 40, and a Y array 20 with elements ranging from 240 to 360, we can call the Make Array subVI to create them as shown in Figure 4.65 (block diagram) and Figure 4.66 (front panel).
Summary 133
Matrix Mathematics
• Adding Matrices or Arrays
• Transpose a Matrix or Array
• Multiplying Matrices or Arrays
• Condition Number The results of using the subVI to create two arrays.
S U M M A R Y Figure 4.65
Creating two arrays for plotting using subVI calls.
Arrays or Matrices?
In LabVIEW, both arrays and matrices are collections of related values, but a matrix is always 2D, whereas an array can have any number of dimensions. Some Lab-VIEW functions (e.g., graphing functions) required 1D arrays, so arrays must be used with those LabVIEW functions.
Array Index Origin
Array (and matrix) indexing begins at zero Creating an Array (front panel)
1. Put the Array container on the front panel:
Controls Palette / Modern Group / Array, Matrix & Cluster Group / Array
2. Drop a control or indicator into the Array container (only one array element will be visible):
Controls Palette / Modern Group / Numeric Group / Numeric Control
Controls Palette / Express Group / Numeric Controls Group / Num Ctrl
3. Resize the array to show the required number of elements 4. Hide the Index Display (optional): Right-click, Visible Items /
Index Display
Placing a Matrix on Front Panel
Move the Real Matrix control from the Array, Matrix & Cluster Group onto the front panel.
Controls Palette / Modern Group / Array, Matrix & Cluster Group / Real Matrix
Summary 135
Extracting a Subarray
Subsets of various size: Functions Palette / Programming Group / Array Group / Array Subset
• Starting Row Index (0)—the index of the first row in the original array that should be included in the subarray. The default value is (0) which is the top row in the original matrix.
• Number of Rows (all)—the number of rows from the original array to include in the subarray. The default is (all) rows (i.e., all rows after the starting row).
• Starting Column Index (0)—the index of the first column in the original array that should be included in the subarray. The default value is (0) which is the left column in the original matrix.
• Number of Columns (all)—the number of columns from the original array to include in the subarray. The default is (all) columns (i.e., all columns after the starting column).
Single rows or columns: Functions Palette / Programming Group / Array Group / Index Array
• Row Index (0)—the index of the row in the original array that should be extracted.
• Column Index (0)—the index of the column in the original array that should be extracted.
Note: Wire only the Row Index or Column Index, not both.
Adding Arrays
Requirement: Arrays must be same size Process: Add element by element
Functions Palette / Mathematics Group / Numeric Group / Add function
Transpose Array
Requirement: Any array can be transposed Process: Interchange rows and columns
• Matrix function: Transpose Matrix
Function Palette / Mathematics Group / Linear Algebra Group / Transpose Matrix
• Array function: Transpose 2D Array
Function Palette / Programming Group / Array Group / Transpose 2D Array
Multiply an Array by a Scalar
Definition: scalar—single value (a number)
Requirement: Any array can be multiplied by a scalar Process: multiply each element in array by scalar
Functions Palette / Mathematics Group / Numeric Group / Multiply Function
Matrix Multiplication
Requirement: Number of columns in the first array must equal the number of rows in the second array. Product array will have as many rows as first array, as many columns as second array.
Process: Multiply across the first matrix and down the second matrix, adding terms.
Function Palette / Mathematics Group / Linear Algebra Group / A × B Function
Element by Element Multiplication
Requirement: Arrays must be same size Process: Multiply element by element
Functions Palette / Mathematics Group / Numeric Group / Multiply Function
Note: The Multiply function performs element by element multiplication on arrays, but matrix multiplication on matrices.
Condition Number
Requirement: Array must be square
Result: A small condition number indicates that a good solution is likely Function Palette / Mathematics Group / Linear Algebra Group / Matrix Condition Number
Determinant
Requirement: Array must be square
Result: A zero determinant indicates that matrix is singular (cannot be inverted) Function Palette / Mathematics Group / Linear Algebra Group /
Determinant Inverse Matrix
Requirement: Array must be square and non-singular Result: matrix inverse
Function Palette / Mathematics Group / Linear Algebra Group / Inverse Matrix
Solving Simultaneous Linear Equations
Requirement: Number of columns in the coefficient array must equal the number of rows in the right-hand-side.
Process:
Function Palette / Mathematics Group / Linear Algebra Group / Solve Linear Equations
For Loops
Commonly used to create arrays of calculated values.
• The constant wired to the N input (called the count terminal) instructs the For Loop how many times to cycle.
[x] = [C]-1[r]
Self-Assessment 137
1. What LabVIEW function is used to extract a subarray from an array?
ANS: Array Subset function:
Functions Palette / Programming Group / Array Group / Array Subset 2. Can the following arrays be added? Why, or why not?
• A and C
• B and D
• C and E
ANS: Arrays must be the same size if they are to be added
• A and C—NO
• The i is the loop index (called the iteration terminal.) The loop index is incremented each time the For Loop cycles. Indexing starts at zero.
• If you wire an array control to an edge of a For Loop, the loop will cycle once for every element in the array (this is termed auto-indexing).
• A wire connection to the boundary of a For Loop is called a tunnel.
• Tunnels can pass information into a loop in two ways:
• indexing enabled—one element of the array is passed into the loop with each iteration
• indexing disabled—entire array is passed into the loop when the loop initializes
• Tunnels can pass information out of a loop in two ways:
• indexing enabled—an array of values is created
• indexing disabled—only the final value is sent out of the loop Creating a subVI
1. Create a working VI containing the programming elements that will become the subVI.
2. Select the programming elements that will become the subVI.
Note: The wires entering and leaving the selection will become the subVI inputs and outputs.