ebook img

Basic MATLAB®, Simulink®, and Stateflow® PDF

445 Pages·2007·14.125 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Basic MATLAB®, Simulink®, and Stateflow®

w w Basic MATLAB , Simulink , w and Stateflow Richard Colgren The University of Kansas Lawrence, Kansas EDUCATION SERIES Joseph A.Schetz Series Editor-in-Chief VirginiaPolytechnic Instituteand State University Blacksburg,Virginia Publishedby AmericanInstituteofAeronauticsandAstronautics,Inc. 1801AlexanderBellDrive,Reston,VA20191-4344 MATLABw,Simulinkw,Statefloww,HandleGraphicsw,Real-TimeWorkshopw,andxPCTargetboxw areregisteredtrademarksofTheMathWorks,Inc. AmericanInstituteofAeronauticsandAstronautics,Inc.,Reston,Virginia 1 2 3 4 5 LibraryofCongressCataloging-in-PublicationData Colgren,RichardD.(RichardDean). BasicMATLAB,Simulink,andStateflow/RichardColgren. p.cm.--(Educationseries) ISBN-13:978-1-56347-838-3 ISBN-10:1-56347-838-2(hardcover:alk.paper) 1. MATLAB.2. SIMULINK.3. Stateflow.4. Computersimulation—Computer programs.5. Engineeringmathematics.6. Aeronautics—Mathematics. I.Title. TA345.C5982007 620.001013--dc22 2006101307 Copyright2007bytheAmericanInstituteofAeronauticsandAstronautics,Inc.Allrightsreserved. PrintedintheUnitedStatesofAmerica.Nopartofthispublicationmaybereproduced,distributed, ortransmitted,inanyformorbyanymeans,orstoredinadatabaseorretrievalsystem,withoutthe priorwrittenpermissionofthepublisher. Dataandinformationappearinginthisbookareforinformationalpurposesonly.AIAAisnotrespon- sibleforanyinjuryordamageresultingfromtheuseorreliance,nordoesAIAAwarrantthattheuseor reliancewillbefreefromprivatelyownedrights. Main AIAA Web Site Programs README File February 2007 The folder, Main AIAA Web Site Programs, contains files that are provided to assist the user in replicating the problems and examples covered in each chapter of the book, Basic MATLAB, Simulink, and Stateflow. The files are arranged by chapter number. These files were created using MATLAB™ Version 7.3.0.267 (R2006b). MATLAB License Number: DEMO Operating System: Microsoft Windows XP Version 5.1 (Build 2600: Service Pack 2) Java VM Version: Java 1.5.0 with Sun Microsystems Inc. Java Hotspot™ Client VM mixed mode. Copyright © 2007 by the American Institute of Aeronautics and Astronautics, Inc. Preface Thisbookisbasedonmaterialsdevelopedduringmorethan22yearsofteaching w w w MATLAB ,Simulink ,andStateflow inavarietyofformatstoadiverserange of audiences. Most of these courses required little to no background in any of these tools from these students. The book can be used for self-instruction on all three of these topics. All of these tools are relatively easy to use once the basics are understood. The hands-on approach taken in this book is designed to provide the user with just such a background. This book is in no way meant to be comprehensive in its coverage of these three tool sets. A comprehensive book on MATLAB was possible 20 years ago. However, with the vast number of toolboxes and model libraries available today, a comprehensive coverage of these three subjects would require a bookshelf,notasingle book. Whenusedasaclassroomtext,thisbookisformattedtosupportaMATLAB/ Simulink/Stateflow course designed to take a total of approximately 40 hours, including in-class exercises. However, the course is designed to be modular and thus flexible for use in a variety of teaching and time formats. As a class, this course is best offered within a computer laboratory environment, with the studentsworking inreal time on examplesalongwith the instructor. NotethatallthematerialscoveredwithinthisbookweregeneratedusingVersion 7.3 of MATLAB (Release 2006b) and all associated toolbox versions. The Math- Worksisnowsupportingatwice-yearlyreleaseschedule,witheachServicePackpro- viding minor upgrades and some new features. These added improvements are designed to have little effect on the vast majority of capabilities offered by this largefamilyofanalysistoolsandthusarerelativelyseamlesstotheuser. Theformatrecommendedforthiscourseasofferedwithinacomputerlabora- tory environment isasfollows. 1) A lecture is given on a MATLAB, Simulink, or Stateflow topic from the appropriate chapter in this book. The students work through the materials on theircomputerswhiletheinstructorsimilarlyworksthroughthelecturematerials. 2)Thestudentsworkthroughanexercisegivenattheendofthechapterafter the lecture. 3)Anappropriatebreakisgivenattheendoftheexercise.Studentscomplet- ingthe exercise early may work onotherside topics. 4)Afterwardtheinstructorassignsexercisesorhomeworkforthestudentsto complete. RichardColgren January2007 xiii Table of Contents Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv w Basic MATLAB w Chapter 1. Introduction toMATLAB . . . . . . . . . . . . . . . . . . . . . 3 1.1 Introduction and Objectives. . . . . . . . . . . . . . . . . . . . . . . . . 3 1.2 Entry . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 1.3 Transpose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 1.4 Addition andSubtraction. . . . . . . . . . . . . . . . . . . . . . . . . . . 5 1.5 Multiplication. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 1.6 Division . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 1.7 Formats. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 1.8 MatrixFunctions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 1.9 Colon Operator. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 1.10 Useful Interface GUIs. . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 1.11 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 Chapter 2. Plottingand Graphics . . . . . . . . . . . . . . . . . . . . . . . 43 2.1 Introduction and Objectives. . . . . . . . . . . . . . . . . . . . . . . . 43 2.2 Plot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 2.3 Log and Semilog Plots . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 2.4 Polar Plots. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 2.5 Subplots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 2.6 Axis. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 2.7 Mesh . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 2.8 Contour Diagrams. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 2.9 FlowDiagrams. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 2.10 Movies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 2.11 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 ix x TABLE OF CONTENTS w Chapter 3. Introduction to MATLAB Toolboxes . . . . . . . . . . . . . 61 3.1 Introductionand Objectives. . . . . . . . . . . . . . . . . . . . . . . . 61 3.2 ContinuousTransferFunctions. . . . . . . . . . . . . . . . . . . . . . 61 3.3 Root Locus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 3.4 Stepand Impulse Responses . . . . . . . . . . . . . . . . . . . . . . . 64 3.5 Bode Plot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 3.6 Nichols Chart. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 3.7 Nyquist Chart. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 3.8 Linear Quadratic Regulator . . . . . . . . . . . . . . . . . . . . . . . . 69 3.9 State-Space Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 3.10 DigitalDesign . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 3.11 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 w Chapter 4. Introduction to MATLAB Cells, Structures, and M-Files 95 4.1 Introductionand Objectives. . . . . . . . . . . . . . . . . . . . . . . . 95 4.2 Cells . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 4.3 Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 4.4 M-Files. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98 4.5 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102 w Chapter 5. HandleGraphics and User Interfaces . . . . . . . . . . . . 105 5.1 Introductionand Objectives. . . . . . . . . . . . . . . . . . . . . . . 105 w 5.2 Handle Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 5.3 Graphical UserInterface Development Environment . . . . . . . 112 5.4 LayoutEditor. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 5.5 PropertyInspector. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 5.6 MenuEditor. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124 5.7 Compiling aStand-AloneExecutable. . . . . . . . . . . . . . . . . 125 5.8 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126 w Chapter 6. Introduction to MATLAB MEX-Files . . . . . . . . . . . . 129 6.1 Introductionand Objectives. . . . . . . . . . . . . . . . . . . . . . . 129 6.2 DynamicallyLinked Subroutines: MEX-Files. . . . . . . . . . . . 129 w 6.3 MATLAB EngineLibrary. . . . . . . . . . . . . . . . . . . . . . . 142 6.4 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148 w Basic Simulink w w Chapter 7. Brief Introduction toSimulink and Stateflow . . . . . . 153 7.1 Introductionand Objectives. . . . . . . . . . . . . . . . . . . . . . . 153 w 7.2 Simulink . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153 7.3 Van derPolEquation. . . . . . . . . . . . . . . . . . . . . . . . . . . 153 7.4 Conditional System Model . . . . . . . . . . . . . . . . . . . . . . . 158 TABLE OF CONTENTS xi w w 7.5 Combined Simulink andStateflow Systems. . . . . . . . . . . 161 7.6 Model Comparison . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166 7.7 F-14 ControlSystem . . . . . . . . . . . . . . . . . . . . . . . . . . . 169 7.8 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172 W Chapter 8. Introduction toSimulink . . . . . . . . . . . . . . . . . . . . . 175 8.1 Introduction and Objectives. . . . . . . . . . . . . . . . . . . . . . . 175 w 8.2 Standard Simulink Libraries. . . . . . . . . . . . . . . . . . . . . . 175 w 8.3 Simulink Aerospace Blockset. . . . . . . . . . . . . . . . . . . . . 191 w 8.4 Simulink Installation and Demonstrations . . . . . . . . . . . . . 194 8.5 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205 w Chapter 9. Buildinga SimpleSimulink Model . . . . . . . . . . . . . . 209 9.1 Introduction and Objectives. . . . . . . . . . . . . . . . . . . . . . . 209 9.2 Population Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209 9.3 Analyzing the Population Model. . . . . . . . . . . . . . . . . . . . 238 9.4 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239 w Chapter 10. Building Simulink Linear Models . . . . . . . . . . . . . . 243 10.1 Introduction and Objectives . . . . . . . . . . . . . . . . . . . . . . 243 w 10.2 TransferFunction ModelinginSimulink . . . . . . . . . . . . . 243 10.3 Zero-Pole Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254 10.4 State-Space Model. . . . . . . . . . . . . . . . . . . . . . . . . . . . 256 10.5 Conclusion. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258 Chapter 11. LTI Viewer and SISO Design Tool . . . . . . . . . . . . . . 261 11.1 Introduction and Objectives . . . . . . . . . . . . . . . . . . . . . . 261 w 11.2 Introduction tothe Simulink LTI Viewer. . . . . . . . . . . . . 261 w 11.3 Using the Simulink LTI Viewer. . . . . . . . . . . . . . . . . . . 262 w 11.4 Equivalent Simulink LTI Models. . . . . . . . . . . . . . . . . . 283 11.5 SISODesign Tool . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285 11.6 Conclusion. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293 Chapter 12. Buildinga Multiple-Input,Multiple-Output w Simulink Model . . . . . . . . . . . . . . . . . . . . . . . . . . 297 12.1 Introduction and Objectives . . . . . . . . . . . . . . . . . . . . . . 297 w 12.2 System Modeling inSimulink . . . . . . . . . . . . . . . . . . . . 297 12.3 Parameter Estimation . . . . . . . . . . . . . . . . . . . . . . . . . . 297 w 12.4 MATLAB Simulation Interface. . . . . . . . . . . . . . . . . . . 315 12.5 Subsystems,Masking, andLibraries. . . . . . . . . . . . . . . . . 316 12.6 Vector Signals. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323 12.7 Using Vector Signals forMath Functions . . . . . . . . . . . . . 325 12.8 Conclusion. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329 xii TABLE OF CONTENTS w Chapter 13. Building Simulink S-Functions . . . . . . . . . . . . . . . . 335 13.1 IntroductionandObjectives . . . . . . . . . . . . . . . . . . . . . . 335 w 13.2 Simulink S-Functions. . . . . . . . . . . . . . . . . . . . . . . . . 335 w 13.3 Simulink C andS-Function Example, Van derPolEquation . . . . . . . . . . . . . . . . . . . . . . . . . . 336 w 13.4 Simulink C andS-Function BuilderExample, VanderPolEquation. . . . . . . . . . . . . . . . . . . . . . . . . 342 13.5 Exampleof a FORTRAN S-Function. . . . . . . . . . . . . . . . 352 13.6 Exampleof a CMEX S-FunctionGateway. . . . . . . . . . . . . 354 w 13.7 Simulink BlockDiagram Using S-Function. . . . . . . . . . . 358 13.8 Conclusion. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 360 w Basic Stateflow w Chapter 14. Introduction to Stateflow . . . . . . . . . . . . . . . . . . . . 369 14.1 IntroductionandObjectives . . . . . . . . . . . . . . . . . . . . . . 369 w 14.2 Opening, Executing, and Saving Stateflow Models . . . . . . 369 w 14.3 Constructing aSimpleStateflow Model. . . . . . . . . . . . . . 376 w 14.4 Using aStateflow TruthTable. . . . . . . . . . . . . . . . . . . . 410 14.5 Conclusion. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 421 w Appendix A. Historyof MATLAB and The MathWorks,Inc. . . . . 425 W W Appendix B. TuningMATLAB , Simulink , and W Stateflow Solvers . . . . . . . . . . . . . . . . . . . . . . . . . 429 B.1 ImprovingSimulation Performance and Accuracy. . . . . . . . . 429 B.2 Selecting Solvers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 432 B.3 Non-Real-Time and Real-Time Simulations. . . . . . . . . . . . . 436 w w w Appendix C. MATLAB ,Simulink , and Stateflow QuickReferenceGuide . . . . . . . . . . . . . . . . . . . . . 437 Bibliography . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 477 SupportingMaterials. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 485 1 w Introduction to MATLAB 1.1 Introduction and Objectives w MATLAB is a high-performance language for technical computing. It integrates computation, visualization, and programming in an easy-to-use environmentwhereproblemsandsolutionsareexpressedinfamiliarmathemat- ical notation. This chapter introduces some of the basic matrix computational tools and graphical user interfaces (GUIs) that are available in MATLAB through the main Command Window. Upon completion of this chapter, the reader will be able to 1) identify some basic computational tools and commands in MATLAB; 2) identify the various components of the MATLAB GUI; 3) input commands into the Command Window; 4) obtain help information using on-line help utilities, local contacts, users groups,and The MathWorksWeb site and help lines. OnWindowsplatforms,tostartMATLAB,double-clicktheMATLABshort- cut icon on your Windowsdesktop(see Fig. 1.1). Fig.1.1 OnUNIXplatforms,tostartMATLAB,typematlabattheoperatingsystem prompt. After you have started the MATLAB program, the standard main MATLAB interface window appearsasshown inFig.1.2. Youcanchangethewayyourdesktoplooksbyopening,closing,moving,and resizing the tools on it. Use the View menu toopen or close the tools. You can alsomovetoolsoutsidethedesktopormovethembackontothedesktop.Allthe desktop tools provide common features such as context menus and keyboard shortcuts.Youcanspecifycertaincharacteristicsforthedesktoptoolsbyselect- ing Preferences from the File menu. For example, you can specify the font characteristics for Command Window text. For more information on this or any topic, click the Help button in the Preferences dialog box. 3 w w w 4 BASIC MATLAB , SIMULINK , AND STATEFLOW Fig.1.2 In the discussion to follow, the given commands will be entered using the Command Window. Statements you enter into the Command Window are logged into the Command History. In the Command History window, you can view previously run statements and copy and execute selected statements. You can also use the up and down arrows on your keyboard to place previous commands directly into the Command Window for execution. YoucanrunexternalprogramsfromtheMATLABCommandWindow.The exclamationpoint!indicatesthattherestoftheinputlineisacommandtotheoper- atingsystem.Thisisusefulforinvokingutilitiesorrunningotherprogramswithout quittingMATLAB.TheMATLABStartbutton provideseasyaccessto tools,demos,anddocumentation.Justclickonthebuttontoseetheoptions. MATLABfileoperationsusethecurrentdirectoryandthesearchpathasrefer- encepoints.Anyfileyouwanttorunmusteitherbeinthecurrentdirectoryoron thesearchpath.Aquickwaytovieworchangethecurrentdirectoryisbyusing the Current Directory field inthe desktoptoolbar. TheMATLABworkspaceconsistsofarraysormatricesgeneratedduringyour MATLABsessionandstoredinmemory.Youaddvariablestotheworkspaceby using functions, running M-files, andloadingsaved workspaces. 1.2 Entry To enter a matrix, spaces or commas are put between the elements. Semico- lons or returns are used to separate the rows. Note that semicolons at the end of a command suppress the echo print. Brackets are placed around the matrix data.Forexample, toenter a3-by-3matrix A, type (cid:2)A5[12 3;45 6;7 80]

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.