Telemark University College Department of Electrical Engineering, Information Technology and Cybernetics LabVIEW MathScript Quick Reference Hans-Petter Halvorsen, 2012.06.14 Faculty of Technology, Postboks 203, Kjølnes ring 56, N-3901 Porsgrunn, Norway. Tel: +47 35 57 50 00 Fax: +47 35 57 54 01
LabVIEW MathScript Quick Reference 2/16 Introduction MathScript is a high-level, text- based programming language. MathScript includes more than 800 built-in functions and the syntax is similar to MATLAB. You may also create custom-made m-file like you do in MATLAB. MathScript is an add-on module to LabVIEW but you don t need to know LabVIEW programming in order to use MathScript. How do you start using MathScript? You need to install LabVIEW and the LabVIEW MathScript RT Module. When necessary software is installed, start MathScript by open LabVIEW: In the Getting Started window, select Tools -> MathScript Window...:
LabVIEW MathScript Quick Reference 3/16 You can use the LabVIEW MathScript Window to enter commands one at time (Command Window). You also can enter batch scripts in a simple text editor window (Script Window), loaded from a text file, or imported from a separate text editor. Below we see the LabVIEW MathScript window:
LabVIEW MathScript Quick Reference 4/16 Basic Operations Basic Functions Here are some descriptions for the most used basic MathScript functions. Function Description Example help help <function> who, whos MathScript displays the help information available Display help about a specific function who lists in alphabetical order all variables in the currently active workspace. >>help >>help plot >>who >>whos clear Clear variables and functions from memory. >>clear >>clear x size Size of arrays, matrices >>x=[1 2 ; 3 4]; >>size(a) length Length of a vector >>x=[1:1:10]; >>length(x) format Set output format disp Display text or array >>A=[1 2;3 4]; >>disp(a) plot This function is used to create a plot >>x=[1:1:10]; >>plot(x) clc Clear the Command window >>cls >>y=sin(x); >>plot(x,y) rand Creates a random number, vector or matrix >>rand >>rand(2,1) max Find the largest number in a vector >>x=[1:1:10] >>max(x) min Find the smallest number in a vector >>x=[1:1:10] >>min(x) mean Average or mean value >>x=[1:1:10] >>mean(x) std Standard deviation >>x=[1:1:10] >>std(x)
LabVIEW MathScript Quick Reference 5/16 Linear Algebra Here are some useful functions for Linear Algebra in MathScript: Function Description Example rank Find the rank of a matrix. Provides an estimate of the number of linearly independent rows or columns of a matrix A. >>A=[1 2; 3 4] >>rank(a) det Find the determinant of a square matrix >>A=[1 2; 3 4] >>det(a) inv Find the inverse of a square matrix >>A=[1 2; 3 4] >>inv(a) eig Find the eigenvalues of a square matrix >>A=[1 2; 3 4] >>eig(a) ones Creates an array or matrix with only ones >>ones(2) >>ones(2,1) eye Creates an identity matrix >>eye(2) diag Find the diagonal elements in a matrix >>A=[1 2; 3 4] >>diag(a) Type help matfun (Matrix functions - numerical linear algebra) in the Command Window for more information, or type help elmat (Elementary matrices and matrix manipulation). You may also type help <functionname> for help about a specific function. Matrix Operators We have the following basic matrix operations: We have the following array operators: The basic matrix operations can be modified for element-by-element operations by preceding the operator with a period. The modified operations are known as array operations.
LabVIEW MathScript Quick Reference 6/16 Colon Notation The colon notation is very useful for creating vectors: Note!
LabVIEW MathScript Quick Reference 7/16 Plotting Plots functions: Here are some useful functions for creating plots: Function Description Example plot Generates a plot. plot(y) plots the columns of y against the indexes of the columns. >X = [0:0.01:1]; >Y = X.*X; >plot(x, Y) figure Create a new figure window >>figure >>figure(1) subplot grid axis title xlabel ylabel legend Create subplots in a Figure. subplot(m,n,p) or subplot(mnp), breaks the Figure window into an m-by-n matrix of small axes, selects the p-th axes for the current plot. The axes are counted along the top row of the Figure window, then the second row, etc. Creates grid lines in a plot. grid on adds major grid lines to the current plot. grid off removes major and minor grid lines from the current plot. Control axis scaling and appearance. axis([xmin xmax ymin ymax]) sets the limits for the x- and y-axis of the current axes. Add title to current plot title('string') Add xlabel to current plot xlabel('string') Add ylabel to current plot ylabel('string') Creates a legend in the corner (or at a specified position) of the plot >>subplot(2,2,1) >>grid >>grid on >>grid off >>axis([xmin xmax ymin ymax]) >>axis off >>axis on >>title('this is a title') >> xlabel('time') >> ylabel('temperature') >> legend('temperature') hold Freezes the current plot, so that additional plots can be overlaid >>hold on >>hold off Type help graphics in the Command Window for more information, or type help <functionname> for help about a specific function. Below we see some examples of how to use the different plot functions:
LabVIEW MathScript Quick Reference 8/16 For line colors and line-styles we have the following properties we can use for the plot function: Line Styles: Colors: Marker specifiers:
LabVIEW MathScript Quick Reference 9/16 User-defined Functions MathScript includes more than 1000 built-in functions that you can use, but sometimes you need to create your own functions. To define your own function in MathScript, use the following syntax: function outputs = function_name(inputs) % documentation Here is the procedure for creating a user-defined function in MathScript: Note! It is recommended that you use lowercase in the function name. You should neither use spaces; use an underscore _ if you need to separate words. Example: function total = add(x,y) % this function add 2 numbers total = x+y;
LabVIEW MathScript Quick Reference 10/16 Flow Control If-Else Example: function x = solveeq(a,b,c) if a~=0 x = zeros(2,1); x(1,1)=(-b+sqrt(b^2-4*a*c))/(2*a); x(2,1)=(-b-sqrt(b^2-4*a*c))/(2*a); elseif b~=0 x=-c/b; elseif c~=0 disp('no solution') else disp('any complex number is a solution') end Note! You have to use if n == 5 not if n = 5 The general syntax is as follows: if expression1 statements1 elseif expression2 statements2 else statements3 Switch-Case Statement Example: function result = calc_circle(r,x) switch x case 1 result=pi*r*r; case 2 result=2*pi*r; otherwise
LabVIEW MathScript Quick Reference 11/16 end disp('only 1 or 2 is legal values for x') The general syntax is as follows: switch variable case case_value1 statements1 case case_value2 statements2 otherwise statements end For Loop Example: function f = fibonacci(n) f=zeros(n,1); f(1)=0; f(2)=1; for k=3:n f(k)=f(k-1)+f(k-2); end The general syntax is as follows: for variable = initval:endval end statement... statement
LabVIEW MathScript Quick Reference 12/16 While Loop Example: function f = fibonacci2(max) f(1)=0; f(2)=1; k=3; while f < max f(k)=f(k-1)+f(k-2); k=k+1; end The general syntax is as follows: while expression end statements
LabVIEW MathScript Quick Reference 13/16 Tips & Tricks Use the Script window not the Command window. You may use the Command window for simple things like, e.g., for getting help about specific functions, etc. help <function>. In the Script window you can save your code as so-called.m files. Comments - % % This is a comment x=2; y=3*x Use comments as part of your code to increase the readability of your program! Decimal point: Use a period (.) not comma (,) i.e. y=3.2 not y=3,2 File Names: Don t use spaces in filenames or in function names! Neither should you use the same names for your own functions as the built-in functions! Use Arrow Up and Arrow Down in order to browse previous used commands and functions in the Command Window A golden rule: One task one file, i.e. don t do all the tasks in one file! User-defined Functions: - Only one function in each.m file! - The filename (.m) and the name of the function need to be the same!
LabVIEW MathScript Quick Reference 14/16 Use semicolon (;) after commands/functions if you don t need to display the answer on the screen: a=2; b=4; y=a+b Use English names on variables, functions, files, etc. This is common practice in programming! Use always variables not use numbers directly in expressions a=2; b=4; y=a+b Not: y=2+4 Help: Use the help functionality in order to find out more about the functions you are going to use each function can be used in different manners. In order to get help for a specific function, type the following in the Command window: help <functionname> Always include the following commands on the top of your scripts: clear clc close all This make sure you don t get in trouble with old variables, figures, etc. Greek letters: In mathematics and control theory it is common to use greek letters in formulas, etc. These letters cannot be used directly in MathScript. So make sure to use good alternatives to these letter, ex.: w0 zeta or just z etc.
LabVIEW MathScript Quick Reference 15/16 Mathematical Expressions: In MathScript you can use the following: x^2 sqrt(x) log(x) Log10(x) exp(x) pi
Telemark University College Faculty of Technology Kjølnes Ring 56 N-3918 Porsgrunn, Norway www.hit.no Hans-Petter Halvorsen, M.Sc. Telemark University College Department of Electrical Engineering, Information Technology and Cybernetics E-mail: hans.p.halvorsen@hit.no Blog: / Room: B-237a