Academic Company Events NI Developer Zone Support Solutions Products & Services Contact NI MyNI

Document Type: Tutorial
NI Supported: Yes
Publish Date: Jul 2, 2008


Feedback


Yes No

Related Categories

Products

Development Topic

Related Links - Developer Zone

Related Links - Products and Services

Tutorial: Mathscript and Formula Nodes

0 ratings | 0.00 out of 5
Print

Overview

The Formula Node in the LabVIEW software is a convenient, text-based node you can use to perform complicated mathematical operations on a block diagram using the C++ syntax structure. It is most useful for equations that have many variables or are otherwise complicated. The text-based code simplifies the block diagram and increases its readability. Furthermore, you can copy and paste existing code directly into the Formula Node rather than recreating it graphically.

In addition to text-based equation expressions, the Formula Node can accept text-based versions of if statements, while loops, for loops, and do loops, which are familiar to C programmers. These programming elements are similar but not identical to those you find in C programming.

The Mathscript Node implements similar functions but with the additional functionality of a full .m file compiler, making it useful as a textual language for signal processing, analysis, and math. LabVIEW Mathscript is generally compatible with .m file script syntax, which is widely used by alternative technical computing software.

Using the Formula Node

Complete the following steps to create a VI that computes different formulas depending on whether the product of the inputs is positive or negative.

  1. Open a blank VI from the toolbar by selecting File»New VI.
  2. Place a Formula Node on the block diagram.
    1. Right-click on the diagram and navigate to Programming»Structures»Formula Node.
    2. Click and drag the cursor to place the Formula Node on the block diagram.
  3. Right-click on the border of the Formula Node and select Add Input.

  1. Name the input variable x.
  2. Repeat steps 3 and 4 to add another input and name it y.
  3. Right-click on the border of the Formula Node and select Add Output.

  1. Create two outputs and name them z1 and z2.

Note: It is considered good programming practice to keep the inputs on the left border and the outputs on the right border of the Formula Node. This helps you follow the data flow in your VI and keep your code organized.

  1. In the Formula Node, type the expressions below. Make sure that you complete each command with a semicolon. Notice, however, that the if statement does not require a semicolon after the first line.

if (x*y>0)
z1 = 3*x**2 - 2*y**3;
else z1 = 0;
z2 = sinh(z1);

  1. Create controls and indicators for the inputs and outputs.
    1. Right-click on each input and select Create»Control.

    1. Right-click on each output and select Create»Indicator.

  1. Place a while loop with a stop button around the Formula Node and the controls. Be sure to include a wait function inside the loop to conserve memory usage. Your block diagram should look like the one below.

  1. Click the Run button to run the VI. Change the values of the input controls to see how the outputs change.

In this case, the Formula Node helps minimize the space required on the block diagram. Accomplishing the same task without the use of a Formula Node requires the following code.

Resources

For more information on the Formula Node syntax or the functions available, see the LabVIEW Help by pressing <ctr-H> while you are developing your code. This opens the Context Help, which includes information about the feature that your mouse is hovering over. In the Context Help box, select Detailed help for more information.

Using the MathScript Node

Complete the following steps to create a VI that performs various operations on a 1D array in LabVIEW.

  1. Open a blank VI from the toolbar by selecting File»New VI.
  2. Place a MathScript Node on the block diagram.
    1. Right-click on the diagram and navigate to Programming»Structures»MathScript Node.
    2. Click and drag the cursor to place the MathScript Node on the block diagram.
  3. In the same manner as you implemented in the Formula Node exercise, right-click on the border and select Add Input. Name the input x.
  4. Right-click on the border and select Add Output. Repeat this process to create three outputs labeled y, y1, and d.
  5. Right-click on the y output and select Choose Data Type»1-D Array»DBL 1D. This is necessary to define the data type of the y output. However, the default data type for the MathScript Node output is a scalar double, so you do not need to specify the data type of y1 and d.

  1. On the front panel, place an array of numeric controls. Name the array x and wire it to the x input of the MathScript Node on the block diagram.
  2. Create indicators for each of the three outputs by right-clicking each output and selecting Create»Indicator.
  3. In the MathScript Node, type the following expressions:

y = x.^2;
y1 = y(1);
d = dot(x,y);

  1. Place a while loop with a stop button around the MathScript Node and the controls. Be sure to include a wait function inside the loop to conserve memory usage. Your block diagram should look like the one below.

  1. On the front panel, expand the arrays to show multiple elements. With the cursor, grab the bottom middle selector of the array and drag it down to show multiple elements.

  1. Begin by placing a 1, 2, and 3 in the first three elements of the x control. Your front panel should look similar to the one below. Note that the fourth and fifth elements are grayed out. This is because they are not initialized. You can initialize them by clicking inside the cell and entering a value. To uninitialize a cell, right-click on the element and select Data Operations»Delete Element.

  1. Click the Run button. Change the values of the elements in the array to see how the outputs change.

Resources

For more information on the Formula Node syntax or the functions available, see the LabVIEW Help.  Remember you can access the help by using the Context Help box (ctr-H). View the related links below to learn more about the MathScript Node.

Developer Zone: Inside LabVIEW MathScript
Developer Zone: Developing Algorithms Using LabVIEW MathScript: Part 1 – The LabVIEW MathScript Node
Developer Zone: Developing Algorithms Using LabVIEW MathScript: Part 2 – The MathScript Interactive Window


Video    Exercise    Arrays, Clusters, and Text Based Nodes    Modules Home   FIRST Community

0 ratings | 0.00 out of 5
Print

Reader Comments | Submit a comment »

 

Legal
This tutorial (this "tutorial") was developed by National Instruments ("NI"). Although technical support of this tutorial may be made available by National Instruments, the content in this tutorial may not be completely tested and verified, and NI does not guarantee its quality in any way or that NI will continue to support this content with each new revision of related products and drivers. THIS TUTORIAL IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND AND SUBJECT TO CERTAIN RESTRICTIONS AS MORE SPECIFICALLY SET FORTH IN NI.COM'S TERMS OF USE (http://ni.com/legal/termsofuse/unitedstates/us/).