Sunteți pe pagina 1din 20

Tips

TIPS

ABOUT

Effects

BASICS

SearchControlTutorials

HARDWARE

INDEX

NEXT

INTRODUCTION CRUISECONTROL MOTORSPEED


SYSTEM
MODELING

SimulinkBasicsTutorial

ANALYSIS

Simulink is a graphical extension to MATLAB for modeling and simulation of


systems. One of the main advantages of Simulink is the ability to model a
CONTROL
PID
ROOTLOCUS
FREQUENCY
STATESPACE
DIGITAL

nonlinearsystem,whichatransferfunctionisunabletodo.Anotheradvantage
ofSimulinkistheabilitytotakeoninitialconditions.Whenatransferfunctionis
built,theinitialconditionsareassumedtobezero.

Contents
StartingSimulink
ModelFiles
BasicElements

SIMULINK
MODELING

SimpleExample
RunningSimulations
BuildingSystems

CONTROL

InSimulink,systemsaredrawnonscreenasblockdiagrams.Manyelementsof
block diagrams are available, such as transfer functions, summing junctions,
etc.,aswellasvirtualinputandoutputdevicessuchasfunctiongeneratorsand
oscilloscopes. Simulink is integrated with MATLAB and data can be easily
transferedbetweentheprograms.Inthesetutorials,wewillapplySimulink to
theexamplesfromtheMATLABtutorialstomodelthesystems,buildcontrollers,
and simulate the systems. Simulink is supported on Unix, Macintosh, and
WindowsenvironmentsandisincludedinthestudentversionofMATLABfor
personal computers. For more information on Simulink, please visit the
Mathworkslinkatthetopofthepage.
Theideabehindthesetutorialsisthatyoucanviewtheminonewindowwhile
running Simulink in another window. Systemmodel files can be downloaded
from the tutorials and opened in Simulink. You will modify and extend these
system while learning to use Simulink for system modeling, control, and
simulation.Donotconfusethewindows,icons,andmenusinthetutorialsfor
youractualSimulinkwindows.Mostimagesinthesetutorialsarenotlivethey
simplydisplaywhatyoushouldseeinyourownSimulinkwindows.AllSimulink
operationsshouldbedoneinyourSimulinkwindows.

StartingSimulink
Simulink is started from the MATLAB command prompt by entering the
followingcommand:
simulink
Alternatively,youcanhittheSimulinkbuttonatthetopoftheMATLABwindow
asshownhere:

Whenitstarts,Simulinkbringsupasinglewindow,entitledSimulink Library
Browserwhichcanbeseenhere.

ModelFiles
In Simulink, a model is a collection of blocks which, in general, represents a
system.Inaddition,todrawingamodelintoablankmodelwindow,previously
savedmodelfilescanbeloadedeitherfromtheFilemenuorfromtheMATLAB
commandprompt.Asanexample,downloadthefollowingmodelfilebyclicking
on the following link and saving the file in the directory you are running
MATLABfrom.
simple.mdl
Open this file in Simulink by entering the following command in the MATLAB
commandwindow.(Alternatively,youcanloadthisfileusingtheOpenoptionin
theFilemenuinSimulink,orbyhittingCtrlOinSimulink).
simple
Thefollowingmodelwindowshouldappear.

A new model can be created by selecting New from the File menu in any
Simulinkwindow(orbyhittingCtrlN).

BasicElements
TherearetwomajorclassesofitemsinSimulink:blocksandlines.Blocksare
usedtogenerate,modify,combine,output,anddisplaysignals.Linesareused
totransfersignalsfromoneblocktoanother.
Blocks
ThereareseveralgeneralclassesofblockswithintheSimulinklibrary:
Sources:usedtogeneratevarioussignals
Sinks:usedtooutputordisplaysignals
Continuous: continuoustime system elements (transfer functions, state
spacemodels,PIDcontrollers,etc.)
Discrete:linear,discretetimesystemelements(discretetransferfunctions,
discretestatespacemodels,etc.)
Math Operations: contains many common math operations (gain, sum,
product,absolutevalue,etc.)
Ports&Subsystems:containsusefulblockstobuildasystem

