LabVIEW MathScript Module

Download as pdf or txt
Download as pdf or txt
You are on page 1of 35

https://www.halvorsen.

blog

LabVIEW MathScript

Hans-Petter Halvorsen
Contents
1. LabVIEW MathScript
– Basic Examples
– Plotting Examples
– Simulation Examples
– Create Functions
2. MathScript Node
LabVIEW MathScript
• You need to install an additional module called
LabVIEW MathScript Module.
• You should also install LabVIEW Control Design
and Simulation Module because it adds Control
and Simulation features to the MathScript Module
• This module can be used in 2 different ways:
– LabVIEW MathScript – A separate Application similar
to MATLAB (But you need to have LabVIEW installed)
– MathScript Node integrated in LabVIEW Code
LabVIEW MathScript Module
• Add-on Module for LabVIEW where we can do text-
based programming and simulations
• GUI and syntax are identical with MATLAB
• You can run MATLAB scripts in LabVIEW MathScript
with almost no changed needed (assuming you use
the core functionality or the MATLAB Control Toolbox)
• LabVIEW MathScript don’t have the same speed,
flexibility and toolboxes as MATLAB
• If you know MATLAB, you know LabVIEW MathScript
How do you start using MathScript?
• You need to install LabVIEW and the
LabVIEW MathScript Module.
• When necessary software is installed,
start MathScript by open LabVIEW
• From the LabVIEW menu, select Tools
-> MathScript Window...
xxx
https://www.halvorsen.blog

Basic Examples

Hans-Petter Halvorsen
Command Window
The Command Window is the main window in MathScript. Use the Command Window to
enter variables and to run functions and M-files scripts (more about m-files later). Its like an
advanced calculator!

Hit “ENTER” in order to


execute a command

Use “Arrow Up” in order to


browse through old Commands
(“Command History”)
Case Sensitive Variables
MathScript/MATLAB is case sensitive! The variables x and X are not the same.

>> x=5;
>> X=6;
>> x+X >> x=3
x =
ans = 3
11
>> y=4;
>>
Unlike many other languages, where the semicolon is used to terminate commands, in
MathScript/MATLAB the semicolon serves to suppress the output of the line that it
concludes.
clear/clc

The “clear” command deletes all


>> clear existing variables” from the memory

>> clc
The “clc” command removes everything
from the Command Window
clc – clear command window
Built-in Constants
Name Description
i, j Used for complex numbers, e.g., z=2+4i
pi π
inf ∞, Infinity

NaN Not A Number. If you, e.g., divide by zero, you get NaN

>> r=5;
>> A=pi*r^2 >> a=2;
>> z1=3+3i; >> b=0;
>> z2=3+5i;
A = >> a/b
>> z = z1+z2
78.5398
z =
6.0000 + 8.0000i
Mathematical Expressions
MATLAB
log(x)
log10(x)
>> x=2;

Which are correct?


>> y=3*x+2/2 sqrt(x)
y =
7 exp(x)
>> y=(3*x+2)/2
y = x^2
4

Calculate this expression, try with


different values for 𝑥 and 𝑦
Mathematical Expressions

>> x=2;, y=2


>> z = 3*x^2 + sqrt(x^2 + y^2) + exp(log(x))

ans =
16.8284
...
Solving Mathematical Problems
We will use MathScript in order to find the surface area of a
cylinder based on the height (ℎ) and the radius (𝑟) of the cylinder

𝑟=3

ℎ=8 𝐴 =?
Solving Mathematical Problems

MathScript Code:
>> h=8
>> r=3
>> A = 2*pi*r^2 +2*pi*r*h;
A =
207.3451
https://www.halvorsen.blog

Plotting

Hans-Petter Halvorsen
Plotting
Example:
y(t) = 2x + 4
interval on x axis

x = 0:5;

y = 2*x + 4;

plot(x,y)

Useful MathScript functions for plotting: plot(), xlabel(), ylabel(), title(), grid()
Some Examples
>> x = 0:0.1:2*pi;
>> y = sin(x);
>> plot(x,y)

>> x = 0:0.1:2*pi;
>> y = sin(x);
>> y2 = cos(x);
>> plot(x,y, x,y2)

...
>> plot(x,y,'r*', x,y2,'g+')
Plotting Functions
Plotting functions:
Name Description

plot Create a Plot Examples:


