You are on page 1of 9

Mastering Simulink

James B. Dabney
and
Thomas L. Harman
University of Houston—Clear Lake

PEARSON
1'1111We
I Iall

Upper Saddle River, New Jersey 07458


Contents

Preface xvii
Using the Book xvii
New Users xvü
Experienced Users xviii
Advanced Users xviii
Acknowledgments xix
1 Introduction 1
1.1 What Is Simulink? 1
1.2 Simulink Examples 2
1.2.1 Steel Rolling Plant Modeling 2
1.2.2 Automotive Engine Modeling 3
1.3 Simulink Employment Strategies 4
1.3.1 Rapid Prototyping 4
1.3.2 Rapid Application Development 5
1.4 Selection Considerations 5
1.5 Outline of Text 6
References 6
2 Block Diagrams 7
2.1 Introduction 7
2.2 Continuous Systems 8
2.2.1 Gain Blocks 8
2.2.2 Sum Block 9
2.2.3 Derivative Block 9
2.2.4 Integrator 10
2.2.5 Simple Physical Models 11
2.2.6 Transfer Function Block 11
2.3 State-Space Block 13
2.3.1 The State-Space Concept ................. 13
2.3.2 Linear Single-Input, Single-Output Systems 15
2.3.3 Multiple Input, Multiple Output Systems 18
2.4 Discrete Systems 20
2.4.1 Discrete Gain Block 22
2.4.2 Discrete Sum Block 22
x Contents
2.4.3 Unit Delay 22
2.4.4 Discrete-Time Integrator Block 23
2.4.5 Simple Discrete System Models 24
2.4.6 Discrete Transfer Function Block 25
2.5 Discrete State-Space Block 26
2.6 Nonlinear Blocks 28
2.7 Hybrid Block Diagrams 28
2.8 Summary 30
References 30

3 Quick Start 32
3.1 Introduction 32
3.1.1 Typographical Conventions 32
3.2 Building a Simple Model 33
3.3 A More Complicated Model 38
3.4 The Simulink Help System 46
3.4.1 Opening the User's Guide Browser 46
3.4.2 User's Guide Browser Window . . . . . 47
3.5 Summary 48
References 48

4 Model Building 49
4.1 Introduction 49
4.1.1 Elements of a Model 49
4.2 Opening a Model 50
4.3 Model Window 50
4.3.1 Zooming 52
4.3.2 Block Data Tips 52
4.3.3 Simulink Block Library 52
4.4 Manipulating Blocks 53
4.4.1 Resizing a Block 53
4.4.2 Rotating a Block 54
4.4.3 Copying a Block within a Model 54
4.4.4 Deleting Blocks 55
4.4.5 Selecting Multiple Blocks 55
4.4.6 Changing a Block Label 56
4.4.7 Changing Label Location 57
4.4.8 Hiding a Label 57
.4.4.9 Adding a Drop Shadow 57
4.4.10 Using Color 58
4.4.11 Configuring Blocks 58
4.5 Signal Lines 58
4.5.1 Moving a Segment 59
4.5.2 Moving a Vertex 61
4.5.3 Deleting a Signal Line 61
4.5.4 Splitting a Signal Line 62
4.5.5 Labeling a Signal Line 62
Contents xi

4.5.6 Moving or Copying a Signal Line Label 63


4.5.7 Editing a Signal Line Label 64
4.5.8 Signal Label Propagation 64
4.6 Annotations ................. . 65
4.6.1 Adding Annotations 65
4.6.2 Annotation Formatting 66
4.7 Adding Sources 66
4.7.1 Common Sources 66
4.7.2 From Workspace Block 68
4.7.3 From File Input Block 70
4.8 Adding Sinks 70
4.8.1 Scope Block 70
4.8.2 XY Graph 75
4.9 Data Types 75
4.10 Configuring the Simulation 77
4.10.1 Solver Page 78
4.10.2 Workspace 1/0 Page 80
4.10.3 Diagnostics Page 82
4.10.4 Advanced Page 83
4.11 Running a Simulation 84
4.12 Printing a Model 85
4.12.1 Printing to the Printer Using Menus 85
4.12.2 Embedding the Model in a Document 86
4.12.3 Using the MATLAB Print Command 86
4.13 Model Building Summary 88
4.14 Summary 89
References 89

5 Continuous Systems 90
5.1 Introduction 90
5.2 Scalar Linear Systems 90
5.2.1 Integrator Block 90
5.2.2 Transfer Function Blocks 94
5.3 Vector Linear Systems 96
5.3.1 Vector Signal Lines 96
5.3.2 State-Space Block 98
5.4 Modeling Nonlinear Systems 98
5.4.1 Function Blocks 102
5.5 Summary 105
References 106

