You are on page 1of 53

MATLAB & SIMULINK

with Applications

Module IV:
SIMULINK and Neuro-Fuzzy Toolboxes

Wahyudi Martono, Dr. Eng.


Department of Mechatronics Engineering
International Islamic University Malaysia
Department of Mechatronics Engineering

SIMULINK

Session 2:
Getting Started with Simulink

Department of Mechatronics Engineering

Session Outlines

Subsytems
Masking
Discrete System

Department of Mechatronics Engineering

Subsystems
What is Subsystems?
We may make a very complex system resulting in a very
complex model.
As subprogram (subroutine or function) of other
programming language, Simulink provides an analogous
capability called subsystems.
Using subsystems, we can make the models easier to
develop, to understand and to maintain.

Department of Mechatronics Engineering

Subsystems
Method of Creating Subsystems

There are two method for creating subsystems:


Encapsulating a Subsystem
A portion of existing model is encapsulated to be
a subsystem.
Use Subsystem blocks from Signals & System
Library.

Department of Mechatronics Engineering

Subsystems
Encapsulating Subsystems
Suppose we want to make our primitive model of massspring-damper as a subsystem.

Department of Mechatronics Engineering

Subsystems
Encapsulating Subsystems
To encapsulate a portion of a Simulink model, select all
the blocks and signal line to be included in the
subsystem using a bounding box using our mouse.

bounding box

Department of Mechatronics Engineering

Subsystems
Encapsulating Subsystems
Choose Create Subsystem under Edit menu.

New subsystem

Department of Mechatronics Engineering

Subsystems
Encapsulating Subsystems
To view or edit a subsystem, double-click the block. A
new window will appear.

Department of Mechatronics Engineering

Subsystems
Subsystems Blocks
If, when building a model, we know that we will need a
subsystem, it will more convenient to build a subsystem
in a subsystem directly.
Drag a Subsystem Block from Ports & Subsystem
Library to the model window.

Department of Mechatronics Engineering

Subsystems
Subsystems Blocks
Double click the subsystem to open subsystem window.

Build subsystem using the standard procedure.


Department of Mechatronics Engineering

Subsystems
Subsystems Blocks
Use Inport Blocks for all signals entering subsystem and
Outport Blocks for all signals leaving the subsystem.
(Both blocks are from Port & Subsytem Library)

You may change the label of Inport and Outpor. Close


the subsystem window if we finished building subsystem.
Department of Mechatronics Engineering

Masking
Concept
A mask is a custom user interface for a subsystem
that hides the subsystems contents, making it appear
to the user as an atomic block with its own icon and
parameter dialog box.
Masking permits us to treat a subsystem as if it was a
simple block.

Department of Mechatronics Engineering

Masking
Concept

We make a masked subsystem for the following reason:


There is an algorithm which we want to hide from
other user.
To prevent unintended modification of subsystems by
hiding their contents behind a mask
Lets build a spring-mass masked block.

Department of Mechatronics Engineering

Masking
Open a new model workspace, and drag the subsystem
of the primitive model into it.

Department of Mechatronics Engineering

Masking
Select the subsystem model and then choose Mask
under Edit menu from the new model window menu bar.

Department of Mechatronics Engineering

Masking
Mask Editor dialog box consists of four tabbed pages
Icon
Initialization
Parameters
Documentation

Department of Mechatronics Engineering

Masking
Mask Editor Documentation Page
The Documentation pane enables you to define or
modify the type, description, and help text for a masked
block.

The page consists of three


field. All of the fields are
optional. You may enter as
shown in the figure.

Department of Mechatronics Engineering

Masking
Mask Editor Parameters Page
The page is used to set parameters of block in
subsystem underlying the masked block.
For our mass-spring-damper, we have the following
parameters:
Mass, symbol: m, editable by user
Damper, c, editable
Spring, k, editable
add icon

To add a variable, click add


icon then enter all the
necessary field.
Department of Mechatronics Engineering

delete icon

Masking
Mask Editor Parameters Page

Department of Mechatronics Engineering

