0% found this document useful (0 votes)
160 views21 pages

Simulink Introduction

Uploaded by

Hien Duong Dang
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
160 views21 pages

Simulink Introduction

Uploaded by

Hien Duong Dang
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 21

SIMULINK INTRODUCTION

1. Simulink Overview
Simulink (Simulation and Link) is an extension of MATLAB by Mathworks Inc. It
works with MATLAB to offer modelling, simulation, and analysis of dynamical systems
under a graphical user interface (GUI) environment. 

The construction of a model is simplified with click-and-drag mouse operations. Simulink


includes a comprehensive block library of toolboxes for both linear and nonlinear analyses.
Models are hierarchical, which allow using both top-down and bottom-up approaches. As
Simulink is an integral part of MATLAB, it is easy to switch back and forth during the
analysis process and thus, the user may take full advantage of features offered in both
environments.  This tutorial presents the basic features of Simulink and is focused on control
systems as it has been written for students in my control systems course.

Thousands of scientists and engineers around the world use Simulink to model and solve
real problems in a variety of industries, including:

•Aerospace and Defense

•Automotive

•Communications

•Electronics and Signal Processing

•Medical Instrumentation

2. Getting Started
To start a Simulink session, you'd need to bring up Matlab program first.

From Matlab command window, enter:

>> simulink

Alternately, you may click on the Simulink icon located on the toolbar as shown:

Simulink's library browser window like one shown below will pop up presenting the
block set for model construction.
Search Window

New File Icon

Selected Library

Block Set

Description Box

To see the content of the blockset, click on the "+" sign at the beginning of each toolbox.

To start a model click on the NEW FILE ICON as shown in the screen shot above.
Alternately, you may use keystrokes CTRL+N.

A new window will appear on the screen. You will be constructing your model in this
window. Also in this window the constructed model is simulated. A screenshot of a typical
working (model) window is shown below:
3. SIMULINK USER INTERFACE
3.1 Simulink Library Browser
The Library Browser displays the Simulink block libraries installed on your system.
You build models by copying blocks from a library into a model window.

3.1.1 Tips for Using the Library Browser


When using the Library Browser, note the following:

 You can view the blocks in a library by selecting the library name on the left side of
the Library Browser, or by double-clicking the library.
 When you select a block, a description of that block appears at the bottom of the
browser.
 For more information on a block, select the block, then select Help > Help on the
Selected Block to display the help page for the block.
 You can view the parameters for a block by right-clicking the block, then selecting
Block Parameters.
 You can search for a specific block by entering the name of the block in the block
search field, then clicking the Find block icon

3.1.2 Standard Block Libraries


Simulink software provides 16 standard block libraries. The following table describes each of
these libraries.

Block Library Description


Commonly Used Blocks Contains a group of the most commonly used
blocks, such as the Constant, In1, Out1,
Scope, and Sum blocks. Each of the blocks in
this library are also included in other
libraries.
Continuous Contains blocks that model linear functions,
such as the Derivative and Integrator blocks.
Discontinuous Contains blocks with outputs that are
discontinuous functions of their inputs, such
as the Saturation block.
Discrete Contains blocks that represent discrete time
functions, such as the Unit Delay block.
Logic and Bit Operations Contains blocks that perform logic or bit
operations, such as the Logical Operator and
Relational Operator blocks.
LookUp Tables Contains blocks that use lookup tables to
determine their outputs from their inputs,
such as the Cosine and Sine blocks.
Math Operations Contains blocks that perform mathematical
and logical functions, such as the Gain,
Product, and Sum blocks.
Model Verification Contains blocks that enable you to create
self-validating models, such as the Check
Input Resolution block.
Model-Wide Utilities Contains blocks that provide information
about the model, such as the Model Info
block.
Ports & Subsystems Contains blocks that allow you to create
subsystems, such as the In1, Out1, and
Subsystem blocks.
Signal Attributes Contains blocks that modify the attributes of
signals, such as the Data Type Conversion
block
Signal Routing Contains blocks that route signals from one
point in a block diagram to another, such as
the Mux and Switch blocks.
Sinks Contains blocks that display or export output,
such as the Out1and Scope blocks.
Sources Contains blocks that generate or import
system inputs, such as the Constant, In1, and
Sine Wave blocks.
User-Defined Functions Contains blocks that allow you to define
custom functions, such as the Embedded
MATLAB™ Function block.
Additional Math & Discrete Contains two additional libraries for
mathematical and discrete function blocks.

