1 lnLroducLlon LabvlLW has loLs of bullL-ln feaLures for conLrol and slmulaLlon. 8elow we see some of Lhe paleLLes and funcLlons avallable lnslde LabvlLW.
lf we have Lhe dlfferenLlal equaLlon(s) or Lhe Lransfer funcLlon(s) for Lhe sysLem we can easlly lmplemenL Lhem ln LabvlLW ln dlfferenL manners. Cne of Lhe mosL used meLhod ls Lo creaLe a block dlagram based on Lhe dlfferenLlal equaLlon(s) or Lhe Lransfer funcLlon(s). 1he block dlagram we creaLe ln LabvlLW wlll Lhen be almosL ldenLlcal Lo Lhe block dlagram creaLed wlLh pen and paper. 8elow we see an example where we conLrol and slmulaLe a dynamlc sysLem based on a dlfferenLlal equaLlon.
LabvlLW offers a speslflc loop used for slmulaLlon purposes, called ConLrol and SlmulaLlon Loop", buL an ordlnary Whlle can also be used. 3 lnLroducLlon LabVIEW Course: Simulation in LabVIEW
ln order Lo creaLe such as sysLem ln LabvlLW, we need Lo use Lhe blocks avallable ln Lhe SlmulaLlon paleLLe, shown below.
Some of Lhe mosL used block are ln Lhe ConLlnuous Llnear SysLems" subpaleLLe. 6
2 ulfferenLlal LquaLlons and 8lock ulagrams Pere you wlll learn how creaLe block dlagrams from your dlfferenLlal equaLlons. ln LabvlLW we can slmulaLe dynamlc sysLems uslng block dlagrams. Lxamp|e: Clven Lhe followlng dynamlc sysLem (dlfferenLlal equaLlon): ! ! !!" !!" 1he block dlagram for Lhls equaLlon ls as follows:
2.1 1. Crder sysLems 1ask 1: 1.order system Clven Lhe followlng sysLem: ! ! !" ! !" uraw a block dlagram for Lhe sysLem uslng pen and paper. Clven Lhe followlng sysLem: ! ! !" ! !" ! !" uraw a block dlagram for Lhe sysLem uslng pen and paper. 7 ulfferenLlal LquaLlons and 8lock ulagrams LabVIEW Course: Simulation in LabVIEW
[Lnd of 1ask] 2.2 2. Crder sysLems 1ask 2: 2.order system Clven Lhe followlng sysLem: !! ! ! ! !! ! !" uraw a block dlagram for Lhe sysLem uslng pen and paper. ! ls Lhe poslLlon ! ls Lhe speed ! ls Lhe acceleraLlon l ls Lhe lorce (conLrol slgnal, u) d and k are consLanLs [Lnd of 1ask] 8
3 SlmulaLlon ln LabvlLW Pere you wlll learn how Lo use Lhe SlmulaLlon paleLLe ln LabvlLW. ?ou need Lhe LabVILW Contro| Des|gn and S|mu|at|on Modu|e". 8elow we see Lhe ConLrol ueslgn & SlmulaLlon paleLLe ln LabvlLW:
8elow we see Lhe SlmulaLlon paleLLe ln LabvlLW:
1he foundaLlon for Lhe slmulaLlons ls Lhe Contro| & S|mu|at|on Loop", whlch ls locaLed ln Lhe upper lefL corner ln Lhe SlmulaLlon" paleLLe. 1he ConLrol & SlmulaLlon Loop ls very slmllar Lo an ordlnary Whlle loop", buL lL has bullL-ln feaLures and ls opLlmlzed for slmulaLlon purposes. Lxamp|e: Clven Lhe followlng dynamlc sysLem (dlfferenLlal equaLlon): 9 SlmulaLlon ln LabvlLW LabVIEW Course: Simulation in LabVIEW
! ! !!" !!" 1he block dlagram for Lhls equaLlon ls as follows:
LabVILW Imp|ementat|on: We can easlly lmplemenL Lhls block dlagram and ploL Lhe resulLs ln LabvlLW:
LeL's seL ! ! !!!" and ! ! !. When we run Lhe slmulaLlon, we geL Lhe followlng resulLs: 10 SlmulaLlon ln LabvlLW LabVIEW Course: Simulation in LabVIEW
S|mu|at|on arameters: ln Lhe example Lhe followlng slmulaLlon parameLers are used (rlghL-cllck on Lhe SlmulaLlon Loop border and selecL Conf|gure S|mu|at|on parameters."):
1ransfer Iunct|on: We can also flnd Lhe Lransfer funcLlon for Lhe sysLem uslng Laplace: 11 SlmulaLlon ln LabvlLW LabVIEW Course: Simulation in LabVIEW
1hls means ! ! !!! and ! ! !!! WlLh ! ! !!!" and ! ! ! we geL: ! ! ! !!!! !!!! ! ! !! ! !
lrom Lhe ploL above we see LhaL Lhls ls correcL (! ! ! and ! ! ! !" ! !!). ln Lhe ConLlnuous Llnear SysLems" paleLLe ln LabvlLW we have a block for deflnlng a Lransfer funcLlon:
1hen we can easlly use Lhe Lransfer funcLlon block lnsLead:
1hls glves Lhe same resulLs: 12 SlmulaLlon ln LabvlLW LabVIEW Course: Simulation in LabVIEW
Note! 1he order of Lhe coefflclenLs are dlfferenL ln LabvlLW and MaLhScrlpL. We deflne a Lransfer funcLlon ln LabvlLW llke Lhls:
1ry wlLh dlfferenL values for ! and ! and see whaL's happen - are Lhe resulLs accordlng Lo Lhe Lheory for 1.order Lransfer funcLlons? 1ask 3: 8acter|a opu|at|on ln Lhls Lask we wlll use LabvlLW and Lhe LabvlLW ConLrol ueslgn and SlmulaLlon Module Lo slmulaLe a slmple model of a bacLerla populaLlon ln a [ar.
1he maLhemaLlcal model for Lhe bacLerla populaLlon ls as follows: 13 SlmulaLlon ln LabvlLW LabVIEW Course: Simulation in LabVIEW
!"#$% #'$()!* +('$% #'$( ) ,* -
1hen Lhe LoLal raLe of change of bacLerla populaLlon ls: ! ! !" ! !! !
SeL !"#/hour and $"%&' bacLerla-hour We wlll slmulaLe Lhe number of bacLerla ln Lhe [ar afLer 1 hour, assumlng LhaL lnlLlally Lhere are 100 bacter|a presenL. ?ou should follow Lhese sLeps: 1. uraw 8lock ulagram uslng pen and paper". 2. SLarL LabvlLW and use Lhe Contro| and S|mu|at|on Loop from ConLrol ueslgn and SlmulaLlon aleLLe ln LabvlLW 3. urag ln Lhe necessary 8locks from Lhe paleLLe (lncludlng Lhe ploL). 4. use Lhe Connect|on W|re" from Lhe 1oo|s pa|ette and draw Lhe necessary wlres. 3. Conf|gure S|mu|at|on arameters (rlghL-cllck on Lhe ConLrol and SlmulaLlon Loop border) 6. SLarL Lhe SlmulaLlon. 1he SlmulaLlon resulL should be presenL ln a ploL. [Lnd of 1ask] 1ask 4: S|mu|at|on of a "spr|ng-mass damper" system |n LabVILW use LabVILW Contro| Des|gn and S|mu|at|on Modu|e" and Lhe Contro| and S|mu|at|on Loop" ln order Lo creaLe a slmulaLlon of a sprlng-mass damper sysLem.
1he dlfferenLlal equaLlon for Lhe sysLem ls as follows: 14 SlmulaLlon ln LabvlLW LabVIEW Course: Simulation in LabVIEW
! ! ! ! !! ! !! ! !"! Where. ! ls an exLernal force applled Lo Lhe sysLem ! ls Lhe damplng consLanL of Lhe sprlng ! ls Lhe sLlffness of Lhe sprlng ! ls a mass ! ls Lhe poslLlon of Lhe mass ! ls Lhe flrsL derlvaLlve of Lhe poslLlon, whlch equals Lhe veloclLy of Lhe mass ! ls Lhe second derlvaLlve of Lhe poslLlon, whlch equals Lhe acceleraLlon of Lhe mass S|mu|ate and p|ot the pos|t|on (!), the ve|oc|ty (!), and the acce|erat|on (!) |n 3 d|fferent p|ots. Note! uraw a block dlagram of Lhe sysLem uslng pen and paper before you lmplemenL Lhe sysLem ln LabvlLW. ln Lhe slmulaLlons you may seL ! ! !!!!! ! ! !! ! ! ! !"# ! ! !"" 1hen Lry Lo seL ! ! !" and see Lhe dlfference. 1ry also dlfferenL values for ! and ! and see whaL happens. 1ry Lo explaln Lhe resulLs. [Lnd of 1ask] 1ask S: S|mu|at|on Loop Clven Lhe followlng (nonllnear) sysLem of a llquld Lank: ! ! !! ! ! ! ! ! ! where ! ls Lhe lnpuL (conLrol value), ! ls Lhe ouLpuL (level), ! ! ls Lhe valve ouLflow parameLer, and ! ! ls Lhe pump lnflow parameLer. SeL ! ! ! !, ! ! ! ! and ! ! ! !. Apply a SLep slgnal for !! CreaLe an appllcaLlon where you use LabVILW Contro| Des|gn and S|mu|at|on Modu|e" and Lhe Contro| and S|mu|at|on Loop" ln order Lo slmulaLe Lhe sysLem. Pere you wlll use Lhe slmulaLlon blocks from Lhe SlmulaLlon paleLLe ln LabvlLW Lo creaLe a conLlnuous model. loL Lhe resulLs. [Lnd of 1ask] 13 SlmulaLlon ln LabvlLW LabVIEW Course: Simulation in LabVIEW
3.1 SlmulaLlon SubsysLem ?ou may creaLe a S|mu|at|on Subsystem (llle - new.):
1he SlmulaLlon SubsysLem ls very useful when deallng wlLh larger slmulaLlon sysLems ln order Lo creaLe a more sLrucLured code. l recommend LhaL you (always) use Lhls feaLure. 1he SlmulaLlon SubsysLem ls almosL equal Lo a normal LabvlLW 8lock ulagram buL noLlce Lhe background color ls sllghLly darker. noLe! ln order Lo open Lhe SlmulaLlon SubsysLem, rlghL-cllck and selecL Cpen SubsysLem". 8elow we se an example of a SlmulaLlon SubsysLem:
16 SlmulaLlon ln LabvlLW LabVIEW Course: Simulation in LabVIEW
As you can see when we use a SlmulaLlon SubsysLem, we don'L need Lo use Lhe SlmulaLlon Loop. 8elow we see an example of a SlmulaLlon SubsysLem used lnslde an ordlnary Whlle loop.
1he advanLage wlLh Lhls soluLlon ls LhaL we can easly reuse Lhe same subsysLem ln many appllcaLlons. AnoLher advanLage ls LhaL we can use an ordlnary Whlle loop lnsLead of a SlmulaLlon Loop. 1hls ls an approach l would recommend ln mosL slLuaLlons. 1ask 6: S|mu|at|on Subsystems CreaLe a SlmulaLlon SubsysLem for one or more of Lhe prevlous Lasks, e.g. Lhe 8acLerla opulaLlon" and/or Lhe Mass-sprlng-damper" sysLem. [Lnd of 1ask] 17
4 ulscreLe SysLems 4.1 lnLroducLlon We wlll use dlfferenL dlscreLlzaLlon meLhods uslng pen and paper exerclses and ln pracLlcal lmplemenLaLlon ln MaLhScrlpL/LabvlLW along wlLh bullL-ln dlscreLlzaLlon meLhods. Lu|er: 1wo commonly used meLhods for dlscreLlzaLlon are as follows: Lu|er forward method: ! ! ! ! ! ! ! !!!! ! !
Lu|er backward method: ! ! ! ! ! !!! ! !! ! !
CLher meLhods are Zero Crder Pold (ZCP), 1usLln's meLhod, eLc. Where ! ! ls Lhe sampllng Llme, and ! ! ! ! , !!!! and !!! ! !! are dlscreLe values. noLe! ulfferenL noLaLlons may be used: ! ! ! ! ! = ! ! ! , ! ! ! ! ! ! !!! ! ! ! !!! , ! ! ! ! ! ! !!! ! ! ! !!! , eLc. Lxamp|e: Clven Lhe followlng sysLem: ! ! !!" ! !" We wlll use Lhe Luler forward meLhod Lo creaLe a dlscreLe verslon of Lhls sysLem. We geL as follows: ! ! ! ! ! !!!! ! ! ! !!"!!! ! !"!!! lurLher: ! ! ! ! ! ! ! ! ! ! !!!"!!! ! !"!!!! 18 ulscreLe SysLems LabVIEW Course: Simulation in LabVIEW
1hls glves: ! ! ! ! ! ! ! ! ! ! !"!!! ! ! ! !"!!!! llnally: ! ! ! ! ! !! ! ! ! !!! ! ! ! ! !!!!! lf we use ! !!!" , ! ! ! and ! ! ! !!!!, we geL: !!! ! !! ! !!!!"#!!!! ! !!!!!!! [Lnd of Lxample] 4.2 ulscreLlzaLlon ln MaLhScrlpL ln MaLhScrlpL we can slmulaLe a dlscreLe sysLem uslng a lor Loop or a Whlle Loop. We can also use some of Lhe bullL-ln funcLlons Lo converL a conLlnuous sLaLe-space model Lo a dlscreLe sLaLe-space model. ln MaLhScrlpL we can use Lhe funcLlon c2d() Lo converL from a conLlnuous sysLem Lo a dlscreLe sysLem. Lxamp|e: We wlll slmulaLe Lhe followlng dlscreLe sysLem ln MaLhScrlpL uslng a lor Loop: ! ! ! ! ! !! ! ! ! !!! ! ! ! ! !"!!! We wlll seL ! ! !!!" and ! ! ! MaLhScrlpL code: % Simulation of discrete model clear clc
% Simulation for k=1:(Tstop/Ts) x(k+1) = (1-a*Ts).*x(k) + Ts*b*uk; end
19 ulscreLe SysLems LabVIEW Course: Simulation in LabVIEW
% Plot the Simulation Results k=0:Ts:Tstop; plot (k, x) grid on We use ! ! ! !!!! as Lhe sampllng Llme. 1hls glves Lhe followlng resulLs:
As you may noLlce, we geL Lhe same resulL as we geL ln Lhe prevlous Lxerclse (10b) when we used LabvlLW. [Lnd of Lxample] Lxamp|e: Clven Lhe followlng sysLem: ! ! !!" ! !" We wlll seL ! ! !!!" and ! ! ! We wlll use Lhe bullL-ln funcLlon c2d() ln order Lo flnd Lhe dlscreLe sysLem: % Find Discrete model clear clc
% Model Parameters a = 0.25; b = 2; Ts = 0.1; %s 20 ulscreLe SysLems LabVIEW Course: Simulation in LabVIEW
% State-space model A = [-a]; B = [b]; C = [1]; D = [0];
model = ss(A,B,C,D) model_discrete = c2d(model, Ts, 'forward') 1hls glves Lhe followlng resulLs: a 0.975 b 0.2 c 1 d 0 or: !!! ! !! ! !!!!"#!!!! ! !!!!!!! !!!! ! !!!! As you see Lhls ls Lhe same resulL as ln Lhe example above. [Lnd of Lxample] 4.3 ulscreLlzaLlon ln LabvlLW 1here are several ways we can make a dlscreLe model ln LabvlLW as well. ln Lhls exerclse we wlll use Lhe lormula node". A lormula node ln LabvlLW evaluaLes maLhemaLlcal formulas and expresslons slmllar Lo C on Lhe block dlagram. ln Lhls way you may use exlsLlng C code dlrecLly lnslde your LabvlLW code. lL ls also useful when you have complex" maLhemaLlcal expresslons.
Lxamp|e: We use Lhe same dlscreLe sysLem as ln prevlous examples: ! ! ! ! ! !! ! ! ! !!! ! ! ! ! !"!!! 21 ulscreLe SysLems LabVIEW Course: Simulation in LabVIEW
WlLh ! ! !!!" and ! ! ! We wlll use Lhe lormula node and a lor Loop ln LabvlLW ln order Lo slmulaLe Lhls dlscreLe model. 1he LabvlLW code ls as follows:
1he slmulaLlon resulLs are as follows:
As you see we geL Lhe same resulLs here as ln Lhe oLher examples. [Lnd of Lxample] 4.4 Lxerclses 22 ulscreLe SysLems LabVIEW Course: Simulation in LabVIEW
1ask 7: D|screte system Clven Lhe followlng sysLem: ! ! ! ! ! ! ! ! !
! ! ! ! ! ! ! !
llnd Lhe dlscreLe sysLem and seL lL on sLaLe-space form (uslng pen and paper). use Luler forward: ! ! ! ! ! ! ! !!!! ! !
ueflne Lhe conLlnuous sLaLe-space model (pen and paper") and Lhen lmplemenL Lhe conLlnuous sLaLe-space model ln MaLhScrlpL. 1hen use MaLhScrlpL Lo flnd Lhe dlscreLe sLaLe-space model. Compare Lhe resulL from Lhe prevlous Lask. SeL ! ! ! ! and ! ! ! !!! [Lnd of 1ask] 1ask 8: D|screte Contro||er A conLroller ls glven by Lhe followlng Lransfer funcLlon: ! ! ! ! !!!! !!!! ! !!! ! !!!! ! ! !
llnd Lhe conLlnuous dlfferenLlal equaLlon. llnd Lhe dlscreLe dlfference equaLlon. use Lhe Luler backward meLhod. [Lnd of 1ask] 1ask 9: D|screte State-space mode| Clven Lhe followlng sysLem: ! ! ! !! ! ! ! ! ! ! ! ! ! !" ! ! ! !! ! ! ! ! ! ! ! ! !! !
23 ulscreLe SysLems LabVIEW Course: Simulation in LabVIEW
llnd Lhe dlscreLe sLaLe-space model ueflne Lhe conLlnuous sLaLe-space model (pen and paper") and Lhen lmplemenL Lhe conLlnuous sLaLe-space model ln MaLhScrlpL. 1hen use MaLhScrlpL Lo flnd Lhe dlscreLe sLaLe-space model. Compare Lhe resulL from Lhe prevlous subLask. use values ! ! ! !! ! ! ! !! ! ! !! ! ! ! and ! ! ! !!!. [Lnd of 1ask] 1ask 10: D|scret|zat|on Clven Lhe followlng (nonllnear) sysLem of a llquld Lank: ! ! !! ! ! ! ! ! ! CreaLe a new appllcaLlon ln LabvlLW where you S|mu|ate Lhe model uslng a Iormu|a Node Lo lmplemenL Lhe dlscreLe model. use one of Lu|ers ulscreLlzaLlon meLhods ln order Lo creaLe a d|screte mode| of Lhe sysLem. Whlch of Lulers meLhods dld you use? Why? use a Whlle Loop or a lor Loop and selecL a proper Llme-sLep ! ! for Lhe slmulaLlon. use Lhe same seLLlngs and values as ln Lhe prevlous case. Compare Lhe resulLs from Lhe 2 dlfferenL meLhods used above. uo you geL Lhe same resulLs? Lxamp|e: lmplemenLlng Lhe model as a Subvl uslng Lhe lormula node:
use Lhe model ln a lor Loop: 24 ulscreLe SysLems LabVIEW Course: Simulation in LabVIEW
[Lnd of 1ask] 1ask 11: 8acter|a opu|at|on - D|screte S|mu|at|on ln Lhls Lask we wlll use LabvlLW and Lhe LabvlLW ConLrol ueslgn and SlmulaLlon Module Lo slmulaLe a slmple model of a bacLerla populaLlon ln a [ar.
1he maLhemaLlcal model for Lhe bacLerla populaLlon ls as follows: !"#$% #'$()!* +('$% #'$( ) ,* -
1hen Lhe LoLal raLe of change of bacLerla populaLlon ls: ! ! !" ! !! !
SeL !"#/hour and $"%&' bacLerla-hour 23 ulscreLe SysLems LabVIEW Course: Simulation in LabVIEW
We wlll slmulaLe Lhe number of bacLerla ln Lhe [ar afLer 1 hour, assumlng LhaL lnlLlally Lhere are 100 bacter|a presenL. llnd Lhe dlscreLe sysLem uslng Luler, Lhen slmulaLe Lhe sysLem ln LabvlLW. use, e.g., Lhe lormula node, eLc. 1ask 12: D|screte Low-pass I||ter 1ransfer funcLlon for a flrsL-order low-pass fllLer may be wrlLLen: ! ! ! ! ! ! ! ! !
Where ! ! ls Lhe Llme-consLanL of Lhe fllLer. CreaLe Lhe dlscreLe low-pass fllLer algorlLhm uslng pen and paper". use Lhe Lu|er 8ackward meLhod. ! ! ! ! ! ! !!! ! !
CreaLe a dlscreLe low-pass fllLer ln LabVILW uslng Lhe Iormu|a Node ln LabvlLW. CreaLe a SubVI of Lhe code. ?ou wlll use Lhls subvl ln your pro[ecL laLer. 1he user needs Lo be able Lo seL Lhe Llme consLanL of Lhe fllLer ! ! from Lhe ouLslde, l.e., lL should be an lnpuL Lo Lhe Subvl. 1he slmulaLlon 1lme-sLep ! ! needs also Lo be seL from Lhe ouLslde. 1esL and make sure your fllLer works! noLe! A golden rule ls LhaL: ! ! ! ! ! !
?ou may, e.g., use Lhe Un|form Wh|te No|se t8yt.v|". Lxample: 26 ulscreLe SysLems LabVIEW Course: Simulation in LabVIEW
[Lnd of 1ask] 1ask 13: D|screte I contro||er A conLlnuous-Llme l conLroller may be wrlLLen: ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !"# ! !
Where . ls Lhe conLroller ouLpuL and ! ls Lhe conLrol error: ! ! ! ! ! ! !!!! 8elow we see a block dlagram of a slmple conLrol sysLem: 27 ulscreLe SysLems LabVIEW Course: Simulation in LabVIEW
CreaLe Lhe dlscreLe l ConLroller algorlLhm uslng pen and paper". use Lhe Luler 8ackward meLhod. ! ! ! ! ! ! !!! ! !
CreaLe a dlscreLe l conLroller ln LabvlLW uslng Lhe Iormu|a Node. CreaLe a SubVI of Lhe code. ?ou wlll use Lhls subvl ln your pro[ecL laLer. [Lnd of 1ask] 1ask 14: S|mu|at|on lmplemenL your l conLroller and Low-pass fllLer ln a slmulaLlon. ?ou may, e.g., use Lhe example Genera| ID S|mu|ator.v|" as a base for your slmulaLlon. use Lhe nl Lxample llnder" (Pelp - llnd Lxamples.) ln order Lo flnd Lhe vl ln LabvlLW. Note! ?ou wlll need Lhe LabVILW ID and Iuzzy Log|c 1oo|k|t" ln order Lo fulflll Lhls 1ask. 8un Lhe example and see how lL ls lmplemenLed and how lL works. Note! Save Lhe vl wlLh a new name and replace Lhe conLroller used ln Lhe example wlLh Lhe conLroller you creaLed ln Lhe prevlous Lask. 28 ulscreLe SysLems LabVIEW Course: Simulation in LabVIEW
nans-etter na|vorsen, M.Sc. 1e|emark Un|vers|ty Co||ege Iacu|ty of 1echno|ogy Department of L|ectr|ca| Lng|neer|ng, Informat|on 1echno|ogy and Cybernet|cs