Tài liệu giới thiệu và hướng dẫn thuật toán trong chương trình LabVIEW để giúp người học hiểu về nguyên lý lập trình trong chương trình. Nội dung trong tài liệu được trình bày một cách dễ hiểu và chi tiêt để người học có thể ứng dụng xây dựng chương trình riêng của bản thân.
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.0/) 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 51 Table of Contents LabVIEW MathScript Basics 1.1 Basic operations in LabVIEW MathScript 1.2 Variables in LabVIEW MathScript 1.3 Vectors and Arrays in LabVIEW MathScript 1.4 Graphical representation of data in LabVIEW MathScript Solutions 13 Programming in LabVIEW MathScript 2.1 A Very Brief Introduction to Programming in LabVIEW MathScript 15 2.2 For Loops 16 2.3 Conditionals 23 2.4 While Loops 30 Solutions 33 Bibliography 48 Index 50 Attributions 51 iv Chapter LabVIEW MathScript Basics 1.1 Basic operations in LabVIEW MathScript 1.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 y x/y x 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 Example 1.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 This content is available online at CHAPTER LABVIEW MATHSCRIPT BASICS Example LABVIEW MATHSCRIPT Expressions formula LABVIEW MATHSCRIPT Expression Computed Value 52 + 42 5^2+4^2 41 (5+4)^2 81 2+3 4−5 (2 + 3)/(4 - 5) -5 log10 (100) log10(100) ln (4 (2 + 3)) log(4*(2+3)) 2.9957 (5 + 4) Table 1.2 1.1.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) T M' 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 ".", e.g *, ^ and / Example1.2 Let A = 1 1 1 1 Then A^2 will return AA = 2 2 , while A.^2 will return 12 12 12 12 = Example 1.3 Given a vector x, compute a vector y having elements y (n) = sin(x(n)) 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 CHAPTER LABVIEW MATHSCRIPT BASICS 1.1.3 Complex numbers LABVIEW MATHSCRIPT has excellent support for complex numbers with several built-in functions available The imaginary unit is denoted by i or (as preferred in electrical engineering) j To create complex variables z1 = + i and z2 = 2eiπ simply enter z1 = + 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 ) Im(z ) real(z) |z| abs(z) Angle(z ) angle(z) z imag(z) conj(z) ∗ Table 1.4 1.1.4 Other Useful Details 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 • A Example 1.4 Splitting y = a + b + c over multiple lines y = a + b c; 1.2 Variables in LabVIEW MathScript 1.2.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 Example 1.5 Suppose we wish to compute the circumference of a circle of diameter units using the formula c = πd We could rst set the variable d to a value of 5: This content is available online at 41 40 20 −20 −40 −60 −80 −100 50 100 150 Plot of velocity versus time The velocity is computed by numerically integrating the measured acceleration Figure 2.10: This code can be easily extended to also compute altitude while it is computing velocity: Acceleration = csvread('Acceleration.csv'); t=Acceleration (:,1); a=Acceleration (:,2); v(1)=0; % Initial velocity s(1)=0; % Initial altitude for n=1:181 v(n+1)=(t(n+1)-t(n))*(a(n+1)+a(n))/2+v(n); s(n+1)=(t(n+1)-t(n))*(v(n+1)+v(n))/2+s(n); end plot(t,s) This code creates the plot in Figure 2.11 42 CHAPTER PROGRAMMING IN LABVIEW MATHSCRIPT 1000 −1000 −2000 −3000 −4000 −5000 Figure 2.11: 50 100 150 Plot of altitude versus time Solution to Exercise 2.19 (p 23) This solution by Nicholas Gruman uses the LABVIEW MATHSCRIPT trapz function to compute velocity with the trapezoidal rule with respect to time at all given points in time: Acceleration = csvread('Acceleration.csv'); t=Acceleration(:,1); A=Acceleration(:,2); c = length(t); for i = 1:c v(i)=trapz(t(1:i),A(1:i)); end Altitude could also be computed by adding the following line in the same For loop as the previous code: s(i)=trapz(t(1:i),v(1:i)); Solution to Exercise 2.20 (p 25) ' a is not equal to 0' Solution to Exercise 2.21 (p 26) ' a is positive' Solution to Exercise 2.22 (p 26) b*c gives a value of 6, and < 6, so this code will print 'Hello world' Solution to Exercise 2.23 (p 26) The parentheses around the relational expression a > will not change its validity, so this code will print 'a is positive' Solution to Exercise 2.24 (p 26) The parentheses in this expression change its meaning completely First, a < b is evaluated, and since it is false for the given values of a and b, it evaluates to zero The zero is than multiplied by c, giving a value of zero which is interpreted by LABVIEW MATHSCRIPT as false So this code prints 'Goodbye world' Solution to Exercise 2.25 (p 27) ' p1 and p2 are not equal' 43 Solution to Exercise 2.26 (p 27) This code will generate an error message, since a = b assigns the value of b to a To check if a and b are equal, use a == b Solution to Exercise 2.27 (p 27) Any value that is not zero Solution to Exercise 2.28 (p 27) Any value greater than or equal to and less than Solution to Exercise 2.29 (p 27) Any value less than or greater than 10 Solution to Exercise 2.30 (p 28) Every value of a will print 'Hello world' Solution to Exercise 2.31 (p 28) if a >= -0.01 && a = && n = && n = 25 && n =1 && n=5 && n=25 && n=100;%if n=100 or more, CA is $11.00 CA=11.00; end%this ends the if statement for CA if n>=1 && n=10 && n=50 && n=100;%if n=100 or more, CB is $23.33 CB=23.33; end%this ends the if statement for CB if n>=1 && n=25 && n=50;%if n=50 or more, CC is $15.78 CC=15.78; end%this ends the if statement for CC if n>=1 && n=10 && n=100;%if n=100 or more, CD is $9.62 CD=9.62; end%this ends the if statement CO=45.00; Cunit=CO + CA + CB + CC + CD; Solution to Exercise 2.34 (p 29) This code was originally written by Bryson Hinton and then modied: cunit = zeros(1,150); c0 = 45; for n=1:150 %compute price for part A if n >= && n = && n = 25 && n = && n = 10 && n = 50 && n = && n = 25 && n = && n = 10 && n