You are on page 1of 77

MATLAB &

SIMULINK with
Applications
Module IV:
SIMULINK and Neuro-Fuzzy Toolboxes

Department of Mechatronics Engineering

SIMULINK

Session 1:
Getting Started with Simulink

Department of Mechatronics Engineering

Outlines

What is SIMULINK
Getting Started with SIMULINK
Continuous-time System
Subsystems
Masking
Discrete-time System

Department of Mechatronics Engineering

Session Outlines

What is SIMULINK?
Getting Started with SIMULINK
Modeling of Continuous Time

Department of Mechatronics Engineering

What is SIMULINK?

SIMULINK is a software package that enables


you to model, simulate and analyze system
dynamic system.
Dynamics system

Real dynamical systems: Electrical circuits, shock


absorber, braking system, etc.
Continuous time, discrete time and hybrid system

Department of Mechatronics Engineering

What is SIMULINK?
Simulink can be launched by using one of the
following ways from MATLAB.
By clicking the Simulink icon on the MATLAB
toolbar
Click the Simulink icon to start simulink

By entering the command simulink at the


MATLAB
prompt. >> simulink
Department of Mechatronics Engineering

What is SIMULINK?
When Simulink opens you will see the box called
the SIMULINK library browser will open as shown
in the figure.
New window icon
Simulink library
icon

To see the content of


the toolbox, click on the
+ sign at the beginning
of each simulink icon.
Department of Mechatronics Engineering

What is SIMULINK?
Two Steps with Simulink
Creating a graphical model
SIMULINK Model Editor
SIMULINK Model Library

Simulate the created model


Specified start and stop times
Simulation parameters

Department of Mechatronics Engineering

What is SIMULINK?
Elements of a Simulink Model
Sources

System Block
Diagram

Three main element of


Simulink model:
Sources
System Block
Diagram
Sinks
Department of Mechatronics Engineering

Sinks

What is SIMULINK?
Sources
The source library contains of data/signals that
one would use in a dynamic system simulation in
Simulink.
The sources may:
constant input
sinusoidal wave
input
step input
a repeating
sequency
ramp input
etc
Department of Mechatronics Engineering

What is SIMULINK?
Sources
You may also open the source library on a
separate window by right-click the source icon
then select open.

Department of Mechatronics Engineering

What is SIMULINK?
Sinks
The sinks are block were signals are terminated
or ultimately used.
The signals could be
displayed
or even stored in a
file/workspace.

Department of Mechatronics Engineering

What is SIMULINK?
Sources
You may also open the sinks library on a
separate window by right-click the source icon
then select open.

Department of Mechatronics Engineering

What is SIMULINK?
Simulink Model Library
Simulink allow you to represent a model as
continuous as well as discrete systems by using
transfer function, integration blocks, etc.

Department of Mechatronics Engineering

What is SIMULINK?
Simulink Model Library
Simulink also provides nonlinear (discontinuous)
blocks and look-up table blocks for building a
model.

Department of Mechatronics Engineering

What is SIMULINK?
Simulink Model Library

Other useful
blocks are Math
block.

Department of Mechatronics Engineering

Getting Started
Lets start by building a simulink model that
solves the following differential equation

x ( t ) 3 sin( 2 t )

where

The two-step processes:


Building the model
Simulate the model

Department of Mechatronics Engineering

x (0) 1

Getting Started
Building a Model
Sources

System Block
Diagram

Sinks

The equation can be sketched as a simulation


diagram.
Output:
Input:
1
x(t)
3sin(2t)

Initial condition:
x(0)=-1
Department of Mechatronics Engineering

Getting Started
Building a Model
If necessary, open the Simulink block library.
Simulink can be launched by using one of the
following ways from MATLAB.
By clicking the Simulink icon on the MATLAB
toolbar
Click the Simulink icon to start simulink

By entering the command simulink at the


MATLAB
prompt. >> simulink
Department of Mechatronics Engineering

Getting Started
Building a Model
When Simulink opens you will see the box called
the SIMULINK library browser will open as shown
in the figure.
New window icon
Simulink library
icon

