Introduction To Matlab For Engineers - CUHK CSE

Transcription

pal34870 ifc.qxd1/7/107:44 PMPage iNumbered Examples:Chapters One to EightNumber and TopicNumber and TopicChapter One4.7–11.1–11.6–1Volume of a circular cylinderPiston motionChapter �22.4–32.4–42.5–12.6–12.7–1Vectors and displacementAortic pressure modelTransportation route analysisCurrent and power dissipation inresistorsA batch distillation processMiles traveledHeight versus velocityManufacturing cost analysisProduct cost analysisEarthquake-resistant building designAn environmental databaseA student databaseChapter Three3.2–1Optimization of an irrigation channel4.9–14.9–2Chapter Five5.2–1Height and speed of a projectileSeries calculation with a for loopPlotting with a for loopData sortingFlight of an instrumented rocketSeries calculation with a while loopGrowth of a bank accountTime to reach a speci ed heightPlotting orbitsChapter rature dynamicsHydraulic resistanceEstimation of traf c owModeling bacteria growthBreaking strength and alloycompositionResponse of a biomedical instrumentChapter Seven7.1–17.2–17.2–27.3–1Chapter ��24.6–3Using the switch structure for calendarcalculationsA college enrollment model: Part IA college enrollment model: Part IIBreaking strength of threadMean and standard deviation of heightsEstimation of height distributionStatistical analysis and manufacturingtolerancesChapter Eight8.1–18.2–18.2–28.2–38.2–4The matrix inverse methodLeft division method with threeunknownsCalculations of cable tensionAn electric resistance networkEthanol production

pal34870 fm i-xii 1.qxd1/7/107:44 PMPage iNumbered Examples:Chapters Eight to ElevenNumber and –2An underdetermined set with threeequations and three unknownsA statically indeterminate problemThree equations in three unknowns,continuedProduction planningTraf c engineeringThe least-squares methodAn overdetermined setChapter ��1Velocity from an accelerometerEvaluation of Fresnel’s cosine integralDouble integral over a nonrectangularregionResponse of an RC circuitLiquid height in a spherical tankA nonlinear pendulum modelTrapezoidal pro le for a dc motorNumber and TopicChapter 210.5–110.6–1#Simulink solution of y 10 sin tExporting to the MATLAB workspace#Simulink model for y - 10y f (t)Simulink model of a two-masssuspension systemSimulink model of a rocket-propelledsledModel of a relay-controlled motorResponse with a dead zoneModel of a nonlinear pendulumChapter Eleven11.3–111.3–211.5–1Intersection of two circlesPositioning a robot armTopping the Green Monster

pal34870 fm i-xii 1.qxd1/7/107:44 PMPage iiiIntroduction to MATLAB for EngineersWilliam J. Palm IIIUniversity of Rhode IslandTM

pal34870 fm i-xii 1.qxd1/15/1011:41 AMPage ivTMINTRODUCTION TO MATLAB FOR ENGINEERS, THIRD EDITIONPublished by McGraw-Hill, a business unit of The McGraw-Hill Companies, Inc., 1221 Avenue of the Americas,New York, NY 10020. Copyright 2011 by The McGraw-Hill Companies, Inc. All rights reserved. Previouseditions 2005 and 2001. No part of this publication may be reproduced or distributed in any form or by anymeans, or stored in a database or retrieval system, without the prior written consent of The McGraw-HillCompanies, Inc., including, but not limited to, in any network or other electronic storage or transmission, orbroadcast for distance learning.Some ancillaries, including electronic and print components, may not be available to customers outside theUnited States.This book is printed on acid-free paper containing 10% postconsumer waste.1 2 3 4 5 6 7 8 9 0 DOC/DOC 1 0 9 8 7 6 5 4 3 2 1 0ISBN 978-0-07-353487-9MHID 0-07-353487-0Vice President & Editor-in-Chief: Martin LangeVice President, EDP: Kimberly Meriwether DavidGlobal Publisher: Raghu SrinivasanSponsoring Editor: Bill StenquistMarketing Manager: Curt ReynoldsDevelopment Editor: Lora NeyensSenior Project Manager: Joyce WattersDesign Coordinator: Margarite ReynoldsCover Designer: Rick D. NoelPhoto Research: John LelandCover Image: Ingram Publishing/AGE FotostockProduction Supervisor: Nicole BaumgartnerMedia Project Manager: Joyce WattersCompositor: MPS Limited, A Macmillan CompanyTypeface: 10/12 Times RomanPrinter: RRDonnellyAll credits appearing on page or at the end of the book are considered to be an extension of the copyright page.Library of Congress Cataloging-in-Publication DataPalm, William J. (William John), 1944–Introduction to MATLAB for engineers / William J. Palm III.—3rd ed.p. cm.Includes bibliographical references and index.ISBN 978-0-07-353487-91. MATLAB. 2. Numerical analysis—Data processing. I. Title.QA297.P33 2011518.0285—dc222009051876www.mhhe.com

