Introduction to Simulink The Use of Mathematic Simulations in Electrical Engineering
Lecture Outline 1) Introduction to Simulink 2) Modelling of dynamics systems 2
Simulink Tool for modeling, simulating, analyzing dynamics systems Graphical block diagramming tool and block libraries Interaction with Matlab environment - scripting Has integrated solvers Can integrate blocks with C code Subject number 3
Simulink Start From Matlab IDE use Simulink icon Subject number 4
Simulink Start Type simulink to Command window Subject number 5
Library browser Quick search for block with particular name Available Toolboxes in your instalation Most used blocks Available Libraries with blocks Subject number 6
Library browser Simulink Library Browser contain blocks that you may use Blocks are organized into Block libraries according to similar functions as: Continuous, Discreet Integrator, Transfer function, Transport delay Math Summ, Add, Product, etc. Sources Ramp, Constant, Step, etc. Sinks Scope, Data to file, etc. Subject number 7
Continuous blocks Here are commonly used blocks in Continuous Block Library 4 Integrator State Space Transfer function Transport delay Here are some common used Math Operations Blocks: Subject number 8
Math blocks Here are commonly used blocks in Math Block Library Here are some common used Math Operations Blocks: Add Gain Product Sum Here are some common used Signal Routing Blocks: Subject number 9
Signal routing blocks Here are commonly used blocks in Signal routing Block Library Here are some common used Signal Routing Blocks: Mux Demux Here are some common used Sinks Blocks: Subject number 10
Sinks blocks Here are commonly used blocks in Sinks Block Library Here are some common used Sinks Blocks: Scope XY Graph Here are some common used Sources Blocks: Subject number 11
Sources blocks Here are commonly used blocks in Sources Block Library Here are some common used Sources Blocks: Step Ramp Random noise Tutorial: Introduction to Simulink Constatnt Subject number 12
New model Click here to create new model This window will appear Subject number 13
New model window Open Library browser Simulation Configuration Run/Stop simulation Simulation time Model hierarchy MODEL AREA Selected solver Subject number 14
New model window MODEL AREA Drag & drop blocks here Subject number 15
Wiring techniques Block can be wired by mouse from input to output Inputs are on LEFT side of the block Outputs are on RIGHT side of the block Input Output When holding the mouse over input or output the cursor will change to crosshair Holding the left mouse button, will drag wires from input to output Subject number 16
Wiring techniques Automatic Block connection Select source block hold Ctrl key and left click on destination block Connection from wire to another block hold Ctrl key while left clicking on the wire and then dragging wire to desired block Subject number 17
TASK 1 Put block of Sine Wave and Scope into model and try different connection modes, like in picture below Subject number 18
Block information To get detailed information about different blocks, use built in HELP system Subject number 19
Simulation configuration Simulation -> Model Configuration Parameters Ctrl + E Icon You can set Start/Stop time Kind of solver will be used (ode45, ode23,...) Fixed/Variable step Tolerance (error) of simulated result Subject number 20
Simulation configuration Subject number 21
Example Integrator with initial value Create simple model of integrator with initial condition Put following blocs into model Subject number 22
Example Integrator with initial value Configure integrator block Double click on it and select Initial condition source = external Subject number 23
Example Integrator with initial value Configure constant block Run the simulation Ctrl + T See the result double click on scope Subject number 24
Example sine wave Create following diagram Subject number 25
Example sine wave Configure integrator to have internal initial condition -1 Subject number 26
Example sine wave Run the simulation Ctrl + T See the result double click on scope Subject number 27
Example sine wave 2 Create following diagram Subject number 28
Example sine wave 2 Double click on Gain block and set the Gain to 3:1:10 In this way the vector signal will be created Subject number 29
Example sine wave 2 Run the simulation Ctrl + T See the result double click on scope Subject number 30
Useful Tricks Sometimes it is good to see dimension of the signal Display -> Signals&Ports -> Wide Nonscalar Lines, Signal Dimensions Subject number 31
Useful Tricks The model then look like this You can put your comment into model by double click You can change the block label Subject number 32
Useful Tricks You can flip the blocks by Ctrl + I You can rotate the blocks by Ctrl + R Or from the context menu Subject number 33
Useful Tricks In context menu (Right click on the block) You can HIDE the name of the block Change the color Subject number 34
Data driven modeling You can use Simulink with the Matlab Variables in Matlab can be used to specify data parameters in your model Prepare this model Subject number 35
Data driven modeling Configure the scope to have 3 inputs double click on scope select Number of axes 3 Subject number 36
Data driven modeling Configure the Zero-Order-Hold blocks Subject number 37
Useful Tricks Sometimes it is good to see that wires have different sample times Display -> Sample Time -> Colors Subject number 38
Data driven modeling Into Matlab Comand window write >> Ts = 0.5 Run the simulation Ctrl + T Subject number 39
Simulation commands All data can be set in m-file The simulation can be executed from m-file by comand Take the model from previous example and create following code in m-file %Simulation settings t_stop = 15; %[s] options = simset('solver','ode45'); %Start the simulation sim('sinecw',t_stop,options) Subject number 40
Example Mass Spring Damper System In this example we will create mass-spring-damper model in Simulink and execute it from m-file The system can be mathematically described by following equation where t is simulation time, F(t) is force applied to the system, c is damping of the spring and k is stiffness of the spring, m is mass, x(t) is actual position, its first derivative correspond to velocity and second derivative to acceleration of the mass Goal is to simulate position of the mass with respect to time Subject number 41
Example Mass Spring Damper System Subject number 42
Example Mass Spring Damper System The position can be calculated by integration of velocity and velocity can be calculated by integration of acceleration When the acting force is known the acceleration can be calculated according to second Newton s Law Force = Mass x Acceleration So the final equation is Subject number 43
Example Mass Spring Damper System Model in Simulink Instead of hard coding values into blocks it is useful to put them externally to a m-file. x_init dxdt_init m c k t_step_f F_beg F_step Subject number 44
Example Mass Spring Damper System m-file content x_init = 4; %[m] init position dxdt_init = 0; %[m/s] init speed m = 20; %[kg] mass c = 4; %[N/(m/s)] damping k = 2; %[N/m] stiffness t_step_f = 50;%[s] F_beg = 0; %[Nm] F_step = 4; %[Nm] %Simulation settings t_stop = 100; %[s] Ts = t_stop/1000; %[s] options = simset('solver','ode5','fixedstep',ts); %Start the simulation sim('msdmodel',t_stop,options); Subject number 45
Example Mass Spring Damper System Model Subject number 46
Example Mass Spring Damper System Results Subject number 47
Example Mass Spring Damper System Try to simulate mass-spring-damper model for different values of initial and end conditions Insert labels into model Use comments in m-file Subject number 48
Parameter Transfer Between Blocks Connections between blocks from outputs to inputs Drag from output to input or Click on first block, hold Ctrl and click on second block Subject number 49
Parameter Transfer Between Blocks Goto From blocks Pair of blocks that acts as connection For better orientation in complicated models In library Simulink - Signal Routing Pair is created by the same Goto Tag (Con1 in the picture) double click to change it or right click Block parameters Subject number 50
Subsystems Wraps blocks so they are not visible in current level of view Blocks are represented only by input and output ports Serves for better orientation in a complicated model Subject number 51
Subsystems Subject number 52
Subsystems - Creation Select blocks Right click Create Subsystem from Selection or Drag and drop Subsystem from library Simulink Ports and Subsystems Subsystem To look inside or change Subsystem, double click on it Subject number 53
Inside the Subsystem Connectivity with outer space is done by: Inports Outports These corresponds with inputs and outputs of the Subsystem block Can be renamed by clicking on its caption Subject number 54
Masking the Subsystem Mask of the subsystem simplifies the input of parameters in the Subsystem (e.g. constants, gains) Prompt message is shown when double clicking on Subsystem -> user does not have to search for the parameters in the subsystem itself Subject number 55
Creation of a Mask Right click on Subsystem Mask Create Mask or Select Subsystem and press Ctrl + M Mask Editor window is then shown Subject number 56
Creation of a Mask Click on Parameters & Dialog Subject number 57
Creation of a Mask Click on Parameters & Dialog Drag Edit icon from Controls panel into Parameters folder in Dialog box New line is created fill in Promp text (will be shown to user) and Name Name represents the parameter value in the model Subject number 58
Creation of a Mask Edit the value of the desired block (by double clicking on it) to Name that has been used in Mask Editor (here g0 ) Subject number 59
Creation of a Mask After double clicking on Subsystem, prompt message is shown where value of the parameter(s) can be entered After execution, model calculates with entered value Default value can be set in Property editor panel in Mask editor Subject number 60
Masking the Subsystem Subsystem structure can be now edited by right click Mask Look Under Mask or By left click and Ctrl + U Subject number 61
Atomic Subsystems Normal subsystems have only visual impact on the model (better orientation for user), however blocks inside it are counted in order as without subsystem Order in which individual blocks are evaluated can be shown through menu Display Block Sorted Execution Order Subject number 62
Atomic Subsystems Format y:x x execution order y identification number of atomic subsystem (0 for main system) Execution order can be changed right click on block Parameters Priority the lower number, the higher priority Right click in free space of model Update Diagram (Ctrl + D) for order recalculation Normal subsystem does not affect this order Atomic subsystem does Subject number 63
Atomic Subsystems We might want to have the subsystem as closed unit with firm border in cases: System, with lockdown (system that can freeze ) System with response called by event (e.g. pulse counter increments value on every rising edge) Subject number 64
Atomic Subsystems - Creation Drag Atomic Subsystem from Simulink Ports & Subsystems library Right click on normal Subsystem Block Parameters (Subsystem) and tick Treat as atomic unit Subject number 65
Atomic Subsystems Visually distinguished by bold border Execution order changed Subject number 66
Enabled Subsystem Enabled subsystem works only if Enable input is in logical 1 By logical 0, the output maintains last value ( freezes ) Enabled Subsystem from Ports & Subsystems or Enable block into normal subsystem Subject number 67
Triggered Subsystem Makes one step based on external event (trigger) Triggered Subsystem from Ports & Subsystems or Trigger block into normal subsystem Can respond to rising or trailing edge or both To set this, double click on trigger block inside the subsystem, change parameter Trigger type Subject number 68
Triggered Subsystem This example transfers the value from input to output at the moment of rising edge It maintains this value on its output till next rising edge Subject number 69
Other Types of Subsystems Enabled and Triggered Subsystem Combination of those two For Iterator Subsystem Conducts given number of cycles within one simulation step While Iterator Subsystem Within one simulation step, it conducts cycle till the condition is fulfilled It behaves similarly as calling function from Matlab Can use only blocks without states or discrete with sampling period -1 Subject number 70
Action Subsystems Consist of pairs of block: condition and action If and else Condition defined in If block (double click to edit) If, else, elseif supported Multiple inputs are possible (double click to edit) Outputs for control of Action blocks Subject number 71
Action Subsystems Action subsystems are active when their respective action signal (if {}, else {} ) are active Pulse generator 0 1 Condition if (u1 > 0.5) Action on else: gain two times Subject number 72
Action Subsystems Outputs of action should be connected by Merge block (from Simulink Signal Routing library) Merge propagates the active input (from multiple inputs) If more inputs are active at the same time, it propagates the last one counted Subject number 73
Other Types of Subsystems Switch Case and Switch Action Subsystem As the above mentioned Action Subsystem, but this one is for switch case command For Each Subsystem For repeating same algorithm with single elements or subfields of input signal Code Reuse Subsystem Code inside is translated only once Subject number 74
End Thank You for Your Attention Subject number 75