figure Define a new Figure/Plot window >> x=0:0.1:2*pi;
grid
on/off
Create Grid lines in a plot >> y=sin(x);
title Add Title to current plot >> plot(x,y)
xlabel Add a Label on the x-axis >> title('Plot Example')
ylabel Add a Label on the x-axis >> xlabel('x')
axis Set xmin,xmax,ymin,ymax
>> ylabel('y=sin(x)')
>> grid on
hold Add several plots in the same Figure
on/off >> axis([0,2*pi,-1,1])
legend Create a legend in the corner (or at a
specified position) of the plot >> legend(’Temperature')
subplot Divide a Figure into several Subplots
Subplots
>> x=0:0.1:2*pi;
>> y=sin(x);
>> y2=cos(x);
>> y3=tan(x);
>> x=0:0.1:2*pi;
>> y=sin(x); >> subplot(3,1,1)
>> plot(x,y)
>> y2=cos(x);
>> subplot(3,1,2)
>> plot(x,y2)
>> subplot(2,1,1)
>> plot(x,y) >> subplot(3,1,3)
>> plot(x,y3)
>> subplot(2,1,2)
>> plot(x,y2) >> x=0:0.1:2*pi;
>> y=sin(x);
>> y2=cos(x);
>> y3=tan(x);
>> y4=atan(x);
>> subplot(2,2,1)
>> plot(x,y)
>> subplot(2,2,2)
>> plot(x,y2)
>> subplot(2,2,3)
>> plot(x,y3)
>> subplot(2,2,4)
>> plot(x,y4)
https://www.halvorsen.blog

Simulation Example

Hans-Petter Halvorsen
Simulation Example
Assume the following model (Differential Equation):
In order to simulate this system in LabVIEW
𝑥̇ = −𝑎𝑥 + 𝑏𝑢 MathScript we typically need to find the
discrete differential equation.
We start by setting 𝑎 = 0.25 and 𝑏 = 2
We can use e.g., the Euler Approximation:

Then we get:
𝑥 𝑘 + 1 − 𝑥(𝑘) 𝑥 𝑘 + 1 − 𝑥(𝑘)
= −𝑎𝑥 𝑘 + 𝑏𝑢 𝑘 𝑥̇ ≈
𝑇! 𝑇!
Finally, we get: Where 𝑇! is the Sampling Time
𝑥 𝑘 + 1 = 1 − 𝑇! 𝑎 𝑥 𝑘 + 𝑇! 𝑏𝑢(𝑘)
This is the discrete version of the differential equation
Code % Simulation of discrete model
clear, clc

% Model Parameters
a = 0.25;b = 2;

% Simulation Parameters
Ts = 0.1; %s
Tstop = 20; %s
uk = 1; % Step Response
x(1) = 0;

% Simulation
for k=1:(Tstop/Ts)
x(k+1) = (1-a*Ts).*x(k) + Ts*b*uk;
end

% Plot the Simulation Results


k=0:Ts:Tstop;
plot(k,x)
grid on
https://www.halvorsen.blog

Creating Functions

Hans-Petter Halvorsen
Create Function
Create Functions in MathScript
Celsius to Fahrenheit
Step 1: Create the Function Step 2: Execute the Function
Function name
Return value input Tc = 23;
Tf = fahrenheit(Tc)
function Tf = fahrenheit(Tc)

Tf = (9/5)*Tc + 32; This can be done from Command


window or Script window
Return value The function body

The function needs to be saved as


“fahrenheit.m” on your hard drive
https://www.halvorsen.blog

Tips and Tricks

Hans-Petter Halvorsen
Tips and Tricks
Use Comments (%)
% This is a comment DO NOT use ”spaces” in Filename or names that are
x=2; % Comment2 similiar to built-in functions in MathScript/MATLAB!
y=3*x % Comment3
- but that have to make sense! Decimal sign: Use ”.”– NOT ”,” !
i.e. y=3.2 – not y=3,2

Yes:
Use english names on variables, functions, files, etc. This No:
is common practice in programming! a=2;
Use always variables – Do not use numbers directly in the b=4; y=2+
expressions! y=a+ 4
b

Functions: clear
Always include these
• Only ONE function in each File! clc
lines in your Script
• The Filename (.m) AND the Name of the Function MUST be close all
the same! …
Tips and Tricks Use help in order to find out
how to use a function in
Greek letters: In math and control theory it is common to use Greek MathScript/MATLAB. In order
letters in formulas, etc. These cannot be used directly in to get help for the tf
function, type the following
MathScript/MATLAB, so you need to find other good alternatives.
in the Command window:
Examples: help tf
𝜔! – w0
𝜁 – zeta or just z Mathematical expressions:
etc. The following applies in MathScript/MATLAB

A Golden Rule: One Task – one file, i.e. DONT


put all the Tasks in one single file!!

x = 2;
y = 2;
z = 3*x^2 + sqrt(x^2 + y^2)+ exp(log(x))
https://www.halvorsen.blog

MathScript Node

Hans-Petter Halvorsen
MathScript Node
With MathScript Node you can create and use
MathScript/MATLAB code within LabVIEW
Example
Alternative: Formula Node
Hans-Petter Halvorsen
University of South-Eastern Norway
www.usn.no

E-mail: [email protected]
Web: https://www.halvorsen.blog

You might also like