LabVIEW Math Functions
1. Show context help with block diagram menu options: Help / Show ContextHelp
3.2.2 Less Commonly Used Numeric Functions
In this section we will take a look at two less commonly used functions:
• Quotient and Remainder function
• Random Number function
• Formula function
We will build the Quotient and Remainder function into a VI to investigate how this function works, and to present an efficient way to create a VI for math functions. Then we’ll look at an example that uses the Random Number function to simulate rolling dice. Finally, we will use LabVIEW’s Formula function, which is a very flexible function that operates much like a scientific calculator.
Quotient and Remainder Function
The Quotient and Remainder function is an interesting function that receives two inputs (numerator and denominator) and returns two results:
• the integer number of times that denominator can divide into numerator (integer quotient)
• the remainder of that division
To test this function the block diagram shown in Figure 3.15 was created.
Note: The “floor” used as floor(x/y) in Figure 3.15 is a mathematical operation that returns the integer smaller than or equal to the result of (x/y). For example, 26/5 5.2 so floor(26/5) 5. This is also called the integer quotient.
Figure 3.15
Block diagram of Quotient and Remainder VI.
This VI was created almost entirely from the block diagram, which can save a lot of steps for VIs designed to solve math problems. To create the Quotient and Remainder VI, use the following steps:
1. Create a blank VI.
2. Add the Quotient and Remainder function to the block diagram:
Functions Palette / Mathematics Group / Numeric Group / Quotient and Remainder Function
3. Add needed controls and indicators; label and wire them.
The Quotient and Remainder function has two inputs and two outputs. If you right-click on an input or an output, a pop-up menu will be displayed, and one of the menu options is Create / Control (for an input) or Create / Indicator (for an output).
By right-clicking on each input and output on the Quotient and Remainder function
Section 3.2 Basic Math Functions 71
block, you can quickly add the needed controls and indicators to the block diagram—
and they will automatically be:
• the correct data type
• labeled as shown in Figure 3.16
• wired to the function
The labels on the outputs seemed a little vague, so all of the labels were modi-fied to add more descriptive names:
• “x” became “Numerator: x”
• “y” became “Denominator: y”
• “x-y*floor(x/y)” became “Remainder: x-y*floor(x/y)”
• “floor(x/y)” became “Integer Quotient: floor(x/y)”
At this point the VI is complete and will run. For appearance, the controls on the front panel may need to be rearranged a little.
Note: The VI will solve for the integer quotient and remainder only once, because the controls were not built into a While Loop to keep the VI running. The While Loop has been omitted here to demonstrate that you can solve problems in Lab-VIEW without the While Loop—you just have to remember to run the VI after entering all of the input values (or use the Run Continuously button).
In Figure 3.17, the value 26 was divided by 5. We expect that the integer quotient will be 5 with a remainder of 1, and that is the LabVIEW result.
Figure 3.16
Block diagram showing default labeling.
Figure 3.17
Using the Quotient and Remainder VI.
Random Number Function
The icon for LabVIEW’s Random Number function looks like a pair of dice (see Figure 3.18), but it does not return an integer between 1 and 6. Instead it returns a
double-precision floating point value greater than or equal to 0 and less than 1. The distribution of returned values is uniform (all values in the range are equally likely to occur).
To simulate rolling a die (one of a pair of dice), we need to
• multiply the random number by 6 (values will range from 0.00 to 5.99)
• round down to the next lower integer (values will range from 0 to 5)—this requires the Round Towards – Infinity function:
Functions Palette / Mathematics Group / Numeric Group / Round Towards – Infinity function
• add 1 (values will range from 1 to 6)
A block diagram that accomplishes this task is shown in Figure 3.18.
The front panel is shown in Figure 3.19.
Each time the VI is run, a value between 1 and 6 is displayed in the Result indicator.
Once you have written a VI, you can use that VI on the block diagram of another VI. For example, the VI in Figure 3.20 calls the Roll One Die.vi three times to simulate rolling three dice.
Figure 3.18
Block diagram for simulated roll of one die.
Figure 3.19 Simulated Die Roll VI.
Figure 3.20 A VI simulating rolling three dice.
Formula Express VI
LabVIEW provides an interesting Express VI, called the Formula Express VI, that provides a lot of problem-solving power.
Section 3.2 Basic Math Functions 73
The Formula Express VI functions, in many ways, like a scientific calculator. It is located in the Script and Formulas Group:
Functions Palette / Mathematics Group / Script and Formulas Group / Formula
When you move the Formula Express VI to a block diagram, the icon for the Formula (see Figure 3.21) is displayed on the block diagram, and the Configure Formula dialog (Figure 3.22) is opened.
Express VI—an Express VI is a more sophisticated VI that can be configured using a dialog box. The dialog box automatically opens when the Express VI is placed on the block diagram. Double-click the VI’s icon to re-open the dialog when needed. Express VIs appear on the Functions Palette with a blue strip across the top of the icon (or blue border when expanded).
Figure 3.21
Icon for Formula Express VI.
Figure 3.22
Configure Formula dialog.
Your formula can accept up to eight inputs, and you can build those input val-ues into a formula that can include a wide range of mathematical functions. In this example, three inputs (X1, X2, and X3) have been used to build the formula
X1 3 * X2 5 * sqrt(X3)
Clicking OK closes the Configure Formula dialog, and the Formula icon on the block diagram is modified to reflect the actual number of inputs as shown in Figure 3.23. The necessary controls and indicator have been created in the block dia-gram to allow values for X1, X2, and X3 to be set, and to display the computed result.
Figure 3.23
Block diagram for solving a formula (incomplete).
Figure 3.24
Using the Formula Express VI in Formula Solver VI.
When the VI is run, the solution to 2 + 3 * 4 - 5 * sqrt(6) is found to be 1.753, as shown in Figure 3.24.