Simulink Tutorial
Simulink Tutorial
Presentation Outline
What Basic
Examples Exercise
What is Simulink?
Simulink is an interactive tool for modeling, simulating and analyzing dynamic systems. systems. Simulink integrates seamlessly with MATLAB, providing immediate access to an extensive range of analysis and design tools tools. . Simulating a dynamic system is a twotwo-step process with Simulink: Simulink:
create a model of the system to be simulated using
Launch Simulink
First launch MATLAB MATLAB. . To open Simulink, type simulink at the MATLAB
command window or click on the Simulink icon on the MATLAB toolbar. toolbar.
describe linear functions Discrete - blocks that describe discrete-time components Functions & Tables general functions and table look-up operations Math - blocks that describe general mathematics functions
Nonlinear - blocks that describe nonlinear functions Signal & systems - blocks that allow multiplexing, de-multiplexing, implement external input/output, pass data to other parts of the model, create subsystems and perform other functions Sinks - blocks that display or write block output Sources - blocks that generate signals Blocksets and toolboxes - the extras block library of specialized blocks
Click the newnewmodel icon in the upper left corner to start a new Simulink file Select the Simulink icon to obtain elements of the model
Your Workspace
Library of elements Model is created in this window
Block Diagram
A Simulink block diagram is a pictorial model of a dynamic system. system. It consists of blocks interconnected by lines. lines. Blocks represent elementary dynamical systems that Simulink knows how to simulate. simulate. A block comprises one or more of the following: following:
A A A
To introduce blocks in your model, choose the block from the library, click on it and drag it in your model. model . Double clicking on the block will allow you to change the block parameters. parameters.
Fixed-step - a smaller step size produces a more Fixedaccurate simulation but results in a longer execution time time. .
Variable step - depending on the application, it can produce more accurate results without sacrificing execution speed speed. .
Parameters set up: up: Simulation > Simulation parameters Simulink simulates a system when you choose start from the model editors simulation menu. menu.
x ! 3 sin 2t
Initial condition
x (0) ! 1.
First, sketch a simulation diagram of this mathematical model (equation) (3 min. min.)
Simulation Diagram
Input is the forcing function 3sin(2 sin(2t) Output is the solution of the differential equation x(t)
x (0) = -1 . x
3sin(2t) (input)
1 s integrator
x(t) (output)
Place your cursor on the output port (>) of the sine wave block Drag from the sine wave output to the integrator input Drag from the integrator output to the scope input Arrows indicate the direction of the signal flow.
Double-click on Doublethe Sine Wave block to set amplitude = 3 and freq = 2 This produces the desired input of 3sin(2t 3sin(2 t)
Double-click Doubleon the I Inte ntegrator block to set initial condition = -1 This sets our IC x(0) = -1.
Simulation Results
To verify that this plot represents the solution to the problem, solve the equation analytically.
Example 2
Build a Simulink model that solves the following differential equation
m x cx kx ! f (t )
2ndnd-order massmass-springspring-damper system Zero ICs Input f(t) is a step with magnitude 3 Parameters: arameters: m = 0.25, 25, c = 0.5, k = 1 m->mass; >mass; c->damping factor; tor; k->spring constant
Example 2
k m x f (t)
The simulation diagram for solving the ODE is created step by step. step. After each step, elements are added to the Simulink model. model.
Optional exercise: exercise: first, complete diagram (5 min. min.).
sketch
the
x ! f (t ) cx kx
Make the leftleft-hand side of this equation the output of a summing block
.. mx
summing block
Add a gain (multiplier) block to eliminate the coefficient and produce the highesthighest-derivative alone
.. mx
summing block
1 m
.. x
the
x
summing block
x
1 s
x
1 s
ICs on the integrators are zero. Add a scope from the Sinks library. brary. Connect output ports to input ports. s. Label the signals by doubleble-clicking on the leader line e. .
Connect to the integrated signals with gain blocks to create the terms on the rightright -hand side of the equation
m x
summing block
1 m
x
cx
1 s
c
x
1 s
kx
c = 0.5
DoubleDouble-click on gain blocks to set parameters Connect from the gain block input backwards up to the branch point. Re Re-title the gain blocks.
K = 1.0
Bring all the signals and inputs to the summing block. block. Check signs on the summer. summer.
f(t) input
+ -
m x
1 m
x
cx
1 s
c
x x
k
1 s
x(t) output
kx
Double-click on Step block Doubleto set parameters. For a step input of magnitude 3, set Final value to 3
Results
Checking Results
Standard form
x k
c 1 x x ! f (t ) k k
Natural frequency
k [n ! ! 2.0 m
Damping ratio
2^ c ! p ^ ! 0.5 [n k
1 K ! !1 k
Static gain
Checking Results
Damping ratio of 0.5 is less than 1:
Expect the system to be underdamped. Expect to see overshoot.
Static gain is 1:
Expect output magnitude to equal input magnitude. Input has magnitude 3, so does output.
Saving to Workspace
Drag the To Workspace block from the Sink library
Saving to Workspace
Double click on the To Workspace block to change the parameters. parameters.
Check on MATLAB workspace if the variable is there. Example: plot (tout, x); y = sqrt (x )
Inserting a SS-Function
Drag a S-Function block from the Functions & Tables library
Inserting a SS-Function
Double click on the SFunction block to change the S-Function name and include additional parameters.
Use the template that comes with Simulink. Change the template based on your project.
Inserting a SS-Function
Type sfundemos at the MATLAB command line line. . Double click on M-files Double click on M-file S-Function Template Save the file in another folder and with another name Change the function name: e:
function [sys,x [sys,x0 0,str,ts]=s ,str,ts]=sfungains ungains(t,x,u, (t,x,u,flag)
Inserting a SS-Function
sizes = simsizes; simsizes; sizes.NumContStates = 0; sizes. sizes. sizes .NumDiscStates = 0; sizes. sizes .NumOutputs = 2; sizes. sizes .NumInputs = 0; sizes. sizes .DirFeedthrough = 0; sizes. sizes .NumSampleTimes = 1; % at least one sample time is
needed
Inserting a SS-Function
Inserting a SS-Function
Inserting a SS-Function
Drag Display blocks from the Sink library Run your project
Exercise
Given the following block diagram:
K3 + u K1 + _
K2 K5
K4
+ +
Exercise
1) Show the correspondence of this block diagram with the RC circuit simulated in Assignment #1 (analytically). (analytically) . 2) Find K1, K2, K3, K4 and K5 in accordance to the parameters of Assignment #1. 3) Implement the system in Simulink. Simulink. Use MATLAB to enter your parameters through a Mfile. file . 4) Simulate your system in Simulink and compare the response with your expected results results. . 5) K3 is related to the initial conditions. conditions. Incorporate I.C. in your integrator block. block.