Blocks have zero to several input terminals and zero to several output
terminals. Unused input terminals are indicated by a small open triangle.
Unused output terminals are indicated by a small triangular point. The block
shownbelowhasanunusedinputterminalontheleftandanunusedoutput
terminalontheright.

Lines
Linestransmitsignalsinthedirectionindicatedbythearrow.Linesmustalways
transmit signals from the output terminal of one block to the input terminal of
anotherblock.Onexceptiontothisisalinecantapoffofanotherline,splitting
the signal to each of two destination blocks, as shown below (click here to
downloadthemodelfilecalledsplit.mdl).

Lines can never inject a signal into another line lines must be combined
throughtheuseofablocksuchasasummingjunction.
A signal can be either a scalar signal or a vector signal. For SingleInput,
SingleOutput (SISO) systems, scalar signals are generally used. For Multi
Input,MultiOutput(MIMO)systems,vectorsignalsareoftenused,consistingof
twoormorescalarsignals.Thelinesusedtotransmitscalarandvectorsignals
areidentical.Thetypeofsignalcarriedbyalineisdeterminedbytheblockson
eitherendoftheline.

SimpleExample

Thesimplemodelconsistsofthreeblocks:Step,TransferFunction,andScope.
The Step is a Source block from which a step input signal originates. This
signalistransferredthroughthelineinthedirectionindicatedbythearrowto
theTransferFunctionContinuousblock.TheTransferFunctionblockmodifies
itsinputsignalandoutputsanewsignalonalinetotheScope.TheScopeisa
Sinkblockusedtodisplayasignalmuchlikeanoscilloscope.
TherearemanymoretypesofblocksavailableinSimulink,someofwhichwill
bediscussedlater.Rightnow,wewillexaminejustthethreewehaveusedin
thesimplemodel.
ModifyingBlocks
Ablockcanbemodifiedbydoubleclickingonit.Forexample,ifyoudouble
click on the Transfer Function block in the Simple model, you will see the
followingdialogbox.

This dialog box contains fields for the numerator and the denominator of the
block'stransferfunction.Byenteringavectorcontainingthecoefficientsofthe
desirednumeratorordenominatorpolynomial,thedesiredtransferfunctioncan
beentered.Forexample,tochangethedenominatorto

(1)
enterthefollowingintothedenominatorfield
[124]
andhittheclosebutton,themodelwindowwillchangetothefollowing,

whichreflectsthechangeinthedenominatorofthetransferfunction.
The Step block can also be doubleclicked, bringing up the following dialog
box.

Thedefaultparametersinthisdialogboxgenerateastepfunctionoccurringat

time=1sec,fromaninitiallevelofzerotoalevelof1(inotherwords,aunit
step at t = 1). Each of these parameters can be changed. Close this dialog
beforecontinuing.
ThemostcomplicatedofthesethreeblocksintheScopeblock.Doubleclicking
onthisbringsupablankoscilloscopescreen.

Whenasimulationisperformed,thesignalwhichfeedsintothescopewillbe
displayedinthiswindow.Detailedoperationofthescopewillnotbecoveredin
this tutorial. The only function we will use is the autoscale button, which
appearsasapairofbinocularsintheupperportionofthewindow.

RunningSimulations
Torunasimulation,wewillworkwiththefollowingmodelfile:
simple2.mdl
DownloadandopenthisfileinSimulinkfollowingthepreviousinstructionsfor
thisfile.Youshouldseethefollowingmodelwindow.

Before running a simulation of this system, first open the scope window by
doubleclickingonthescopeblock.Then,tostartthe simulation, either select
StartfromtheSimulationmenu,clickthePlaybuttonatthetopofthescreen,or
hitCtrlT.

Thesimulationshouldrunveryquicklyandthescopewindowwillappearas
shownbelow.

Notethatthesimulationoutput(showninyellow)isataverylowlevelrelative
totheaxesofthescope.Tofixthis,hittheautoscalebutton(binoculars),which
willrescaletheaxesasshownbelow.