Department of Mechatronics Engineering

Getting Started
Building a Model
To build the model, you first need a space to
create model.

An empty window
for building the
model.
Click New window
icon to create a
new space
Department of Mechatronics Engineering

Getting Started
Building a Model
Next, select the Simulink icon to expand the list
of avalaible elements which can be used to create
a system model

Click Simulink icon

Department of Mechatronics Engineering

Getting Started
Building a Model
Each folder contains elements which may be used
to create a system model.

When needed, these elements may simply be


clicked, dragged and dropped into the model
workspace.
Department of Mechatronics Engineering

Getting Started
Building a Model
Lets refer back the sketched model.
Input:
3sin(2t)

1
s

Output:
x(t)

Initial condition:
x(0)=-1
To construct this model, we require three
elements
A Sine Wave Block
An Integrator Block
A Scope Block.
Department of Mechatronics Engineering

Getting Started
Building a Model

Locate the sine wave


under the source
folder and click, drag
and drop it in the
model workspace
window to create
input block.
Department of Mechatronics Engineering

Getting Started
Building a Model

Locate the integrator


under the continuous
folder and click, drag
and drop it in the
model workspace
window to create
operator block.
Department of Mechatronics Engineering

Getting Started
Building a Model

Then, locate the


scope under the sinks
folder and click, drag
and drop it in the
model workspace
window to create
output block.
Department of Mechatronics Engineering

Getting Started
Building a Model
The blocks can be connected by dragging a line
from the output of one block to the input of
another block.

Make the following connection


output of the sine wave input of the
integrator
output of the integrator input of the scope
Department of Mechatronics Engineering

Getting Started
Building a Model
The resulting model is as follows

The next step is setting the model parameters.


Department of Mechatronics Engineering

Getting Started
Building a Model
Lets refer back the sketched model.
Input:
3sin(2t)

1
s

Output:
x(t)

Initial condition:
x(0)=-1
To construct this model, we require three
elements
A Sine Wave Block
amplitude 1 and frequency
An Integrator Block
initialof condition
-1
Department
Mechatronics Engineering

Getting Started
Building a Model
Most blocks have different parameters that are
associated with them. To access parameters,
simply double-click on the block of interest. This
will bring up a dialog box which allows the
parameters to be changed.

double-click on the block of interest


Department of Mechatronics Engineering

Getting Started
Building a Model
Lets modify the sine wave parameters.

Double-click on the sine


wave block and then set
the following
parameters:
Amplitude = 3
Frequency = 2
Department of Mechatronics Engineering

Getting Started
Building a Model
Next, lets modify the integrator block.

Double-click on the
integrator block and
then set the initial
condition equal to -1

Department of Mechatronics Engineering

Getting Started
Building a Model
Next, lets modify the integrator block.

Double-click on the
scope block.
This block does not
have any parameters
which need setting, but
instead displays the
scope display screen.
Department of Mechatronics Engineering

Getting Started
Saving a Model
Now the model has been created and defined.
We may save this model before we run the
model.
To save the created model, click Save as under
File menu, then enter the name of the model.
Click OK.

Department of Mechatronics Engineering

Getting Started
Running a Model
Now the model has been created and defined,
the model is ready to run.
To start simulation,
pull down menu the
simulation menu and
select start.
View the output of the
simulation on the
scope window.
Department of Mechatronics Engineering

Getting Started
Running a Model

x ( t ) 3 sin( 2 t )
where

x (0) 1

Department of Mechatronics Engineering

Getting Started
Model Modification
We can modify the simulink block. For example,
we want to display both input and output in a
scope. To do so, drag mux block to the model
workspace.

Delete the line


connecting output
integrator and scope
input.

Department of Mechatronics Engineering

Getting Started
Model Modification
Then, connect the mux input to the both sine
and integrator block outputs.

Department of Mechatronics Engineering

Getting Started
Running a Model
Now the model has been modified, the model is
ready to run.
To start simulation,
pull down menu the
simulation menu and
select start.
View the output of the
simulation on the
scope window.
Department of Mechatronics Engineering

