MathScript Node and XY Graph
Graphical Instrument Scripting: From Equations to XY Data
Graphical Instrument Scripting connects a text-based numerical calculation to a graphical instrument display. You supply parameters, evaluate an equation at selected inputs, and send the resulting coordinate pairs to an XY Graph. The implementation here assumes a legacy LabVIEW installation that supports MathScript and has the required module installed. The focus is the calculation-to-display connection, rather than data acquisition or repeated execution.
Connect the script to the diagram
A MathScript Node is a block-diagram structure that executes text-based numerical code and exchanges values through named terminals. Writing a variable inside the script does not automatically create a terminal on the diagram.
Add input terminals on the node's left border for externally supplied parameters. Add output terminals on its right border for results needed elsewhere. Match each terminal name exactly to its script variable, including capitalization. Assign each requested output during the calculation.
Set output types to match the results. A single real number uses a scalar floating-point type; a sequence of real samples uses a one-dimensional double-precision array, commonly shown as DBL 1D. A scalar and an array containing one value still have different shapes.
Evaluate an equation across an array
The governing relationship is a point-by-point calculation:
An element-wise operation applies an operation separately to corresponding array entries. In MathScript, dotted operators distinguish these calculations from matrix operations.
Intended calculation | MathScript operator | Meaning for arrays |
|---|---|---|
Multiply corresponding entries | .* | One product per corresponding pair |
Divide corresponding entries | ./ | One quotient per corresponding pair |
Raise each entry to a power | .^ | One powered value per entry |
Multiply matrices | * | Matrix product; scalar multiplication also works |
For example, let displacement x be measured in metres and force follow
With scalar inputs k and b, the script is:
x = linspace(0, 0.2, 3);
F = k*x.^2 + b;The function linspace(start, stop, N) creates N equally spaced values, including both endpoints. Here, x contains 0, 0.1, and 0.2 m. Squaring each entry, multiplying by 200, and adding 3 produces forces of 3, 5, and 11 N. Scalar multiplication by k is valid without a dot.
Build the XY Graph input
For the single-plot representation used here, an XY Graph accepts a cluster whose first element is the x array and whose second element is the y array. Use Bundle to create that cluster, then wire its output to the graph.
The pairing rule is
Check that both arrays contain the intended N samples in corresponding order. An XY Graph uses the supplied x coordinates; they need not be equally spaced. It does not infer physical coordinates from array positions.
For several plots, bundle each plot's x and y arrays separately, then build an array of those clusters. Each plot can have its own sampling locations.
Check the numerical meaning
Numerical arrays do not explain their physical units by themselves. Keep a consistent unit convention in the calculation and label the graph axes accordingly. Changing an axis label from metres to millimetres does not multiply the underlying numbers by 1000.
Before trusting a curve, calculate one point by hand, inspect array lengths, and check the endpoints. If points are joined by lines, their array order determines the connection order. A smooth-looking line alone does not establish that coordinates, units, or equations are correct.
What to remember
- Match script variables to named terminals and suitable output types.
- Use element-wise operators for point-by-point array calculations.
- Bundle x first and y second, preserving corresponding entries.
- Convert numerical values when changing units, then label the axes.
Example: Displaying a Predicted Actuator Motion
An actuator's predicted position over a short test is
where d0=0.015 m, v0=0.020 m/s, and a=0.030 m/s2. Treat acceleration as constant over 0≤t≤2.00 s. Use a legacy LabVIEW installation with MathScript support.
The scalar parameters enter a MathScript Node through terminals named d0, v0, and a.
a) Write a script that creates five equally spaced time samples, including both endpoints, and calculates the position array. Specify the output terminals and their types.
Five samples spanning both endpoints create ________ equal intervals. Their spacing is
Use linspace to generate those samples, then evaluate the motion model with an element-wise square:
t = linspace(0, 2.00, 5);
d = d0 + v0*t + 0.5*a*t.^2;The resulting time array is
Add output terminals named t and d. Configure both as one-dimensional DBL arrays because each contains five real-valued samples. The inputs remain scalars. The expression t.^2 squares each time sample; multiplication by the scalar coefficients then applies the physical model to every entry.
b) Calculate the position at t=1.50 s by hand to check the script.
The time 1.50 s is the ________ stored sample. Evaluate the initial position, velocity contribution, and acceleration contribution separately before adding them.
Every contribution has units of metres. The fourth entry of the scripted position array should therefore be 0.07875 m, apart from floating-point rounding.
c) Prepare an XY Graph with time in seconds and position in millimetres. Describe the wiring and calculate the final displayed coordinate.
Convert the position values before sending them to the graph. Merely changing an axis label would leave the numerical values in metres.
d_mm = 1000*d;Add d_mm as a one-dimensional DBL output. Use Bundle with t as its first element and d_mm as its second element, then wire the cluster to the XY Graph. Label the horizontal axis Time (s) and the vertical axis Position (mm).
At the final sample, t=2.00 s:
The final displayed coordinate is (2.00 s,115 mm). Both graph-input arrays contain five entries, and the conversion preserves their sample order.
A LabVIEW VI computes a physical model in a script and plots the result as paired coordinates on the front panel. Match each element or operator used in that VI to the description of its role.
Generates values starting at a and stepping by h, stopping at the last value that does not pass b
Combines a horizontal-coordinate array and a vertical-coordinate array into the single cluster a paired-coordinate plot accepts
A block-diagram structure that runs text-based, .m-style commands between its input and output terminals
Multiplies two arrays entry by entry instead of forming a matrix product
Returns n evenly spaced values from a to b, including both endpoints
A front-panel indicator that plots each vertical value against its own paired horizontal value, so samples need not be evenly spaced
The .* operator
XY Graph
Colon range a:h:b
Bundle function
linspace(a, b, n)
MathScript Node
In a legacy LabVIEW MathScript Node, x = [0.2 0.5 0.9]; stores three displacements in metres. A scalar input k equals 40 N/m2. The force model is Fi=kxi2 for each displacement sample.
Which statement produces the required three-entry force vector in newtons?