3.2 Simulink Model Window


The model window contains the block diagram of the model. You build models in the
model window by arranging blocks logically, setting the parameters for each block, and then
connecting the blocks with signal lines.

The model window also allows you to:


 Set configuration parameters for the model, including the start and stop time, type of
solver to use, and data import/export settings.
 Start and stop simulation of the model.
 Save the model.
 Print the block diagram.

4. How to build a Simulink Program


4.1.1 Moving Blocks into the Model Window
The next step in building the model is to move blocks from the Simulink Library
Browser into the model window.

1. Click the + sign next to Signal Processing Blockset in the left pane of the Library
Browser. This displays a list of the Signal Processing Blockset libraries.
2. Click DSP Sources in the left pane. This displays a list of the DSP Sources library
blocks in the right pane. If you do not see the Sine Wave block, scroll down the list
until it is visible.
3. Click the Sine Wave block and drag it into the model window.
4. Click DSP Sinks in the left pane of the Library Browser.
5. Scroll down in the right pane of the Library Browser until you see the

Vector Scope block, and drag the block into the model window to the right of the Sine Wave
block.

Once a block is in the model window, you can move it to another position by dragging the
block with the mouse.

Dragging a Block into a Model Window


4.1.2 Connecting Blocks
The small arrowhead pointing outward from the right side of the Sine Wave block is an
output port for the data the block generates. The arrowhead pointing inward on the Vector
Scope block is an input port. To connect the two blocks, click the output port of the Sine
Wave block and drag the mouse toward the input port of the Vector Scope block, as shown in
the following figure.

When the pointer is on the input port of the Vector Scope block, release the mouse button.
You should see a solid arrow appear, as in the following figure.

If you do not see a solid arrowhead, you have not made a connection. In this case, click the
arrowhead again, drag it all the way to the Vector Scope’s input port, and release the mouse
button.

4.1.3 Setting Block Parameters


To set parameters for the Sine Wave block, double-click the block to open its dialog, as
shown in the following figure. Change the following parameters by clicking in the field next
to the parameter, deleting the default setting, and entering the new setting in its place:

1. Set Amplitude to 5.
2. Set Frequency to 10.
3. Set Sample time to 1/200.
4. Click OK.
4.1.4 Setting Simulation Parameters
Besides individual block parameters, the model also has overall simulation parameters. To
view the current settings,

1. Select the Simulation menu at the top of the model window.


2. Select Configuration parameters to open the Configuration

Parameters dialog box, as shown in the following figure

Configuration Parameters Dialog Box


If you typed commstartup before creating the model, the Stop time should be set to inf.
The Stop time determines the time at which the simulation ends. Setting Stop time to inf
causes the simulation to run indefinitely, until you stop it by selecting Stop from the
Simulation menu.

The Stop time is not the actual time it takes to run a simulation. The actual run-time for a
simulation depends on factors such as the model’s complexity and your computer’s clock
speed.

The settings in the Configuration Parameters dialog box affect only the parameters of the
current model.

Note To conserve memory in a model, click Data Import/Export on the left, and clear the
boxes next to Time and Output under Save to workspace on the right.

4.1.5 Running The Model


Run the model by selecting Start from the Simulation menu. When you do so, a scope
window appears, displaying a sine wave as shown in the following figure.

Sine Wave Displayed in a Scope