Notethatthestepresponsedoesnotbeginuntilt=1.Thiscanbechangedby
doubleclickingonthestepblock.Now,wewillchangetheparametersofthe
systemandsimulatethesystemagain.DoubleclickontheTransfer Function
blockinthemodelwindowandchangethedenominatorto:
[120400]
Rerunthesimulation(hitCtrlT) and you should see what appears as a flat
line in the scope window. Hit the autoscale button, and you should see the
followinginthescopewindow.

Noticethattheautoscalebuttononlychangestheverticalaxis.Sincethenew
transferfunctionhasaveryfastresponse,itcompressedintoaverynarrowpart
ofthescopewindow.Thisisnotreallyaproblemwiththescope,butwiththe
simulation itself. Simulink simulated the system for a full ten seconds even
thoughthesystemhadreachedsteadystateshortlyafteronesecond.
Tocorrectthis,youneedtochangetheparametersofthesimulationitself.In
the model window, select Configuration Parameters from the Simulation
menu.Youwillseethefollowingdialogbox.

Therearemanysimulationparameteroptionswewillonlybeconcernedwith
thestartandstoptimes,whichtellSimulinkoverwhattimeperiodtoperformthe
simulation.ChnageStarttimefrom0.0to0.8(sincethestepdoesn'toccuruntil
t=1.0).ChangeStoptimefrom10.0to2.0,whichshouldbeonlyshortlyafter
thesystemsettles.Closethedialogboxandrerunthesimulation.After hitting
theautoscalebutton,thescopewindowshouldprovideamuchbetterdisplay
ofthestepresponseasshownbelow.

BuildingSystems
In this section, you will learn how to build systems in Simulink using the
building blocks in Simulink's Block Libraries. You will build the following
system.

Ifyouwouldliketodownloadthecompletedmodel,clickhere.
First,youwillgatherallofthenecessaryblocksfromtheblocklibraries.Then
you will modify the blocks so they correspond to the blocks in the desired
model. Finally, you will connect the blocks with lines to form the complete
system.Afterthis,youwillsimulatethecompletesystemtoverifythatitworks.
GatheringBlocks
Followthestepsbelowtocollectthenecessaryblocks:
Createanewmodel(NewfromtheFilemenuorhitCtrlN).Youwillgeta
blankmodelwindow.
ClickontheSourceslistinginthemainSimulinkwindow.

ThiswillbringuptheSourcesblocklibrary.Sourcesareusedtogenerate
signals.

Drag the Step block from the Sources window into the left side of your
modelwindow.

ClickontheMathOperationslistinginthemainSimulinkwindow.
Fromthislibrary,dragaSumandGainblockintothemodelwindowand
placethemtotherightoftheStepblockinthatorder.
ClickontheContinuouslistinginthemainSimulinkwindow.
First,fromthislibrary,dragaPIDController block into the model window
andplaceittotherightoftheGainblock.
From the same library, drag a Transfer Function block into the model
windowandplaceittotherightofthePIDControllerblock.

ClickontheSinkslistinginthemainSimulinkwindow.
DragtheScopeblockintotherightsideofthemodelwindow.

ModifyBlocks
Followthesestepstoproperlymodifytheblocksinyourmodel.
DoubleclickontheSumblock.Sinceyouwillwantthesecondinputtobe
subtracted,enter+intothelistofsignsfield.Closethedialogbox.
DoubleclicktheGainblock.Changethegainto2.5andclosethedialog
box.
DoubleclickthePIDControllerblockandchangetheProportionalgainto
1andtheIntegralgainto2.Closethedialogbox.
Doubleclick the Transfer Function block. Leave the numerator [1], but
changethedenominatorto[124].Closethedialogbox.Themodelshould
appearas:

ChangethenameofthePIDController block to PI Controller by double


clickingonthewordPIDController.

Similarly,changethenameoftheTransferFunctionblocktoPlant.Now,all
theblocksareenteredproperly.Yourmodelshouldappearas:

