Sie sind auf Seite 1von 40

Simulink Tutorial ADU, 2010 E.

Department of Electrical and Computer Engineering

Simulink Tutorial for 4th Year Electrical and


Computer Engineering Students
Adigrat University, Tigray, Ethiopia
2010 E.C

Page|i ______________________________________________________________________________
Compiled by Alem T. Bsc
Target Audiences- Electrical Eng. 4th year students
Ver. 1
Simulink Tutorial ADU, 2010 E.C

About the Tutorial:


MATLAB (Matrix Laboratory) is a programming language developed by Mathworks
(official website: https://www.mathworks.com). It is a software program that any student in the
field of science and Engineering is going to use it. Any professional Engineer from chemical,
electrical, computer engineering can use this platform to solve very sophisticated problems like
matrix calculation, vector calculation & manipulation, complex graph plotting (2D, 3D plotting)
etc. in general it is super vital platform to do our work.
In MATLAB programming any problem can be solved either by commands, series of codes (the
codes can be C, C++, java ) or graphical user interface (GUI) based model design and
simulation. The later one is called Simulink which is a simulation and model-based design
environment for dynamic and embedded systems, integrated with MATLAB. Its primary interface
is a graphical block diagramming tool and a customizable set of block libraries. It offers tight
integration with the rest of the MATLAB environment and can either drive MATLAB or be scripted
from it.
The purpose of this tutorial is to introduce students to SIMULINK and will give them the basic
knowledge of Simulink and how they can use it together with MATLAB. After this tutorial;

Electrical students in power stream will be capable of designing, modeling and


simulating Electrical power problems like transmission line and distribution
modeling, fault detecting & locating on overhead or underground transmission
lines and other more related problems (projects).
Electrical students in control stream will help them to do open loop, closed
loop, PID, fuzzy logic, neural network, machine learning related projects.
Electrical students in communication stream will help them to simulate in
areas like channel modeling, FM, AM, all digital communication problems etc.

Audience:
The motive of this tutorial is to give hints, highlights and some insight about Simulink to 4 th year
Electrical engineering students and hence will help them to do their projects (course project, final
project, mini projects ). However, any student in the field of science, Engineering and computing
can use this note to advance his/her knowledge of MATLAB.

Prerequisite:
This tutorial will start at very basic level that is intended to audiences with no prior knowledge
about it. However, to study MATLAB including the code part (command part) fundamental
understanding of concepts like variables, constants, expression, statements, etc. is needed. If you
have done programming in any other high-level programming language like C, C++ or Java, then
it will be very much beneficial and learning MATLAB will be like a fun for you.

Page|ii ______________________________________________________________________________
Compiled by Alem T. Bsc
Target Audiences- Electrical Eng. 4th year students
Ver. 1
Simulink Tutorial ADU, 2010 E.C

Table of Contents page No


About the Tutorial: ....................................................................................................................................... ii
Audience: ...................................................................................................................................................... ii
Prerequisite: ................................................................................................................................................. ii
1.Introduction to Simulink: .......................................................................................................................... 1
2.Starting Simulink: ...................................................................................................................................... 2
2.1 Block Libraries: ................................................................................................................................... 5
2.2. Creating New Model: ........................................................................................................................ 7
2.3 Wiring Techniques:............................................................................................................................. 9
2.4. Help Window: .................................................................................................................................. 10
2.5. Configuration:.................................................................................................................................. 12
2.6. Examples:......................................................................................................................................... 13
3. Useful Features:...................................................................................................................................... 23
3.1. Comments/Labels: .......................................................................................................................... 23
3.2. Flip Block:......................................................................................................................................... 23
3.3. Hide Name: ...................................................................................................................................... 23
4. Data Driven Model: ................................................................................................................................ 24
4.1. Command Window: ........................................................................................................................ 24
5. Example: Mass-Spring Damper System ................................................................................................. 26
6. Subsystems: ............................................................................................................................................ 31
7. Recommendation! .................................................................................................................................. 36
References: ................................................................................................................................................. 37

Page|iii ______________________________________________________________________________
Compiled by Alem T. Bsc
Target Audiences- Electrical Eng. 4th year students
Ver. 1
Simulink Tutorial ADU, 2010 E.C

1.Introduction to Simulink:
Simulink is an environment for simulation and model-based design for dynamic and
embedded systems. It provides an interactive graphical environment and a customizable set
of block libraries that let you design, simulate, implement, and test a variety of time-varying
systems, including communications, controls, signal processing, video processing, and image
processing. Simulink offers:
A quick way of develop your model in contrast to text based-programming
language such as e.g., C.
Simulink has integrated solvers. In text based-programming language
such as e.g., C you need to write your own solver.
It allows you to incorporate MATLAB algorithms into models as well as export the simulation
results into MATLAB for further analysis.

Simulink supports:

system-level design
simulation
automatic code generation, i.e. we can generate a code with .m extension file
from the graphical model.
testing and verification of embedded systems

Page|1 ______________________________________________________________________________
Compiled by Alem T. Bsc
Target Audiences- Electrical Eng. 4th year students
Ver. 1
Simulink Tutorial ADU, 2010 E.C

2.Starting Simulink:
start Simulink from the MATLAB IDE:
Open MATLAB and select the Simulink icon in the Toolbar:

Or type Simulink in the Command window, like this:

Page|2 ______________________________________________________________________________
Compiled by Alem T. Bsc
Target Audiences- Electrical Eng. 4th year students
Ver. 1
Simulink Tutorial ADU, 2010 E.C

Then the following window appears (Simulink Library Browser):

Page|3 ______________________________________________________________________________
Compiled by Alem T. Bsc
Target Audiences- Electrical Eng. 4th year students
Ver. 1
Simulink Tutorial ADU, 2010 E.C

The Simulink Library Browser is the library where you find all the blocks you may use in
Simulink. Simulink software includes an extensive library of functions commonly used in
modeling a system. There are two major classes of items in Simulink: blocks and lines. Blocks
are used to generate, modify, combine, output, and display signals. Lines are used to transfer
signals from one block to another.
Blocks
There are several general classes of blocks within the Simulink library:
Sources: used to generate various signals
Sinks: used to output or display signals

Page|4 ______________________________________________________________________________
Compiled by Alem T. Bsc
Target Audiences- Electrical Eng. 4th year students
Ver. 1
Simulink Tutorial ADU, 2010 E.C

Continuous: continuous-time system elements (transfer functions, state-space models, PID


controllers, etc.)
Discrete: linear, discrete-time system elements (discrete transfer functions, discrete state-space
models, etc.)
Math Operations: contains many common math operations (gain, sum, product, absolute value,
etc.)
Ports & Subsystems: contains useful blocks to build a system

2.1 Block Libraries:


Here are some common used Continuous Blocks:

Here are some common used Math Operations Blocks:

Page|5 ______________________________________________________________________________
Compiled by Alem T. Bsc
Target Audiences- Electrical Eng. 4th year students
Ver. 1
Simulink Tutorial ADU, 2010 E.C

Here are some common used Signal Routing Blocks:

Here are some common used Sinks Blocks:

Here are some common used Sources Blocks:

Page|6 ______________________________________________________________________________
Compiled by Alem T. Bsc
Target Audiences- Electrical Eng. 4th year students
Ver. 1
Simulink Tutorial ADU, 2010 E.C

In addition, there are lots of block in different Toolboxes:

2.2. Creating New Model:


Click the New icon on the Toolbar in order to create a new Simulink model:

Page|7 ______________________________________________________________________________
Compiled by Alem T. Bsc
Target Audiences- Electrical Eng. 4th year students
Ver. 1
Simulink Tutorial ADU, 2010 E.C

The following window appears:

You may now drag the blocks you want to use from the Simulink Library Browser to the
model surface (or right-click on a block and select Add to).
Example: In this example we place (drag and drop) two blocks, a Sine Wave and a Scope, on the
model surface:

Page|8 ______________________________________________________________________________
Compiled by Alem T. Bsc
Target Audiences- Electrical Eng. 4th year students
Ver. 1
Simulink Tutorial ADU, 2010 E.C

2.3 Wiring Techniques:


Use the mouse to wire the inputs and outputs of the different blocks. Inputs are located on the
left side of the blocks, while outputs are located on the right side of the blocks.

inputs

output

When holding the mouse over an input or an output the mouse changes to the following
symbol.

Use the mouse, while holding the left button down, to drag wires from the input to the
output.
Page|9 ______________________________________________________________________________
Compiled by Alem T. Bsc
Target Audiences- Electrical Eng. 4th year students
Ver. 1
Simulink Tutorial ADU, 2010 E.C

Automatic Block Connection:


Another wiring technique is to select the source block, then hold down the Ctrl key while
left-clicking on the destination block.
Try the different techniques on the example above.
Connection from a wire to another block:
If wire a connection from a wire to another block, like the example below, you need to hold
down the Ctrl key while left-clicking on the wire and then to the input of the desired block.

2.4. Help Window:


In order to see detailed information about the different blocks, use the built-in Help system.

Page|10 ______________________________________________________________________________
Compiled by Alem T. Bsc
Target Audiences- Electrical Eng. 4th year students
Ver. 1
Simulink Tutorial ADU, 2010 E.C

All standard blocks in Simulink have detailed Help. Click the Help button in the Block
Parameter window for the specific block in order to get detailed help for that block.
The Help Window then appears with detailed information about the selected block:

Page|11 ______________________________________________________________________________
Compiled by Alem T. Bsc
Target Audiences- Electrical Eng. 4th year students
Ver. 1
Simulink Tutorial ADU, 2010 E.C

2.5. Configuration:
There are lots of parameters you may want to configure regarding your simulation. Select
Configuration Parameters in the Simulation menu.
Select, Simulation menu > model configuration parameter

Page|12 ______________________________________________________________________________
Compiled by Alem T. Bsc
Target Audiences- Electrical Eng. 4th year students
Ver. 1
Simulink Tutorial ADU, 2010 E.C

The following window appears:

Here you set important parameters such as:


Start and Stop time for the simulation
What kind of Solver to be used (ode45, ode23 etc.)
Fixed-step/Variable-step
Solvers are numerical integration algorithms that compute the system dynamics over time
using information contained in the model. Simulink provides solvers to support the
simulation of a broad range of systems, including continuous-time (analog), discrete-time
(digital), hybrid (mixed-signal), and multi-rate systems of any size.
2.6. Examples:
Below we will go through some examples in order to illustrate how to create block diagrams
and related functionality.
Example: Integrator with initial value
Create the following model (an integrator) and run the simulation:

Page|13 ______________________________________________________________________________
Compiled by Alem T. Bsc
Target Audiences- Electrical Eng. 4th year students
Ver. 1
Simulink Tutorial ADU, 2010 E.C

Step1: Place the blocks on the model surface


This example uses the following blocks:

Step 2: Configuration

Double-click on the Integrator block. The Parameter window for the Integrator block
appears:

Page|14 ______________________________________________________________________________
Compiled by Alem T. Bsc
Target Audiences- Electrical Eng. 4th year students
Ver. 1
Simulink Tutorial ADU, 2010 E.C

Select Initial condition source=external. The Integrator block now looks like this:

Double-click on the Constant block. The Parameter window for the Constant block appears:

Page|15 ______________________________________________________________________________
Compiled by Alem T. Bsc
Target Audiences- Electrical Eng. 4th year students
Ver. 1
Simulink Tutorial ADU, 2010 E.C

In the Constant value field, we type in the initial value for the integrator, e.g., type the value
1.
Step 3: Wiring
Use the mouse to wire the inputs and outputs of the different blocks.

inputs

output

Wire all the blocks together and you will get the following diagram:

Page|16 ______________________________________________________________________________
Compiled by Alem T. Bsc
Target Audiences- Electrical Eng. 4th year students
Ver. 1
Simulink Tutorial ADU, 2010 E.C

Step 4: Simulation
Start the simulation by clicking the Start Simulation icon in the Toolbar:

Step 5: The Results


Double-click in the Scope block in order to see the simulated result:

Page|17 ______________________________________________________________________________
Compiled by Alem T. Bsc
Target Audiences- Electrical Eng. 4th year students
Ver. 1
Simulink Tutorial ADU, 2010 E.C

Example: Sine Wave


Create the block diagram as shown below:

Set the following parameter for the Integrator block:

Page|18 ______________________________________________________________________________
Compiled by Alem T. Bsc
Target Audiences- Electrical Eng. 4th year students
Ver. 1
Simulink Tutorial ADU, 2010 E.C

The result should be like this:

Example: Using vectors


Create the following block diagram:
For the Gain block, type the following parameters:

Page|19 ______________________________________________________________________________
Compiled by Alem T. Bsc
Target Audiences- Electrical Eng. 4th year students
Ver. 1
Simulink Tutorial ADU, 2010 E.C

As you see, we can use standard MATLAB syntax to create a vector. If you want to see the
signal dimensions, select Signal Dimensions and Wide Non-Scalar Lines.
Select Display>signals & ports> Signal Dimensions, Wide Non-Scalar Lines
The block diagram should now look like this:

The thick lines indicate vectors, while the number (8) is the size of the vector.
Lets change the Saturation block:

Run the simulation and see the results in the Scope block.

Page|20 ______________________________________________________________________________
Compiled by Alem T. Bsc
Target Audiences- Electrical Eng. 4th year students
Ver. 1
Simulink Tutorial ADU, 2010 E.C

The result should look like this:

Example: Transfer Function, build the following diagram

Page|21 ______________________________________________________________________________
Compiled by Alem T. Bsc
Target Audiences- Electrical Eng. 4th year students
Ver. 1
Simulink Tutorial ADU, 2010 E.C

By double clicking on the Transfer Function Set the numerator & denominator coefficient:

The output should look like as below:

Page|22 ______________________________________________________________________________
Compiled by Alem T. Bsc
Target Audiences- Electrical Eng. 4th year students
Ver. 1
Simulink Tutorial ADU, 2010 E.C

3. Useful Features:
You should know about these features in Simulink in order to take full advantage of
Simulink.
3.1. Comments/Labels:
Double-click on your surface in order to write Labels or Comments in your model block
diagram.

Double-click to create Change the default


Labels block labels

3.2. Flip Block:


Normally the inputs are on the left and the outputs on the right, but in some cases, it is
convenient to have the inputs on the right and output on the left side.

In order to flip the input and outputs right-click on the block and select Flip Block.

3.3. Hide Name:


Hide default labels that appear on the diagram, such as Constant1, Integrator1, etc.
Select the block and right click then in the format tab deactivate the show block name.

Page|23 ______________________________________________________________________________
Compiled by Alem T. Bsc
Target Audiences- Electrical Eng. 4th year students
Ver. 1
Simulink Tutorial ADU, 2010 E.C

4. Data Driven Model:


You may use Simulink together with MATLAB in order to specify data and parameters to your
Simulink model. You may specify commands in the MATLAB Command Window or as
commands in an m-file. This is called data-driven modeling.
4.1. Command Window:
Example:

Configure the zero-order hold blocks like this:

Page|24 ______________________________________________________________________________
Compiled by Alem T. Bsc
Target Audiences- Electrical Eng. 4th year students
Ver. 1
Simulink Tutorial ADU, 2010 E.C

Write the following in the Command window in MATLAB:

Run the Simulink model from the Simulink:

We then get the following results:

Page|25 ______________________________________________________________________________
Compiled by Alem T. Bsc
Target Audiences- Electrical Eng. 4th year students
Ver. 1
Simulink Tutorial ADU, 2010 E.C

N.B: You may mix continuous blocks and discrete blocks in the same system, so-called Hybrid
systems. So, the above example is hybrid system.

5. Example: Mass-Spring Damper System


In this example we will create a mass-spring-damper model in Simulink and configure and
run the simulation from a MATLAB m-file.
In this exercise you will construct a simulation diagram that represents the behavior of a
dynamic system. You will simulate a spring-mass damper system.
F(t) cx(t) kx(t) = mx(t)
where t is the simulation time, F(t) is an external force applied to the system, c is the
damping constant of the spring, k is the stiffness of the spring, m is a mass, and x(t) is the
position of the mass. X is the first derivative of the position, which equals the velocity of
the mass. X is the second derivative of the position, which equals the acceleration of the
mass. The following figure shows this dynamic system.

Page|26 ______________________________________________________________________________
Compiled by Alem T. Bsc
Target Audiences- Electrical Eng. 4th year students
Ver. 1
Simulink Tutorial ADU, 2010 E.C


[ Figure: Wikipedia]
The goal is to view the position x(t) of the mass m with respect to time t. You can calculate
the position by integrating the velocity of the mass. You can calculate the velocity by
integrating the acceleration of the mass. If you know the force and mass, you can calculate
this acceleration by using Newton's Second Law of Motion, given by the following equation:
Force = Mass Acceleration
Therefore,
Acceleration = Force / Mass
Substituting terms from the differential equation above yields the following equation:
X = 1/m (F cx - kx)
You will construct a simulation diagram that iterates the following steps over a period of
time.
Create the block diagram for the mass-spring-damper model above.
Instead of hard-coding the model parameters in the blocks you should refer to them as variables
set in an m-file.
These variables should be configured:
x_init
dxdt_init
m=
c=
k
t_step_F
F_O
F_1

Page|27 ______________________________________________________________________________
Compiled by Alem T. Bsc
Target Audiences- Electrical Eng. 4th year students
Ver. 1
Simulink Tutorial ADU, 2010 E.C

The following variables should then be set in the m-file:


x_init=4; %[m]. Initial position.
dxdt_init=0; %[m/s]. Initial Speed.
m=20; %[kg]
c=4; %[N/(m/s)]
k=2; %[N/m]
t_step_F=50; %[s]
F_O=0; %[N]
F_1=4; %[N]
The Block Diagram should look something like this:

Page|28 ______________________________________________________________________________
Compiled by Alem T. Bsc
Target Audiences- Electrical Eng. 4th year students
Ver. 1
Simulink Tutorial ADU, 2010 E.C

The m-File should look something like this:

Note: the keyword simset in the above script is used to configure parameters in the block and
the keyword sim is used to simulate the Simulink model. But to see the output via the scope
you have to double click the scope in the Simulink model.

The output should look like as bellows:

Page|29 ______________________________________________________________________________
Compiled by Alem T. Bsc
Target Audiences- Electrical Eng. 4th year students
Ver. 1
Simulink Tutorial ADU, 2010 E.C

Figure: force applied

Figure: Position x and speed x

Page|30 ______________________________________________________________________________
Compiled by Alem T. Bsc
Target Audiences- Electrical Eng. 4th year students
Ver. 1
Simulink Tutorial ADU, 2010 E.C

6. Subsystems:
Subsystems allow you to create a hierarchical model comprising many layers. A subsystem is a
set of blocks that you replace with a single Subsystem block. As your model increases in size and
complexity, you can simplify it by grouping blocks into subsystems. Using subsystems:
Establishes a hierarchical block diagram, where a Subsystem block is on one
layer and the blocks that make up the subsystem are on another
Keeps functionally related blocks together
Helps reduce the number of blocks displayed in your model window
When you make a copy of a subsystem, that copy is independent of the source subsystem. To
reuse the contents of a subsystem across a model or across models, use either model referencing
or a library.
Example: let you have the Simulink model given in the below.

Now, if you went to create a subsystem for the blocks saturation, Zero-Order Hold & Zero-Order
Hold1, the first thing you have to do is Select the part of your system from which you want to
create a subsystem, right-click and then select Create Subsystem.

Page|31 ______________________________________________________________________________
Compiled by Alem T. Bsc
Target Audiences- Electrical Eng. 4th year students
Ver. 1
Simulink Tutorial ADU, 2010 E.C

So, model with the new created subsystem will be shown in below:

If we double-click on the subsystem we see the blocks in the subsystem:

Page|32 ______________________________________________________________________________
Compiled by Alem T. Bsc
Target Audiences- Electrical Eng. 4th year students
Ver. 1
Simulink Tutorial ADU, 2010 E.C

Practice Problems:
1.Use Simulink to construct the block diagram shown below.
Enter the following parameters:
For the step block, step time=1, initial time=0, final value=0, sample time=0
For the transfer function block, numerator coefficient=[3.78], denominator coefficient=[1 16.9 0]
Gain=1

Show and examine the output?


After simulating output should be like this:

Page|33 ______________________________________________________________________________
Compiled by Alem T. Bsc
Target Audiences- Electrical Eng. 4th year students
Ver. 1
Simulink Tutorial ADU, 2010 E.C

2.closed loop control system

Build the model above and show the output?


The output should look like as below:

3.design the following single phase full wave controlled rectifier and simulate the output voltage
at the load (inductor)?
Parameters to be entered:
For the Ac voltage: amplitude=50, frequency=50, sample time=0 and phase=0
For all the thyristors: resistance (Ron)=0.00001, Rs=500, Cs=250e-9 and the rest zero
For pulse generator and pulse generator1: amplitude=1, period=0.02, Pw=0.015, Pd=0.005
For pulse generator2 and pulse generator3: amplitude=1, period=0.02, Pw=0.005, Pd=0.015
Page|34 ______________________________________________________________________________
Compiled by Alem T. Bsc
Target Audiences- Electrical Eng. 4th year students
Ver. 1
Simulink Tutorial ADU, 2010 E.C

The output should look like this:

Page|35 ______________________________________________________________________________
Compiled by Alem T. Bsc
Target Audiences- Electrical Eng. 4th year students
Ver. 1
Simulink Tutorial ADU, 2010 E.C

4.enter the command sldemo_auto_carelec in the command window of the MATLAB, after
that built-in Simulink model will pop-up as it can be seen below. Analyze and figure-out the
model in addition observe the output via the scope.

7. Recommendation!
This tutorial is just an introductory part which will give you some hint, means and insight about
Simulink. However, if you went to master the platform and hence will help you to do your
project, you need to refer additional concepts about it. This tutorial is a gate like the door so the
door is open learn more & faster, explore more.

Page|36 ______________________________________________________________________________
Compiled by Alem T. Bsc
Target Audiences- Electrical Eng. 4th year students
Ver. 1
Simulink Tutorial ADU, 2010 E.C

References:
[1]. Simulink., Internet: https://www.mathworks.com/help/simulink/index.html?s_cid=doc_ftr
accessed: Nov 27, 2017
[2]. MATLAB, internet: http://home.hit.no/~hansha/ accessed: Nov 27, 2017
[3].Simulink tutorial , internet:
http://home.hit.no/~hansha/documents/lab/Lab%20Work/MATLAB/MATLAB%20Course%20-
%20Part%203.pdf accessed: Nov 28, 2017
[4]. Simulink, internet: https://www.tutorialspoint.com/matlab/matlab_simulink.htm accessed:
Nov 28, 2017
[5]. MATLAB internet: [Video], http://www.mathtutordvd.com accessed: Nov 26, 2017

Page|37 ______________________________________________________________________________
Compiled by Alem T. Bsc
Target Audiences- Electrical Eng. 4th year students
Ver. 1

Das könnte Ihnen auch gefallen