Note:

If you do not see the sine wave in the scope, make sure that the Samples per frame
parameter for the Sine Wave block is set to 1000. When you are finished observing the
simulation, select Stop from the Simulation menu.
5. Simulink Online Help
Simulink software provides comprehensive online help that describes Simulink features,
blocks, and functions, and provides detailed procedures for common tasks. The help includes
online versions of all Simulink documentation, including:

 Simulink Getting Started Guide(this guide)


 Simulink User’s Guide
 Simulink Reference
 Writing S-Functions
 Simulink Release Notes

You access the online help from the Help menu of the Simulink Library Browser or model
window.

To access the online help:

 From the Simulink Library Browser, select Help > Simulink Help.
 From the Simulink model window, select Help > Using Simulink.

6. Practice
6.1 Example 1:
Simulation of an Equation.

In this example we will use Simulink to model an equation. Let's consider

x ( t )= Acos( ωt+ φ) (1)

where the displacement x is a function of time t, frequency w, phase angle phi, and amplitude
A. In this example the values for these parameters are set as follows: frequency=5 rad/sec;
phase=pi/2; A=2.

1. From Simulink's library drag the following blocks to the Model Window

Blocks to be dragged to Where located in Simulink library


the model window browser

Ramp Sources

Constant Sources

Gain Math Operation

Sum Math Operation

Product Math Operation


Trigonometry Function Math Operation

Scope Sinks

Mux Signal Routing

2. The next step is to connect these blocks as shown below

Double click on the blocks and enter the appropriate values as prompted by the pop-
up dialog windows. Note that the cosine function can be selected from the pull-down menu in
the pop-up window. In the arrangement shown above, the input signal (a ramp function) is to
be displayed along with the output (displacement) via the use of the mux tool as
demonstrated earlier in this tutorial. To view the plots, double click on the scope.

3. Make sure all blocks are connected correctly then run the simulation (CTRL+T).

You may find the sinusoidal plots to be a bit jaggy. You may want to improve the
resolution of the displayed plot by redefining the Max Step Side value ("auto" is set a default
value) in Simulation Parameters window (with keystrokes CTRL+E in the model window).
Just for fun, you may want to experiment with different choice of solver. ODE45 is a default
choice. You are encouraged to learn more about the solver methods by checking out the help
files in Matlab command window. For instance, help ODE45 for parameters in non-stiff
differential equations.

This example has demonstrated the use Simulink with built-in mathematical functions
and other supporting toolboxes to simulate an equation. The same output/result can also be
obtained with the following set of instructions entered in Matlab command window:

>> t=(0:.01:10);A=2;phi=pi/2;omega=5;

>>xt=A*cos(omega*t+phi);

>> plot(t,xt);grid
6.2 Example 2:
Mass-Spring-Dashpot System Simulation

Consider a mass-spring-dashpot system as shown in Figure E2-1. The


mathematical model for this system is described by

m ẍ + c ẋ+ kx=f (t) (2)

where m is the equivalent mass of the system, c is the damping ratio, k is


the spring stiffness, and f(t) is the forcing function in the x-direction.

Figure E2-1 In this example I will illustrate how to use Simulink to simulate the
response of this system to unit step input.

Step 1:

In Simulink, create a new model window (CTRL+N) and drag the following blocks from the
Simulink library window:

Blocks to be dragged to the Where located in Simulink library browser


model window
Step Sources
Gain Math Operation
Sum Math Operation
Integrator Continuous
Scope Sinks
To Workspace Sinks

Step 2:

By re-arranging Equation (2) to yield an expression for the acceleration term, Eqn (2)
becomes

1
ẍ= ( f ( t ) −c ẋ−kx) (3)
m

