Introduction to LabVIEW MathScript By: Anthony Antonacci Darryl Morrell Introduction to LabVIEW MathScript By: Anthony Antonacci Darryl Morrell Online: < http://cnx.org/content/col10370/1.3/ > CONNEXIONS Rice University, Houston, Texas This selection and arrangement of content as a collection is copyrighted by Anthony Antonacci. It is licensed under the Creative Commons Attribution 2.0 license (http://creativecommons.org/licenses/by/2. Collection structure revised: August 6, 2006 PDF generated: October 30, 2009 For copyright and attribution information for the modules contained in this collection, see p. Table of Contents 1 LabVIEW MathScript Basics 1.1 Basic operations in LabVIEW MathScript .2 Variables in LabVIEW MathScript .3 Vectors and Arrays in LabVIEW MathScript .4 Graphical representation of data in LabVIEW MathScript.
13 2 Programming in LabVIEW MathScript 2.1 A Very Brief Introduction to Programming in LabVIEW MathScript. 51 iv Chapter 1 LabVIEW MathScript Basics 1 1.1 Basic operations in LabVIEW MathScript 1.1 Basic Operations on Numbers LABVIEW MATHSCRIPT has many arithmetic operations and functions built in. Most of them are straightforward to use. The Table (Table 1.1: Common scalar mathematical operations in LABVIEW MATHSCRIPT) below lists some commonly used scalar operations; in this table, x and y are scalars.
(A scalar is a single number.) Common scalar mathematical operations in LABVIEW MATHSCRIPT Operation LABVIEW MATHSCRIPT x−y x-y x+y x+y xy x*y x y x/y x y x^y ex exp(x) log10 (x) log10(x) ln (x) log(x) log2 (x) log2(x) Table 1.1 Expressions are formed from numbers, variables, and these operations. The operations have dierent precedences. The ^ operation has the highest precedence; ^ operations are evaluated before any other operations. Multiplication and division have the next highest precedence, and addition and subtraction have the lowest precedence.
Precedence is altered by parentheses; expressions within parenthesesare evaluated before expressions outside parentheses.1 The Table (Table 1.2: Example LABVIEW MATHSCRIPT Expressions) below shows several mathematical formulas, the corresponding LABVIEW MATHSCRIPT expressions, and the values that LABVIEW MATHSCRIPT would compute for the expressions. 1 This content is available online at <http://cnx. LABVIEW MATHSCRIPT BASICS Example LABVIEW MATHSCRIPT Expressions 3 formula LABVIEW MATHSCRIPT Computed Value Expression 52 + 42 5^2+4^2 41 (5 + 4) 2 (5+4)^2 81 2+3 4−5 (2 + 3)/(4 - 5) -5 log10 (100) log10(100) 2 ln (4 (2 + 3)) log(4*(2+3)) 2.2 Basic Operations on Matrices In addition to scalars, LABVIEW MATHSCRIPT can operate on matrices. Some common matrix operations are shown in the Table (Table 1.3: Common matrix mathematical operations in LABVIEW MATHSCRIPT) below; in this table, M and N are matrices.
Common matrix mathematical operations in LABVIEW MATHSCRIPT Operation LABVIEW MATHSCRIPT MN M*N M −1 inv(M) M T M' det(M ) det(M) Table 1.3 LABVIEW MATHSCRIPT functions length and size are used to nd the dimensions of vectors and matrices, respectively. LABVIEW MATHSCRIPT can perform an operation on each element of a vector or matrix. To perform an arithmetic operation on each element in a vector (or matrix), rather than on the vector (matrix) itself, then the operator should be preceded by ". Then A^2 will return AA = , while A.3 Given a vector x, compute a vector y having elements y (n) = sin(x(n)) 1.
This can be easily be done in LABVIEW MATHSCRIPT by typing y=1./sin(x) Note that using / in place of ./ would result in the (common) error Matrix dimensions must agree. LABVIEW MATHSCRIPT BASICS 1.3 Complex numbers LABVIEW MATHSCRIPT has excellent support for complex numbers with several built-in functions avail- able. The imaginary unit is denoted by i or (as preferred in electrical engineering) j. To create complex variables z1 = 7 + i and z2 = 2eiπ simply enter z1 = 7 + j and z2 = 2*exp(j*pi) The Table below gives an overview of the basic functions for manipulating complex numbers, where z is a complex number.
Manipulating complex numbers in LABVIEW MATHSCRIPT LABVIEW MATHSCRIPT Re(z ) real(z) Im(z ) imag(z) |z| abs(z) Angle(z ) angle(z) z ∗ conj(z) Table 1.4 Other Useful Details • A semicolon added at the end of a line tells LABVIEW MATHSCRIPT to suppress the command output to the display. • LABVIEW MATHSCRIPT Version 1.0 is case sensitive for both variables and functions; for example, b and B are dierent variables and LABVIEW MATHSCRIPT will recognize the built-in function sum but not SUM. In previous versions, LABVIEW MATHSCRIPT was not case sensitive for function names. • Often it is useful to split a statement over multiple lines.
To split a statement across multiple lines, enter three periods. at the end of the line to indicate it continues on the next line.4 Splitting y = a + b + c over multiple lines.2 Variables in LabVIEW MathScript 1.1 Variables in LABVIEW MATHSCRIPT A variable in LABVIEW MATHSCRIPT is a named value. Using variables allows us to manipulate values symbolically, which is particularly useful when programming.5 Suppose we wish to compute the circumference of a circle of diameter 5 units using the formula c = πd. We could rst set the variable d to a value of 5: 2 This content is available online at <http://cnx.000 Then we could compute the circumference and assign its value to the variable c: c = pi*d c = 15.708 To execute this command, LABVIEW MATHSCRIPT computes the product of the value of d (which LABVIEW MATHSCRIPT knows because we earlier set it to 5) and the value of pi (which is a pre dened variable in LABVIEW MATHSCRIPT) and stores the value of the product in the variable c.
Variable names must begin with an upper- or lower-case letter. They may contain letters, digits, and underscores; they may not contain spaces or punctuation characters. LABVIEW MATHSCRIPT is case sensitive, so A and a are dierent variables.) Valid variable names Which of the following are valid variable names? 1. ecky_ecky_ecky_ecky_ptang_zoo_boing 4.
ecky ecky ecky ecky ptang zoo boing 5. John-Bigboote LABVIEW MATHSCRIPT has several predened variables. The most commonly used include • ans - the default variable in which computation results are stored. Once assigned, variable names remain until they are reassigned or eliminated by the clear command.
LABVIEW MATHSCRIPT variables can contain several types of numerical values. These types include the following: • Scalar values - scalar is a mathematical term for a single value (i. c and d in Example 1.5 are scalar variables. • Vectors - a vector is an ordered series of numbers.
• strings - LABVIEW MATHSCRIPT variables may also contain strings of characters. LABVIEW MATHSCRIPT BASICS 3 1.3 Vectors and Arrays in LabVIEW MathScript 1.1 Vectors and Arrays in LABVIEW MATHSCRIPT An excellent tutorial on how to use LABVIEW MATHSCRIPT's vector and array capabilities is at the Inside LABVIEW MATHSCRIPT Tutorial. 4 Here you can view the general use of LABVIEW MATHSCIRPT and nd usefull information about data handling capabilities such as arrays and vecotrs. One useful method of accessing entire rows or entire columns of the matrix is not mentioned in the tutorial.
Suppose that the matrix A is dened as A = [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20] A = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 An entire row of A can be obtained by specifying a single ":" as the column index: A(2,:) ans = 6 7 8 9 10 Similarly, an entire column of A can be obtained by specifying a single ":" as the row index: A(:,3) ans = 3 8 13 18 5 1.4 Graphical representation of data in LabVIEW MathScript 1.1 Graphical representation of data in LABVIEW MATHSCRIPT LABVIEW MATHSCRIPT provides a great variety of functions and techniques for graphical display of data. The exibility and ease of use of LABVIEW MATHSCRIPT's plotting tools is one of its key strengths. In LABVIEW MATHSCRIPT graphs are shown in a gure window. Several gure windows can be displayed simultaneously, but only one is active.
All graphing commands are applied to the active gure. The command figure(n)will activate gure number n or create a new gure indexed by n. 3 This content is available online at <http://cnx.com/devzone/conceptd.nsf/webmain/76529B03846251A58625709600631C80 5 This content is available online at <http://cnx.2 Tools for plotting In this section we present some of the most commonly used functions for plotting in LABVIEW MATH- SCRIPT. • plot- The plot and stem functions can take a large number of arguments, see help plot and help stem.
For example the line type and color can easily be changed. plot(y) plots the values in vector yversus their index. plot(x,y) plots the values in vector yversus x. The plot function produces a piecewise linear graph between its data values.
With enough data points it looks continuous. • stem- Using stem(y)the data sequence yis plotted as stems from the x-axis terminated with circles for the data values. stem is the natural way of plotting sequences. stem(x,y) plots the data sequence y at the values specied in x.
• xlabel('string')- Labels the x-axis with string. • ylabel('string')- Labels the y-axis with string. • title('string')- Gives the plot the title string. To illustrate this consider the following example.6 In this example we plot the function y = x2 for x 2 [-2; 2].^2; figure(1); plot(x,y); xlabel('x'); ylabel('y=x^2'); title('Simple plot'); figure(2); stem(x,y); xlabel('x'); ylabel('y=x^2'); title('Simple stem plot'); This code produces the following two gures.
LABVIEW MATHSCRIPT BASICS Figure 1.2 Some more commands that can be helpful when working with plots: • hold on / o - Normally hold is o. This means that the plot command replaces the current plot with the new one. To add a new plot to an existing graph use hold on. If you want to overwrite the current plot again, use hold off.
• legend('plot1','plot2',.,'plot N')- The legend command provides an easy way to identify individual plots when there are more than one per gure. A legend box will be added with strings matched to the plots. • axis([xmin xmax ymin ymax])- Use the axis command to set the axis as you wish. Use axis on/off to toggle the axis on and o respectively.
• subplot(m,n,p) -Divides the gure window into m rows, n columns and selects the pp'th subplot as the current plot, e.g subplot(2,1,1) divides the gure in two and selects the upper part. subplot(2,1,2) selects the lower part. • grid on/off - This command adds or removes a rectangular grid to your plot.7 This example illustrates hold, legend and axis.^2; 9 figure(1); plot(x,y1); hold on; plot(x,y2,''); hold off; xlabel('x'); ylabel('y_1=-x^2 and y_2=x^2'); legend('y_1=-x^2','y_2=x^2'); figure(2); plot(x,y1); hold on; plot(x,y2,''); hold off; xlabel('x'); ylabel('y_1=-x^2 and y_2=x^2'); legend('y_1=-x^2','y_2=x^2'); axis([-1 1 -10 10]); The result is shown below. LABVIEW MATHSCRIPT BASICS (a) (b) Figure 1.8 In this example we illustrate subplot and grid.^2; subplot(2,1,1); plot(x,y1); xlabel('x'); ylabel('y_1=-x^2'); grid on; subplot(2,1,2); plot(x,y2); xlabel('x'); ylabel('y_2=x^2'); Now, the result is shown below.3 Printing and exporting graphics After you have created your gures you may want to print them or export them to graphic les.
In the "File" menu use "Print" to print the gure or "Save As" to save your gure to one of the many available graphics formats. Using these options should be sucient in most cases, but there are also a large number of adjustments available by using "Export setup", "Page Setup" and "Print Setup".4 3D Graphics We end this module on graphics with a sneak peek into 3D plots. The new functions here are meshgrid and mesh. In the example below we see that meshgridproduces xand yvectors suitable for 3D plotting and that mesh(x,y,z) plots z as a function of both x and y.9 Example: Creating our rst 3D plot.^2; mesh(x,y,z); xlabel('x'); ylabel('y'); zlabel('z=x^2+y^2'); This code gives us the following 3D plot.
LABVIEW MATHSCRIPT BASICS Figure 1.5 13 Solutions to Exercises in Chapter 1 Solution to Exercise 1. Invalid, because the variable name contains spaces. Invalid, because the variable name begins with a number. Invalid, because the variable name contains a dash.
LABVIEW MATHSCRIPT BASICS Chapter 2 Programming in LabVIEW MathScript 2.