ConnectingBlockswithLines
Nowthattheblocksareproperlylaidout,youwillnowconnectthemtogether.
Followthesesteps.
DragthemousefromtheoutputterminaloftheStepblocktothepositive
inputoftheSuminput.AnotheroptionistoclickontheStepblockandthen
CtrlClickontheSumblocktoconnectthetwotogther.Youshouldseethe
following.

Theresultinglineshouldhaveafilledarrowhead.Ifthearrowheadisopen
andred,asshownbelow,itmeansitisnotconnectedtoanything.

You can continue the partial line you just drew by treating the open
arrowheadasanoutputterminalanddrawingjustasbefore.Alternatively,
ifyouwanttoredrawtheline,orifthelineconnectedtothewrongterminal,
you should delete the line and redraw it. To delete a line (or any other
object),simplyclickonittoselectit,andhitthedeletekey.
DrawalineconnectingtheSumblockoutputtotheGaininput.Alsodrawa
linefromtheGaintothePIController,alinefromthePIController to the
Plant, and a line from the Plant to the Scope. You should now have the
following.

The line remaining to be drawn is the feedback signal connecting the


output of the Plant to the negative input of the Sum block. This line is
differentintwoways.First,sincethislineloopsaroundanddoesnotsimply
followtheshortest(rightangled)routesoitneedstobedrawninseveral
stages.Second,thereisnooutputterminaltostartfrom,sothelinehasto
tapoffofanexistingline.
Drag a line off the negative portion of the Sum block straight down and
releasethemousesothelineisincomplete.Fromtheendpointofthisline,
click and drag to the line between the Plant and the Scope. The model
shouldnowappearasfollows.

Finally,labelswillbeplacedinthemodeltoidentifythesignals.Toplacea
labelanywhereinthemodel,doubleclickatthepointyouwantthelabelto
be.StartbydoubleclickingabovethelineleadingfromtheStepblock.You
willgetablanktextboxwithaneditingcursorasshownbelow.

Typeanrinthisbox,labelingthereferencesignalandclickoutsideitto
endediting.
Labeltheerror(e)signal,thecontrol(u)signal,andtheoutput(y)signalin
thesamemanner.Yourfinalmodelshouldappearas:

Tosaveyourmodel,selectSaveAsintheFilemenuandtypeinanydesired
modelname.Thecompletedmodelcanbefoundhere.
Simulation
Nowthatthemodeliscomplete,youcansimulatethemodel.SelectStartfrom
theSimulationmenutorunthesimulation.Doubleclickonthe_Scope_block
toviewitsoutput.Hittheautoscalebutton(binoculars)andyoushouldseethe
following:

TakingVariablesfromMATLAB
Insomecases,parameters,suchasgain,maybecalculatedinMATLABtobe
used in a Simulink model. If this is the case, it is not necessary to enter the
resultoftheMATLABcalculationdirectlyintoSimulink.Forexample,suppose
wecalculatedthegaininMATLABinthevariableK.Emulatethisbyentering
thefollowingcommandattheMATLABcommandprompt.
K=2.5
This variable can now be used in the Simulink Gain block. In your Simulink
model,doubleclickontheGainblockandenterthefollowingtheGainfield.
K

Close this dialog box. Notice now that the Gain block in the Simulink model
showsthevariableKratherthananumber.

Now, you can rerun the simulation and view the output on the Scope. The
resultshouldbethesameasbefore.

Now, if any calculations are done in MATLAB to change any of the variables
usedintheSimulinkmodel,thesimulationwillusethenewvaluesthenexttime

itisrun.Totrythis,inMATLAB,changethegain,K,byenteringthefollowingat
thecommandprompt.
K=5
Start the Simulink simulation again, bring up the Scope window, and hit the
autoscale button. You will see the following output which reflects the new,
highergain.

Besides variables and signals, even entire systems can be exchanged


betweenMATLABandSimulink.

PublishedwithMATLAB7.14

AllcontentslicensedunderaCreativeCommonsAttributionShareAlike4.0International
License.

S-ar putea să vă placă și