Masking
Mask Initiation Page
The Initialization pane allows you to enter MATLAB
commands that initialize the masked subsystem.
Simulink executes the initialization commands when it
Loads the model
Starts the simulation or updates the block diagram
Rotates the masked block
Redraws the blocks icon (if the masks icon creation
code depends on variables defined in the
initialization code)

Department of Mechatronics Engineering

Masking
Mask Initiation Page
For example, if we want
our masked system has
the following value:
m=1;
c=2;
k=10
Type that parameters in
initialization commands.
For our example, leave this page empty since we want to
make the parameters are editable through block
parameters.
Department of Mechatronics Engineering

Masking
Mask Editor Icon Page

The Mask Editors Icon pane enables you to create icons that
can contain descriptive text, state equations, images, and
graphics.

Commands that draw icon

Parameters that control icon


appearance.
Example of drawing Commands
we can use to draw the block icon.

Department of Mechatronics Engineering

Masking
Mask Editor Icon Page

Drawing commands
This field allows us to enter commands that draw the
blocks icon. Simulink provides a set of commands that
can display text, one or more plots, or show a transfer
function. We may use the following commands: text,
plot, displ, dpoly, etc as shown in the example
command. We must use these commands to draw your
icon.

Department of Mechatronics Engineering

Masking
Mask Editor Icon Page

Icon options
These controls allow you to specify the attributes of the
block icon.

Frame

Rotation
Department of Mechatronics Engineering

Transparency

Unit

Masking
Mask Editor Icon Page
Let draw the following figure for our masked subsystem.
Set the following icon
option:
Frame: Visible
Transparency: Opaque
Rotation: fixed
Unit: Normalized

Department of Mechatronics Engineering

Masking
Mask Editor Icon Page
Type the following commands into commands field
plot([0.1 0.1],[0.2 0.8])
plot([0.4 0.7 0.7 0.4 0.4],[0.2 0.2 0.8 0.8 0.2])
plot([0.1 0.2],[0.4 0.4])
plot([0.3 0.2 0.2 0.3],[0.3 0.3 0.5 0.5])
plot([.1 .2 .2 .3 .3 .4],[.7 .7 .8 .6 .7 .7])
plot([0.3 0.4],[0.4 0.4])
plot([0.7 0.9],[0.5 0.5])
text(.5,.5,'m')
text(.25,.35,'c')
text(.25,.85,'k')
text(.8,.6,'f')
Department of Mechatronics Engineering

Masking
Mask Editor Icon Page
We will get the following masked subsystem:

Department of Mechatronics Engineering

Masking
Mask Editor Icon Page
We may also displaying a tif image on a block icon using
the following drawing command:
image(imread(file_name.tif))

Department of Mechatronics Engineering

Masking
Close the mask editor, now, our subsystem look like
blocks of Simulink library. Double-click to see the effect
of the masking.

Department of Mechatronics Engineering

Masking
Mask Editor Documentation Page

Department of Mechatronics Engineering

Masking
Mask Editor Documentation Page

Department of Mechatronics Engineering

Discrete System
Concept
Discrete system is dynamical system that can be
described using difference equation.
For example, lets consider the following system:

x1 (k) x1 (k 1) 0.1x 2 (k 1)
x 2 (k) 0.05 sin x1 (k 1) 0.094x 2 (k 1) u(k)
The process is assumed to have a sample time of 0.1
sec.

Department of Mechatronics Engineering

Discrete System
Concept
Simulink has the ability to
simulate discrete system,
including system whose
components operate at
different rates (multi rate)
and system that mix discrete
and continuous components
(hybrid system).

Department of Mechatronics Engineering

Discrete System
Lets build a model and simulate the following system:

x1 (k) x1 (k 1) 0.1x 2 (k 1)
x 2 (k) 0.05 sin x1 (k 1) 0.094x 2 (k 1) u(k)
The process is assumed to have a sample time of 0.1
sec.
We may construct a Simulink model by using
primitive blocks
transfer function block
state variable model.
Department of Mechatronics Engineering

Discrete System
Primitive Blocks
We can build a block diagram as the following figure:
(User-defined functions)
(Math)