6 Discrete-Time Systems 107


6.1 Introduction 107
6.1.1 Scalar Linear Discrete-Time Systems 107
6.1.2 Unit Delay 108
6.1.3 Discrete-Time Integrator 108
6.1.4 Discrete Transfer Function Blocks 110
xii Contents
6.2 Logical Blocks 112
6.3 Vector Discrete-Time Systems 114
6.4 Multirate Discrete-Time Systems 114
6.5 Hybrid Systems 116
6.6 Summary 118
References 119

7 Subsystems and Masking 120


7.1 Introduction 120
7.2 Simulink Subsystems 120
7.2.1 Encapsulating a Subsystem ..... • • . . 122
7.2.2 Subsystem Blocks 124
7.3 Masked Blocks 126
7.3.1 Converting a Subsystem into a Masked Subsystem 128
7.3.2 Mask Editor Documentation Page 128
7.3.3 Parameters Page 129
7.3.4 Initialization Page 134
7.3.5 Mask Editor Icon Page 137
7.3.6 Looking Under and Removing Masks 145
7.3.7 Using Masked Blocks 145
7.3.8 Creating a Block Library 146
7.3.9 Configurable Subsystems 148
7.4 Conditionally Executed Subsystems 151
7.4.1 Enabled Subsystems 151
7.4.2 Triggered Subsystems 155
7.4.3 Trigger-When-Enabled Subsystems 156
7.4.4 Discrete Conditionally Executed Subsystems . 156
7.5 Logical Subsystems 156
7.6 Iterative Subsystems 157
7.6.1 For Iterator Subsystem 157
7.6.2 While Iterator Subsystem 158
7.7 Summary 161
References 161
8 Simulink Analysis Tools 162
8.1 Introduction 162
8.2 Determining Model Characteristics 162
8.2.1 Simulink State Vector Definition 162
8.2.2 Using the model Command 163
8.3 Executing Models from MATLAB . . . .. 167
8.3.1 Using sim to Run a Simulation 167
8.4 Setting Simulation Parameters with simset 169
8.4.1 Obtaining Simulation Parameters with simget 172
8.5 Linearization Tools 176
8.5.1 Linearization 176
8.5.2 Simulink Linearization Commands 177
8.6 Trim Tools 186
Contents xiii
8.7 Optimization Toolbox and Simulink 192
8.8 Other Toolboxes Useful with Simulink 195
8.9 Summary 195
References 195
9 Callbacks 197
9.1 Introduction 197
9.2 Callback Function Overview 197
9.2.1 Callback Installation Dialog Boxes 197
9.2.2 Installing Callbacks Using set_param 198
9.3 Model Construction Commands 201
9.3.1 Finding the Name of the Current Block 201
9.3.2 Finding the Name of the Current Model 202
9.3.3 Finding the Name of the Current System 202
9.3.4 Setting Parameter Values 203
9.3.5 Reading Parameter Values 203
9.4 Graphical User Interfaces with Callbacks 204
9.4.1 Graphic User Interface Callback 204
9.4.2 Programming Issues 207
9.5 Callback-Based Animation 215
9.6 Summary 218
References 218

10 S-Functions 219
10.1 Introduction 219
10.2 S-Function Block 219
10.3 S-Function Overview 220
10.4 M-file S-Functions 221
10.4.1 Initialization (f lag = 0) 222
10.4.2 Continuous State Derivatives (f lag = 1) 224
10.4.3 Discrete State Updates (f lag = 2) 224
10.4.4 Block Outputs (f lag = 3) 225
10.4.5 Next Sample Time (f lag = 4) . . .... 226
10.4.6 Terminate (f lag = 9) 226
10.4.7 Programming Considerations 226
10.4.8 M-file S-Function Examples 227
10.5 C Language S-Functions 235
10.5.1 C File Header 239
10.5.2 Initializing the Simulation Structure 240
10.5.3 Defining Sample Times and Offsets 242
10.5.4 Accessing Model Variables . . . . . . . ...... . . 243
10.5.5 Setting Initial Conditions 244
10.5.6 Setting the Output Vector 244
10.5.7 Updating the Discrete States 245
10.5.8 Computing the State Derivatives 245
10.5.9 End-of-Simulation Tasks 246
10.5.10 C S-Function File Trailer 246
xiv Contents
10.5.11 Programming Considerations 246
10.5.12 Compiling a C S-Function 250
10.5.13 C S-Function Examples 250
10.6 Summary 258
References 258