Based on Equation 3, we connect the blocks in the diagram as shown in Figure E2-2.
Use CTRL+F and CTRL+R to flip and rotate the blocks as necessary (select the block first
then execute the key sequence). Note that you can use CTRL + right mouse button to create
branches of the connecting lines. Don't worry about the parameter values and the signs for
these blocks at this point as we'll take care of this in STEP 3. Just get them connected first.
Figure E2-2

Step 3

Enter the values of the parameters for each block. In this example, we will set m = 2.0; c=0.7;
k=1. You are encouraged to try different values and observe the system's response to step
input.

To show that you may obtain different form of output, I included another block (in
addition to the scope block) called "simout". This block can be found in the Sinks group from
the Simulink Library browser. The output from this block is used in Matlab workspace. To
illustrate how this block works, I will select a name for the output called "simout" as the
variable name in the block's parameter setting (double click on the "simput" block to bring up
the parameter dialog window). In addition, I will need a time array from the simulation. This
can be specifed as a parameter in the Simulation Parameter window (CTRL+E) under the
Workspace I/O tab as shown in Figure E2-3.

Figure E2-3

 
6.3 Example 3:
Using the same system presented in Example 2, we will simulate the response using transfer
function approach.

Step 1:

In a new Simulink model window, drag the following blocks from the Simulink
library window: Step (from Sources), Transfer Function (from Continous),Scope (from
Sinks), and Save File to Workspace (from Sinks).

Step 2:

Arrange the blocks as shown in Figure E3-1 below.

Figure E3-1

NOTE: Block's background colour: Right click on the block and select the colour from
Background Color menu.

Step 3:

Entering blocks' parameters values.

We'll use the same values for m, c, and k as in Example 2. Double click on the transfer
function to bring up the parameter diaglog window and enter the values for the coefficients in
the denominator as shown in Figure E3-2.

Note that the Transfer function block has a default form of first order in the
denominator (s+1). You may specify different order for the numerator and the denominator
by entering the coefficients associated with the polynomials.
Figure E3-2