(Math)
0.05sin[x1(k-1)]

x1(k)
u(k) (Sources)

++

x2(k) 1

0.1

z
0.094

++
x2(k-1)

1
z
x1(k-1)
(Discrete)

Department of Mechatronics Engineering

Discrete System
Primitive Blocks
Therefore, we have the following Simulink model.

Next, we should define the f(u) function and sample


time.
Department of Mechatronics Engineering

Discrete System
Primitive Blocks
Double click the f(u) block, then modify the desired
function and sampling time. Click OK to close the
window.

0.05*sin(u)

0.1

Department of Mechatronics Engineering

Discrete System
Primitive Blocks
The Simulink model become as follows:

Department of Mechatronics Engineering

Discrete System
Primitive Blocks
Double click the unit delay
blocks, then set sample time
to 0.1.

Do the same process for


other blocks: sum, gain and
source.
Department of Mechatronics Engineering

Discrete System
Primitive Blocks
Before we run the Simulink model, we should carefully
determine the step size of the simulation parameter
especially when we use fix step.

In general, we can select the step size at integer of


multiples of the shortest systems sample time.
In our example, the sample time is 0.1 so we can
select 0.1 as step size. Then we can simulate the
model.
Department of Mechatronics Engineering

Discrete System
Transfer Function
Lets build a model and simulate the following system:

x1 (k) x1 (k 1) 0.1x 2 (k 1)
x 2 (k) 0.05 sin x1 (k 1) 0.094x 2 (k 1) u(k)
In z-domain,

0.1
X1 ( z )
X 2 (z)
z 1
z
0.05 sin X1 (z) U(z)
X 2 (z)
z 0.094
Department of Mechatronics Engineering

Discrete System
Transfer Function
Then we can construct the following block diagram:
(Discrete)
(Math)

U(z)
+(Sources)

X2(z)
z
z 0.094

0.1z
z 1

X1(z)

F(X1)
(Sinks)
(User-defined functions)

We can make the discrete model easily based on this


block.
Department of Mechatronics Engineering

Discrete System
Transfer Function
Therefore, we have the following Simulink model.

Next, we should define the f(u) function and sample


time.
Department of Mechatronics Engineering

Discrete System
Transfer Function
Double click the f(u) block, then modify the desired
function and sampling time. Click OK to close the
window.

0.05*sin(u)

0.1

Department of Mechatronics Engineering

Discrete System
Transfer Function
The Simulink model become as follows:

Department of Mechatronics Engineering

Discrete System
Transfer Function
Double click the discrete
transfer functions, then
modify the transfer function
parameters and sample time.

[1 0]
[1 -0.094]
0.1

[0.1 0]
[1 1]
0.1
Department of Mechatronics Engineering

Discrete System
Transfer Function
The Simulink model become as follows:

Department of Mechatronics Engineering

Discrete System
Transfer Function
Before we run the Simulink model, we should carefully
determine the step size of the simulation parameter
especially when we use fix step.
In general, we can select the step size at integer of
multiples of the shortest systems sample time.
In our example, the sample time is 0.1 so we can
select 0.1 as step size. Then we can simulate the
model.

Department of Mechatronics Engineering

Discrete System
Multirate Discrete Time
Simulink also has capability to handel discrete systems
with involve subsystems that operate at different.
The Simulink sample-time color capability provides help
in keeping track of sample tiems.
To activates the feature, select Format:Port/Signals
Display and then check Sample time colors.

Department of Mechatronics Engineering

Discrete System

Department of Mechatronics Engineering

Hybrid System
Mixed continuous and discrete systems are composed
of both sampled and continuous blocks. Such systems
can be simulated using any of the integration
methods, although certain methods are more efficient
and accurate than others.
For most mixed continuous and discrete systems, the
Runge-Kutta variable-step methods, ode23 and ode45,
are superior to the other methods in terms of efficiency
and accuracy.
Department of Mechatronics Engineering

End of Session 2

Further Reading
Using Simulink

Department of Mechatronics Engineering

You might also like