Getting Started Guide: Simulink
Getting Started Guide: Simulink
R2012b
How to Contact MathWorks
www.mathworks.com Web
comp.soft-sys.matlab Newsgroup
www.mathworks.com/contact_TS.html Technical Support
508-647-7000 (Phone)
508-647-7001 (Fax)
Trademarks
MATLAB and Simulink are registered trademarks of The MathWorks, Inc. See
www.mathworks.com/trademarks for a list of additional trademarks. Other product or brand
names may be trademarks or registered trademarks of their respective holders.
Patents
MathWorks products are protected by one or more U.S. patents. Please see
www.mathworks.com/patents for more information.
Revision History
September 2005 Online only New for Version 6.3 (Release 14SP3)
March 2006 Online only Revised for Simulink 6.4 (Release 2006a)
September 2006 Online only Revised for Simulink 6.5 (Release 2006b)
March 2007 First printing Revised for Simulink 6.6 (Release 2007a)
September 2007 Second printing Revised for Simulink 7.0 (Release 2007b)
March 2008 Third printing Revised for Simulink 7.1 (Release 2008a)
October 2008 Fourth printing Revised for Simulink 7.2 (Release 2008b)
March 2009 Fifth printing Revised for Simulink 7.3 (Release 2009a)
September 2009 Online only Revised for Simulink 7.4 (Release 2009b)
March 2010 Online only Revised for Simulink 7.5 (Release 2010a)
September 2010 Online only Revised for Simulink 7.6 (Release 2010b)
April 2011 Online only Revised for Simulink 7.7 (Release 2011a)
September 2011 Sixth printing Revised for Simulink 7.8 (Release 2011b)
March 2012 Seventh printing Revised for Simulink 7.9 (Release 2012a)
September 2012 Eighth printing Revised for Simulink 8.0 (Release 2012b)
Contents
Introduction
1
Product Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-2
Key Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-2
v
Create a Simulink Model
3
Create a Simple Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-2
Overview of the Simple Model . . . . . . . . . . . . . . . . . . . . . . . 3-2
Create the Simple Model . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-3
Connect Blocks in the Simple Model . . . . . . . . . . . . . . . . . . 3-8
Simulate the Simple Model . . . . . . . . . . . . . . . . . . . . . . . . . 3-12
vi Contents
Basic Simulation Workflow
5
Simulation with Data Import and Signal Inspection . . 5-2
Data Import and Logging Workflow . . . . . . . . . . . . . . . . . . 5-2
Example Used in this Tutorial . . . . . . . . . . . . . . . . . . . . . . . 5-2
Import Signal Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-4
Simulate Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-10
Compare Signals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-14
Create, Save, and Print a Figure . . . . . . . . . . . . . . . . . . . . . 5-16
Index
vii
viii Contents
1
Introduction
Product Description
Simulation and Model-Based Design
Simulink® is a block diagram environment for multidomain simulation and
Model-Based Design. It supports system-level design, simulation, automatic
code generation, and continuous test and verification of embedded systems.
Simulink provides a graphical editor, customizable block libraries, and
solvers for modeling and simulating dynamic systems. It is integrated with
MATLAB®, enabling you to incorporate MATLAB algorithms into models and
export simulation results to MATLAB for further analysis.
Key Features
• Graphical editor for building and managing hierarchical block diagrams
• Libraries of predefined blocks for modeling continuous-time and
discrete-time systems
• Simulation engine with fixed-step and variable-step ODE solvers
• Scopes and data displays for viewing simulation results
• Project and data management tools for managing model files and data
• Model analysis tools for refining model architecture and increasing
simulation speed
• MATLAB Function block for importing MATLAB algorithms into models
• Legacy Code Tool for importing C and C++ code into models
1-2
Modeling, Simulation, and Analysis with Simulink®
Models are hierarchical, so you can build models using both top-down
and bottom-up approaches. You can view the system at a high level, then
double-click blocks to see increasing levels of model detail. This approach
provides insight into how a model is organized and how its parts interact.
1-3
1 Introduction
See “Model-Based Design” on page 1-5 for more information about the
modeling process.
Using scopes and other display blocks, you can see the simulation results
while the simulation runs. You can then change parameters and see what
happens for “what if” exploration. The simulation results can be put in the
MATLAB workspace for postprocessing and visualization.
1-4
Model-Based Design
Model-Based Design
In this section...
“What Is Model-Based Design?” on page 1-5
“Model-Based Design Process” on page 1-6
1-5
1 Introduction
You perform the first three steps of this process outside of the Simulink
software environment before you begin building your model.
• Heater subsystem
• Thermostat subsystem
• Thermodynamic model subsystem
The most effective way to build a model of this system is to consider each
of these subsystems independently.
1-6
Model-Based Design
Once you have answered these questions, you should have a comprehensive
list of system components, and you are ready to begin modeling the system.
• Algebraic equations
• Logical equations
• Differential equations, for continuous systems
• Difference equations, for discrete systems
1-7
1 Introduction
Build the block diagram for each of your subcomponents separately. After
you have modeled each subcomponent, you can then integrate them into a
complete model of the system.
See “Create the Simple Model” on page 3-3 for more information on building
the block diagram.
See “Simulate the Simple Model” on page 3-12 for more information on
running a simulation.
You can use the linearization and trimming tools available from the MATLAB
command line, plus the many tools in MATLAB and its application toolboxes
to analyze and validate your model.
1-8
Documentation and Additional Resources
Access online help from Help menus and context-sensitive block labels.
• From the Simulink Library Browser, select Help > Simulink Help.
• From the Simulink Editor, select Help > Simulink > Simulink Help.
• Right-click a Simulink block, and then select Help.
• From the model Configuration Parameters or a block parameters dialog
box, right-click a parameter label, then select What’s This?
1-9
1 Introduction
Simulink Examples
Simulink software provides a variety of example models that illustrate key
modeling concepts and Simulink features.
You can access these examples from the Help menu or from the
documentation.
• From the Simulink Editor, select Help > Simulink > Examples.
• From the documentation, click Examples at the top of the product landing
page.
Featured examples include a button at the top of the Help browser for opening
the model in the Simulink Editor.
http://www.mathworks.com/products/simulink
1-10
2
simulink
You can also open the Simulink Library Browser from the MATLAB Toolstrip,
2-2
Start the Simulink® Software
If you have not already loaded Simulink, a short delay occurs while it loads.
The Library Browser opens.
To keep the Library Browser above all other windows on your desktop, in the
Library Browser, select View > Stay on Top.
2-3
2 Simulink® Software Basics
1 From the Simulink Library Browser menu, select File > New > Model.
3 In the Save As dialog box, enter a name for your model, and then click Save.
2-4
Start the Simulink® Software
1 From the Simulink Library Browser menu, select File > Open.
2 In the Open dialog box, select the model file that you want to open, and
then click Open.
Alternatively, in MATLAB you can set the current folder to the folder
containing your model, and then in the MATLAB Command Window, enter
the model name. For more information about the MATLAB current folder, see
“Working with Files and Folders”.
2-5
2 Simulink® Software Basics
• In the Libraries pane on the left, the Sources library is the selected library.
• The Sine Wave block is selected.
2-6
Simulink® User Interface
.
Get summary information Select View > Show Block Descriptions.
about a block Then select the block.
Get detailed information Select the block, and then select Help >
about a block Help for the Selected Block. The Help
browser opens with the reference page for
the block.
View block parameters Right-click a block, and then select Block
parameters.
Get detailed information Select the block, and then select Help >
about a block Help for the Selected Block. The Help
browser opens with the reference page for
the block.
Copy a block from the Library Drag a block from the Library Browser to a
Browser to a model Simulink Editor model window.
2-7
2 Simulink® Software Basics
2-8
Simulink® User Interface
2-9
2 Simulink® Software Basics
Simulink Editor
The Simulink Editor contains a block diagram of your model. You build
models by dragging blocks from the Simulink Library Browser window to
the Simulink Editor model window. In the model window, you build a block
diagram by arranging the blocks logically, connecting the blocks with signal
lines, and setting the parameters for each block.
• 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 a simulation of the model.
• Save the model.
• Print the block diagram.
2-10
3
After you connect the blocks, simulating the model integrates a sine wave
signal and displays the result, along with the original signal.
3-2
Create a Simple Model
You can build this simple model yourself, starting with “Create a New Model”
on page 3-3.
simulink
2 From the Simulink Library Browser menu, select File > New > Model.
3-3
3 Create a Simulink® Model
3 Select File > Save as. The Save As dialog box opens.
4 In the File name box, enter a name for your model. For example, enter
simple_model. Then click Save.
The Simulink Library Browser displays blocks from the Sources library in
the right-hand pane.
3-4
Create a Simple Model
3-5
3 Create a Simulink® Model
3 Add the following blocks to your model, using the same approach that you
used to add the Sine Wave block.
Library Block
Sinks Scope
Continuous Integrator
Signal Routing Mux
3-6
Create a Simple Model
Your model now has the blocks you need for the simple model.
1 Move the Scope block after the Mux block output. To move a block in a
model, use one of these approaches:
2 Move the Sine Wave and Integrator blocks before the Mux block.
3-7
3 Create a Simulink® Model
Most blocks have angle brackets on one or both sides. These angle brackets
represent input and output ports:
3-8
Create a Simple Model
1 Position the mouse pointer over the output port on the right side of the
Sine Wave block.
The pointer changes to a cross hairs (+) shape while over the port.
2 Click and drag a line from the output port to the top input port of the
Mux block.
3-9
3 Create a Simulink® Model
4 Connect the output port of the Integrator block to the bottom input port on
the Mux block:
a Select the Integrator block.
The Integrator block automatically connects to the Mux block with a signal
line.
Note The Ctrl+click shortcut is useful when you are connecting widely
separated blocks, or when working with complex models.
3-10
Create a Simple Model
This final connection is different from the other three connections, which all
connect output ports to input ports. Because the output port of the Sine Wave
block is already connected, you must connect this existing line to the input
port of the Integrator block. The new line, called a branch line, carries the
same signal that passes from the Sine Wave block to the Mux block.
1 Position the mouse pointer on the line between the Sine Wave and the Mux
block. Click the left mouse button.
2 Hold down the Ctrl key and drag the cursor to form a vertical line segment
that aligns with input port of the Integrator block.
3 With the cursor at the end of the dotted red line, move the cursor so that it
lines up with the light blue guide arrow that points in the direction of the
Integrator input port. The cursor turns into a hollow arrow.
Drag the cursor to the Integrator input port and release the mouse button.
3-11
3 Create a Simulink® Model
4 If necessary, smooth out the line segment that connects to the Integrator
block. Select the unaligned line segment and drag it until the line is
straight.
After you finish building a model, you can simulate the dynamic behavior of
the model. See “Simulate the Simple Model” on page 3-12.
1 In the Stop time field, enter 20. In the Max step size field, enter 0.2.
3-12
Create a Simple Model
2 Click OK.
The Simulink software updates the parameter values with your changes
and closes the Configuration Parameters dialog box.
The simulation runs. The simulation stops when it reaches the stop time
specified in the Configuration Parameters dialog box.
The Scope window opens and displays the simulation results. The plot
shows a sine wave signal with the resulting cosine wave signal from the
Integrator block.
3-13
3 Create a Simulink® Model
3 From the Scope block toolbar, click the Parameters button . Select the
Style tab. The Scope Parameters dialog box displays figure editing options.
4 Change the appearance of the figure. For example, select white for the
Figure color and Axes background color (the icon with a pitcher). Change
the signal line colors for signal 1 to blue and for signal 2 to green. To see
your changes, click Apply.
3-14
Create a Simple Model
The Simulink Editor closes with changes to your model and model
configuration parameters.
3-15
3 Create a Simulink® Model
3-16
4
Modeling a Dynamic
Control System
Use this tutorial to explore common Simulink modeling tasks, such as:
4-2
Anatomy of the Control System Model
sldemo_househeat
4-3
4 Modeling a Dynamic Control System
The example model includes many of the same blocks you used to create the
simple model in “Create a Simple Model” on page 3-2. These blocks include:
4-4
Anatomy of the Control System Model
• A Sine Wave block (labeled Daily Temp Variation) at the bottom left —
provides one of three data sources for the model.
The three data inputs (sources) are provided by two Constant blocks (labeled
Set Point and Avg Outdoor Temp), and the Sine Wave block (labeled Daily
Temp Variation). The Scope block labeled PlotResults is the one output
(sink).
4-5
4 Modeling a Dynamic Control System
sldemo_househeat
4-6
Anatomy of the Control System Model
2 Subsystems can be complex and contain many blocks that might otherwise
clutter a diagram. For example, to open the House subsystem, double-click
the House Subsystem block.
4-7
4 Modeling a Dynamic Control System
4-8
Anatomy of the Control System Model
3 A subsystem can also be simple and contain only a few blocks. For example,
to open the Thermostat subsystem:
a Display the sldemo_househeat top model. To do so, in the breadcrumb
4-9
4 Modeling a Dynamic Control System
4-10
Anatomy of the Control System Model
Creating a Subsystem
Subsystems allow you to group related blocks into one block. They are also
reusable, enabling you to implement an algorithm once and use it multiple
times.
To create a subsystem:
2 In the model window, click and drag a bounding box around the Set Point
and Fahrenheit to Celsius blocks.
4-11
4 Modeling a Dynamic Control System
4-12
Anatomy of the Control System Model
To mask a subsystem:
1 In the model window, right-click the new subsystem block (as described in
“Creating a Subsystem” on page 4-11). In the context menu, select Mask >
Create Mask. The Mask Editor dialog box opens.
2 At the bottom of the Mask Editor dialog box, expand the Command list.
Select disp (show text in center of block). The dialog box displays
the syntax for this command below the list.
4 Click OK. The subsystem block displays the text that you entered.
To see the full text in the block icon, select a corner of the block and drag to
expand the size of the block.
4-13
4 Modeling a Dynamic Control System
The software simulates the model dynamics, and as the simulation runs,
it displays the results in a Scope window. The cumulative heating cost
is drawn on the HeatCost graph. The indoor and outdoor temperatures
appear on the Temperatures graph.
4-14
Simulate the Control System Model
2 In the Subsystem model window, double-click the Set Point block. The
Source Block Parameters dialog box opens.
4-15
4 Modeling a Dynamic Control System
4 Click OK. The software applies your changes and closes the dialog box.
5 To rerun the simulation, you can either select Simulation > Run, or from
The software simulates the model dynamics. Notice that a lower thermostat
setting reduces the cumulative heating cost.
4-16
Simulate the Control System Model
4-17
4 Modeling a Dynamic Control System
The software simulates the model dynamics. Notice that a colder outdoor
temperature increases the cumulative heating cost.
4-18
Simulate the Control System Model
The software simulates the model dynamics. Notice that a more stable
outdoor temperature alters the frequency with which the heater operates.
4-19
4 Modeling a Dynamic Control System
4-20
Move Data Between MATLAB® and Simulink® Model
In this example, you create a vector of temperature data in MATLAB and use
that data as an input to the Simulink model.
x = (0:0.01:4*pi)';
y = 32 + (5*sin(x));
z = linspace(0,48,1257)';
2 In the Simulink Editor model window, select the Avg Outdoor Temp block,
and then press the Delete key to remove it from the model.
3 Delete the following items from the model in the same way:
4-21
4 Modeling a Dynamic Control System
The model should now look similar to the following figure. Notice that the
output signal from the Sum1 block changes to a red, dotted line, indicating
that it is not connected to a block.
4 In the model window, select View > Library Browser. The Simulink
Library Browser window opens.
5 In the Library Browser, in the Libraries pane, select the Sources library.
6 From the Sources library in the right pane, select the In1 block, and then
drag it to the Simulink Editor model window.
7 Connect the dotted line (originally connected to the Sum1 block) to the In1
block.
4-22
Move Data Between MATLAB® and Simulink® Model
9 From the menu on the left side of the Configuration Parameters dialog
box, select Data Import/Export.
10 On the Data Import/Export pane, select the Input check box. In the
Input field, enter [z,y].
4-23
4 Modeling a Dynamic Control System
11 Click OK. The software applies your changes and closes the dialog box.
The software simulates the model. Notice that the model ran using the
imported data, showing colder outdoor temperatures and higher heat use.
4-24
Move Data Between MATLAB® and Simulink® Model
To export the HeatCost data from the model to the MATLAB workspace:
2 From the Sinks library, select the Out1 block, and then drag it to the top
right of the Simulink Editor model window.
3 Draw a branch line from the HeatCost signal line to the Out1 block. For
more information, see “Draw a Branch Line” on page 3-11.
4-25
4 Modeling a Dynamic Control System
5 From the menu on the left side of the Configuration Parameters dialog
box, select Data Import/Export.
6 On the Data Import/Export pane, select the Time and Output check
boxes.
4-26
Move Data Between MATLAB® and Simulink® Model
7 Click OK. The software applies your changes and closes the dialog box.
The software simulates the model and saves the time and HeatCost data to
the MATLAB workspace in the variables tout and yout.
4-27
4 Modeling a Dynamic Control System
4-28
5
2 Simulate the model after enabling the logging of output signals during
simulation. See “Simulate Model” on page 5-10.
3 View and compare the output signals in the Simulation Data Inspector.
See “Compare Signals” on page 5-14.
4 Create, print, and save a plot from the Simulation Data Inspector.
See “Create, Save, and Print a Figure” on page 5-16.
5-2
Simulation with Data Import and Signal Inspection
5-3
5 Basic Simulation Workflow
5-4
Simulation with Data Import and Signal Inspection
sldemo_2counters
The example model with a Signal Builder block opens in a Simulink editor
window.
2 Double-click the Signal Builder block. A Signal Builder window opens and
displays one data group with an Amplitude signal.
3 From the Signal Builder menu, select Group > Copy. A second group
named Group 2 appears in the Active Group drop-down list.
4 Select Group > Rename, and enter more meaningful names that describes
the input data. For example, rename Group 1 and Group 2 to Impulse
Input and Ramp Input.
5-5
5 Basic Simulation Workflow
1 From the Signal Builder menu, select File > Import from File.
In the Import File dialog box, click the Browse button, find the
folder matlabroot\help\toolbox\simulink\examples, and select
ex_signaldata.xls.
The Data to Import section of the Import File dialog box displays the
Excel file name with a list of sheets in the file.
3 From the Placement for Selected Data list, select Append selected
signals to different groups (in order). Signal Builder converts
the data on each sheet into a group.
5-6
Simulation with Data Import and Signal Inspection
6 Click OK.
5-7
5 Basic Simulation Workflow
2 Select the signal labeled Amplitude, then press the Delete key. The
original Amplitude signals are removed from both the Impulse Input
and Ramp Input groups.
3 From the menu, select Axes > Set T Display Limits. In the Set time
range dialog box, enter 2.5 for the maximum time.
4 Repeat Step 3 to set the time range for the other input group.
Your signal groups should look like the following. Keep this window open.
5-8
Simulation with Data Import and Signal Inspection
5-9
5 Basic Simulation Workflow
If you don’t see the wave in either group, click Amplitude1 in the lower
right box.
Note After deleting the original Amplitude signal from the Signal Builder,
you may need to reconnect the Input Signal line to the Signal Builder
block in your model.
You can now set up the model to log output signals and simulate. See
“Simulate Model” on page 5-10.
Simulate Model
• “Configuring the Model to Log Output Signals” on page 5-11
5-10
Simulation with Data Import and Signal Inspection
• Time
• Output
• Signal logging
• Record and inspect simulation output
5-11
5 Basic Simulation Workflow
5 Click OK.
5-12
Simulation with Data Import and Signal Inspection
Simulink runs the model simulation from 0.0 s to 2.00 s for the Impulse
Input data and then runs again for the Ramp Input amplitude data.
If you select the Input Signal and the Triggered Counter signal for Run
1, two line plots appear in the right pane of the Simulation Data Inspector
window.
5-13
5 Basic Simulation Workflow
Note If you do not see data in the Simulation Data Inspector window, you
may need to reconnect the Input Signal line to the Signal Builder block in
your model, and run the simulation again.
Now, you can inspect the results further. See “Compare Signals” on page 5-14.
Compare Signals
The Simulation Data Inspector allows you to quickly compare two signals
from the same or different runs.
1 In the Simulation Data Inspector window, click the Compare Signals tab.
By default, five columns are displayed, including Sig 1 and Sig 2.
2 In the Sig 1 column, for Run 1, select the Enable Counter signal. Likewise,
in the Sig 2 column, for Run 2, select the Enable Counter signal.
5-14
Simulation with Data Import and Signal Inspection
3 Inspect the signals. In the top pane, the plot contains the two individual
signals. In the bottom pane, a single plot represents the difference of these
two signals.
5-15
5 Basic Simulation Workflow
To work with your plots as figures, see “Create, Save, and Print a Figure”
on page 5-16.
1 In the Simulation Data Inspector window, select the Plot options button
in the upper right of a signal plot. From the submenu, select Plot data
in new figure.
5-16
Simulation with Data Import and Signal Inspection
5-17
5 Basic Simulation Workflow
1 In the Figures window, select File > Save as. A Save as dialog box opens.
4 From the Save as type menu, select a file format. If you keep the default,
MATLAB Figure (*.fig), then when you reopen the figure, the model
also opens.
5-18
Index
A
Index E
adding blocks to a model 3-4 editor window
overview 2-10
example model 3-2
B
opening 4-3
basics overview 4-4
Simulink 2-1 running 4-14
block libraries examples
descriptions 2-7 accessing 1-10
blocks exporting
connecting 3-8 data from Simulink to MATLAB 4-25
copying 3-4
masking 4-10
moving 3-7 H
ports 3-8 help
subsystems 4-6 Simulink 1-9
browser house model
library 2-6 overview 4-4
C I
closing import data
model 3-15 Excel spreadsheet 5-4
Configuration Parameters dialog box 3-12 Signal Builder block 5-4
connecting importing
blocks 3-8 MATLAB data to Simulink 4-21
lines to input ports 3-8 input data
control system importing from MATLAB 4-21
model overview 4-4 input port 3-8
modeling 4-2
copying blocks to a model 3-4
L
creating
new model 2-4 3-3 library
subsystem mask 4-12 descriptions 2-7
searching 2-7
selecting 2-7
D viewing 2-7
data Library Browser
exporting to MATLAB 4-25 opening 2-2
importing from MATLAB 4-21 overview 2-6
Index-1
Index
Index-2
Index
Index-3