Getting Started
Running a Model

x ( t ) 3 sin( 2 t )
where

x (0) 1

Department of Mechatronics Engineering

Getting Started
Configuring the Simulation
When we choose Simulation:Start from the
model window menu bar, Simulink solves the
model numerically using one of its solver
methods.
Before we run a simulation we may set various
parameters such as starting and ending time,
simulation step size, a number of tolerance and
solver method.

Department of Mechatronics Engineering

Getting Started
Configuring the Simulation
To set simulation parameters, choose Simulation
Parameters under Simulation menu.

Simulation
parameters dialog
box opens.
Department of Mechatronics Engineering

Four tabbed
pages

Getting Started
Configuring the Simulation
Solver page consists of three section; simulation
time, solver option and output option.
Change the following:
start 0
end 20
fixed step solver
Ode4 (Runge-Kutta)
Fixed step size 0.01

Department of Mechatronics Engineering

Getting Started
Configuring the Simulation
The Workspace I/O page permits us to acquire
simulation input from the MATLAB workspace
and send output directly to the MATLAB
workspace.
Enter the following:
Time: t
Output: y
Limit data point:
2000
Click OK to close the box.
Department of Mechatronics Engineering

Getting Started
Configuring the Simulation
In MATLAB 7, to set simulation parameters,
choose Configuration Parameters under
Simulation menu.

Department of Mechatronics Engineering

Getting Started
Configuring the Simulation
Solver page consists of three section; simulation
time, solver option and output option.
Change the following:
start 0
end 20
fixed step solver
Ode4 (Runge-Kutta)
Fixed step size 0.01

Department of Mechatronics Engineering

Getting Started
Configuring the Simulation
The Data Export/Import permits us to acquire
simulation input from the MATLAB workspace
and send output directly to the MATLAB
workspace.
Enter the following:
Time: t
Output: y
Limit data point:
2000
Check
State
Click
OK to
close the box.
Department of Mechatronics Engineering

Getting Started
Configuring the Simulation

The Diagnostic page allows us to select the


action taken for various exceptional conditions
and it also includes options to control automatic
block output consistency checking, bounds
checking and simulation profiling.

Department of Mechatronics Engineering

Getting Started
Running a Model
Now the model has been modified, the model is
ready to run.
To start simulation,
pull down menu the
simulation menu and
select start.
View the output of the
simulation on the
scope window.
Department of Mechatronics Engineering

Getting Started
Running a Model
Now the model has been modified, the model is
ready to run.
To start simulation in
MATLAB 7, pull down
menu the simulation
menu and select start.
View the output of the
simulation on the
scope window.
Department of Mechatronics Engineering

Getting Started
Running a Model

Department of Mechatronics Engineering

Getting Started
Simulink Help System
Simulink includes an extensive help system in
the form of on-line Users Guide in HTML format
and an integrated browser.

This help system is available by clicking the Help


button in the dialog box of a particular block.

Department of Mechatronics Engineering

Getting Started
Simulink Help System
For example, we want to access the help system
for integrator block.

Double-click on the
integrator block and
then click help button.

Department of Mechatronics Engineering

Getting Started
Simulink Help System

Department of Mechatronics Engineering

Continuous System
Concept
Continuous system is dynamical system that can
be described using differential equation.
Lets consider a mass-spring-damper system as
our example.
Motion
x(t)
equation
f ( t ) mx ( t ) cx ( t ) kx ( t ) f ( t )
c

We may construct a Simulink model by using


primitive blocks, transfer function block and
state variable model.
Department of Mechatronics Engineering

Continuous System
Primitive Blocks
The motion equation can rewrite as follows

1
mx ( t ) f ( t ) cx ( t ) kx ( t )
m

f (t)

1/ m

+
-

x ( t )

x ( t )

c
k
Department of Mechatronics Engineering

x(t)

Continuous System
Primitive Blocks

ADD
(Math)

f (t)

1/ m

+
-

Step
(Source
)

x ( t )

x ( t )

x(t)

Scope
(Sinks)

