Simulink

Simulink
Simulink® 7
Getting Started Guide
How to Contact The MathWorks
Web
Newsgroup
www.mathworks.com/contact_TS.html Technical Support
www.mathworks.com
comp.soft-sys.matlab
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
Product enhancement suggestions
Bug reports
Documentation error reports
Order status, license renewals, passcodes
Sales, pricing, and general information
508-647-7000 (Phone)
508-647-7001 (Fax)
The MathWorks, Inc.
3 Apple Hill Drive
Natick, MA 01760-2098
For contact information about worldwide offices, see the MathWorks Web site.
Simulink® Getting Started Guide
© COPYRIGHT 1990–2009 by The MathWorks, Inc.
The software described in this document is furnished under a license agreement. The software may be used
or copied only under the terms of the license agreement. No part of this manual may be photocopied or
reproduced in any form without prior written consent from The MathWorks, Inc.
FEDERAL ACQUISITION: This provision applies to all acquisitions of the Program and Documentation
by, for, or through the federal government of the United States. By accepting delivery of the Program
or Documentation, the government hereby agrees that this software or documentation qualifies as
commercial computer software or commercial computer software documentation as such terms are used
or defined in FAR 12.212, DFARS Part 227.72, and DFARS 252.227-7014. Accordingly, the terms and
conditions of this Agreement and only those rights specified in this Agreement, shall pertain to and govern
the use, modification, reproduction, release, performance, display, and disclosure of the Program and
Documentation by the federal government (or other entity acquiring for or through the federal government)
and shall supersede any conflicting contractual terms or conditions. If this License fails to meet the
government’s needs or is inconsistent in any respect with federal procurement law, the government agrees
to return the Program and Documentation, unused, to The MathWorks, Inc.
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
The MathWorks products are protected by one or more U.S. patents. Please see
www.mathworks.com/patents for more information.
Revision History
September 2005
March 2006
September 2006
March 2007
September 2007
March 2008
October 2008
March 2009
September 2009
Online only
Online only
Online only
First printing
Second printing
Third printing
Fourth printing
Fifth printing
Online only
New for Version 6.3 (Release 14SP3)
Revised for Simulink® 6.4 (Release 2006a)
Revised for Simulink® 6.5 (Release 2006b)
Revised for Simulink® 6.6 (Release 2007a)
Revised for Simulink® 7.0 (Release 2007b)
Revised for Simulink® 7.1 (Release 2008a)
Revised for Simulink® 7.2 (Release 2008b)
Revised for Simulink® 7.3 (Release 2009a)
Revised for Simulink® 7.4 (Release 2009b)
Contents
Introduction
1
Product Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Tool for Model-Based Design . . . . . . . . . . . . . . . . . . . . . . . .
Tool for Simulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Tool for Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
How Simulink Software Interacts with the MATLAB
Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1-2
1-2
1-3
1-4
1-4
What Is Model-Based Design? . . . . . . . . . . . . . . . . . . . . . . .
Model-Based Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Modeling Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1-5
1-5
1-6
Related Products . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1-9
1-4
Simulink Software Basics
2
Starting Simulink Software . . . . . . . . . . . . . . . . . . . . . . . .
Opening the Simulink Library Browser . . . . . . . . . . . . . . .
Opening a Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2-2
2-2
2-4
Simulink User Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Simulink Library Browser . . . . . . . . . . . . . . . . . . . . . . . . . .
Simulink Model Window . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2-6
2-6
2-9
Getting Help with Simulink Software . . . . . . . . . . . . . . .
Simulink Online Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Simulink Demo Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Web Site Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2-10
2-10
2-11
2-14
v
Creating a Simulink Model
3
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3-2
Creating a Simple Model . . . . . . . . . . . . . . . . . . . . . . . . . . .
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Creating a New Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Adding Blocks to Your Model . . . . . . . . . . . . . . . . . . . . . . . .
Moving Blocks in the Model Window . . . . . . . . . . . . . . . . . .
Connecting Blocks in the Model Window . . . . . . . . . . . . . . .
Saving the Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3-3
3-3
3-3
3-5
3-8
3-9
3-13
Simulating the Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Setting Simulation Options . . . . . . . . . . . . . . . . . . . . . . . . .
Running the Simulation and Observing Results . . . . . . . . .
3-14
3-14
3-14
3-16
Modeling a Dynamic Control System
4
vi
Contents
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4-2
Understanding the Demo Model . . . . . . . . . . . . . . . . . . . . .
Opening the Demo Model . . . . . . . . . . . . . . . . . . . . . . . . . . .
Anatomy of the Demo Model . . . . . . . . . . . . . . . . . . . . . . . .
Using Subsystems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Masking Subsystems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4-3
4-3
4-5
4-6
4-9
Simulating the Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Running the Simulation . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Modifying Simulation Parameters . . . . . . . . . . . . . . . . . . . .
Importing Data from the MATLAB Workspace . . . . . . . . .
Exporting Simulation Data to the MATLAB Workspace . .
4-11
4-11
4-13
4-20
4-25
Index
vii
viii
Contents
1
Introduction
• “Product Overview” on page 1-2
• “What Is Model-Based Design?” on page 1-5
• “Related Products” on page 1-9
1
Introduction
Product Overview
In this section...
“Overview” on page 1-2
“Tool for Model-Based Design” on page 1-3
“Tool for Simulation” on page 1-4
“Tool for Analysis” on page 1-4
“How Simulink Software Interacts with the MATLAB Environment” on
page 1-4
Overview
Simulink® software models, simulates, and analyzes dynamic systems. It
enables you to pose a question about a system, model the system, and see
what happens.
With Simulink, you can easily build models from scratch, or modify existing
models to meet your needs. Simulink supports linear and nonlinear systems,
modeled in continuous time, sampled time, or a hybrid of the two. Systems
can also be multirate — having different parts that are sampled or updated
at different rates.
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
1-2
Product Overview
Tool for Model-Based Design
With Simulink, you can move beyond idealized linear models to explore more
realistic nonlinear models, factoring in friction, air resistance, gear slippage,
hard stops, and the other things that describe real-world phenomena.
Simulink turns your computer into a laboratory for modeling and analyzing
systems that would not be possible or practical otherwise.
Whether you are interested in the behavior of an automotive clutch system,
the flutter of an airplane wing, or the effect of the monetary supply on the
economy, Simulink provides you with the tools to model and simulate almost
any real-world problem. Simulink also provides demos that model a wide
variety of real-world phenomena (see “Simulink Demo Models” on page 2-11).
Simulink provides a graphical user interface (GUI) for building models as
block diagrams, allowing you to draw models as you would with pencil and
paper. Simulink also includes a comprehensive block library of sinks, sources,
linear and nonlinear components, and connectors. If these blocks do not meet
your needs, however, you can also create your own blocks. The interactive
graphical environment simplifies the modeling process, eliminating the need
to formulate differential and difference equations in a language or program.
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
Tool for Simulation
After you define a model, you can simulate it, using a choice of mathematical
integration methods, either from the Simulink menus or by entering
commands in the MATLAB® Command Window. The menus are convenient
for interactive work, while the command line is useful for running a batch of
simulations (for example, if you are doing Monte Carlo simulations or want to
apply a parameter across a range of values).
Using scopes and other display blocks, you can see the simulation results
while the simulation runs. You can then change many parameters and see
what happens for “what if” exploration. The simulation results can be put in
the MATLAB workspace for postprocessing and visualization.
Tool for Analysis
Model analysis tools include linearization and trimming tools, which can be
accessed from the MATLAB command line, plus the many tools in MATLAB
and its application toolboxes. Because MATLAB and Simulink are integrated,
you can simulate, analyze, and revise your models in either environment
at any point.
How Simulink Software Interacts with the MATLAB
Environment
Simulink software is tightly integrated with the MATLAB environment. It
requires MATLAB to run, depending on it to define and evaluate model and
block parameters. Simulink can also utilize many MATLAB features. For
example, Simulink can use the MATLAB environment to:
• Define model inputs.
• Store model outputs for analysis and visualization.
• Perform functions within a model, through integrated calls to MATLAB
operators and functions.
1-4
What Is Model-Based Design?
What Is Model-Based Design?
In this section...
“Model-Based Design” on page 1-5
“Modeling Process” on page 1-6
Model-Based Design
Model-Based Design is a process that enables faster, more cost-effective
development of dynamic systems, including control systems, signal processing,
and communications systems. In Model-Based Design, a system model is
at the center of the development process, from requirements development,
through design, implementation, and testing. The model is an executable
specification that is continually refined throughout the development process.
After model development, simulation shows whether the model works
correctly.
When software and hardware implementation requirements are included,
such as fixed-point and timing behavior, you can automatically generate code
for embedded deployment and create test benches for system verification,
saving time and avoiding the introduction of manually coded errors.
Model-Based Design allows you to improve efficiency by:
• Using a common design environment across project teams
• Linking designs directly to requirements
• Integrating testing with design to continuously identify and correct errors
• Refining algorithms through multidomain simulation
• Automatically generating embedded software code
• Developing and reusing test suites
• Automatically generating documentation
• Reusing designs to deploy systems across multiple processors and hardware
targets
1-5
1
Introduction
Modeling Process
There are six steps to modeling any system:
1 Defining the System
2 Identifying System Components
3 Modeling the System with Equations
4 Building the Simulink Block Diagram
5 Running the Simulation
6 Validating the Simulation Results
You perform the first three steps of this process outside of the Simulink
software before you begin building your model.
Defining the System
The first step in modeling a dynamic system is to fully define the system. If
you are modeling a large system that can be broken into parts, you should
model each subcomponent on its own. Then, after building each component,
you can integrate them into a complete model of the system.
For example, the demo model used later in this guide models the heating
system of a house. This system can be broken down into three main parts:
• 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.
Identifying System Components
The second step in the modeling process is to identify the system components.
Three types of components define a system:
1-6
What Is Model-Based Design?
• Parameters — System values that remain constant unless you change
them
• States — Variables in the system that change over time
• Signals — Input and output values that change dynamically during the
simulation
In Simulink, parameters and states are represented by blocks, while signals
are represented by the lines that connect blocks.
For each subsystem that you identified, ask yourself the following questions:
• How many input signals does the subsystem have?
• How many output signals does the subsystem have?
• How many states (variables) does the subsystem have?
• What are the parameters (constants) in the subsystem?
• Are there any intermediate (internal) signals in the subsystem?
Once you have answered these questions, you should have a comprehensive
list of the system components, and are ready to begin modeling the system.
Modeling the System with Equations
The third step in modeling a system is to formulate the mathematical
equations that describe the system.
For each subsystem, use the list of system components you identified to
describe the system mathematically. Your model may include:
• Algebraic equations
• Logical equations
• Differential equations, for continuous systems
• Difference equations, for discrete systems
You use these equations to create the block diagram in Simulink.
1-7
1
Introduction
Building the Simulink Block Diagram
After you have defined the mathematical equations that describe each
subsystem, you can begin building a block diagram of your model in Simulink.
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 “Creating a Simple Model” on page 3-3 for more information on building
the block diagram.
Running the Simulation
After you build the Simulink block diagram, you can simulate the model
and analyze the results.
Simulink allows you to interactively define system inputs, simulate the
model, and observe changes in behavior. This allows you to quickly evaluate
your model.
See “Simulating the Model” on page 3-14 for more information on running a
simulation.
Validating the Simulation Results
Finally, you must validate that the model accurately represents the physical
characteristics of the system.
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
Related Products
Related Products
The MathWorks™ provides many additional products that extend the
capabilities of Simulink software. For information about these related
products, see http://www.mathworks.com/products/simulink/.
1-9
1
1-10
Introduction
2
Simulink Software Basics
• “Starting Simulink Software” on page 2-2
• “Simulink User Interface” on page 2-6
• “Getting Help with Simulink Software” on page 2-10
2
Simulink® Software Basics
Starting Simulink Software
In this section...
“Opening the Simulink Library Browser” on page 2-2
“Opening a Model” on page 2-4
Opening the Simulink Library Browser
Your MATLAB environment must be running before you can open Simulink
software. You start Simulink from within MATLAB.
To start Simulink and open the Simulink Library Browser:
1 Start MATLAB. For more information, see “Starting a MATLAB Session”
in the MATLAB Getting Started Guide.
2 Enter simulink in the MATLAB Command Window.
The Simulink Library Browser opens.
2-2
Starting Simulink® Software
Note You can also start Simulink by:
• Clicking the Simulink icon
in the MATLAB toolbar
• Clicking the MATLAB Start button, then selecting Simulink > Library
Browser
2-3
2
Simulink® Software Basics
Opening a Model
You can open existing Simulink models or create new models from the
Simulink Library Browser.
To create a new model:
• Select File > New > Model in the Simulink Library Browser.
The software opens an empty model window.
2-4
Starting Simulink® Software
To open an existing model:
1 Select File > Open in the Simulink Library Browser.
The Open dialog box appears.
2 Select the model (.mdl file) you want to open, then click Open.
The software opens the selected model in the model window.
2-5
2
Simulink® Software Basics
Simulink User Interface
In this section...
“Simulink Library Browser” on page 2-6
“Simulink Model Window” on page 2-9
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.
Block
Search
Selected
Library
Block
Description
Simulink® Library Browser
2-6
Selected
Block
Simulink® User Interface
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
.
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.
Discontinuities
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.
2-7
2
2-8
Simulink® Software Basics
Block Library
Description
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 Out1 and 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.
Simulink® User Interface
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.
2-9
2
Simulink® Software Basics
Getting Help with Simulink Software
In this section...
“Simulink Online Help” on page 2-10
“Simulink Demo Models” on page 2-11
“Web Site Resources” on page 2-14
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.
Note To quickly access the help page for a specific block, right-click the
block and select Help.
2-10
Getting Help with Simulink® Software
Simulink Demo Models
Simulink software provides a variety of demo models that illustrate key
modeling concepts and Simulink features. You can access these demos from
the MATLAB Command Window.
To access Simulink demos:
1 On the bottom left corner of the MATLAB Command Window, click the
Start button.
The Start menu appears.
2 Select Simulink > Demos from the Start menu.
2-11
2
2-12
Simulink® Software Basics
Getting Help with Simulink® Software
3 From the Simulink demos page in the MATLAB Help browser, click the
demo model you want to open.
2-13
2
Simulink® Software Basics
Web Site Resources
You can access additional Simulink resources on the MathWorks™ Web
site, including Simulink related books, prerecorded webinars, and technical
support.
To access the Simulink product page, go to:
http://www.mathworks.com/products/simulink
2-14
3
Creating a Simulink Model
• “Overview” on page 3-2
• “Creating a Simple Model” on page 3-3
• “Simulating the Model” on page 3-14
3
Creating a Simulink® Model
Overview
This chapter describes how to create a simple model using Simulink software,
and how to simulate that model. The basic techniques you use to create and
simulate this simple model are the same as those for more complex models.
The model described in this chapter integrates a sine wave and displays the
result along with the original wave. When completed, the block diagram of
the model should look similar to this:
The instructions for constructing and simulating the example model are brief.
However, each task is described in more detail in Simulink User’s Guide.
3-2
Creating a Simple Model
Creating a Simple Model
In this section...
“Overview” on page 3-3
“Creating a New Model” on page 3-3
“Adding Blocks to Your Model” on page 3-5
“Moving Blocks in the Model Window” on page 3-8
“Connecting Blocks in the Model Window” on page 3-9
“Saving the Model” on page 3-13
Overview
This section describes how to model a simple dynamic system using Simulink
software. After you create the model, you can simulate it as described in
“Simulating the Model” on page 3-14.
Creating a New Model
Before you can begin building your model, you must start Simulink and
create an empty model.
To create a new model:
1 If Simulink is not running, enter simulink in the MATLAB Command
Window to open the Simulink Library Browser.
2 Select File > New > Model in the Simulink Library Browser to create a
new model.
The software opens an empty model window.
3-3
3
3-4
Creating a Simulink® Model
Creating a Simple Model
Adding Blocks to Your Model
To construct a model, you first copy blocks from the Simulink Library Browser
to the model window. To create the simple model in this chapter, you need
four blocks:
• Sine Wave — To generate an input signal for the model
• Integrator — To process the input signal
• Scope — To visualize the signals in the model
• Mux — To multiplex the input signal and processed signal into a single
scope
To add blocks to your model:
1 Select the Sources library in the Simulink Library Browser.
The Simulink Library Browser displays the Sources library.
3-5
3
Creating a Simulink® Model
2 Select the Sine Wave block in the Simulink Library Browser, then drag it
to the model window.
A copy of the Sine Wave block appears in the model window.
3-6
Creating a Simple Model
3 Select the Sinks library in the Simulink Library Browser.
4 Select the Scope block from the Sinks library, then drag it to the model
window.
A Scope block appears in the model window.
5 Select the Continuous library in the Simulink Library Browser.
6 Select the Integrator block from the Continuous library, then drag it to
the model window.
An Integrator block appears in the model window.
7 Select the Signal Routing library in the Simulink Library Browser.
8 Select the Mux block from the Sinks library, then drag it to the model
window.
A Mux block appears in the model window.
3-7
3
Creating a Simulink® Model
Moving Blocks in the Model Window
Before you connect the blocks in your model, you should arrange them
logically to make the signal connections as straightforward as possible.
To move a block in the model window, you can either:
• Drag the block.
• Select the block, then press the arrow keys on the keyboard.
Arrange the blocks in the model to look like the following figure.
3-8
Creating a Simple Model
Connecting Blocks in the Model Window
After you add blocks to the model window, you must connect them to represent
the signal connections within the model.
Notice that each block has angle brackets on one or both sides. These angle
brackets represent input and output ports:
• The > symbol pointing into a block is an input port.
• The > symbol pointing out of a block is an output port.
Input port
Output port
The following sections describe how to connect blocks by drawing lines from
output ports to input ports:
• “Drawing Lines Between Blocks” on page 3-9
• “Drawing a Branch Line” on page 3-11
Drawing Lines Between Blocks
You connect the blocks in your model by drawing lines between output ports
and input ports.
To draw a line between two blocks:
1 Position the mouse pointer over the output port on the right side of the
Sine Wave block.
Note that the pointer changes to a cross hairs (+) shape while over the port.
3-9
3
Creating a Simulink® Model
2 Drag a line from the output port to the top input port of the Mux block.
Note that the line is dashed while you hold the mouse button down, and
that the pointer changes to a double-lined cross hairs as it approaches the
input port of the Mux block.
3 Release the mouse button over the output port.
The software connects the blocks with an arrow that indicates the direction
of signal flow.
4 Drag a line from the output port of the Integrator block to the bottom input
port on the Mux block.
The software connects the blocks.
3-10
Creating a Simple Model
5 Select the Mux block, then Ctrl+click the Scope block.
The software automatically draws the connection line between the blocks.
Note The Ctrl+click shortcut is especially useful when you are connecting
widely separated blocks, or when working with complex models.
The model should now look similar to the following figure.
Drawing a Branch Line
The model is almost complete, but one connection is missing. To finish the
model, you must connect the Sine Wave block to the Integrator block.
This final connection is somewhat different from the other three, which all
connect output ports to input ports. Because the output port of the Sine Wave
block already has a connection, 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.
To weld a connection to an existing line:
1 Position the mouse pointer on the line between the Sine Wave and the
Mux block.
3-11
3
Creating a Simulink® Model
2 Press and hold the Ctrl key, then drag a line to the Integrator block’s
input port.
The software draws a line between the starting point and the input port of
the Integrator block.
The model is now complete. It should look similar to the following figure.
3-12
Creating a Simple Model
Saving the Model
After you complete the model, you should save it for future use.
To save the model:
1 Select File > Save in the model window.
2 Specify the location in which you want to save the model.
3 Enter simple_model in the File name field.
4 Click Save.
The software saves the model with the file name simple_model.mdl.
3-13
3
Creating a Simulink® Model
Simulating the Model
In this section...
“Overview” on page 3-14
“Setting Simulation Options” on page 3-14
“Running the Simulation and Observing Results” on page 3-16
Overview
After you complete the model block diagram, you can simulate the system and
visualize the results. This section describes how to simulate the sample model
you created in the previous section, “Creating a Simple Model” on page 3-3.
Setting Simulation Options
Before simulating a model, you can set simulation options such as the start
and stop time, and the type of solver that Simulink software uses to solve the
model at each time step. You specify these options using the Configuration
Parameters dialog box.
To specify simulation options for the sample model:
1 Select Simulation > Configuration Parameters in the model window.
The software displays the Configuration Parameters dialog box.
3-14
Simulating the Model
2 Enter 20 in the Stop time field.
3 Click OK.
The software applies your changes to the parameters and closes the
Configuration Parameters dialog box.
Note For more information on Simulink configuration parameters, see
“Configuration Parameters Dialog Box” in the Simulink online documentation.
3-15
3
Creating a Simulink® Model
Running the Simulation and Observing Results
Now you are ready to simulate your example model and observe the
simulation results.
To run the simulation:
1 Select Simulation > Start in the model window.
The software runs the model, stopping when it reaches the stop time
specified in the Configuration Parameters dialog box.
Tip On computers running the Microsoft® Windows® operating system, you
can click the Start simulation button
and Stop simulation button
in the model window toolbar to start and stop a simulation.
2 Double-click the Scope block in the model window.
The Scope window displays the simulation results.
3-16
Simulating the Model
3 Select File > Save in the model window.
The software saves the model.
4 Select File > Close in the model window.
The software closes the model.
3-17
3
3-18
Creating a Simulink® Model
4
Modeling a Dynamic
Control System
• “Overview” on page 4-2
• “Understanding the Demo Model” on page 4-3
• “Simulating the Model” on page 4-11
4
Modeling a Dynamic Control System
Overview
This chapter illustrates how Simulink software can model a dynamic control
system, using an example that simulates the thermodynamics of a house. The
system models the outdoor environment, the thermal characteristics of the
house, and the house heating system.
This chapter allows you to explore common Simulink modeling tasks,
including:
• Grouping multiple blocks in a model into a single subsystem, simplifying
the block diagram
• Customizing the appearance of blocks using the masking feature
• Simulating a model and observing the results using a Scope block
• Changing the input parameters of the model to investigate how the system
responds
• Importing data from the MATLAB workspace into a model before
simulation
• Exporting simulation data from the model back to the MATLAB workspace
4-2
Understanding the Demo Model
Understanding the Demo Model
In this section...
“Opening the Demo Model” on page 4-3
“Anatomy of the Demo Model” on page 4-5
“Using Subsystems” on page 4-6
“Masking Subsystems” on page 4-9
Opening the Demo Model
The demo model described in this chapter is called sldemo_househeat. You
can open it from the MATLAB Command Window.
To open the demo model:
1 Ensure that MATLAB is open.
2 Enter sldemo_househeat in the MATLAB Command Window.
The software starts and opens the sldemo_househeat model.
4-3
4
4-4
Modeling a Dynamic Control System
Understanding the Demo Model
Anatomy of the Demo Model
The demo system models the outdoor environment, the thermal characteristics
of the house, and the house heating system. It allows you to simulate how the
thermostat setting and outdoor environment affect the indoor temperature
and cumulative heating costs.
The demo model includes many of the same blocks you used to create the
simple model in Chapter 3, “Creating a Simulink Model”. These include:
• A Scope block (labeled PlotResults) on the far right displays the
simulation results.
• A Mux block at the bottom right combines the indoor and outdoor
temperature signals for the Scope.
• A Sine Wave block (labeled Daily Temp Variation) at the bottom left
provides one of three data sources for the model.
In the demo, the thermostat is set to 70 degrees Fahrenheit. The system
models fluctuations in outdoor temperature by applying a sine wave with
amplitude of 15 degrees to a base temperature of 50 degrees.
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
Using Subsystems
The sldemo_househeat demo model uses subsystems to simplify the
appearance of the block diagram, create reusable components, and customize
the appearance of blocks. A subsystem is a hierarchical grouping of blocks
encapsulated by a single Subsystem block.
The demo model uses the following subsystems:
• Thermostat
• Heater
• House
• Fahrenheit to Celsius
• Celsius to Fahrenheit
4-6
Understanding the Demo Model
Subsystems can be complex and contain many blocks that might otherwise
clutter a diagram. For example, double-click the House subsystem to open it.
Contents of House subsystem
You can see that the subsystem receives heat flow and external temperature
as inputs, which it uses to compute the current room temperature. You could
leave each of these blocks in the main model window, but combining them as
a subsystem helps simplify the block diagram.
4-7
4
Modeling a Dynamic Control System
Subsystems can also be simple and contain only a few blocks. For example,
double-click the Thermostat subsystem to open it.
Contents of Thermostat subsystem
This subsystem models the operation of a thermostat, determining when the
heating system is on or off. It contains only one Relay block, but logically
represents the thermostat in the block diagram.
Subsystems are also reusable, enabling you to implement an algorithm once
and use it multiple times. For example, the model contains two instances of
identical subsystems named Fahrenheit to Celsius. These subsystems
convert the inside and outside temperatures from degrees Fahrenheit to
degrees Celsius.
Creating a Subsystem
Creating a subsystem allows you to group multiple related blocks into one
subsystem block.
To create a subsystem:
1 Select the Thermostat block in the demo model.
2 Shift-click the Heater block in the demo model to select it.
3 Select Edit > Create Subsystem in the model window.
4-8
Understanding the Demo Model
The software creates a Subsystem block containing the Thermostat and
Heater blocks.
4 Select Edit > Undo Create Subsystem to return the model to its original
configuration.
For more information about working with subsystems, see “Creating
Subsystems” in the Simulink User’s Guide.
Masking Subsystems
You can customize the appearance of a subsystem by using a process known
as masking. Masking a subsystem allows you to specify a unique icon and
dialog box for the Subsystem block. For example, the House and Thermostat
subsystems display custom icons that depict physical objects, while the
conversion subsystems display custom dialog boxes when you double-click
them.
To view the underlying blocks in the conversion subsystem, right-click the
subsystem block, then select Look Under Mask.
4-9
4
Modeling a Dynamic Control System
Contents of Fahrenheit to Celsius subsystem
Creating a Subsystem Mask
To mask a subsystem:
1 Select the Heater block in the demo model.
2 Select Edit > Mask Subsystem in the model window.
The Mask Editor dialog box appears.
3 Select disp (show text in center of block) in the Command
drop-down menu.
4 Enter disp('HEATER') in the Drawing commands field.
5 Click OK.
The software masks the subsystem block with the text you entered.
For more information about masking subsystems, see “Working with Block
Masks” in the Simulink User’s Guide.
4-10
Simulating the Model
Simulating the Model
In this section...
“Running the Simulation” on page 4-11
“Modifying Simulation Parameters” on page 4-13
“Importing Data from the MATLAB Workspace” on page 4-20
“Exporting Simulation Data to the MATLAB Workspace” on page 4-25
Running the Simulation
Simulating the model allows you to observe how the thermostat setting and
outdoor environment affect the indoor temperature and the cumulative
heating cost.
To run the simulation:
1 In the demo model window, double-click the Scope block named
PlotResults.
The software opens a Scope window that contains two axes with the labels
“HeatCost” and “Temperatures.”
2 Select Simulation > Start in the model window.
The software simulates the model. As the simulation runs, the
cumulative heating cost appears on the “HeatCost” graph at the top
of the Scope window. The indoor and outdoor temperatures appear on
the “Temperatures” graph as yellow (top) and magenta (bottom) signals,
respectively.
4-11
4
4-12
Modeling a Dynamic Control System
Simulating the Model
Modifying Simulation Parameters
One of the most powerful benefits of modeling a system with Simulink is the
ability to interactively define the system inputs and observe changes in the
behavior of your model. This allows you to quickly evaluate your model and
validate the simulation results. This section describes:
• “Changing the Thermostat Setting” on page 4-13
• “Changing the Average Outdoor Temperature” on page 4-16
• “Changing the Daily Temperature Variation” on page 4-18
Changing the Thermostat Setting
Change the thermostat setting to 68 degrees Fahrenheit and observe how
the model responds.
To change the thermostat setting:
1 Double-click the Set Point block in the model window.
4-13
4
Modeling a Dynamic Control System
The Source Block Parameters dialog box appears.
2 Enter 68 in the Constant value field.
3 Click OK.
The software applies your changes.
4 Select Simulation > Start to rerun the simulation.
The software simulates the model.
4-14
Simulating the Model
Notice that a lower thermostat setting reduces the cumulative heating cost.
4-15
4
Modeling a Dynamic Control System
Changing the Average Outdoor Temperature
Change the average outdoor temperature to 45 degrees Fahrenheit and
observe how the model responds.
To change the average outdoor temperature:
1 Double-click the Avg Outdoor Temp block.
The Source Block Parameters dialog box appears.
2 Enter 45 in the Constant value field.
3 Click OK.
The software applies your changes and closes the dialog box.
4 Select Simulation > Start to rerun the simulation.
4-16
Simulating the Model
The software simulates the model.
Notice that a colder outdoor temperature increases the cumulative heating
cost.
4-17
4
Modeling a Dynamic Control System
Changing the Daily Temperature Variation
Decrease the temperature variation to see how the model responds.
1 Double-click the Daily Temp Variation block.
The Source Block Parameters dialog box appears.
2 Enter 5 in the Amplitude field.
4-18
Simulating the Model
3 Click OK.
The software applies your changes and closes the dialog box.
4 Select Simulation > Start to rerun the simulation.
The software simulates the model.
In the Scope window, notice that a more stable outdoor temperature alters
the frequency with which the heater operates.
4-19
4
Modeling a Dynamic Control System
Importing Data from the MATLAB Workspace
Simulink also allows you to import data from the MATLAB workspace to the
model’s input ports. This allows you to import actual physical data into your
model. (For information about other data import capabilities, see “Importing
Data from a Workspace” in the Simulink User’s Guide.)
Note In this example, you will create a vector of temperature data in
MATLAB, and use that data as an input to the Simulink model.
To import data from the MATLAB workspace:
1 In the MATLAB Command Window, create time and temperature data by
entering the following commands:
x = (0:0.01:4*pi)';
y = 32 + (5*sin(x));
z = linspace(0,48,1257)';
2 In the Simulink model window, select the Avg Outdoor Temp block, then
press the Delete key to delete it.
3 Delete the following items from the model in the same way:
• Daily Temp Variation block
• Two input signal lines to the Sum block
• Sum block
The model should now look similar to the following figure. Notice that the
output signal from the Sum block changes to a red, dotted line, indicating
that it is not connected to a block.
4-20
Simulating the Model
4 If the Simulink Library Browser is not open, select View > Library
Browser in the model window to open it.
5 Select the Sources library in the Simulink Library Browser.
6 Select the In1 block in the Simulink Library Browser, then drag it to the
model window.
An In1 block appears in the model window.
7 Connect the dotted line (originally connected to the Sum block) to the In1
block.
4-21
4
Modeling a Dynamic Control System
8 Select Simulation > Configuration Parameters in the model window.
The Configuration Parameters dialog box appears.
9 Select Data Import/Export in the menu on the left side of the dialog box.
4-22
Simulating the Model
The Data Import/Export pane appears.
10 Select the Input check box in the Load from workspace section.
11 Enter [z,y] in the Input field.
12 Click OK.
4-23
4
Modeling a Dynamic Control System
The software applies your changes and closes the dialog box.
13 Select Simulation > Start to rerun the simulation.
The software simulates the model.
In the Scope window, notice that the model ran using the imported data,
showing colder temperatures and higher heat use.
4-24
Simulating the Model
Exporting Simulation Data to the MATLAB Workspace
Once you have completed a model, you may want to export your simulation
results to MATLAB for further data analysis or visualization. (See “Exporting
Data to the MATLAB Workspace” for information about additional data
export capabilities.)
To export the HeatCost data from the model to the MATLAB workspace:
1 Select the Sinks library in the Simulink Library Browser.
2 Select the Out1 block in the Simulink Library Browser, then drag it to
the top right of the model window.
An Out1 block appears in the model window.
3 Draw a branch line from the HeatCost signal line to the Out1 block. For
more information, see “Drawing a Branch Line” on page 3-11.
4-25
4
Modeling a Dynamic Control System
4 Select Simulation > Configuration Parameters in the model window.
The Configuration Parameters dialog box appears.
5 Select Data Import/Export in the menu on the left side of the dialog box.
The Data Import/Export pane appears.
4-26
Simulating the Model
6 Select the Time check box in the Save to workspace section.
7 Select the Output check box in the Save to workspace section.
8 Click OK.
The software applies your changes and closes the dialog box.
9 Select Simulation > Start to rerun the simulation.
The software simulates the model and saves the time and HeatCost data
to the MATLAB workspace.
Notice that the tout and yout variables now appear in the MATLAB
workspace.
4-27
4
4-28
Modeling a Dynamic Control System
Index
A
Index
adding blocks to a model 3-5
B
basics
Simulink 2-1
block libraries
descriptions 2-7
blocks
connecting 3-9
copying 3-5
masking 4-9
moving 3-8
ports 3-9
subsystems 4-6
browser
library 2-6
importing from MATLAB 4-20
demo model
opening 4-3
overview 4-5
running 4-11
demos
accessing 2-11
E
example model 3-2
exporting
data from Simulink to MATLAB 4-25
H
help
Simulink 2-10
house model
overview 4-5
C
closing
model 3-17
Configuration Parameters dialog box 3-14
connecting
blocks 3-9
lines to input ports 3-9
control system
model overview 4-5
modeling 4-2
copying blocks to a model 3-5
creating
new model 2-4 3-3
subsystem 4-8
subsystem mask 4-10
D
data
exporting to MATLAB 4-25
I
importing
MATLAB data to Simulink 4-20
input data
importing from MATLAB 4-20
input port 3-9
L
library
descriptions 2-7
searching 2-7
selecting 2-7
viewing 2-7
Library Browser
opening 2-2
overview 2-6
using 3-5
lines
Index-1
Index
branching a signal 3-11
carrying the same signal 3-9
connecting to input ports 3-9
M
masking
creating a mask 4-10
overview 4-9
MATLAB
exporting data from Simulink 4-25
importing data to Simulink 4-20
Simulink interaction 1-4
model window
moving blocks 3-8
overview 2-9
Model-Based Design
overview 1-5
process 1-6
modeling
control system 4-2
process 1-6
models
closing 3-17
control system example 4-5
creating new 2-4
creating simple 3-3
empty 3-3
opening 2-4
opening existing 2-5
saving 3-13
simple example 3-2
simulating 3-14
modifying
simulation parameters 4-13
moving
blocks in the model window 3-8
Index-2
O
online help
accessing 2-10
Simulink 2-10
opening
demo model 4-3
existing model 2-5
model 2-4
Simulink 2-2
output data
exporting to MATLAB 4-25
output port 3-9
overview
demo model 4-5
Simulink 1-2
P
parameters
modifying 4-13
product overview 1-2
products
related 1-9
R
related products 1-9
running
demo model 4-11
simulation 3-14
Simulink 2-2
S
sample
model 3-2
saving
models 3-13
simulating models
exercise 3-14
Index
simulation
modifying parameters 4-13
running 4-11
specifying options 3-14
starting 3-16
stopping 3-16
Simulink
basics 2-1
demos 2-11
getting help 2-10
Library Browser 2-6
MATLAB interaction 1-4
model window 2-9
online help 2-10
opening 2-2
overview 1-2
related products 1-9
starting 2-2
user interface 2-6
web resources 2-14
starting
MATLAB 2-2
simulation 3-16
Simulink 2-2
stopping
simulation 3-16
subsystems
creating 4-8
masking 4-9
overview 4-6
T
thermo model
overview 4-5
U
user interface
Simulink 2-6
W
web resources
Simulink 2-14
Index-3
Was this manual useful for you? yes no
Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Download PDF

advertisement