This action might not be possible to undo. Are you sure you want to continue?
Simulink
Presentation Outline
What is Simulink?
Basic operations with Simulink
Examples
Exercise
What is Simulink?
Simulink is an interactive tool for modeling,
simulating and analyzing dynamic systems.
Simulink integrates seamlessly with MATLAB,
providing immediate access to an extensive range of
analysis and design tools.
Simulating a dynamic system is a twostep process
with Simulink:
create a model of the system to be simulated using
Simulink’s model editor (BLOCK DIAGRAM)
use Simulink to simulate the behavior of the system for
a specified time span
Launch Simulink
• First launch MATLAB.
• To open Simulink, type simulink at the MATLAB
command window or click on the Simulink icon
on the MATLAB toolbar.
Simulink Block Libraries
Simulink provides a library
browser that allows you to
select blocks from libraries
of standard blocks:
Continuous  blocks that
describe linear functions
Discrete  blocks that
describe discretetime
components
Functions & Tables 
general functions and table
lookup operations
Math  blocks that describe
general mathematics
functions
Simulink Block Libraries
Nonlinear  blocks that describe nonlinear functions
Signal & systems  blocks that allow multiplexing,
demultiplexing, 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
Creating a New Model
Click the new
model 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
Show/hide
Library
Browser
Block Diagram
A Simulink block diagram is a pictorial model of a
dynamic system. It consists of blocks
interconnected by lines.
Blocks represent elementary dynamical systems
that Simulink knows how to simulate. A block
comprises one or more of the following:
A set of inputs.
A set of states.
A set of outputs.
To introduce blocks in your model, choose the block
from the library, click on it and drag it in your
model. Double clicking on the block will allow you
to change the block parameters.
Model Execution Phase
In this phase Simulink successively computes the
states and the outputs of the system at intervals
from the simulation start time to the stop time,
using information provided by the model.
Time steps  successive time points at which the
states and the outputs are computed.
Step size  the length of time between steps. It
depends on the type of solver:
Fixedstep  a smaller step size produces a more
accurate simulation but results in a longer
execution time.
Model Execution Phase
Variable step  depending on the application, it
can produce more accurate results without
sacrificing execution speed.
Parameters set up:
Simulation > Simulation parameters …
Simulink simulates a system when you choose start
from the model editor’s simulation menu.
Example 1: a Simple Model
Build a Simulink model that solves the
differential equation
Initial condition
First, sketch a simulation diagram of this
mathematical model (equation) (3 min.)
( ) t x 2 sin 3 =
. 1 ) 0 ( ÷ = x
Input is the forcing function 3sin(2t)
Output is the solution of the differential
equation x(t)
Now build this model in Simulink
Simulation Diagram
3sin(2t)
(input)
x(t)
(output)
integrator
x (0) = 1
x x
.
1
s
Select in Input Block
Drag a Sine Wave block
from the Sources library
to the model window
Select an Operator Block
Drag an Integrator block
from the Continuous
library to the model
window
Select an Output Block
Drag a Scope block from
the Sinks library to the
model window
Connect Blocks with Signals
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.
Select Simulation Parameters
Doubleclick on
the Sine Wave
block to set
amplitude = 3
and freq = 2
This produces the
desired input of
3sin(2t)
Doubleclick
on the
Integrator
block to set
initial
condition = 1
This sets our
IC x(0) = 1.
Select Simulation Parameters
Run the Simulation
In the model
window, from
the Simulation
pulldown menu,
select Start
Doubleclick on
the Scope to
view the
simulation
results
Simulation Results
The analytical
result,
matches the plot
(the simulation
result) exactly.
( ) t t x 2 cos ) (
2
3
2
1
÷ =
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
– 2ndorder massspringdamper system
– Zero ICs
– Input f(t) is a step with magnitude 3
– Parameters: m = 0.25, c = 0.5, k = 1
– m>mass; c>damping factor; k>spring
constant
) (t f kx x c x m = + +
Example 2
m
c
k
f (t)
x
Create the Simulink Diagram
On the following slides:
– The simulation diagram for solving the
ODE is created step by step.
– After each step, elements are added to
the Simulink model.
Optional exercise: first, sketch the
complete diagram (5 min.).
Create the Block Diagram
First, solve for the term with highest
order derivative
Make the lefthand side of this equation
the output of a summing block
summing
block
mx
..
kx x c t f x m ÷ ÷ = ) (
Drag a Sum block from the Math library
Doubleclick to change
the block parameters
to rectangular and +  
Create the Block Diagram
Add a gain (multiplier) block to
eliminate the coefficient and produce
the highestderivative alone
summing
block
mx
..
x
..
1
m
Drag a Gain block from the Math library
Doubleclick to change
the block parameters.
Add a title.
The gain is 4 since 1/m = 4.
Create the Block Diagram
Add integrators to obtain the
desired output variable
x m
m
1
summing
block
s
1
s
1 x
x
x
Drag Integrator blocks from
the Continuous library
Add a scope from the Sinks library.
Connect output ports to input ports.
Label the signals by doubleclicking on the leader line.
ICs on the
integrators are
zero.
Create the Block Diagram
Connect to the integrated signals with
gain blocks to create the terms on the
righthand side of the equation
x m
m
1
summing
block
s
1
s
1 x x x
c
k
x c
kx
Drag new Gain blocks
from the Math library
Doubleclick on gain
blocks to set parameters
Connect from the gain
block input backwards up
to the branch point.
Retitle the gain blocks.
To flip the gain block, select it and
choose Flip Block in the Format pull
down menu or doubleclock on it.
c = 0.5
K = 1.0
Complete the Model
Bring all the signals and inputs to the
summing block.
Check signs on the summer.
x m
m
1
s
1
s
1 x
x
c
k
x c
kx
f(t)
input
+


x
x
x(t)
output
x
Drag the Step function from the
Source library
Doubleclick on Step block
to set parameters. For a
step input of magnitude 3,
set Final value to 3
Final Simulink Model
Run the Simulation
Results
Underdamped
response.
Overshoot of 0.5.
Final value of 3.
Is this expected?
Checking Results
Standard form
Natural frequency
Damping ratio
Static gain
) (
1
t f
k
x x
k
c
m
k
x
= + +
5 . 0
2
= ÷ = ,
e
,
k
c
n
0 . 2 = =
m
k
n
e
1
1
= =
k
K
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.
Simulation results conform to
expectations
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.
Check on MATLAB workspace if the variable is there.
Example: plot (tout, x); y = sqrt (x )
Inserting a SFunction
Drag a SFunction block
from the Functions &
Tables library
Inserting a SFunction
Double click on the S
Function block to
change the SFunction
name and include
additional parameters.
Use the template that comes with
Simulink.
Change the template based on
your project.
Inserting a SFunction
• Type sfundemos at the MATLAB
command line.
• Double click on Mfiles
• Double click on Mfile SFunction
Template
• Save the file in another folder and with
another name
• Change the function name:
function [sys,x0,str,ts]=sfungains(t,x,u,flag)
Inserting a SFunction
• Change the SFunction size parameters:
sizes = simsizes;
sizes.NumContStates = 0;
sizes.NumDiscStates = 0;
sizes.NumOutputs = 2;
sizes.NumInputs = 0;
sizes.DirFeedthrough = 0;
sizes.NumSampleTimes = 1; % at least one sample time is
needed
sys = simsizes(sizes);
Inserting a SFunction
• Edit the mdlOutputs mfunction in
accordance to your project:
function sys=mdlOutputs(t,x,u)
K1 = 50;
K2 = 20;
sys = [K1 K2];
Inserting a SFunction
Drag a Demux block from
the Signals & Systems
library
Inserting a SFunction
Drag Display blocks from
the Sink library
Run your project
Exercise
Given the following block diagram:
K
2
K
1
K
3
K
4
K
5
u y
+
_
+
+
+
Exercise
1) Show the correspondence of this block diagram
with the RC circuit simulated in Assignment #1
(analytically).
2) Find K
1
, K
2
, K
3
, K
4
and K
5
in accordance to the
parameters of Assignment #1.
3) Implement the system in Simulink. Use
MATLAB to enter your parameters through a M
file.
4) Simulate your system in Simulink and compare
the response with your expected results.
5) K
3
is related to the initial conditions.
Incorporate I.C. in your integrator block.