c
Gain
(Sinks)

Department of Mechatronics Engineering

Integrator
(Math)

Continuous System
Primitive Blocks
To build the model, you first need a space to
create model.

An empty window
for building the
model.
Click New window
icon to create a
new space
Department of Mechatronics Engineering

Continuous System
Primitive Blocks
Locate the Step Block under the source folder and
click, drag and drop it in the model workspace
window to create input block.

Department of Mechatronics Engineering

Continuous System
Primitive Blocks
Locate the Sum Block in the Math Operation
library and Click, Drag and Drop it into your new
workspace.

The Sum Block is to have 3 inputs. Double click


the block and set the parameter to rectangular
and +-Department of Mechatronics Engineering

Continuous System
Primitive Blocks
Locate the Gain Block in the Math Operation
library and Click, Drag and Drop it into your new
workspace. You need to do this three times or you
may use Copy and Paste for making three Gain
Blocks.

Department of Mechatronics Engineering

Continuous System
Primitive Blocks
Double click for each Gain Block and modify the
gain parameters as 1/m, c and k repectively.

Department of Mechatronics Engineering

Continuous System
Primitive Blocks
If necessary, we can resize the block to improve
the appearance of a model. For example
parameter 1/m of the gain block doesnt fit on the
block icon, the display value is K-.

To resize, select the block causing the handles to


appear. Click the desired handle, and, drag the
handle to resize the block.

Department of Mechatronics Engineering

Continuous System
Primitive Blocks
The second and third gain blocks may be flipped
to make an easier connection. Click the
respective gain block, then select Flip Blocks
under Format Menu.

Department of Mechatronics Engineering

Continuous System
Primitive Blocks
Next, insert two integrator and scope blocks by
drag and drop them into the model workspace.

Department of Mechatronics Engineering

Continuous System
Primitive Blocks
Finally, make connection between block correctly
to finish the model building.

Department of Mechatronics Engineering

Continuous System
Transfer Function Blocks
Taking the Laplace Transform, the motion
equation can rewrite as follows

ms

cs k X s F s
F s

X s
1

F s ms 2 cs k

1
2
ms cs k

X s

Hence, we can use only a transfer function block.

Department of Mechatronics Engineering

Continuous System
Transfer Function Blocks
Locate the Transfer Function in the Continuous
library and Click, Drag and Drop it into the
workspace.

Double click the transfer function then enter [1]


and [m c k] on the numerator and denominator
respectively.
Department of Mechatronics Engineering

Continuous System
Transfer Function Blocks
Add one Scope Block to the model workspace
then connect it to the Transfer Function Block.
Connect also the Step Block output to the
Transfer Function Block input.

Department of Mechatronics Engineering

Continuous System
State Space Block
The motion equation can also be rewritten in the
form of state space as follows

x Ax Bu

y Cx Du
where

k / m c / m

C 1 0
Department of Mechatronics Engineering

0
B

1 / m

D 0

Continuous System
Transfer Function Blocks
Locate the State Space Block in the Continuous
library and Click, Drag and Drop it into the
workspace.

Double click the transfer function then enter A, B,


C and D state space parameter fields.
Department of Mechatronics Engineering

Continuous System
Transfer Function Blocks
Add one Scope Block to the model workspace
then connect it to the State Space Block. Connect
also the Step Block output to the Transfer
Function Block input.

Department of Mechatronics Engineering

Continuous System
Parameters Definition
Before we run the model, we have to define the
model parameters since we just enter constant
variable.
In the MATLAB prompt, write the following
>>
>>
>>
>>
>>

m=1;c=2;k=10;
A=[0 1;-k/m -c/m];
B=[0;1/m];
C=[1 0];
D=[0];

Department of Mechatronics Engineering

Continuous System
Running a Model
Now we have simulation model, and we are ready
to simulate it. You may change the simulation
parameters if necessary.

Department of Mechatronics Engineering

End of Session 1

Further Reading
Using Simulink

Department of Mechatronics Engineering

End of Session 1

Further Reading
Using Simulink

Department of Mechatronics Engineering

You might also like