Click OK to close the dialog.Double click on the Save Output To A File block to open
the dialog window as shown in Figure E3-3. In the Filename box specify the path and the
name of the file you wish to save. The saved file will be in .MAT format (yes, it's in binary
format! So don't try to read it). You will be able to load this file to Matlab workspace later on.
Make sure to remember where you save the file to. In this example, I'll put the file in my D
drive under "temp" directory and name the file "example3out.mat". You may choose other
convenient location on your computer.

In the Variable name box, enter a name of your choice. Here I name the variable "simout".

Leave other parameters in default settings unless you wish to obtain more plot points by
changing the Sample Time setting.

Figure E3-3

Step 4:
Run the simulation by clicking on the button (alternately you may use keyboard
command CTRL+T ). The screenshot of the output from the Scope block is show in Figure
E3-4.

Figure E3-4

Now by comparing Figure E2-3 and Figure E3-4 you will see that the reponses are
identical. No surprise here since these figures effectively represent the same system, only the
latter involves fewer number of blocks!

LOADING MAT FILE

Use the load command to "import" the save *.mat file generated from the simulation.

Note: In this example, I set the path to the directory where the file "example3out.mat" is
saved. If you do not set the path in Matlab, you have to specify the full path in the load
command, e.g., >>load D:/temp/example3out.mat.

In Matlab command window, enter the followings:

>> load example3out

You may want to verify the loading by asking Matlab to list all of the variables in the
current workspace:

>> who

If the loading is successful, you should see the variable you specified earlier listed the
current workspace. In this example, Matlab echos:

Your variables are:

simout

"simout" is a 2 x m matrix, where m is number of columns reflecting the number of data


points generated from the simulation. The first row contains the time array.
To generate a plot of the step input response from the simulation, simply use the plot
command on "simout" matrix. For this example, the following command produces the plot
shown in Figure E3-5.

>> plot(simout(1,:),simout(2,:));grid

Figure E3-5

In summary, the simulations of a second-order continuous system modeled by Equation 2


have been shown using two different approaches. Different file output options from Simulink
were also demonstrated.

6.4 Example 4:
In this example, we'll consider the same system as described in Example 3. But instead
of using step input, we'll excite the second-order system with impulse load. In addition, we
will simulate the response using a state-space model.

NOTES ON IMPULSE FUNCTION

Impulse function is useful in the simulation of impact or sudden loads such as the striking of
a the tip of the pole against a ball in a pool game. Unit impulse function at a desired instant a
is defined by

δ ( t−a )=0 , t ≠ a ∫ δ ( t ) dt =1 (4)
−∞

Equation 4 is also known as Dirac delta function.

To simulate unit impulse in Simulink, we'll use a two-block step function set as shown below.
SOME NOTES ON STATE-SPACE APPROACH

The concept of the state of the system is utilized extensively in the time-domain
analysis and design of control systems. The state variables (along with the input functions)
used in equations describing the dynamics of a system provide the future state of the system.
Mathematically, the state of the system is described by a set of first-order differential
equation in terms of state variables.

For our example, we will express the natural state variables, e.g., position and velocity, of the
system in terms of the following variables:

x 1=x
dx (5)
x 2= ẋ=
dt

where x1 represents the position and x2 represents the velocity of the system. With the new
state variables defined in Eq. 5, Eq. 3 becomes

x˙1=x 2 (6)
1
x˙2= ( f −c x 2−k x 1)
m

In a matrix from

0 1 0 (7)
{}[
ẋ1 =
ẋ2
−k
m
−c
m
x2 ]{ } [ ]{ }
x1 +
1 0
m
f

Equation 7 may be expressed in a more compact form:

where A is known as the system matrix and B as the input matrix.

ẋ= Ax+ Bu (8)

The output equation is expressed by

y=C x+ D u (9)
where

C=[ 1 0 ] (10)

And

D=0 (11)

C is called the output matrix and D is called the direct transmittance matrix.

MATLAB AND SIMULINK APPLICATION

 In this problem we will first ask Matlab to convert the transfer function as shown in Figure
E3-1 using the following command:

>> [A,B,C,D]=tf2ss(1,[2,0.7,1])

NOTE: You are encouraged to read the help file on tf2ss function (>> help tf2ss).

Matlab's answers:

Now, let's get back to SIMULINK window and construct the necessary blocks as
shown below. Note that the additional blocks (Transfer Fcn and its "scope") shown in light
blue are provided for comparison with the output from state-space block.
As demonstrated in previous examples, you may change the parameters in the state-space
block or any other block by double clicking on it to bring up a parameter editing window.

For state space model, enter the following parameters:

A: [-0.35 -0.5;1 0]
B: [1;0]
C: [0 0.5]
D: 0

Once the entries are completed, click OK button to close the panel and continue on making
necessary entries for other blocks.

For the impulse simulation:

In this example, let's consider an unit impulse at 0.2 second with pulse duration of
0.01 second and a magnitude of 40. To simulate this impulse, we'll enter in the first block
(Step start) the following parameters (double click on the block to bring up the parameter
windows):

Step time: 0.2


Initial value: 0
Final value: 40

For the second block (Step end):

Step time: 0.21


Initial value: 0
Final value: 40

This will produce the following impulse:


WARNING: Cares must be taken in selecting the proper pulse duration. A duration that is too
short may yield computational error. A duration that is too long could misrepresent the
dynamics of the modelled system.

To run the simulation use keystrokes: CTRL+T or click on the button.

To change the simulation parameters and make adjustment to simulation duration, press
CTRL+E or choose Simulation parameters... from Simulation menu.

Enter 30 for Stop time.

The screenshot below shows the impulse response of the system described by Eq. 2 with m =
2; k = 1; c=0.7

For the fun of it, you may want to make adjustments to damping ratio and/or spring stiffness
parameters and run the simulation again to see how these changes would affect the response
to the impulse input.

You might also like