11 Graphical Animations 259


11.1 Introduction 259
11.2 S-Function Animations 259
11.2.1 Animation S-Function Initialization 260
11.2.2 Animation S-Function Updates 261
11.3 Animation Toolbox 265
11.3.1 Obtaining the Animation Toolbox 265
11.3.2 Using the Animation Toolbox 265
11.3.3 Animation Object Properties 268
11.3.4 Configuring an Animation 268
11.3.5 Modifying an Animation 268
11.3.6 Setting Initial Inputs 269
11.3.7 Saving and Loading Animation Files 269
11.4 Dials & Gauges Blockset 269
11.5 Virtual. Reality Toolbox 271
11.6 Summary 272
References 272

12 Debugging 273
12.1 Introduction 273
12.2 Finding Model-Building Errors 273
12.3 Finding Implementation Problems 275
12.3.1 Simulink Execution Errors 275
12.3.2 Slow Execution 275
12.3.3 Incorrect Results 277
12.4 Simulink Debugger 277
12.4.1 Running the Debugger 278
12.4.2 Block Index 279
12.4.3 Single-Step Execution 280
12.4.4 Breakpoints 281
12.4.5 Inspecting Block Data 283
12.4.6 Algebraic Loop Commands 283
12.4.7 Debugger Status Commands 284
12.4.8 Ending Debugging 284
12.4.9 Debugger Command Summary 284
12.5 Summary 286

13 Numerical Issues 287


13.1 Introduction 287
13.2 Choosing a Solver 287
Contents xv
13.3 Algebraic Loops 293
13.3.1 Newton—Raphson Method 294
13.3.2 Eliminating Algebraic Loops 295
13.4 Multiple Sample Time Issues 298
13.5 Zero-Crossing Detection 299
13.6 Summary 299
References 299

14 Introduction to Stateflow 301


14.1 Introduction 301
14.2 Finite State Machines 301
14.3 State Transition Diagrams 302
14.4 Stateflow Charts 303
14.4.1 Stateflow Chart Hierarchy 305
14.4.2 Compound Transitions 307
14.4.3 Inner Transitions 308
14.5 Stateflow Quick Start 308
14.6 Drawing Stateflow Charts 314
14.6.1 State Boxes 314
14.6.2 Transitions 315
14.6.3 Connective Junctions 316
14.6.4 Default Transitions 316
14.6.5 History Junctions 316
14.6.6 Specifying Parallel Decomposition 316
14.7 Labeling States and Transitions 317
14.7.1 State Labels 317
14.7.2 Transition Labels 318
14.8 Stateflow Explorer 318
14.9 Interfacing with Simulink 318
14.9.1 Inputs 319
14.9.2 Outputs 319
14.10 Chart Update Options 320
14.11 Flowcharts 322
14.12 Graphical Functions 326
14.13 Printing Stateflow Charts 327
14.14 Stateflow Debugger 327
14.15 Summary 328
References 329

15 Introduction to Real-Time Workshop 330


15.1 Introduction 330
15.1.1 Development Scenarios 330
15.1.2 Build Process 331
15.2 Generic Real-Time Target 332
15.2.1 Stand-Alone GRT Target 332
15.2.2 Running the Stand-Alone GRT Model 333
15.2.3 Using External Mode 335
xvi Contents

15.3 Rapid Simulation Target 336


15.3.1 Basketball Dynamical System 336
15.3.2 Basketball Simulink Model 337
15.3.3 Building the Rapid Simulation Target 338
15.3.4 Rapid Simulation Options 339
15.3.5 Minimizing Miss Distance 341
15.4 xPC Target 342
15.4.1 Initialization 342
15.4.2 Building a Target 343
15.4.3 Starting Target 345
15.4.4 Controlling the Target 345
15.5 Summary 347
References 347

Appendix A Block Reference 348


A.1 Continuous Block Library 348
A.2 Discontinuities Block Library 349
A.3 Discrete Block Library 350
A.4 Lookup Tables Block Library 351
A.5 Math Operations Block Library 352
A.6 Ports & Subsystems Block Library 354
A.7 Signal Attributes Block Library 356
A.8 Signal Routing Block Library 357
A.9 Sinks Block Library 358
A.10 Sources Block Library 359
A.11 User-Defined Functions Block Library 361
Appendix B Parameter Reference 362
B.1 Model Parameters 362
B.2 Common Block Parameters 367
B.3 Mask Parameters 368
B.4 Identifying Block Parameters 368
Index 371.

You might also like