Professional Documents
Culture Documents
02 Tutorial Simulink
02 Tutorial Simulink
02 Tutorial Simulink
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 two-step 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 discrete-time
components
Functions & Tables -
general functions and table
look-up operations
Math - blocks that describe
general mathematics
functions
Simulink Block Libraries
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
Creating a New Model
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:
n A set of inputs.
n A set of states.
n 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:
Fixed-step - 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.
x 3 sin 2t
Initial condition x(0) 1.
x (0) = -1
.
x 1 x
3sin(2t) x(t)
(input) s (output)
integrator
Double-click on the
Sine Wave block to
set amplitude = 3
and freq = 2
Double-click on
the Integrator
block to set
initial condition =
-1
In the model
window, from the
Simulation pull-
down menu, select
Start
Double-click on
the Scope to view
the simulation
results
Simulation Results
x(t ) 12 32 cos 2t
matches the plot (the
simulation result)
exactly.
Example 2
mx cx kx f (t )
– 2nd-order mass-spring-damper 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
Example 2
k
m f (t)
x c
Create the Simulink Diagram
mx f (t ) cx kx
Make the left-hand side of this equation the
output of a summing block
..
mx
summing
block
Drag a Sum block from the Math library
.. ..
mx 1 x
m
summing
block
Drag a Gain block from the Math library
mx 1 x 1 x 1 x
m s s
summing
block
Drag Integrator blocks from the
Continuous library
ICs on the
integrators are
zero.
mx 1 x 1 x 1 x
m s s
summing cx c
block
kx k
Drag new Gain blocks
from the Math library
To flip the gain block, select it and
choose Flip Block in the Format pull-
down menu or double-clock on it.
c = 0.5
f(t) +
input mx 1 x 1 x 1 x x(t)
-
m s s output
-
cx x
c
kx x
k
Drag the Step function from the Source
library
Double-click 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
x c 1
Standard form x x f (t )
k k k
m
Natural frequency k
n 2.0
m
2 c
Damping ratio 0.5
n k
Static gain 1
K 1
k
Checking Results
function [sys,x0,str,ts]=sfungains(t,x,u,flag)
Inserting a S-Function
• Change the S-Function 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 S-Function
function sys=mdlOutputs(t,x,u)
K1 = 50;
K2 = 20;
sys = [K1 K2];
Inserting a S-Function