0% found this document useful (0 votes)
51 views16 pages

Simulink - A Brief: After Reading This Chapter You Should Be Able To

simulink

Uploaded by

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

Simulink - A Brief: After Reading This Chapter You Should Be Able To

simulink

Uploaded by

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

CHAPTER

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.

16.2 GETTING STARTED


To start Simulink®, open MATLAB® and type
simulink®

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

models to demonstrate the problem-solving process. To create a new model, select


File ➞ New ➞ Model from the browser window. The model window opens on top
of the library browser (Figure 16.3). For convenience, resize the library browser
window and the model window so that you can see both on the computer screen.
You’ll also want to keep the MATLAB® desktop open, but resize it so that it also
fits on your computer screen without overlapping the other windows. See, for
example, Figure 16.4.
Our first model will simply add two numbers. From either the Source library or
the Commonly Used Blocks library, click and drag the constant block into the
model window. Repeat the process, so that you have two copies of the constant
block in the model, as shown in Figure 16.5.
Now drag the sum block into the model. It is found both in the Commonly
Used Blocks library and the Math Operations library. Notice that the sum block has
two “ports.” You can draw connections between the constants and the sum block by

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.

Run the simulation

(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')

which results in the plot shown in Figure 16.9.


4. Develop a Simulink® Solution
Simulink® includes blocks for creating both sine waves, and for uniform ran-
dom number generators. You can find both in the Source Library. You’ll also
Figure 16.9 A sine wave with noise added
Adding noise to a sine 6
wave can be
4
accomplished using
MATLAB®, as well as
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

16.3 SOLVING DIFFERENTIAL EQUATIONS WITH SIMULINK®


Thus far the problems we’ve solved by creating models in Simulink® could have
been solved more readily in MATLAB®. Where Simulink® really excels is in solving
differential equations. In general, a differential equation includes a dependent var-
iable, an independent variable, and the derivative of the dependent variable with
respect to the independent variable. For example,
dy
 t2  y
dt
is a differential equation. In this case y is the dependent variable, t is the independ-
ent variable, and dy/dt is the derivative with respect to t. In function notation,
dy
 f (t, y)
dt
To find y, we could integrate
dy
y dt  f (t, y)dt
L dt L
This equation has an infinite number of solutions, unless the initial value of y is
defined. For this problem we’ll set y(0) = 0.
To solve this problem in Simulink®, create a model by dragging the appropriate
blocks onto the model window, and connecting them as shown in Figure 16.14.
The blocks include the following:
• A clock, to generate times (Source library)
• A math function block, modified in the parameter window to square the block
input (Math Operations library)
• A sum block (Commonly Used Blocks library)
• An integrator block (Continuous library)
• A scope block (Sink library)
Adjust the integrator block in the parameter window so that the initial condi-
tion is 0. The scope output, after running the model, is shown in Figure 16.15. (You
may need to click on the binocular icon to see the entire plot in the scope screen.)

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)

An alternative approach to this problem might be to use MATLAB®’s symbolic


algebra capability to solve the same problem, as discussed in an earlier chapter.
Because this is a simple differential equation, the dsolve function can be used.
y = dsolve('Dy = t^2 + y','y(0) = 0')
ezplot(y,[0,10])

The solution to the differential equation is shown analytically in the command


window as
y =
2*exp(t) – 2*t - t^2 – 2

and the plot is shown in Figure 16.15b.

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

2. Describe the Input and Output


Input g = 9.81 m/s2
m = 70 kg
c = 0.3 kg/m
v(0) = 0 m/s

Output Plot of velocity versus time from 0 to 15 seconds


3. Develop a Hand Example
Given that the initial velocity is 0, we would expect that the velocity would rap-
idly increase—but would eventually level off and reach a terminal value. We
would expect a plot much like the sketch shown in Figure 16.16.
4. Develop a Simulink® Solution
The Simulink® model is shown in Figure 16.17 along with the resulting plot
displayed on the scope. It is composed of
• Three constant blocks
• Both a divide and product block
• An add block
• An integrator block
• Math function block, set to square the output of the integrator block
As you build the model, you will notice that some of the blocks are reversed
from their standard orientation. You can accomplish this by placing the block
into the model, right clicking the icon, and selecting Format from the drop-
down menu. There are a number of choices that allow the user to select a con-
venient block orientation. In particular, notice that the math function block
has been flipped to accommodate the data flow leaving the integrator block.
Also notice that the time block has been set to 15 seconds.
If a simout block is used to replace the scope block, the output data is sent to
MATLAB®, where it could be used in other programs, or plotted in the usual manner.
5. Test the Solution
Because we are well versed in MATLAB®, we could also solve the problem using
MATLAB® and the tools found in the symbolic algebra toolbox.

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

curve, for a falling


object.

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

that in both cases the 30


velocity levels-off
around 47 m/s. 20

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

We could reformulate the velocity equation in terms of position as


d2x c dx 2
2
g a b
dt m dt
Use Simulink® to create a plot showing how far the object has fallen, as a function
of time.
1. State the Problem
Solve the second-order differential equation
d2x c dx 2
 g  a b
dt 2 m dt
for x as a function of t.
2. Describe the Input and Output
Input
g = 9.81 m/s2
m = 70 kg
c = 0.3 kg/m
v(0) = 0
x(0) = 0
t = 0–15 seconds
Output Create a plot, showing how x changes with time, using the Simulink®
Scope. Also send the results to MATLAB®.
3. Develop a Hand Example
As the object falls, it eventually reaches a terminal velocity, as shown in the pre-
vious example problem. At that point, x should be increasing at a steady rate. A
sketch of the expected behavior is shown in Figure 16.19.
4. Develop a Simulink® Solution
The model created in the previous example can be expanded by adding an
integration block, and by splitting the output into feeds leading to both the
scope and the simout block (see Figure 16.20). Be sure to adjust the simout
block to report the data as an array.
The plot created in the scope is shown in Figure 16.21a.
5. Test the Solution
Once again we could use MATLAB® to solve this second-order differential
equation using the symbolic algebra toolbox.
x = dsolve('D2x = g-c/m*Dx^2','x(0) = 0','Dx(0) = 0')
x = subs(x,{'g','c','m'},{9.81,0.30,70})
ezplot(x,[0,15])
title('A falling object'), xlabel('time,s'), ylabel('position, m')

Figure 16.19
Expected position of an
distance, m

object reaching terminal


velocity.

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

Simulink® is part of the MATLAB® family of programs. It uses a graphical user


interface to facilitate the development of models that represent real systems.
Simulink® is especially useful for modeling dynamic systems––those that can be
mathematically described as differential equations.
Problems 619

Simulink® relies on a large library of blocks, which can be combined to solve a


wide variety of problems. Its visual approach offers an alternative to building m-file
programs using the numerical techniques described in earlier chapters. However,
these same techniques (for example, ode45) are used by Simulink® when its mod-
els are executed.
The MATLAB® help function includes an extensive tutorial on using Simulink®,
including many examples.

Command and Function

simulink® opens the Simulink® library browser

KEY TERMS
dynamic systems differential equations block
analog computers model

You might also like