pal34870 fm i-xii 1.qxd1/7/107:44 PMPage vTo my sisters, Linda and Chris, and to my parents, Lillian and William

pal34870 fm i-xii 1.qxd1/7/107:44 PMPage viABOUT THE AUTHORis Professor of Mechanical Engineering at the University ofRhode Island. In 1966 he received a B.S. from Loyola College in Baltimore, andin 1971 a Ph.D. in Mechanical Engineering and Astronautical Sciences fromNorthwestern University in Evanston, Illinois.During his 38 years as a faculty member, he has taught 19 courses. One ofthese is a freshman MATLAB course, which he helped develop. He has authoredeight textbooks dealing with modeling and simulation, system dynamics, controlsystems, and MATLAB. These include System Dynamics, 2nd Edition (McGrawHill, 2010). He wrote a chapter on control systems in the Mechanical Engineers’Handbook (M. Kutz, ed., Wiley, 1999), and was a special contributor to the ftheditions of Statics and Dynamics, both by J. L. Meriam and L. G. Kraige (Wiley,2002).Professor Palm’s research and industrial experience are in control systems,robotics, vibrations, and system modeling. He was the Director of the RoboticsResearch Center at the University of Rhode Island from 1985 to 1993, and is thecoholder of a patent for a robot hand. He served as Acting Department Chairfrom 2002 to 2003. His industrial experience is in automated manufacturing;modeling and simulation of naval systems, including underwater vehicles andtracking systems; and design of control systems for underwater-vehicle enginetest facilities.William J. Palm IIIvi

pal34870 fm i-xii 1.qxd1/9/103:59 PMPage viiCONTENTSPreface ixCHAPTERCHAPTER11.1 MATLAB Interactive Sessions 41.2 Menus and the Toolbar 161.3 Arrays, Files, and Plots 181.4 Script Files and the Editor/Debugger 271.5 The MATLAB Help System 331.6 Problem-Solving Methodologies 381.7 Summary 46Problems 472Numeric, Cell, and Structure ArraysProgram Design and Development 148Relational Operators and LogicalVariables 1554.3 Logical Operators and Functions 1574.4 Conditional Statements 1644.5 for Loops 1714.6 while Loops 1834.7 The switch Structure 1884.8 Debugging MATLAB Programs 1904.9 Applications to Simulation 1934.10 Summary 199Problems 2004.14.2An Overview of MATLAB 3CHAPTER53One- and Two-Dimensional NumericArrays 542.2 Multidimensional Numeric Arrays 632.3 Element-by-Element Operations 642.4 Matrix Operations 732.5 Polynomial Operations Using Arrays 852.6 Cell Arrays 902.7 Structure Arrays 922.8 Summary 96Problems 972.1CHAPTER4Programming with MATLAB 147CHAPTERxy Plotting Functions 219Additional Commands andPlot Types 2265.3 Interactive Plotting in MATLAB5.4 Three-Dimensional Plots 2465.5 Summary 251Problems 2515.15.23Functions and Files 1133.1 Elementary Mathematical Functions 1133.2 User-De ned Functions 1193.3 Additional Function Topics 1303.4 Working with Data Files 1383.5 Summary 140Problems 1405Advanced Plotting 219CHAPTER2416Model Building and Regression2636.1 Function Discovery 2636.2 Regression 2716.3 The Basic Fitting Interface 2826.4 Summary 285Problems 286vii

pal34870 fm i-xii 1.qxdviii1/7/107:44 PMPage viiiContentsCHAPTER7Statistics, Probability, andInterpolation 2957.1 Statistics and Histograms 2967.2 The Normal Distribution 3017.3 Random Number Generation 3077.4 Interpolation 3137.5 Summary 322Problems 324CHAPTER8Linear Algebraic Equations3318.1 Matrix Methods for Linear Equations 3328.2 The Left Division Method 3358.3 Underdetermined Systems 3418.4 Overdetermined Systems 3508.5 A General Solution Program 3548.6 Summary 356Problems 357CHAPTER9Numerical Methods for Calculus andDifferential Equations 3699.1 Numerical Integration 3709.2 Numerical Differentiation 3779.3 First-Order Differential Equations 3829.4 Higher-Order Differential Equations 3899.5 Special Methods for Linear Equations 3959.6 Summary 408Problems ms 443Dead Time in Models 448Simulation of a Nonlinear VehicleSuspension Model 45110.10 Summary 455Problems 45610.710.810.910419Simulation Diagrams 420Introduction to Simulink 421Linear State-Variable Models 427Piecewise-Linear Models 430Transfer-Function Models 437Nonlinear State-Variable Models 441CHAPTERMuPAD11465Introduction to MuPAD 466Symbolic Expressions and Algebra 472Algebraic and TranscendentalEquations 47911.4 Linear Algebra 48911.5 Calculus 49311.6 Ordinary Differential Equations 50111.7 Laplace Transforms 50611.8 Special Functions 51211.9 Summary 514Problems 51511.111.211.3APPENDIXAGuide to Commands and Functions inThis Text 527APPENDIXBAnimation and Sound in MATLABAPPENDIXCFormatted Output in MATLAB 549APPENDIXReferencesAPPENDIXD553ESome Project Suggestionswww.mhhe.com/palmAnswers to Selected Problems 554Index 557538

