introduction to simulink with engineering applications

716

Click here to load reader

Upload: akshay-sharma

Post on 24-Nov-2015

159 views

Category:

Documents


29 download

DESCRIPTION

230262

TRANSCRIPT

  • Orchard Publicationswww.orchardpublications.com

    Introduction to Simulinkwith Engineering Applications

    Second EditionSteven T. Karris

  • Introduction to Simulinkwith Engineering Applications

    Second Edition

    Steven T. Karris

    Orchard Publicationswww.orchardpublications.com

  • Introduction to Simulink with Engineering Applications, Second EditionCopyright 2008 Orchard Publications. All rights reserved. Printed in the United States of America. No part of thispublication may be reproduced or distributed in any form or by any means, or stored in a data base or retrieval system,without the prior written permission of the publisher.

    Direct all inquiries to Orchard Publications, [email protected]

    Product and corporate names are trademarks or registered trademarks of The MathWorks, Inc. They are used onlyfor identification and explanation, without intent to infringe.

    Library of Congress Cataloging-in-Publication Data

    Library of Congress Control Number 2008923972

    ISBN-10: 1-934404-10-1

    ISBN-13: 978-1-934404-10-2

    TXu 1303-668

    Disclaimer

    The author has made every effort to make this text as complete and accurate as possible, but no warranty is implied.The author and publisher shall have neither liability nor responsibility to any person or entity with respect to any lossor damages arising from the information contained in this text.

  • PrefaceThis text is an introduction to Simulink , a companion application to MATLAB . It is writtenfor students at the undergraduate and graduate programs, as well as for the working professional.

    The author claims no originality of the content, and the description of the Simulink blocks isextracted from The MathWorks documentation without intent to infringe. The intent is toprovide a complete reference text, and whenever necessary, the author refers the reader to TheMathWorks documentation. Whenever there is a conflict between this text and TheMathWorks documentation, the latter takes precedence.

    Although some previous knowledge of MATLAB would be helpful, it is not absolutely necessary;Appendix A of this text is an introduction to MATLAB to enable the reader to begin learningboth MATLAB and Simulink simultaneously, and to perform graphical computations andprogramming.

    Chapters 2 through 19 describe the blocks in all Simulink Version 7.1 libraries. Their applicationis illustrated with Simulink models that contain the pertinent blocks, and some are supplementedwith MATLAB functions, commands, and statements. Some background information is providedfor lesser known definitions and topics. Chapters 1 and 20 contain several Simulink models toillustrate various applied math and engineering applications. Appendix B is an introduction tomasked subsystems, and Appendix C introduces the reader to random generation procedures.Appendix D is an introduction to Weighted Moving Averages.

    This text supplements our Numerical Analysis Using MATLAB and Excel, ISBN 9781934404034. It is self-contained; the blocks of each library are described in an orderly fashion that isconsistent with Simulinks documentation. This arrangement provides insight into how a model isused and how its parts interact with each another.

    Like MATLAB, Simulink can be used with both linear and nonlinear systems, which can bemodeled in continuous time, sample time, or a hybrid of these. Examples are provided in this text.

    Most of the examples presented in this book can be implemented with the Student Versions ofMATLAB and Simulink. A few may require the full versions of these outstanding packages, andthese examples may be skipped. Some addons, known as Toolboxes and Blocksets can beobtained from The MathWorks, Inc., 3 Apple Hill Drive, Natick, MA, 01760-2098, USA,www.mathworks.com.

    To get the most out of this outstanding application, it is highly recommended that this text is usedin conjunction with the MATLAB and Simulink Users Guides. Other references are provided inthe reference section of this text.

  • This is the second edition of this title, and although every effort was made to correct possibletypographical errors and erroneous references to figures and tables, some may have beenoverlooked. Accordingly, the author will appreciate it very much if any such errors are brought tohis attention so that corrections can be made for the next edition.

    The author wishes to express his gratitude to the staff of The MathWorks, the developers ofMATLAB and Simulink for the encouragement and unlimited support they have provided mewith during the production of this text.

    Our heartfelt thanks also to Mr. Howard R. Hansen, and Dr. Niel Ransom, former CTO ofAlcatel, for bringing some errors on the first print to our attention.

    Orchard Publicationswww.orchardpublications.cominfo@orchardpublications.com

  • Introduction to Simulink with Engineering Applications, Second Edition iCopyright Orchard Publications

    Table of Contents1 Introduction to Simulink 11

    1.1 Simulink and its Relation to MATLAB....................................................................111.2 Simulink Demos ......................................................................................................1201.3 Summary ..................................................................................................................1281.4 Exercises ..................................................................................................................1291.5 Solutions to EndofChapter Exercises ..................................................................130

    2 The Commonly Used Blocks Library 212.1 Inport, Outport, and Subsystem Blocks................................................................ 222.2 Ground Block ........................................................................................................ 242.3 Terminator Block .................................................................................................. 252.4 Constant and Product Blocks ............................................................................... 262.5 Scope Block ........................................................................................................... 282.6 Bus Creator and Bus Selector Blocks ................................................................... 282.7 Mux and Demux Blocks ..................................................................................... 2122.8 Switch Block ....................................................................................................... 2152.9 Sum Block ........................................................................................................... 2172.10 Gain Block .......................................................................................................... 2182.11 Relational Operator Block .................................................................................. 2192.12 Logical Operator Block ....................................................................................... 2202.13 Saturation Block ................................................................................................. 2212.14 Integrator Block .................................................................................................. 2222.15 Unit Delay Block ................................................................................................ 2272.16 Discrete-Time Integrator Block .......................................................................... 2292.17 Data Types and The Data Type Conversion Block ........................................... 2322.18 Summary ............................................................................................................. 2422.19 Exercises .............................................................................................................. 2462.20 Solutions to EndofChapter Exercises ............................................................. 248

    3 The Continuous Blocks Library 313.1 ContinuousTime Linear Systems SubLibrary ....................................................... 32

    3.1.1 Integrator Block .............................................................................................. 323.1.2 Derivative Block.............................................................................................. 323.1.3 StateSpace Block ........................................................................................... 373.1.4 Transfer Fcn Block .......................................................................................... 373.1.5 ZeroPole Block............................................................................................... 39

    3.2 ContinuousTime Delays SubLibrary .................................................................. 310

  • ii Introduction to Simulink with Engineering Applications, Second EditionCopyright Orchard Publications

    3.2.1 Transport Delay Block................................................................................. 3113.2.2 Variable Time Delay Block ......................................................................... 3123.2.3 Variable Transport Delay Block.................................................................. 313

    3.3 Summary ............................................................................................................... 3153.4 Exercises ............................................................................................................... 3173.5 Solutions to EndofChapter Exercises ............................................................... 318

    4 The Discontinuities Blocks Library 414.1 Saturation Block ..................................................................................................... 424.2 Saturation Dynamic Block ..................................................................................... 434.3 Dead Zone Block .................................................................................................... 444.4 Dead Zone Dynamic Block..................................................................................... 454.5 Rate Limiter Block.................................................................................................. 464.6 Rate Limiter Dynamic Block .................................................................................. 484.7 Backlash Block........................................................................................................ 494.8 Relay Block ........................................................................................................... 4114.9 Quantizer Block.................................................................................................... 4124.10 Hit Crossing Block................................................................................................ 4134.11 Coulomb and Viscous Friction Block................................................................... 4144.12 Wrap to Zero Block .............................................................................................. 4164.13 Summary ............................................................................................................... 4174.14 Exercises ............................................................................................................... 4194.15 Solutions to EndofChapter Exercises ............................................................... 420

    5 The Discrete Blocks Library 515.1 DiscreteTime Linear Systems SubLibrary............................................................ 52

    5.1.1 Unit Delay Block.......................................................................................... 525.1.2 Integer Delay Block...................................................................................... 525.1.3 Tapped Delay Block..................................................................................... 535.1.4 DiscreteTime Integrator Block .................................................................. 545.1.5 Discrete Transfer Fcn Block ........................................................................ 545.1.6 Discrete Filter Block .................................................................................... 555.1.7 Discrete ZeroPole Block............................................................................. 585.1.8 Difference Block........................................................................................... 595.1.9 Discrete Derivative Block .......................................................................... 5105.1.10 Discrete StateSpace Block ....................................................................... 5115.1.11 Transfer Fcn First Order Block.................................................................. 5145.1.12 Transfer Fcn Lead or Lag Block................................................................. 5155.1.13 Transfer Fcn Real Zero Block .................................................................... 5185.1.14 Discrete FIR Filter Block ........................................................................... 519

  • Introduction to Simulink with Engineering Applications, Second Edition iiiCopyright Orchard Publications

    5.2 Sample & Hold Delays SubLibrary..................................................................... 5215.2.1 Memory Block ............................................................................................ 5215.2.2 FirstOrder Hold Block.............................................................................. 5225.2.3 ZeroOrder Hold Block.............................................................................. 523

    5.3 Summary ............................................................................................................... 5255.4 Exercises................................................................................................................ 5275.5 Solutions to EndofChapter Exercises ............................................................... 529

    6 The Logic and Bit Operations Library 616.1 Logic Operations Group SubLibrary ..................................................................... 62

    6.1.1 Logical Operator Block ................................................................................ 626.1.2 Relational Operator Block............................................................................ 626.1.3 Interval Test Block ....................................................................................... 626.1.4 Interval Test Dynamic Block ....................................................................... 636.1.5 Combinatorial Logic Block........................................................................... 646.1.6 Compare to Zero Block ................................................................................ 696.1.7 Compare to Constant Block....................................................................... 610

    6.2 Bit Operations Group SubLibrary ...................................................................... 6116.2.1 Bit Set Block ............................................................................................... 6126.2.2 Bit Clear Block ........................................................................................... 6136.2.3 Bitwise Operator Block............................................................................... 6146.2.4 Shift Arithmetic Block ............................................................................... 6166.2.5 Extract Bits Block....................................................................................... 617

    6.3 Edge Detection Group SubLibrary ..................................................................... 6186.3.1 Detect Increase Block................................................................................. 6186.3.2 Detect Decrease Block ............................................................................... 6206.3.3 Detect Change Block ................................................................................. 6216.3.4 Detect Rise Positive Block ......................................................................... 6226.3.5 Detect Rise Nonnegative Block ................................................................. 6236.3.6 Detect Fall Negative Block ........................................................................ 6246.3.7 Detect Fall Nonpositive Block ................................................................... 625

    6.4 Summary............................................................................................................... 6276.5 Exercises ............................................................................................................... 6316.6 Solutions to EndofChapter Exercises ............................................................... 632

    7 The Lookup Tables Library 717.1 Lookup Table Block ................................................................................................727.2 Lookup Table (2D) Block .....................................................................................737.3 Lookup Table (nD) Block .....................................................................................767.4 PreLookup Index Search Block...............................................................................77

  • iv Introduction to Simulink with Engineering Applications, Second EditionCopyright Orchard Publications

    7.5 Interpolation (nD) Using PreLookup Block......................................................... 787.6 Direct Lookup Table (nD) Block ....................................................................... 7107.7 Lookup Table Dynamic Block .............................................................................. 7167.8 Sine and Cosine Blocks ........................................................................................ 7177.9 Summary ............................................................................................................... 7217.10 Exercises................................................................................................................ 7237.11 Solutions to EndofChapter Exercises ............................................................... 724

    8 The Math Operations Library 818.1 Math Operations Group SubLibrary ..................................................................... 82

    8.1.1 Sum Block .................................................................................................... 828.1.2 Add Block .................................................................................................... 828.1.3 Subtract Block.............................................................................................. 838.1.4 Sum of Elements Block ................................................................................ 848.1.5 Bias Block..................................................................................................... 848.1.6 Weighted Sample Time Math Block ........................................................... 858.1.7 Gain Block ................................................................................................... 868.1.8 Slider Gain Block ......................................................................................... 868.1.9 Product Block............................................................................................... 878.1.10 Divide Block................................................................................................. 878.1.11 Product of Elements Block........................................................................... 878.1.12 Dot Product Block ....................................................................................... 888.1.13 Sign Block .................................................................................................... 898.1.14 Abs Block ................................................................................................... 8108.1.15 Unary Minus Block .................................................................................... 8118.1.16 Math Function Block................................................................................. 8118.1.17 Rounding Function Block.......................................................................... 8138.1.18 Polynomial Block ....................................................................................... 8148.1.19 MinMax Block ........................................................................................... 8148.1.20 MinMax Running Resettable Block .......................................................... 8158.1.21 Trigonometric Function Block .................................................................. 8168.1.22 Sine Wave Function Block ........................................................................ 8178.1.23 Algebraic Constraint Block........................................................................ 818

    8.2 Vector / Matrix Operations Group SubLibrary ................................................... 8198.2.1 The Assignment Block............................................................................... 8198.2.2 The Reshape Block .................................................................................... 8218.2.3 The Squeze Block....................................................................................... 8218.2.4 The Matrix Concatenate Block ................................................................. 8238.2.5 The Vector Concatenate Block................................................................. 8248.2.6 The Permute Block .................................................................................... 825

    8.3 The Complex Vector Conversions Group SubLibrary ........................................ 826

  • Introduction to Simulink with Engineering Applications, Second Edition vCopyright Orchard Publications

    8.3.1 Complex to MagnitudeAngle Block........................................................ 8268.3.2 MagnitudeAngle to Complex Block....................................................... 8278.3.3 Complex to RealImag Block.................................................................... 8288.3.4 RealImag to Complex Block.................................................................... 829

    8.4 Summary................................................................................................................ 8308.5 Exercises ................................................................................................................ 8348.6 Solutions to EndofChapter Exercises................................................................ 836

    9 The Model Verification Library 919.1 Check Static Lower Bound Block.......................................................................... 929.2 Check Static Upper Bound Block.......................................................................... 939.3 Check Static Range Block ..................................................................................... 949.4 Check Static Gap Block......................................................................................... 959.5 Check Dynamic Lower Bound Block .................................................................... 969.6 Check Dynamic Upper Bound Block .................................................................... 989.7 Check Dynamic Range Block ................................................................................ 999.8 Check Dynamic Gap Block ................................................................................. 9109.9 Assertion Block.................................................................................................... 9129.10 Check Discrete Gradient Block........................................................................... 9139.11 Check Input Resolution Block ............................................................................ 9149.12 Summary .............................................................................................................. 9169.13 Exercises............................................................................................................... 9189.14 Solutions to EndofChapter Exercises .............................................................. 919

    10 The ModelWide Utilities Library 10110.1 Linearization of Running Models SubLibrary..................................................102

    10.1.1 TriggerBased Linearization Block........................................................10210.1.2 TimeBased Linearization Block...........................................................104

    10.2 Documentation SubLibrary ............................................................................10710.2.1 Model Info Block...................................................................................10710.2.2 Doc Text Block .....................................................................................109

    10.3 Modeling Guides SubLibrary ..........................................................................109Block Support Table Block...............................................................................109

    10.4 Summary .........................................................................................................1011

    11 The Ports & Subsystems Library 11111.1 Inport, Outport, and Subsystem Blocks ........................................................ 11211.2 Trigger Block ................................................................................................. 11211.3 Enable Block.................................................................................................. 11211.4 FunctionCall Generator Block .................................................................... 113

  • vi Introduction to Simulink with Engineering Applications, Second EditionCopyright Orchard Publications

    11.5 Atomic Subsystem Block ............................................................................... 11411.6 Code Reuse Subsystem Block ........................................................................ 11911.7 Model Block................................................................................................. 111711.8 Configurable Subsystem Block..................................................................... 111911.9 Triggered Subsystem Block.......................................................................... 112511.10 Enabled Subsystem Block............................................................................ 112711.11 Enabled and Triggered Subsystem Block.................................................... 113011.12 FunctionCall Subsystem Block ................................................................. 113411.13 For Iterator Subsystem Block ...................................................................... 113711.14 While Iterator Subsystem Block.................................................................. 113911.15 If and If Action Subsystem Blocks .............................................................. 114111.16 Switch Case and The Switch Case Action Subsystem Blocks.................... 114311.17 Subsystem Examples Block ......................................................................... 114611.18 SFunctions in Simulink............................................................................. 114911.19 Summary...................................................................................................... 1155

    12 The Signal Attributes Library 12112.1 Signal Attribute Manipulation SubLibrary .................................................... 122

    12.1.1 Data Type Conversion Block ............................................................. 12212.1.2 Data Type Duplicate Block ................................................................ 12212.1.3 Data Type Propagation Block ............................................................ 12412.1.4 Data Type Scaling Strip Block ........................................................... 12512.1.5 Data Conversion Inherited Block ...................................................... 12512.1.6 IC (Initial Condition) Block............................................................... 12612.1.7 Signal Conversion Block .................................................................... 12712.1.8 Rate Transition Block ........................................................................ 12812.1.9 Signal Specification Block ................................................................ 121112.1.10 Bus to Vector Block.......................................................................... 121212.1.11 Data Type Propagation Examples Block .......................................... 1214

    12.2 Signal Attribute Detection SubLibrary ........................................................ 121612.2.1 Probe Block....................................................................................... 121712.2.2 Weighted Sample Time Block.......................................................... 121812.2.3 Width Block ..................................................................................... 1219

    12.3 Summary ........................................................................................................ 1220

    13 The Signal Routing Library 13113.1 Signal Routing Group SubLibrary ................................................................. 132

    13.1.1 Bus Creator Block .............................................................................. 13213.1.2 Bus Selector Block ............................................................................. 13213.1.3 Bus Assignment Block ....................................................................... 132

  • Introduction to Simulink with Engineering Applications, Second Edition viiCopyright Orchard Publications

    13.1.4 Mux Block ..........................................................................................13613.1.5 Demux Block ......................................................................................13613.1.6 Selector Block.....................................................................................13613.1.7 Index Vector Block.............................................................................13813.1.8 Merge Block........................................................................................13813.1.9 Environmental Controller Block......................................................131013.1.10 Manual Switch Block .......................................................................131213.1.11 Multiport Switch Block ....................................................................131313.1.12 Switch Block.....................................................................................131413.1.13 From Block .......................................................................................131413.1.14 Goto Tag Visibility Block .................................................................131513.1.15 Goto Block........................................................................................1316

    13.2 Signal Storage and Access Group SubLibrary .............................................131813.2.1 Data Store Read Block .....................................................................131813.2.2 Data Store Memory Block................................................................131813.2.3 Data Store Write Block....................................................................1319

    13.3 Summary ........................................................................................................1322

    14 The Sinks Library 14114.1 Models and Subsystems Outputs SubLibrary................................................142

    14.1.1 Outport Block.......................................................................................14214.1.2 Terminator Block .................................................................................14214.1.3 To File Block........................................................................................14214.1.4 To Workspace Block ...........................................................................144

    14.2 Data Viewers SubLibrary...............................................................................14614.2.1 Scope Block .........................................................................................14614.2.2 Floating Scope Block ...........................................................................14814.2.3 XY Graph Block.................................................................................141214.2.4 Display Block .....................................................................................1416

    14.3 Simulation Control SubLibrary ...................................................................1417Stop Simulation Block...................................................................................1417

    14.4 Summary........................................................................................................1418

    15 The Sources Library 15115.1 Models and Subsystems Inputs SubLibrary .................................................. 152

    15.1.1 Inport Block .......................................................................................15215.1.2 Ground Block.................................................................................... 15215.1.3 From File Block................................................................................. 15215.1.4 From Workspace Block .................................................................... 152

    15.2 Signal Generators SubLibrary ...................................................................... 153

  • viii Introduction to Simulink with Engineering Applications, Second EditionCopyright Orchard Publications

    15.2.1 Constant Block ................................................................................. 15315.2.2 Signal Generator Block ..................................................................... 15415.2.3 Pulse Generator Block ...................................................................... 15515.2.4 Signal Builder Block.......................................................................... 15615.2.5 Ramp Block ....................................................................................... 15915.2.6 Sine Wave Block............................................................................... 15915.2.7 Step Block....................................................................................... 151215.2.8 Repeating Sequence Block ............................................................. 151315.2.9 Chirp Signal Block.......................................................................... 151415.2.10 Random Number Block .................................................................. 151515.2.11 Uniform Random Number Block ................................................... 151615.2.12 Band Limited White Noise Block................................................... 151715.2.13 Repeating Sequence Stair Block..................................................... 152215.2.14 Repeating Sequence Interpolated Block ........................................ 152215.2.15 Counter FreeRunning Block ........................................................ 152415.2.16 Counter Limited Block ................................................................... 152515.2.17 Clock Block..................................................................................... 152615.2.18 Digital Clock Block......................................................................... 1527

    15.3 Summary....................................................................................................... 1529

    16 The UserDefined Functions Library 16116.1 Fcn Block....................................................................................................... 16216.2 MATLAB Fcn Block ..................................................................................... 16316.3 Embedded MATLAB Function Block .......................................................... 16316.4 SFunction Block .......................................................................................... 16716.5 Level2 Mfile SFunction Block................................................................. 16716.6 SFunction Builder Block ........................................................................... 161116.7 SFunction Examples Block........................................................................ 161116.8 Summary...................................................................................................... 1612

    17 The Additional Discrete Library 17117.1 Transfer Fcn Direct Form II Block............................................................... 17217.2 Transfer Fcn Direct Form II Time Varying Block........................................ 17317.3 Fixed-Point State-Space Block..................................................................... 17417.4 Unit Delay External IC Block ...................................................................... 17617.5 Unit Delay Resettable Block ........................................................................ 17717.6 Unit Delay Resettable External IC Block..................................................... 17817.7 Unit Delay Enabled Block ............................................................................ 17917.8 Unit Delay Enabled Resettable Block ........................................................ 171117.9 Unit Delay Enabled External IC Block ...................................................... 1712

  • Introduction to Simulink with Engineering Applications, Second Edition ixCopyright Orchard Publications

    17.10 Unit Delay Enabled Resettable External IC Block .................................... 171317.11 Unit Delay With Preview Resettable Block............................................... 171517.12 Unit Delay With Preview Resettable External RV Block ......................... 171617.13 Unit Delay With Preview Enabled Block .................................................. 171717.14 Unit Delay With Preview Enabled Resettable Block................................. 171917.15 Unit Delay With Preview Enabled Resettable External RV Block ........... 172017.16 Summary .................................................................................................... 1722

    18 The Additional Math Increment / Decrement Library 18118.1 Increment Real World Block...........................................................................18218.2 Decrement Real World Block .........................................................................18318.3 Increment Stored Integer Block ......................................................................18418.4 Decrement Stored Integer Block .....................................................................18518.5 Decrement to Zero Block ................................................................................18618.6 Decrement Time To Zero Block......................................................................18718.7 Summary ..........................................................................................................188

    19 The Simulink Extras Library 19119.1 Additional Discrete Group blocks ...................................................................192

    19.1.1 Discrete Transfer Fcn (with initial states) block.................................19219.1.2 Discrete Transfer Fcn (with initial outputs) block..............................19519.1.3 Discrete ZeroPole (with initial states) block .....................................19819.1.4 Discrete ZeroPole (with initial outputs) block ................................191219.1.5 Idealized ADC Quantizer block ........................................................1915

    19.2 Additional Linear Group blocks ....................................................................191819.2.1 Transfer Fcn (with initial states) block .............................................191819.2.2 Transfer Fcn (with initial outputs) block ..........................................192119.2.3 ZeroPole (with initial states) block..................................................192319.2.4 ZeroPole (with initial outputs) block ..............................................192619.2.5 StateSpace (with initial outputs) block...........................................192719.2.6 PID Controller block .........................................................................192919.2.7 PID Controller (with Approximate Derivative) block......................1931

    19.3 Additional Sinks Group blocks......................................................................193319.3.1 Power Spectral Density block ............................................................193319.3.2 Averaging Power Spectral Density block ..........................................193719.3.3 Spectrum Analyzer block...................................................................193819.3.4 Averaging Spectrum Analyzer block .................................................194119.3.5 Cross Correlator block .......................................................................194319.3.6 Auto Correlator block........................................................................194519.3.7 Floating Bar Plot block ......................................................................1946

  • x Introduction to Simulink with Engineering Applications, Second EditionCopyright Orchard Publications

    19.4 Flip Flops Group blocks................................................................................. 194819.4.1 Clock block ....................................................................................... 194819.4.2 D Latch block.................................................................................... 194919.4.3 SR Flip Flop block........................................................................... 195019.4.4 D Flip Flop block............................................................................... 195119.4.5 JK Flip Flop block............................................................................ 1952

    19.5 Linearization Group blocks ........................................................................... 195319.5.1 Switched Derivative for Linearization block ..................................... 195319.5.2 Switched Transport Delay for Linearization block............................ 1956

    19.6 Transformations Group blocks...................................................................... 195919.6.1 Polar to Cartesian block .................................................................... 195919.6.2 Cartesian to Polar block .................................................................... 196019.6.3 Spherical to Cartesian block.............................................................. 196119.6.4 Cartesian to Spherical block.............................................................. 196219.6.5 Fahrenheit to Celsius block ............................................................... 196319.6.6 Celsius to Fahrenheit block ............................................................... 196419.6.7 Degrees to Radians block .................................................................. 196519.6.8 Radians to Degrees block .................................................................. 1965

    19.7 Summary........................................................................................................ 1967

    20 Engineering Applications 20120.1 Applications to Differential Equations ........................................................... 201

    20.1.1 Math Example .................................................................................. 20120.1.2 Dynamics Example ........................................................................... 20320.1.3 Chemical Solutions Example ........................................................... 20620.1.4 Heat Flow Example .......................................................................... 20820.1.5 Cantilever Beam Deflection Example ............................................ 201020.1.6 Tractrix Curve Example ................................................................. 201320.1.7 Bessel Differential Equation Example ............................................ 201520.1.8 Van der Pol Differential Equation Example .................................. 201820.1.9 The Simple Pendulum Example ..................................................... 202220.1.10 Simple Oscillator Example ............................................................. 2026

    20.2 ZeroOrder Hold and FirstOrder Hold Circuits as Reconstructors .......... 202820.3 Digital Filter Realization Forms ................................................................... 2030

    20.3.1 Direct Form I Realization of a Digital Filter .................................. 203020.3.2 Direct Form II Realization of a Digital Filter ................................. 203120.3.3 Series Form Realization of a Digital Filter ..................................... 203420.3.4 Parallel Form Realization of a Digital Filter ................................... 2036

    20.4 Models for Binary Counters ......................................................................... 203920.4.1 Model for a 3bit Up / Down Counter .......................................... 2039

  • Introduction to Simulink with Engineering Applications, Second Edition xiCopyright Orchard Publications

    20.4.2 Model for a 4bit Ring Counter .....................................................204020.5 Models for Mechanical Systems .................................................................2041

    20.5.1 Model for a MassSpringDashpot .................................................204120.5.2 Model for a Cascaded MassSpring System ....................................204320.5.3 Model for a Mechanical Accelerometer ..........................................2045

    20.6 Feedback Control Systems ..........................................................................204620.7 Models for Electrical Systems .....................................................................2049

    20.7.1 Model for an Electric Circuit in Phasor Form .................................204920.7.2 Model for the Application of the Superposition Principle ..............2051

    20.8 Transformations ..........................................................................................205320.9 Discrete Time Integration with Variable Amplitude Input .......................205420.10 The Digital Filter Design Block ..................................................................205720.11 S-Function Examples ..................................................................................2065

    20.11.1 Temperature Coefficients for Semiconductor Diodes ...................206520.11.2 Simple Pendulum ..........................................................................2067

    20.12 Concluding Remarks ...................................................................................207020.13 Summary ......................................................................................................2071

    A Introduction to MATLAB A1A.1 MATLAB and Simulink............................................................................A1A.2 Command Window..........................................................................................A1A.3 Roots of Polynomials ........................................................................................A3A.4 Polynomial Construction from Known Roots..................................................A4A.5 Evaluation of a Polynomial at Specified Values...............................................A6A.6 Rational Polynomials........................................................................................A8A.7 Using MATLAB to Make Plots .....................................................................A10A.8 Subplots ..........................................................................................................A18A.9 Multiplication, Division, and Exponentiation ...............................................A18A.10 Script and Function Files ...............................................................................A26A.11 Display Formats..............................................................................................A31

    B Masked Subsystems B1B.1 Masks Defined.................................................................................................... B1B.2 Advantages Using Masked Subsystems ............................................................. B1B.3 Mask Features .................................................................................................... B1B.4 Creating a Masked Subsystem ........................................................................... B2

    C Random Number Generation C1C.1 Random Numbers ..............................................................................................C1C.2 An Example .......................................................................................................C1

  • xii Introduction to Simulink with Engineering Applications, Second EditionCopyright Orchard Publications

    D Weighted Moving Average ....................................................................................... D1 References R1 Index IN1

  • Introduction to Simulink with Engineering Applications, Second Edition 11Copyright Orchard Publications

    Chapter 1

    Introduction to Simulink

    his chapter is an introduction to Simulink. This author feels that it is best to introduce Sim-ulink in this chapter with a few examples. Tools for simulation and modelbased designs arepresented in the subsequent chapters. Some familiarity with MATLAB is essential in

    understanding Simulink, and for this purpose, Appendix A is included as an introduction toMATLAB.

    1.1 Simulink and its Relation to MATLABThe MATLAB and Simulink environments are integrated into one entity, and thus we cananalyze, simulate, and revise our models in either environment at any point. We invoke Simulinkfrom within MATLAB. We begin with a few examples and we will discuss generalities in subse-quent chapters. Throughout this text, a left justified horizontal bar will denote the beginning ofan example, and a right justified horizontal bar will denote the end of the example. These barswill not be shown whenever an example begins at the top of a page or at the bottom of a page.Also, when one example follows immediately after a previous example, the right justified bar willbe omitted.

    Example 1.1

    For the electric circuit of Figure 1.1, *is the input and the initial conditions are ,

    and . We will compute .

    Figure 1.1. Circuit for Example 1.1

    For this example,(1.1)

    * Throughout this text, the designation will be used to denote the unit step function. We will use todenote any other input to be consistent with the MATLAB and Simulink designations.

    T

    u0 t( ) iL 0( ) 0=

    u0 t( ) u t( )

    vc 0( ) 0.5 V= vC t( )

    +R L

    +

    C1

    vs t( ) u0 t( )=

    vC t( )i t( )

    1 4 H

    4 3 F

    i iL iC CdvCdt

    ---------= = =

  • Chapter 1 Introduction to Simulink

    12 Introduction to Simulink with Engineering Applications, Second Edition Copyright Orchard Publications

    and by Kirchoffs voltage law (KVL),

    (1.2)

    Substitution of (1.1) into (1.2) yields

    (1.3)

    Substituting the values of the circuit constants and rearranging we obtain:

    (1.4)

    (1.5)

    To appreciate Simulinks capabilities, for comparison, three different methods of obtaining thesolution are presented, and the solution using Simulink follows.

    First Method Assumed SolutionEquation (1.5) is a secondorder, nonhomogeneous differential equation with constant coeffi-cients, and thus the complete solution will consist of the sum of the forced response and the natu-ral response. It is obvious that the solution of this equation cannot be a constant since the deriva-tives of a constant are zero and thus the equation is not satisfied. Also, the solution cannotcontain sinusoidal functions (sine and cosine) since the derivatives of these are also sinusoids.

    However, decaying exponentials of the form where and are constants, are possible can-didates since their derivatives have the same form but alternate in sign.

    It can be shown* that if and where and are constants and and are theroots of the characteristic equation of the homogeneous part of the given differential equation,

    the natural response is the sum of the terms and . Therefore, the total solution willbe

    * For a thorough discussion, please refer to Circuit Analysis II with MATLAB Applications, ISBN 0970951159, Appendix B.

    RiL LdiLdt------- vC+ + u0 t( )=

    RCdvCdt

    --------- LCd2vCdt2

    ----------- vC+ + u0 t( )=

    13---d

    2vCdt2

    ----------- 43---dvC

    dt--------- vC+ + u0 t( )=

    d2vCdt2

    ----------- 4dvCdt

    --------- 3vC+ + 3u0 t( )=

    d2vCdt2

    ----------- 4dvCdt

    --------- 3vC+ + 3= t 0>

    ke at k a

    k1es1t k2e

    s2t k1 k2 s1 s2

    k1es1t k2e

    s2t

  • Introduction to Simulink with Engineering Applications, Second Edition 13Copyright Orchard Publications

    Simulink and its Relation to MATLAB

    (1.6)

    The values of and are the roots of the characteristic equation

    (1.7)

    Solution of (1.7) yields of and and with these values (1.6) is written as

    (1.8)

    The forced component is found from (1.5), i.e.,

    (1.9)

    Since the right side of (1.9) is a constant, the forced response will also be a constant and wedenote it as . By substitution into (1.9) we obtain

    or (1.10)

    Substitution of this value into (1.8), yields the total solution as

    (1.11)

    The constants and will be evaluated from the initial conditions. First, using and evaluating (1.11) at , we obtain

    (1.12)

    Also,

    and

    (1.13)

    Next, we differentiate (1.11), we evaluate it at , and equate it with (1.13). Thus,

    (1.14)

    vc t( ) natural response forced response+ vcn t( ) vcf t( )+ k1es1t k2e

    s2t vcf t( )+ += = =s1 s2

    s2 4s 3+ + 0=

    s1 1= s2 3=

    vc t( ) k1e t k2e 3 t vcf t( )+ +=vcf t( )

    d2vCdt2

    ----------- 4dvCdt

    --------- 3vC+ + 3= t 0>

    vCf k3=

    0 0 3k3+ + 3=

    vCf k3 1= =

    vC t( ) vCn t( ) vCf+= k1e t k2e 3 t 1+ +=k1 k2 vC 0( ) 0.5 V=

    t 0=

    vC 0( ) k1e0 k2e0 1+ + 0.5= =k1 k2+ 0.5=

    iL iC CdvCdt

    ---------= = dvCdt

    --------- iLC----=,

    dvCdt

    ---------t 0=

    iL 0( )C

    ------------ 0C---- 0= = =

    t 0=

    dvCdt

    ---------t 0=

    k1 3k2=

  • Chapter 1 Introduction to Simulink

    14 Introduction to Simulink with Engineering Applications, Second Edition Copyright Orchard Publications

    Equating the right sides of (1.13) and (1.14) we obtain

    (1.15)

    Simultaneous solution of (1.12) and (1.15), gives and . By substitution into(1.8), we obtain the total solution as

    (1.16)

    Check with MATLAB:

    syms t % Define symbolic variable ty0=0.75*exp(t)+0.25*exp(3*t)+1; % The total solution y(t), for our example, vc(t)y1=diff(y0) % The first derivative of y(t)

    y1 =3/4*exp(-t)-3/4*exp(-3*t)

    y2=diff(y0,2) % The second derivative of y(t)

    y2 =-3/4*exp(-t)+9/4*exp(-3*t)

    y=y2+4*y1+3*y0 % Summation of y and its derivatives

    y =3

    Thus, the solution has been verified by MATLAB. Using the expression for in (1.16), wefind the expression for the current as

    (1.17)

    Second Method Using the Laplace TransformationThe transformed circuit is shown in Figure 1.2.

    Figure 1.2. Transformed Circuit for Example 1.1

    k1 3k2 0=

    k1 0.75= k2 0.25=

    vC t( ) 0.75 e t 0.25e 3 t 1+ +( )u0 t( )=

    vC t( )

    i iL= iC CdvCdt

    ---------- 43--- 3

    4---e t 3

    4--- e 3t e

    t e 3t A= == =

    +R L

    +

    C 1

    VS s( ) 1 s=VC s( )

    I s( )

    0.25s

    3 4s + VC 0( )0.5 s

  • Introduction to Simulink with Engineering Applications, Second Edition 15Copyright Orchard Publications

    Simulink and its Relation to MATLAB

    By the voltage division* expression,

    Using partial fraction expansion, we let

    (1.18)

    and by substitution into (1.18)

    Taking the Inverse Laplace transform we find that

    Third Method Using State Variables**

    By substitution of given values and rearranging, we obtain

    * For derivation of the voltage division and current division expressions, please refer to Circuit Analysis I withMATLAB Applications, ISBN 0970951124.

    A thorough discussion of partial fraction expansion with MATLAB Applications is presented in NumericalAnalysis Using MATLAB and Excel, ISBN 9781934404034.

    For an introduction to Laplace Transform and Inverse Laplace Transform, please refer to Circuit Analysis IIwith MATLAB Applications, ISBN 0970951159.

    ** Usually, in StateSpace and State Variables Analysis, denotes any input. For distinction, we will denotethe Unit Step Function as . For a detailed discussion on StateSpace and State Variables Analysis, pleaserefer to Signals and Systems with MATLAB Computing and Simulink Modeling , ISBN 0974423998.

    VC s( ) 3 4s1 0.25s 3 4s+ +( )----------------------------------------------1s--- 0.5

    s------- 0.5s-------+=

    1.5s s2 4s 3+ +( )--------------------------------- 0.5

    s-------+ 0.5s

    2 2s 3+ +s s 1+( ) s 3+( )------------------------------------= =

    0.5s2 2s 3+ +s s 1+( ) s 3+( )------------------------------------

    r1s----

    r2s 1+( )----------------

    r3s 3+( )----------------+ +=

    r10.5s2 2s 3+ +s 1+( ) s 3+( )---------------------------------- s 0=

    1= =

    r20.5s2 2s 3+ +

    s s 3+( )---------------------------------- s 1=0.75= =

    r30.5s2 2s 3+ +

    s s 1+( )---------------------------------- s 3=0.25= =

    VC s( ) 0.5s2 2s 3+ +

    s s 1+( ) s 3+( )------------------------------------1s--- 0.75

    s 1+( )----------------0.25s 3+( )----------------+ += =

    vC t( ) 1 0.75e t 0.25e 3t+=

    RiL LdiLdt------- vC+ + u0 t( )=

    u t( )u0 t( )

  • Chapter 1 Introduction to Simulink

    16 Introduction to Simulink with Engineering Applications, Second Edition Copyright Orchard Publications

    or

    (1.19)

    Next, we define the state variables and . Then,

    * (1.20)

    and(1.21)

    Also,

    and thus,

    or(1.22)

    Therefore, from (1.19), (1.20), and (1.22), we obtain the state equations

    and in matrix form,

    (1.23)

    Solution of (1.23) yields

    Then,

    (1.24)

    * The notation (x dot) is often used to denote the first derivative of the function , that is, .

    The detailed solution of (1.23) is given in Signals and Systems with MATLAB Applications, ISBN 0974423998, Chapter 5.

    14---diL

    dt------- 1( )iL vC 1+=

    diLdt------- 4iL 4vC 4+=

    x1 iL= x2 vC=

    x 1diLdt-------=

    x x x dx dt=

    x 2dvCdt

    ---------=

    iL CdvCdt

    ---------=

    x1 iL CdvCdt

    --------- Cx 243---x 2= = = =

    x 234---x1=

    x 1 4x1 4x2 4+=

    x 234--- x1=

    x 1x 2

    4 43 4 0

    x1x2

    40

    u0 t( )+=

    x1x2

    e t e 3t

    1 0.75 e t 0.25e 3t+=

    x1 iL et e 3t= =

  • Introduction to Simulink with Engineering Applications, Second Edition 17Copyright Orchard Publications

    Simulink and its Relation to MATLAB

    and(1.25)

    Modeling the Differential Equation of Example 1.1 with Simulink

    To run Simulink, we must first invoke MATLAB. Make sure that Simulink is installed in your sys-tem. At the MATLAB command prompt (>>), we type:

    simulink

    Alternately, we can click the Simulink icon shown in Figure 1.3. It appears on the top bar on theMATLAB Command Window.

    Figure 1.3. The Simulink icon

    Upon execution of the Simulink command, the Commonly Used Blocks are shown in Figure1.4.

    In Figure 1.4, the left side is referred to as the Tree Pane and displays all Simulink librariesinstalled. The right side is referred to as the Contents Pane and displays the blocks that reside inthe library currently selected in the Tree Pane.

    Let us express the differential equation of Example 1.1 as

    (1.26)

    A block diagram representing (1.26) is shown in Figure 1.5. Now, we will use Simulink to draw asimilar block diagram.

    x2 vC 1 0.75et 0.25e 3t+= =

    d2vCdt2

    ----------- 4dvCdt

    --------- 3vC 3u0 t( )+=

  • Chapter 1 Introduction to Simulink

    18 Introduction to Simulink with Engineering Applications, Second Edition Copyright Orchard Publications

    Figure 1.4. The Simulink Library Browser

    Figure 1.5. Block diagram for equation (1.26)

    To model the differential equation (1.26) using Simulink, we perform the following steps:

    1. On the Simulink Library Browser, we click the leftmost icon shown as a blank page onthe top title bar. A new model window named untitled will appear as shown in Figure 1.6.

    3u0 t( ) dt dt4

    3

    d2vCdt2

    ----------- dvCdt

    ---------

    vC

  • Introduction to Simulink with Engineering Applications, Second Edition 19Copyright Orchard Publications

    Simulink and its Relation to MATLAB

    Figure 1.6. The Untitled model window in Simulink.

    The window of Figure 1.6 is the model window where we enter our blocks to form a block dia-gram. We save this as model file name Equation_1_26. This is done from the File drop menu ofFigure 1.6 where we choose Save as and name the file as Equation_1_26. Simulink will add theextension .mdl. The new model window will now be shown as Equation_1_26, and all savedfiles will have this appearance. See Figure 1.7.

    Figure 1.7. Model window for Equation_1_26.mdl file

    2. With the Equation_1_26 model window and the Simulink Library Browser both visible, weclick the Sources appearing on the left side list, and on the right side we scroll down until wesee the unit step function block shown as Step block. See Figure 1.8. We select it, and we dragit into the Equation_1_26 model window which now appears as shown in Figure 1.8. We savefile Equation_1_26 using the File drop menu on the Equation_1_26 model window (right sideof Figure 1.8).

    3. With reference to block diagram of Figure 1.5, we observe that we need to connect an ampli-fier with Gain 3 to the unit step function block. The Gain block in Simulink is under Com-monly Used Blocks (first item under Simulink on the Simulink Library Browser). See Figure1.8. If the Equation_1_26 model window is no longer visible, it can be recalled by clicking onthe white page icon on the top bar of the Simulink Library Browser.

    4. We choose the Gain block and we drag it to the right of the Step block (unit step function) asshown in Figure 1.9. The triangle on the right side of the unit step function block and the >symbols on the left and right sides of the gain block are connection points. We point themouse close to the connection point of the unit step function until is shows as a cross hair, anddraw a straight line to connect the two blocks. We doubleclick the Gain block and on theFunction Block Parameters, we change the gain from unity to 3. See Figure 1.10.

  • Chapter 1 Introduction to Simulink

    110 Introduction to Simulink with Engineering Applications, Second Edition Copyright Orchard Publications

    Figure 1.8. Dragging the unit step function into File Equation_1_26

    Figure 1.9. File Equation_1_26 with added Step and Gain blocks

    5. Next, we need to add a theeinput adder. The adder block appears on the right side of theSimulink Library Browser under Math Operations. We select it, and we drag it into theEquation_1_26 model window. We double click it, and on the Function Block Parameterswindow which appears, we specify 3 inputs. We then connect the output of the Step block tothe input of the Gain block, and the output of the of the Gain block to the first input of theAdd block as shown in Figure 1.10.

  • Introduction to Simulink with Engineering Applications, Second Edition 111Copyright Orchard Publications

    Simulink and its Relation to MATLAB

    Figure 1.10. File Equation_1_26 with added Add block and connections between the blocks

    6. From the Commonly Used Blocks of the Simulink Library Browser, we choose the Integra-tor block, we drag it into the Equation_1_26 model window, and we connect it to the outputof the Add block. We repeat this step and to add a second Integrator block. We click the textIntegrator under the first integrator block, and we change it to Integrator 1. Then, wechange the text Integrator 1 under the second Integrator to Integrator 2 as shown in Fig-ure 1.11.

    Figure 1.11. File Equation_1_26 with the addition of two integrators

    7. To complete the model to represent the block diagram in Figure 1.5, we add the Scope blockwhich is found in the Commonly Used Blocks on the Simulink Library Browser, we clickthe Gain block, and we copy and paste it twice. We flip the pasted Gain blocks by using theFlip Block command from the Format drop menu, and we label these as Gain 2 and Gain 3.Finally, we doubleclick these gain blocks and in the Function Block Parameters dialog box,we change the gains in Gain 2 and Gain 3 blocks from unity to 4 and 3 as shown in Figure1.12.

    Figure 1.12. File Equation_1_26 complete block diagram

    8. The initial conditions , and are entered by doubleclicking the Integrator blocks and entering the values for the first integrator, and for thesecond integrator. To obtain a true picture of the output (voltage across the capacitor), we

    iL 0( ) CdvCdt--------- t 0=

    0= = vc 0( ) 0.5 V=

    0 0.5

  • Chapter 1 Introduction to Simulink

    112 Introduction to Simulink with Engineering Applications, Second Edition Copyright Orchard Publications

    doubleclick the Unit block and in the Source Block Parameters window we change theStep time value from 1 to 0. We leave all other parameters in their default state. We also needto specify the simulation time. This is done by specifying the simulation time to be secondson the Configuration Parameters from the Simulation drop menu. We can start the simula-

    tion on Start from the Simulation drop menu or by clicking the icon.

    9. To see the output waveform, we double click the Scope block, and then clicking on the

    Autoscale icon. Then we rightclick near the vertical axis, we click on Axes properties,we specify Ymin =0, Ymax = 1.5, we click OK, and we obtain the waveform shown in Fig-ure 1.13. Henceforth, we will use this procedure to scale the vertical axis in our subsequentScope block displays.

    Figure 1.13. The waveform for the function in Example 1.1

    Another easier method to obtain and display the output for Example 1.1, is to use StateSpace block from Continuous in the Simulink Library Browser, as shown in Figure 1.14.

    Figure 1.14. Obtaining the function for Example 1.1 with the StateSpace block.

    10

    vC t( )vC t( )

    vC t( )

  • Introduction to Simulink with Engineering Applications, Second Edition 113Copyright Orchard Publications

    Simulink and its Relation to MATLAB

    The simout To Workspace block shown in Figure 1.14 writes its input to the workspace. In thisexample, we have assigned the name Example_1_1 to it, and Simulink appends it with the .matextension. As we know from our MATLAB studies, the data and variables created in the MAT-LAB Command window, reside in the MATLAB Workspace. This block writes its output to anarray or structure that has the name specified by the block's Variable name parameter. It is highlyrecommended that this block is included in the saved model. This gives us the ability to delete ormodify selected variables at a later time. To see what variables reside in the MATLAB Work-space, we issue the command who or whos.*

    From Equation 1.23,

    The output equation is

    or

    We doubleclick the StateSpace block, and in the Functions Block Parameters window weenter the constants shown in Figure 1.15.

    Figure 1.15. The Function block parameters for the StateSpace block.

    * who displays only the variables names, not the function to which each variable belongs. whos listsmore information about each variable.

    x 1x 2

    4 43 4 0

    x1x2

    40

    u0 t( )+=

    y Cx du+=

    y 0 1[ ] x1x2

    0[ ]u+=

  • Chapter 1 Introduction to Simulink

    114 Introduction to Simulink with Engineering Applications, Second Edition Copyright Orchard Publications

    The initials conditions are specified at the MATLAB command prompt as

    x1=0; x2=0.5;

    As before, to start the simulation we click the icon, and to see the output waveform, we dou-

    ble click the Scope block. Then we click on the Autoscale icon, and we scale the verticalaxis as we did with the waveform of Figure 1.13. The waveform shown in Figure 1.16.

    Figure 1.16. The waveform for the function for Example 1.1 with the StateSpace block.

    The statespace block is the best choice when we need to display the output waveform of three ormore variables as illustrated by the following example.

    Example 1.2 A fourthorder network is described by the differential equation

    (1.27)

    where is the output representing the voltage or current of the network, and is any input,and the initial conditions are .

    a. We will express (1.27) as a set of state equations

    x1 x2[ ]'

    vC t( )

    d 4ydt4--------- a3

    d 3ydt3--------- a2

    d2ydt2-------- a1

    dydt------ a0 y t( )+ + + + u t( )=

    y t( ) u t( )y 0( ) y' 0( ) y'' 0( ) y''' 0( ) 0= = = =

  • Introduction to Simulink with Engineering Applications, Second Edition 115Copyright Orchard Publications

    Simulink and its Relation to MATLAB

    b. It is known that the solution of the differential equation

    (1.28)

    subject to the initial conditions , has the solution

    (1.29)

    In our set of state equations, we will select appropriate values for the coefficients so that the new set of the state equations will represent the differential equa-

    tion of (1.28) and using Simulink, we will display the waveform of the output .

    1. The differential equation of (1.28) is of fourthorder; therefore, we must define four state vari-ables that will be used with the four firstorder state equations. We denote the state variables as , and , and we relate them to the terms of thegiven differential equation as

    (1.30)

    We observe that

    (1.31)

    and in matrix form

    (1.32)

    In compact form, (1.32) is written as(1.33)

    Also, the output is(1.34)

    where

    d4ydt4-------- 2d

    2ydt2-------- y t( )+ + tsin=

    y 0( ) y' 0( ) y'' 0( ) y''' 0( ) 0= = = =y t( ) 0.125 3 t2( ) 3t tcos[ ]=

    a3 a2 a1 and a0, , ,y t( )

    x1 x2 x3, , x4

    x1 y t( )= x2 dydt------= x3d 2ydt2---------= x4

    d 3ydt3---------=

    x 1 x2=x 2 x3=x 3 x4=

    d 4ydt4--------- x 4 a0x1 a1x2 a2x3 a3x4 u t( )+= =

    x 1x 2x 3x 4

    0 1 0 00 0 1 00 0 0 1a0 a1 a2 a3

    x1x2x3x4

    0001

    u t( )+=

    x Ax bu+=

    y Cx du+=

  • Chapter 1 Introduction to Simulink

    116 Introduction to Simulink with Engineering Applications, Second Edition Copyright Orchard Publications

    (1.35)

    and since the output is defined as

    relation (1.34) is expressed as

    (1.36)

    2. By inspection the differential equation of (1.27) will be reduced to the differential equation of(1.28) if we let

    and thus the differential equation of (1.28) can be expressed in statespace form as

    (1.37)

    where

    (1.38)

    Since the output is defined as

    in matrix form it is expressed as

    x

    x 1x 2x 3x 4

    = A

    0 1 0 00 0 1 00 0 0 1a0 a1 a2 a3

    = x

    x1x2x3x4

    = b

    0001

    and u,=, , , u t( )=

    y t( ) x1=

    y 1 0 0 0[ ]x1x2x3x4

    0[ ]u t( )+=

    a3 0= a2 2= a1 0= a0 1= u t( ) tsin=

    x 1x 2x 3x 4

    0 1 0 00 0 1 00 0 0 1a0 0 2 0

    x1x2x3x4

    0001

    tsin+=

    x

    x 1x 2x 3x 4

    = A

    0 1 0 00 0 1 00 0 0 1a0 0 2 0

    = x

    x1x2x3x4

    = b

    0001

    and u,=, , , tsin=

    y t( ) x1=

  • Introduction to Simulink with Engineering Applications, Second Edition 117Copyright Orchard Publications

    Simulink and its Relation to MATLAB

    (1.39)

    We invoke MATLAB, we start Simulink by clicking on the Simulink icon, on the SimulinkLibrary Browser, we click the Create a new model (blank page icon on the left of the topbar), and we save this model as Example_1_2. On the Simulink Library Browser we selectSources, we drag the Signal Generator block on the Example_1_2 model window, we clickand drag the StateSpace block from the Continuous on Simulink Library Browser, and weclick and drag the Scope block from the Commonly Used Blocks on the Simulink LibraryBrowser. We also add the Display block found under Sinks on the Simulink LibraryBrowser. We connect these four blocks and the complete block diagram is as shown in Figure1.17.

    Figure 1.17. Model for Example 1.2 with the entries specified below

    We now doubleclick the Signal Generator block and we enter the following in the FunctionBlock Parameters dialog box the following:

    Wave form: sine

    Time (t): Use simulation time

    Amplitude: 1

    Frequency: 2

    Units: Hertz

    Next, we doubleclick the StateSpace block and we enter the following parameter values inthe Function Block Parameters:

    A: [0 1 0 0; 0 0 1 0; 0 0 0 1; a0 a1 a2 a3]B: [0 0 0 1]

    C: [1 0 0 0]

    D: [0]

    Initial conditions: x0

    Absolute tolerance: auto

    y 1 0 0 0[ ]x1x2x3x4

    0[ ] tsin+=

  • Chapter 1 Introduction to Simulink

    118 Introduction to Simulink with Engineering Applications, Second Edition Copyright Orchard Publications

    Now, we switch to the MATLAB Command window and at the command prompt we type thefollowing values:

    a0=1; a1=0; a2=2; a3=0; x0=[0 0 0 0];

    We change the Simulation Stop time to , and we start the simulation by clicking on the icon. To see the output waveform, we doubleclick the Scope block, then we click theAutoscale icon, and we obtain the waveform shown in Figure 1.18.

    Figure 1.18. Waveform for Example 1.2

    The Display block in Figure 1.17 shows the value at the end of the simulation stop time.

    Examples 1.1 and 1.2 have clearly illustrated that the StateSpace is indeed a powerful block. Wecould have also obtained the solution of Example 1.2 using four Integrator blocks.

    Example 1.3 We will create a model that will produce the simultaneous solution of three equations with threeunknowns using Algebraic Constraint blocks found in the Math Operations library, Displayblocks found in the Sinks library, and Gain blocks found in the Commonly Used Blocks library.

    The model will display the values for the unknowns , , and for the system of the equations

    (1.40)

    The model is shown in Figure 1.19.

    25

    z1 z2 z3

    a1z1 a2z2 a3z3 k1+ + + 0=

    a4z1 a5z2 a6z3 k2+ + + 0=

    a7z1 a8z2 a9z3 k3+ + + 0=

  • Introduction to Simulink with Engineering Applications, Second Edition 119Copyright Orchard Publications

    Simulink and its Relation to MATLAB

    Figure 1.19. Model for Example 1.3 with the entries specified below

    Next, at the MATLAB command prompt we enter the following values:

    a1=2; a2=3; a3=1; a4=1; a5=5; a6=4; a7=6; a8=1; a9=2;...k1=8; k2=7; k3=5;After clicking on the simulation icon, we obtain the values of the unknowns as , ,and as shown in the Display blocks in Figure 1.19.

    An Algebraic Constraint block constrains the input signal to zero, outputs a value for ,and this value eventually produces a zero at the input. Thus, output is fed back to the input via afeedback path. We can improve the efficiency of the algebraic loop solver by providing an initialguess for the algebraic state that is close to the final solution value. By default, the initial guessvalue is zero.

    An outstanding feature in Simulink is the representation of a large model consisting of manyblocks and lines, to be shown as a single Subsystem block.

    z1 2= z2 3=

    z3 5=

    f z( ) z

    z

  • Chapter 1 Introduction to Simulink

    120 Introduction to Simulink with Engineering Applications, Second Edition Copyright Orchard Publications

    For instance, to group all blocks and lines in the model of Figure 1.19 except the display blocks,from the Edit drop menu we choose Create Subsystem and this model will be shown as in Figure1.20* where at the MATLAB command prompt we have entered the following values:

    a1=5; a2=1; a3=4; a4=11; a5=6; a6=9; a7=8; a8=4; a9=15;...k1=14; k2=6; k3=9;

    Figure 1.20. The model in Figure 1.19 represented as a subsystem

    The Display blocks in Figure 1.20 show the values of , , and for the values that we speci-fied at the MATLAB command prompt above.

    The Subsystem block is described in detail in Chapter 2, Section 2.1, Page 22.1.2 Simulink DemosAt this time, the reader with no prior knowledge of Simulink, should be ready to learn Simulinksadditional capabilities. We will explore other features in the subsequent chapters. However, it ishighly recommended that the reader becomes familiar with the block libraries found in the Sim-ulink Library Browser. Then, the reader can follow the steps delineated in The MathWorks Sim-ulink Users Manual to run the Demo Models beginning with the thermo model. This model caninvoked by typing thermo at the MATLAB command prompt.

    In the subsequent chapters, we will study each of the blocks under each of libraries in the TreePane. They are listed in Table 1.1 below in alphabetical order, the library where they appear, thechapter where they are described in this text, section/subsection, and page number in which theyare described.

    * The contents of the Subsystem block are not lost. We can doubleclick on the Subsystem block to see its con-tents. The Subsystem block replaces the inputs and outputs of the model with Inport and Outport blocks. Theseblocks along with the Subsystem block are described in Section 2.1, Chapter 2, Page 22.

    z1 z2 z3

  • Introduction to Simulink with Engineering Applications, Second Edition 121Copyright Orchard Publications

    Simulink Demos

    TABLE 1.1 Simulink blocks Block Name Library Chapter Section/Subsection Page

    Abs Math Operations Group 8 8.1.14 810Add Math Operations Group 8 8.1.2 82Algebraic Constraint Math Operations Group 8 8.1.23 818Assertion Model Verification 9 9.9 912Assignment Vector / Matrix Operations 8 8.2.1 819Atomic Subsystem Ports & Subsystems 11 11.5 114Averaging Power Spectral Density

    Simulink Extras 19 19.3.2 1937Averaging Spectrum Analyzer Simulink Extras 19 19.3.2 1941Auto Correlator Simulink Extras 19 19.3.6 1945Backlash Discontinuities 4 4.7 49BandLimited White Noise Signal Generators 15 15.2.12 1517Bias Math Operations Group 8 8.1.5 84Bit Clear Bit Operations Group 6 6.2.2 613Bit Set Bit Operations Group 6 6.2.1 612Bitwise Operator Bit Operations Group 6 6.2.3 614Block Support Table Modeling Guides 10 10.3 109Bus Assignment Signal Routing Group 13 13.1.3 132Bus Creator Commonly Used blocks 2 2.6 28Bus Selector Commonly Used blocks 2 2.6 28Bus-to-Vector Signal Attrinutes 12 12.1.10 1212Cartesian to Polar Simulink Extras 19 19.6.2 1960Cartesian to Spherical Simulink Extras 19 19.6.4 1962Celsius to Fahrenheit Simulink Extras 19 19.6.6 1964Check Discrete Gradient Model Verification 9 9.10 913Check Dynamic Gap Model Verification 9 9.8 910Check Dynamic Lower Bound Model Verification 9 9.5 96Check Dynamic Range Model Verification 9 9.7 99Check Dynamic Upper Bound Model Verification 9 9.6 98Check Input Resolution Model Verification 9 9.11 914Check Static Gap Model Verification 9 9.4 95Check Static Lower Bound Model Verification 9 9.1 92Check Static Range Model Verification 9 9.3 94Check Static Upper Bound Model Verification 9 9.2 93Chirp Signal Signal Generators 15 15.2.9 1514Clock Signal Generators

    Simulink Extras1519

    15.2.1719.4.1

    15261948

    CodeReuse Subsystem Ports & Subsystems 11 11.6 119Combinational Logic Logic Operations Group 6 6.1.5 64Compare To Constant Logic Operations Group 6 6.1.7 610

  • Chapter 1 Introduction to Simulink

    122 Introduction to Simulink with Engineering Applications, Second Edition Copyright Orchard Publications

    (cont)Block Name Library Chapter Section/Subsection Page

    Compare To Zero Logic Operations Group 6 6.1.6 69Complex to MagnitudeAngle Complex Vector Conversions Group 8 8.3.1 826Complex to RealImag Complex Vector Conversions Group 8 8.3.3 828Configurable Subsystem Ports & Subsystems 11 11.8 1119Constant Commonly Used blocks 2 2.4 26Cosine Lookup Tables 7 7.8 717Coulomb and Viscous Friction Discontinuities 4 4.11 414Cross Correlator Simulink Extras 19 19.3.5 1943Counter FreeRunning Signal Generators 15 15.2.16 1524Counter Limited Signal Generators 15 15.2.15 1525D Flip Flop Simulink Extras 19 19.4.4 1951D Latch Simulink Extras 19 19.4.2 1949Data Store Memory Signal Storage and Access Group 13 13.2.2 1319Data Store Read Signal Storage and Access Group 13 13.2.1 1318Data Store Write Signal Storage and Access Group 13 13.2.3 1319Data Type Conversion Commonly Used blocks 2 2.17 232Data Type Conversion Inherited

    Signal Attribute Manipulation 12 12.1.5 125

    Data Type Duplicate Signal Attribute Manipulation 12 12.1.2 122Data Type Propagation Signal Attribute Manipulation 12 12.1.3 124Data Type Propagation Examples

    Signal Attribute Manipulation 12 12.1.10 1214

    Data Type Scaling Strip Signal Attribute Manipulation 12 12.1.4 125Dead Zone Discontinuities 4 4.3 44Dead Zone Dynamic Discontinuities 4 4.4 45Decrement Real World Increment / Decrement 18 18.2 183Decrement Stored Integer Increment / Decrement 18 18.4 185Decrement Time To Zero Increment / Decrement 18 18.6 187Decrement To Zero Increment / Decrement 18 18.5 186Degrees to Radians Simulink Extras 19 19.6.7 1965Demux Commonly Used blocks 2 2.7 212Derivative ContinuousTime Linear Systems 3 3.1.2 32Detect Change Edge Detection Group 6 6.3.3 621Detect Decrease Edge Detection Group 6 6.3.2 620Detect Fall Negative Edge Detection Group 6 6.3.6 624Detect Fall Nonpositive Edge Detection Group 6 6.3.7 625Detect Increase Edge Detection Group 6 6.3.1 618Detect Rise Nonnegative Edge Detection Group 6 6.3.5 623

    TABLE 1.1 Simulink blocks

  • Introduction to Simulink with Engineering Applications, Second Edition 123Copyright Orchard Publications

    Simulink Demos

    (cont)Block Name Library Chapter Section/Subsection Page

    Detect Rise Positive Edge Detection Group 6 6.3.4 622Difference DiscreteTime Linear Systems 5 5.1.8 59Digital Clock Signal Generators 15 15.2.18 1527Direct Lookup Table (nD) Lookup Tables 7 7.6 710Discrete Derivative DiscreteTime Linear Systems 5 5.1.9 510Discrete Filter DiscreteTime Linear Systems 5 5.1.6 55Discrete FIR Filter DiscreteTime Linear Systems 5 5.1.14 5-19Discrete StateSpace DiscreteTime Linear Systems 5 5.1.10 511Discrete Time Integrator Commonly Used blocks 2 2.16 229Discrete Transfer Fcn DiscreteTime Linear Systems 5 5.1.5 54Discrete Transfer Fcn (with initial outputs)

    Simulink Extras 19 19.1.2 19-5

    Discrete Transfer Fcn (with initial states)

    Simulink Extras 19 19.1.1 19-2

    Discrete ZeroPole DiscreteTime Linear Systems 5 5.1.7 58Discrete ZeroPole (with initial outputs)

    Simulink Extras 19 19.1.4 19-12

    Discrete ZeroPole (with initial states)

    Simulink Extras 19 19.1.3 19-8

    Display Data Viewers 14 14.2.4 1416Divide Math Operations Group 8 8.1.10 87Doc Text (DocBlock) Documentation 10 10.2.2 109Dot Product Math Operations Group 8 8.1.12 88Embedded MATLAB Function

    UserDefined Functions 16 16.3 163

    Enable Ports & Subsystems 11 11.3 112Enabled and Triggered Subsystem

    Ports & Subsystems 11 11.11 1130

    Enabled Subsystem Ports & Subsystems 11 11.10 1127Environment Controller Signal Routing Group 13 13.1.9 1310Extract Bits Bit Operations Group 6 6.2.5 617Fahrenheit to Celsius Simulink Extras 19 19.6.5 1963Fcn UserDefined Functions 16 16.1 162FirstOrder Hold Sample & Hold Delays 5 5.2.2 522FixedPoint StateSpace Additional Discrete 17 17.3 174Floating Bar Plot Simulink Extras 19 19.3.7 1946Floating Scope Data Viewers 14 14.2.2 148For Iterator Subsystem Ports & Subsystems 11 11.13 1137From Signal Routing Group 13 13.1.13 1314From File Models and Subsystems Inputs 15 15.1.3 152

    TABLE 1.1 Simulink blocks

  • Chapter 1 Introduction to Simulink

    124 Introduction to Simulink with Engineering Applications, Second Edition Copyright Orchard Publications

    (cont)Block Name Library Chapter Section/Subsection Page

    From Workspace Models and Subsystems Inputs 15 15.1.4 152FunctionCall Generator Ports & Subsystems 11 11.4 113FunctionCall Subsystem Ports & Subsystems 11 11.12 1134Gain Commonly Used blocks 2 2.10 218Goto Signal Routing Group 13 13.1.15 1316Goto Tag Visibility Signal Routing Group 13 13.1.14 1315Ground Commonly Used blocks 2 2.2 24Hit Crossing Discontinuities 4 4.10 413IC (Initial Condition) Signal Attribute Manipulation 12 12.1.6 126If Ports & Subsystems 11 11.15 1141If Action Subsystem Ports & Subsystems 11 11.15 1141Increment Real World Increment / Decrement 18 18.1 182Increment Stored Integer Increment / Decrement 18 18.3 184Index Vector Signal Routing Group 13 13.1.7 138Inport Commonly Used blocks 2 2.1 22Integer Delay DiscreteTime Linear Systems 5 5.1.2 52Integrator Commonly Used blocks 2 2.14 222Interpolation (nD) Using PreLookup

    Lookup Tables 7 7.5 78Interval Test Logic Operations Group 6 6.1.3 62Interval Test Dynamic Logic Operations Group 6 6.1.4 63JK Flip Flop Simulink Extras 19 19.4.5 1952Level2 MFile SFunction UserDefined Functions 16 16.5 167Logical Operator Commonly Used blocks 2 2.12 220Lookup Table Lookup Tables 7 7.1 72Lookup Table (2D) Lookup Tables 7 7.2 73Lookup Table (nD) Lookup Tables 7 7.3 76Lookup Table Dynamic Lookup Tables 7 7.7 716MagnitudeAngle to Complex Complex Vector Conversions Group 8 8.3.2 827Manual Switch Signal Routing Group 13 13.1.10 1312Math Function Math Operations Group 8 8.1.16 811MATLAB Fcn UserDefined Functions 16 16.2 163Matrix Concatenate Vector / Matrix Operations 8 8.2.3 823Memory Sample & Hold Delays 5 5.2.1 521Merge Signal Routing Group 13 13.1.8 138MinMax Math Operations Group 8 8.1.19 814MinMax Running Resettable Math Operations Group 8 8.1.20 815

    TABLE 1.1 Simulink blocks

  • Introduction to Simulink with Engineering Applications, Second Edition 125Copyright Orchard Publications

    Simulink Demos

    (cont)Block Name Library Chapter Section/Subsection Page

    Model Ports & Subsystems 11 11.7 1117Model Info Documentation 10 10.2.1 107Multiport Switch Signal Routing Group 13 13.1.11 1313Mux Commonly Used blocks 2 2.7 212Outport Commonly Used blocks 2 2.1 22Permute Math Operations Group 8 8.2.6 825PID Controller Simulink Extras 19 19.2.6 1929PID Controller (with approximate derivative)

    Simulink Extras 19 19.2.7 1931Polar to Cartesian Simulink Extras 19 19.6.1 1959Polynomial Math Operations Group 8 8.1.18 814Power Spectral Density Simulink Extras 19 19.3.1 1933Prelookup Index Search Lookup Tables 7 7.4 77Probe Signal Attribute Detection 12 12.2.1 1217Product Commonly Used blocks 2 2.4 26Product of Elements Math Operations Group 8 8.1.11 87Pulse Generator Signal Generators 15 15.2.3 155Quantizer Discontinuities 4 4.9 412Radians to Degrees Simulink Extras 19 19.6.8 1965Ramp Signal G