Simulink Tutorial PDF
Simulink Tutorial PDF
Simulink Tutorial PDF
SIMULINK TUTORIAL
Revised 4/25/03 1
Simulink Tutorial 2003 OSU-ME
TABLE OF CONTENTS
TABLE OF CONTENTS..........................................................................................................2
LIST OF FIGURES ..................................................................................................................3
INTRODUCTION: CONCEPT OF DYNAMIC SYSTEM SIMULATION ..........................4
CONCEPT OF SIGNAL AND LOGIC FLOW ......................................................................................4
CONNECTING BLOCKS...............................................................................................................6
SOURCES AND SINKS ...........................................................................................................7
CONTINUOUS AND DISCRETE SYSTEMS ........................................................................8
NON-LINEAR OPERATORS................................................................................................12
USING FUNCTIONS (WRITTEN AS M, C, ETC..) ............................................................15
MATHEMATICAL OPERATIONS......................................................................................17
SIGNALS & DATA TRANSFER...........................................................................................21
OPTIMIZING VISUAL APPEAL .........................................................................................22
USE OF SUBSYSTEMS AND MASKS ............................................................................................22
MAKING SUBSYSTEMS ............................................................................................................26
VISUAL AIDS ..........................................................................................................................29
SETTING SIMULATION PARAMETERS ..........................................................................31
CONCEPT OF HARDWARE IN THE LOOP......................................................................32
TIPS AND TRICKS................................................................................................................33
RESOURCES..........................................................................................................................34
Revised 4/25/03 2
Simulink Tutorial 2003 OSU-ME
LIST OF FIGURES
Revised 4/25/03 3
Simulink Tutorial 2003 OSU-ME
Computers have provided engineers with immense mathematical powers, which can be
used to simulate (or mimic) dynamic systems without the actual physical system. Simulation of
Dynamic Systems has been proven to be immensely useful when it comes to system modeling
and control design. This because it saves the time and money that would otherwise be spent in
prototyping a physical system. Simulink is a software add-on to MATLAB which is a
mathematical tool developed by The Mathworks, (http://www.mathworks.com) a company based
in Natick, MA. MATLAB is powered by extensive numerical analysis capability. Simulink is a
tool used to visually program a dynamic system (those governed by Ordinary Differential
equations) and look at results. Any logic circuit, or a control system for a dynamic system can be
built by using standard BUILDING BLOCKS available in Simulink Libraries. Various toolboxes
for different techniques, such as Fuzzy Logic, Neural Networks, DSP, Statistics etc. are available
with Simulink, which enhance the processing power of the tool. The main advantage is the
availability of templates / building blocks, which avoid the necessity of typing code for various
mathematical processes.
In Simulink, data/information from various blocks are sent to another block by lines
connecting the relevant blocks. Signals can be generated and fed into blocks (dynamic / static).
Data can be fed into functions. Data can then be dumped into sinks, which could be virtual
oscilloscopes, displays or could be saved to a file. Data can be connected from one block to
another, can be branched, multiplexed etc. In simulation, data is processed and transferred only
at discrete times, since all computers are discrete systems. Thus, a SIMULATION time step
(otherwise called an INTEGRATION time step) is essential, and the selection of that step is
determined by the fastest dynamics in the simulated system. In the following sections, the
different blocks that are available are explained. Figure 1 shows the overview of the Simulink
libraries available. More toolboxes may be available based on what has been purchased. The
latest version is Simulink 4.0, which is used with MATLAB 6.1 (Release 12.1).
Revised 4/25/03 4
Simulink Tutorial 2003 OSU-ME
Revised 4/25/03 5
Simulink Tutorial 2003 OSU-ME
Connecting blocks
To connect blocks, left-click and drag the mouse from the output of one block to the input
of another block. Figure 2 shows the steps involved. Tips for branches and quick connections are
provided at the end of this document.
Revised 4/25/03 6
Simulink Tutorial 2003 OSU-ME
The sources library contains the sources of data/signals that one would use in a dynamic
system simulation. One may want to use a constant input, a sinusoidal wave, a step, a repeating
sequence such as a pulse train, a ramp etc. One may want to test disturbance effects, and can use
the random signal generator to simulate noise. The clock may be used to create a time index for
plotting purposes. The ground could be used to connect to any unused port, to avoid warning
messages indicating unconnected ports.
The sinks are blocks where signals are terminated or ultimately used. In most cases, we
would want to store the resulting data in a file, or a matrix of variables. The data could be
displayed or even stored to a file. The STOP block could be used to stop the simulation if the
input to that block (the signal being sunk) is non-zero. Figure 3 shows the available blocks in the
sources and sinks libraries. Unused signals must be terminated, to prevent warnings about
unconnected signals.
Revised 4/25/03 7
Simulink Tutorial 2003 OSU-ME
Figure 5 contains some advanced linear blocks, available in the Simulink Extras
library. They contain certain advanced blocks, such as a PID control block, transfer functions
with initial conditions, etc.
Revised 4/25/03 8
Simulink Tutorial 2003 OSU-ME
The example is shown in Figure 6. A step input is used as the control input. (It is an open
loop example). The top portion of the block contains the transfer function representation of the
dynamic system. We can observe only the outputs, and cannot monitor the states. Also, initial
conditions cannot be specified. (By using the special transfer function block in the
Simulink\Extras toolbox, initial conditions can be specified). The bottom portion of the Simulink
diagram shows the same 2nd order system in state space representation. The highest derivative
(acceleration in our case) is represented as a function of the input and the other states. This input
is integrated to form the next lower state. Initial conditions for each state can be specified in the
integration block. States can be individually monitored and manipulated.
where
x Output variable
m Mass
c Damping coefficient
k Spring stiffness
f Control force (multiplied by a constant qi)
Equation (1) can be represented in Laplace domain (as a transfer function) as follows:
X(s) K N 2
= 2 (2)
F(s) s + 2 N s + N 2
where
c
Damping coefficient =
2 km
k
N Natural frequency n =
m
1
K Steady Stategain (or Static sensitivity) K =
ks
In the state formulation the system is represented in terms of its highest derivative:
Revised 4/25/03 9
Simulink Tutorial 2003 OSU-ME
1
From (1) mx = ( f cx k s x) x = ( f cx k s x) (3)
m
or it can also be written in terms of its damping and natural frequency as (with qi = 1):
x = K N2 f 2 N x N2 x (4)
In our example below, with zero initial conditions, both the transfer function and the state
representations provide similar results. In general both diagrams are NOT necessary. The steps
for the state formulation are as follows:
1. Solve the differential equation in question for the highest derivative. If the equation is not
normalized (as in the first of equation 3) the highest derivative may be multiplied by a term.
You can divide all the values by that term as was done in the second part of equation 3. You
should now have your single term with the highest derivative on the left side and the rest of
the terms on the right side of the equation.
2. Draw a summer block. The block should have as many plusses and minuses as there are
terms in the right side of the equation (in equation (3) we have 3 components and two of
them are negative, thus we add 2 minus sings and 1 plus sign to our summer). The output of
the summing block should equal the highest derivative term multiplied by a constant. You
can now multiply or divide the constant out to get the derivative by itself.
3. Add integrators. The total number of integrators should equal the total number of derivatives
that you want to remove. For example, if you have a second order mechanical system (like
the one in equation 3) and you want position, you need to integrate twice. Put a block at the
end for the output variable.
4. After each integrator, feed the signal back to its proper place on the summer. Immediately to
the right of an integrator is a value equal to the integral of the value on the left. Be sure to
use a gain block to multiply any value by its proper constant before feeding the value back.
Notice in the state formulation example that the lower derivatives (or states) are accessible
(Internal Variables). This accessibility makes the state formulation a better methodology for
dynamic systems classes. In addition, it is easier to adapt the system to nonlinear components.
The transfer function methodology is simpler (only one block), but it is limited in is application.
Revised 4/25/03 10
Simulink Tutorial 2003 OSU-ME
Note that figure 6 does not show the spring and damper forces. This is rectified in figure 7. Note
neither is incorrect, but figure 7 provides the engineer with more useful information. Figure 7 is
the figure that is produced when one uses the first part of equation (3). Figure 6 is produced
when one uses the second part of equation (3).
Figure 7: A mass-spring-damper system showing the spring and the damper forces.
Revised 4/25/03 11
Simulink Tutorial 2003 OSU-ME
Non-linear operators
A main advantage of using tools such as Simulink is the ability to simulate non-linear
systems and arrive at results without having to solve analytically. It is virtually impossible to
arrive at an analytical solution for a system having non-linearities such as saturation, signum
function, limited slew rates etc. In simulation, systems are analyzed by numerical differentiation
thus non-linearities are not a hindrance. Figure 8 shows some of the non-linear components that
can be incorporated into a simulation. One such could be a saturation block, to indicate a
physical limitation on a parameter, such as a voltage signal to a motor etc. Manual switches are
useful when trying simulations with different cases. Switches are the logical equivalent of IF-
THEN statements in programming. Slew rates using the rate limiter could control the rate of
change of a physical parameter, such as the speed of a DC motor, etc.
Figure 8: Non-linearities
EXAMPLE:
Here is an example using a non-linear block. Consider a sine wave of amplitude 1 (signal
varies between +1 and 1). A saturation block is used to limit the output to an amplitude of 0.5
and the saturated and unsaturated (original) signals are compared. The example is shown in
Figure 9. The saturated and unsaturated signals are clearly seen.
Revised 4/25/03 12
Simulink Tutorial 2003 OSU-ME
A mass-spring-damper system is created with Coulomb friction for the damper force.
The Coulomb friction (from the non-linear library block) is represented as an offset at zero
velocity. The offset for our example is given as 0.5 (with a slope of 1). The coding is shown in
Figure 10. The output for a combination input = ramp(2t) + step + ramp (5t) is shown in Figure
11. The combination input is available as the repeating sequence in the sources library block. As
expected, the Coulomb Friction creates undesired response in the output of the system.
Coulomb
Friction with an
offset of 0.5
Revised 4/25/03 13
Simulink Tutorial 2003 OSU-ME
Revised 4/25/03 14
Simulink Tutorial 2003 OSU-ME
Look-up tables are very useful in mapping different data points and functions. N-
dimensional look-up tables are available. Figure 12 shows the various functions and tables used
in Simulink.
EXAMPLE:
Look-up tables are used for producing outputs based on a pattern of inputs. If the pattern
is known, then the data could be entered in a Look-up table, and linear interpolation is performed
to produce the outputs based on the new set of inputs. Consider the simple example where you
want to multiply 2 inputs and get the output. A 2-D look-up table is created in Simulink, and the
values for 1, 2 and 3 as inputs are entered in the output block, as seen in Figure 13. The block is
used to multiply 2 inputs, and the output is shown as follows (2 * 2.5 = 5):
Revised 4/25/03 15
Simulink Tutorial 2003 OSU-ME
The visualization of the 2-D look-up table is shown in Figure 14. Any 2-D surface can be
represented as a look-up table if data exists for specific points on the inputs. 1-D and n-D look-
up tables are also available in Simulink.
Revised 4/25/03 16
Simulink Tutorial 2003 OSU-ME
EXAMPLE:
To show a more complete use of the look up table, let us consider a non-linear spring with the
spring force defined as, f = 10 5 x + 3 1011 x 3 attached to a mass that weighs 100 lbf.
As an initial condition let us assume that there is an initial deflection of 0.001 inches due to an
internal force of 400 pounds. The time variation of the external force is shown in the figure 16.
The equation of the exact (nonlinearized) model is given by:
Revised 4/25/03 17
Simulink Tutorial 2003 OSU-ME
d2x dx
M 2
= (10 5 x + 3 1011 x 3 ) B + F (5)
dt dt
To compare the nonlinearized model with the linearized one, let us linearize the spring force by
Taylor series at F = 400 and x=0.001 inch. The linearized equation for the force is given by
f 600 10 6 x . Thus the linearized equation for the system is:
d2x dx
M 2 = (10 6 x 600) B + F (6)
dt dt
Figure 17: Simulink block diagram with linearized and nonlinearized spring system
Figure 17 is the Simulink block diagram used to solve both the equations in one run. The
external force F is created by using the look up table. This force can also be created by using 3
step functions, a constant and a summer.
As always the Simulink model is started by the highest order derivative. Further integration of
this derivative gives the velocity and the displacement parameter. So for convenience, arrange
the differential equation such that the highest order derivative (2nd in our case) is the only term
on the left hand side of the equation. Then start drawing the block diagram using the velocity and
position vectors according to the given equation.
Revised 4/25/03 18
Simulink Tutorial 2003 OSU-ME
Remember that we have a system with an initial deflection of 0.001 inch. So double click the
integrator, whose output is the position vector and insert the value of 0.001 as the initial
condition. Similarly, make sure that the initial condition of the integrator with the output as the
velocity is zero for the present case.
The Scope block (see figure 3) is used to see the plots of the position vector x. We can use any
number of these scopes to see the variation of any parameter. But to print the graphs it is a better
idea to save the values using the to Workspace block (see figure 3) and plot it later in the
command window. In the current case the values of the displacement vector are saved in the
workspace with the variable name of out and out1 for nonlinearized and linearized spring
respectively.
Figure 18: Figure showing the variation of displacement with time for linearized and
nonlinearized spring system.
Mathematical operations
Mathematical operators such as products, sum, logical operations such as AND, OR, etc.
can be programmed along with the signal flow. Matrix multiplication becomes easy with the
matrix gain block. Trigonometric functions such as sin or tan inverse (atan) are also available.
Relational operators such as equal to, greater than etc. can also be used in logic
circuits. Figure 19 depicts the available mathematical tools in Simulink 4.0.
Revised 4/25/03 19
Simulink Tutorial 2003 OSU-ME
Revised 4/25/03 20
Simulink Tutorial 2003 OSU-ME
In complicated block diagrams, there may arise the need to transfer data from one portion
to another portion of the block. They may be in different subsystems. That signal could be
dumped into a GOTO block, which is used to send signals from one subsystem to another.
Multiplexing helps us remove clutter due to excessive connectors, and makes matrix
(column/row) visualization easier.
Revised 4/25/03 21
Simulink Tutorial 2003 OSU-ME
Many times, when a complex Simulink diagram is built, the number of connectors and
blocks on a particular level may prevent proper comprehension of the flow of logic. In such
cases, one can create a hierarchical flow of blocks using subsystems, which help keep the block
diagram simple and comprehendible.
Masks are interfaces between the functionality of a subsystem and the user. For example,
if there exists an algorithm that the programmer would like to hide from the user, or will be too
confusing for the user, the programmer uses a mask and hides the algorithm after placing it in a
subsystem.
Revised 4/25/03 22
Simulink Tutorial 2003 OSU-ME
The Simulink Extras block, contains a PID controller. When double-clicked, it asks the
user for the P, I and D gains of the system. The system inside (which can be observed by right-
clicking on the block and clicking on Look under mask) is shown in Figure 22.
Revised 4/25/03 23
Simulink Tutorial 2003 OSU-ME
The following illustrations in Figure 23 show the components of the mask. There are spaces
provided for typing help messages, sketching figures on the face of the block, accepting variables
and creating prompts, etc.
Revised 4/25/03 24
Simulink Tutorial 2003 OSU-ME
In case of complex block diagrams, cluttering of smaller blocks makes the block difficult
to understand. In that case, based on functionality, blocks from the main window can be placed
inside sub-systems and the subsystems make up the main block. Figure 24 shows an example of a
dynamic system with a feedback controller and actuator dynamics. The three functional modules
are now placed in their respective subsystems.
Revised 4/25/03 25
Simulink Tutorial 2003 OSU-ME
Making Subsystems
The following is the procedure for making subsystems such as the block in Figure 24.
1. Drag a subsystem from the Simulink Library Browser and place it in the parent block
where you would like to hide the code. The type of subsystem depends on the purpose of
the block. In general one will use the standard subsystem but other subsystems can be
chosen. For instance, the subsystem can be a triggered block, which is enabled only
when a trigger signal is received. Figure 25 shows the procedure for creating a subsystem
block.
Revised 4/25/03 26
Simulink Tutorial 2003 OSU-ME
2. Open (double click) the subsystem and create input / output PORTS, which transfer
signals into and out of the subsystem. The input and output ports are created by dragging
them from the Sources and Sinks directories respectively. When ports are created in the
subsystem, they automatically create ports on the external (parent) block. This allows for
connecting the appropriate signals from the parent block to the subsystem. Figure 26
shows the creation of the input / output ports.
External Input /
Output PORTS
Internal Input /
Output PORTS
Figure 26: Create input / output ports
Revised 4/25/03 27
Simulink Tutorial 2003 OSU-ME
3. Once the subsystem is created create blocks or code to be enclosed. This is shown in the
bottom part of Figure 27. These blocks contain the code that would be hidden from the
parent block and they communicate with the parent block using the Input / output
PORTS. Figure 27 shows how the hidden code uses the input output ports to
communicate to the parent block.
Hidden code
Revised 4/25/03 28
Simulink Tutorial 2003 OSU-ME
Visual aids
The following visual aids can be used to provide more information about the simulated
block.
- Signal Type
Based on the type of signal, whether double, Boolean etc., signals ca be labeled, that help
us identify what each signal represents.
- Execution order
Sometimes, it is useful to know the order of execution of the blocks in the Simulink
diagram. This command places a number next to the specific block indicating its order of
execution.
The commands are shown in Figure 28. A sample of the features is displayed in Figure 29.
Revised 4/25/03 29
Simulink Tutorial 2003 OSU-ME
VECTOR
WIDE LINES
and WIDTH
Revised 4/25/03 30
Simulink Tutorial 2003 OSU-ME
Revised 4/25/03 31
Simulink Tutorial 2003 OSU-ME
An example is shown below in Figure 33. A first order model is replaced by a Digital to Analog
Converter (DAC) and an Analog to Digital converter (ADC) feeding information from and to the
actual hardware. The DAC signal is sent to an actuator, and the ADC signal is acquired from a
sensor. An example of a Real-time control system is dSPACE, who provide the hardware (data
acquisition and connectivity boards) and the necessary hardware-software interfaces in Simulink.
The interface blocks are available from a dSPACE library in Simulink.
Revised 4/25/03 32
Simulink Tutorial 2003 OSU-ME
1. To copy a block, right click and drop the block onto the target Simulink window.
2. LIBRARIES
To create a template block, save the Simulink block as a library. In the future, you can
copy the block from the library onto any Simulink block where it needs to be used.
Changing the block structure or parameters in the library activates the changes in all the
blocks where they may be used. If you want to break the link of a particular block from
its library source, right-click and say Break Library Link.
3. To create a branch from a signal, right click on the source signal at the point where you
would like to branch to start, and drag it to the target location.
4. Always connect all open ports in a block diagram, to prevent warnings about
unconnected ports. Ground (in Sources) and terminator (in Sinks) can be used to plug
open ports.
Revised 4/25/03 33
Simulink Tutorial 2003 OSU-ME
Resources
The Mathworks Website (contains online documentation)
http://www.mathworks.com
MATLAB/Simulink Resources
http://www.eng.fsu.edu/~cockburn/matlab/matlab_help.html
Simulink: A graphical tool for dynamic system simulation (by G.D. Buckner, NCSU)
http://www.mae.ncsu.edu/org/asme/webpages/tutorial1.pdf
Revised 4/25/03 34