pal34870 fm i-xii 1.qxd1/7/107:44 PMPage ixP R E FA C EFormerly used mainly by specialists in signal processing and numericalanalysis, MATLAB in recent years has achieved widespread and enthusiastic acceptance throughout the engineering community. Many engineering schools now require a course based entirely or in part on MATLAB early inthe curriculum. MATLAB is programmable and has the same logical, relational,conditional, and loop structures as other programming languages, such as Fortran,C, BASIC, and Pascal. Thus it can be used to teach programming principles. Inmost schools a MATLAB course has replaced the traditional Fortran course, andMATLAB is the principal computational tool used throughout the curriculum. Insome technical specialties, such as signal processing and control systems, it isthe standard software package for analysis and design.The popularity of MATLAB is partly due to its long history, and thus it iswell developed and well tested. People trust its answers. Its popularity is also dueto its user interface, which provides an easy-to-use interactive environment thatincludes extensive numerical computation and visualization capabilities. Itscompactness is a big advantage. For example, you can solve a set of many linearalgebraic equations with just three lines of code, a feat that is impossible with traditional programming languages. MATLAB is also extensible; currently morethan 20 “toolboxes” in various application areas can be used with MATLAB toadd new commands and capabilities.MATLAB is available for MS Windows and Macintosh personal computersand for other operating systems. It is compatible across all these platforms, whichenables users to share their programs, insights, and ideas. This text is based onMATLAB version 7.9 (R2009b). Some of the material in Chapter 9 is basedon the control system toolbox, Version 8.4. Chapter 10 is based on Version 7.4 ofSimulink . Chapter 11 is based on Version 5.3 of the Symbolic Math toolbox.TEXT OBJECTIVES AND PREREQUISITESThis text is intended as a stand-alone introduction to MATLAB. It can be used inan introductory course, as a self-study text, or as a supplementary text. The text’smaterial is based on the author’s experience in teaching a required two-creditsemester course devoted to MATLAB for engineering freshmen. In addition,the text can serve as a reference for later use. The text’s many tables and itsreferencing system in an appendix have been designed with this purpose in mind.A secondary objective is to introduce and reinforce the use of problemsolving methodology as practiced by the engineering profession in general and MATLAB and Simulink are a registered trademarks of The MathWorks, Inc.ix

pal34870 fm i-xii 1.qxdx1/7/107:44 PMPage xPrefaceas applied to the use of computers to solve problems in particular. This methodology is introduced in Chapter 1.The reader is assumed to have some knowledge of algebra and trigonometry;knowledge of calculus is not required for the rst seven chapters. Some knowledge of high school chemistry and physics, primarily simple electric circuits, andbasic statics and dynamics is required to understand some of the examples.TEXT ORGANIZATIONThis text is an update to the author’s previous text.* In addition to providing newmaterial based on MATLAB 7, especially the addition of the MuPAD program,the text incorporates the many suggestions made by reviewers and other users.The text consists of 11 chapters. The rst chapter gives an overview ofMATLAB features, including its windows and menu structures. It also introducesthe problem-solving methodology. Chapter 2 introduces the concept of an array,which is the fundamental data element in MATLAB, and describes how to use numeric arrays, cell arrays, and structure arrays for basic mathematical operations.Chapter 3 discusses the use of functions and les. MA TLAB has an extensive number of built-in math functions, and users can de ne their own functionsand save them as a le for reuse.Chapter 4 treats programming with MATLAB and covers relational and logical operators, conditional statements, for and while loops, and the switchstructure. A major application of the chapter’s material is in simulation, to whicha section is devoted.Chapter 5 treats two- and three-dimensional plotting. It rst establishes standards for professional-looking, useful plots. In the author’s experience, beginningstudents are not aware of these standards, so they are emphasized. The

10.2–3 Simulink model for 10.3–1 Simulink model of a two-mass suspension system 10.4–1 Simulink model of a rocket-propelled sled 10.4–2 Model of a relay-controlled motor 10.5–1 Response with a dead zone 10.6–1 Model of a nonlinear pendulum Chapter Eleven 11.3–1 Intersection of two circles 11.3–2 Positioning a robot arm