Simulink - A Brief: After Reading This Chapter You Should Be Able To
Simulink - A Brief: After Reading This Chapter You Should Be Able To
16 Simulink®—
A Brief
Introduction
Objectives
After reading this chapter you • Create and run a simple
should be able to: Simulink® Model
• Understand how Simulink® • Import Simulink® results
uses blocks to represent into MATLAB®
common mathematical
processes
INTRODUCTION
Simulink® is an interactive, graphics-based program that allows you to solve problems
by creating models using a set of built-in “blocks.” It is part of the MATLAB® software
suite, and requires MATLAB® to run. Simulink® is included with the student edition
of the software, but is not part of the standard installation of the professional edition;
this means that it may or may not be included on your version of MATLAB®. LabView,
produced by National Instruments, is Simulink®’s biggest competitor.
16.1 APPLICATIONS
Simulink® is designed to provide a convenient method for analyzing dynamic sys-
tems, i.e., systems that change with time. In particular, it found early acceptance in
the signal processing community, and is reminiscent of the approach used to pro-
gram analog computers. In fact, one way to think of Simulink® is as a virtual analog
computer. Analog computers required the user to make actual physical connections
between electrical components that acted as adders, multipliers, integrators, etc.
Output from the computer was viewed on an oscilloscope. This is reflected in both
16.2 Getting Started 605
the names of the blocks used in Simulink®, and in the icons used to represent
various operations.
One shouldn’t jump to the conclusion that Simulink® is only useful for analyz-
ing electrical systems. Similar mathematical equations describe the behavior of
dynamic mechanical systems, reactive chemical systems, and dynamic fluid systems.
In fact, it is common to introduce students to the behavior of electricity through
analogy with pipe flow problems.
Simulink®’s strength is its ability to model dynamic systems—which are mod-
eled mathematically as differential equations. Usually these systems change with time,
but the independent variable could also be location. Differential equations can be
solved numerically in MATLAB® by making use of functions such as ode45, which
utilizes Runge–Kutta techniques. They can also be solved analytically using the sym-
bolic algebra toolbox, which utilizes the MuPad engine. Simulink® uses similar
methods, but they are transparent to the user. Instead of programming equations
directly, a visual model is created by collecting appropriate Simulink® blocks and
connecting them together, using a graphical user interface.
into the command window. (Or select the Simulink® icon from the Shortcut tool-
bar as shown in Figure 16.1).
The Simulink® Library Browser opens, showing the available libraries of blocks
used to create a Simulink® model (Figure 16.2). The browser is the location where
you’ll select blocks and drag them into the model workspace. Spend a few minutes
exploring the browser. To view the blocks available in each library, either select the
library from the left-hand pane or double click on the icons in the right-hand pane.
In particular, take a look at the Commonly Used Blocks library—the Source and
Sink libraries and the Math Operations library.
Simulink®’s strength is in solving complex dynamic systems, but before we try
to work on a complex system, it would be better to build some very simple static
Figure 16.1
Access Simulink® either
from the command
window, or by selecting the
icon from the shortcut
toolbar.
606 Chapter 16 Simulink®—A Brief Introduction
Figure 16.2
The Simulink® Library
Browser contains numerous
blocks that are used to Double-click on a
library name to see the
create a Simulink® model.
available blocks
Figure 16.3
The model window is the
workspace where
Simulink® models are
created and executed.
Drag blocks into the
model window to solve
problems
16.2 Getting Started 607
Figure 16.4
Simulink® uses multiple
windows. Arrange them on
your computer desktop so
that you can easily drag
blocks from the Simulink®
Library Browser to the
model window.
Figure 16.5
Two copies of the constant
block were added to the
model.
clicking and dragging between the ports, as shown in Figure 16.6. You should notice
that the cursor changes to a cross-hair as you connect the ports. The model we’ve
created thus far just adds 1 + 1, and doesn’t display the answer. We’ll need to modify
the constant blocks to specify a value different from the default, which in this case
is 1. Double click on each constant block, and change the “constant value” field, for
example, to 5 in the top block and 6 in the bottom block.
To add a display option, look in the sink library. For this case, the display block
is all we need, so drag it to the model and connect it to the output port of the sum
block. The last thing we need to do before running the model is to adjust the simu-
lation time, from the box on the menu bar (see Figure 16.7). Since nothing in this
608 Chapter 16 Simulink®—A Brief Introduction
Figure 16.6
The constants are
connected to the sum
block. Change the values
in the constant blocks by
double clicking and
modifying the “constant
value” field.
Figure 16.7
(a) The completed model.
Simulation time
(b) Results are shown in the
display block.
(a)
Result
(b)
16.2 Getting Started 609
Figure 16.8
The sum block can be used
to perform subtraction
operations, as well as for
adding more than two
input values.
calculation will change with time, we can change the value to zero. Run the simula-
tion by selecting the run button on the toolbar (the black triangle) or by selecting
Simulation ➞ Start from the menu bar.
Save this model in the usual way, by selecting File ➞ Save and adding an appro-
priate name. The files are stored with the extension, .mdl.
As the sum block serves both the addition and subtraction functions, you
could use this same model to perform subtraction operations. Double click the
sum block in the model and the block parameter window opens, as shown in
Figure 16.8.
The block description is located near the top of the window, and provides
information on how to use the block—in this case the sum block. This description
includes instructions to change the block into a “subtraction block” by changing
the input from |++ to |+−. We could also adjust the block to add three inputs by
changing the list of signs field to the number 3. Adjust your model and run it sev-
eral more times as you explore the possibilities for the sum block.
HINT
Simulink® includes a “subtraction” block, but if you open its block parameter
window you’ll notice the block title is “sum.”
The previous example was trivial. A slightly more complex model, with results
that change with time, is described in Example 16.1.
610 Chapter 16 Simulink®—A Brief Introduction
EXAMPLE 16.1
RANDOM NUMBERS
As we saw in Example 3.5, random numbers can be used to simulate the noise we
hear on the radio as static. Although we could solve a similar problem in MATLAB®,
let’s use Simulink®. In this case, instead of a music file use a sine wave as the input
to which we want to add the noise, using the following equation:
y 5*sin(2t) noise
The noise should be the result of a uniform random number generator, with a
range of 0 to 1.
1. State the Problem
Create a Simulink® model of the equation
y 5*sin(2t) noise
where the noise is based on a random number.
2. Describe the Input and Output
Input Use Simulink®’s built-in sine wave generator to provide the sine wave.
Use Simulink®’s built-in random number generator to simulate the
noise.
Output View the results using the Simulink® Scope block.
3. Develop a Hand Example
In this case, since we are well versed in MATLAB®, a MATLAB® solution will
substitute for a hand example.
t=0:0.1:10;
noise = rand(size(t));
y=5*sin(2*t)+noise;
plot(t,y)
title('A sine wave with noise added')
xlabel('time,s'), ylabel ('function value')
2
Simulink®.
0
2
4
6
0 2 4 6 8 10
time, s
16.2 Getting Started 611
need to include an add block. Finally, add a scope (the name comes from the
word “oscilloscope”) to view the plotted result. Your model should resemble
the one shown in Figure 16.10. Notice that the time field in the upper right
corner of the model is set to 10 seconds, and that two additional scopes were
added so that we can observe the behavior of the sine wave generator, the ran-
dom number generator, and the combined output.
The model specifies only a sine wave, not the entire sine portion of the
expression, 5*sin(2t). Open the Sine Wave block by double clicking on the icon
inside the model. The Source Block Parameters window opens (as shown in
Figure 16.11), allowing us to specify the amplitude, the frequency and addi-
tional parameters as needed. By changing the amplitude to 5 and the frequency
to 2, the block now represents the first term in our equation.
Similarly, the random number generator parameter window can be modi-
fied to specify a minimum value of 0 and a maximum value of 1. Run the model
by selecting the black start simulation triangle, or by selecting Simulation ➞
Start. To view the output, double click on each of the scopes. Scale the images
by selecting the binocular icon as shown in Figure 16.12, which shows the
results of the combined inputs.
5. Test the Solution
Compare the results to those found with the MATLAB® solution. We could also
revise the model, so that the results are sent to MATLAB® by replacing the scope
for the combined output with the simout block, as shown in Figure 16.13. The
simout block is found in the sinks library. Before running the model, you’ll need
to modify the block parameters (double click on the block to open the window).
Change the Save format from Structure to Array. Re-execute the model, and
observe that two new arrays have appeared in the MATLAB® workspace window,
simout and tout, both of which are 101x1 double precision arrays. The values
in the arrays can now be used for plotting, or in other calculations.
Figure 16.10
Simulink® model to add
noise to a sine wave.
(continued)
612 Chapter 16 Simulink®—A Brief Introduction
Figure 16.11
(a) The Sine Wave
parameter window.
(b) The Uniform
Random Number
parameter window. The
Source Block Parameter
window for each
Simulink® block allows
the user to modify the
default values of the
input parameters.
Access the parameter
window by double
clicking on the block in
the model window.
(a) (b)
Figure 16.12
The scope output from Binocular icon used to resize
the plotting window
the three oscilloscopes
specified in the
Simulink® model.
Figure 16.13
The simout block sends
simulation results to the
MATLAB® workspace,
where they can be used
in other calculations as
needed.
16.3 Solving Differential Equations with Simulink® 613
Figure 16.14
Simulink® model to solve
the differential equation
dy
t 2 y.
dt t2+y
t
t2
y
614 Chapter 16 Simulink®—A Brief Introduction
Figure 16.15
A plot of the solution to the
ordinary differential
equation, dy/dt t 2 y ,
with y(0) = 0. (a) Plot
created with Simulink®.
(b) Plot created in
MATLAB® using symbolic
algebra.
(a) (b)
EXAMPLE 16.2
VELOCITY OF A FALLING OBJECT
Consider an object, falling toward the ground. A widely reported equation describ-
ing the resulting velocity is the differential equation:
dv c
g v2
dt m
where
g is the acceleration due to gravity
v is the velocity
m is the mass
c is the second-order drag coefficient
Solve this equation by finding velocity as a function of time, for the first 15 seconds.
1. State the Problem
Use Simulink® to find the time versus velocity behavior of a falling object.
16.3 Solving Differential Equations with Simulink® 615
clear,clc
y = dsolve('Dv = g-c/m*v^2','v(0) = 0')
y = subs(y,{'g','c','m'},{9.81,0.30,70})
ezplot(y,[0,15])
title('A falling object'), xlabel('time,s')
ylabel('velocity, m/s')
The resulting plot is shown in Figure 16.18, and corresponds well to the scope
output from the Simulink® model.
Figure 16.16
Projected behavior of
the velocity versus time
velocity, m/s
time, s (continued)
616 Chapter 16 Simulink®—A Brief Introduction
Figure 16.17
Simulink® model to
solve the falling object
problem.
v
dv/dt
Figure 16.18
The velocity plot for a
falling object. (a) Plot A falling object
created using Simulinkk®. 50
(b) Plot created using
MATLAB®’s symbolic 40
algebra tools. Notice
velocity, m/s
10
0
0 5 10 15
time, s
(a) (b)
EXAMPLE 16.3
POSITION OF A FALLING OBJECT
In the previous example, we solved the following differential equation for velocity
as a function of time.
dv c
g v2
dt m
However, velocity can also be described as a derivative; it is the rate of change of
position with time.
dx
v
dt
16.3 Solving Differential Equations with Simulink® 617
Figure 16.19
Expected position of an
distance, m
time, s
(continued)
618 Chapter 16 Simulink®—A Brief Introduction
Figure 16.20
Simulink® model to
solve the second-order
differential equation,
d2x c dx 2
⫽g⫺ a b . acceleration, d2x/dt2 velocity, dx/dt
dt 2 m dt
position, x
Figure 16.21
The position of a falling
object. (a) Results from
A falling object
the Simulink® Scope.
600
(b) Results from
MATLAB® using a
symbolic algebra 400
position, m
solution.
200
⫺200
0 5 10 15
time, s
(a) (b)
The resulting plot (Figure 16.21b) matches the scope output, and thus verifies
our calculations.
By using both Simulink® and a symbolic algebra approach, we can develop
confidence in the Simulink® solutions. Not all problems can be solved symboli-
cally, so having both approaches available is important. This example was inspired
by Steven Chapra’s use of a skydiver to illustrate techniques to solve differential
equations, in ‘Numerical Methods for Engineers’, McGraw-Hill, 2010.
SUMMARY
KEY TERMS
dynamic systems differential equations block
analog computers model