pal34870_ifc.qxd 1/7/10 7:44 PM Page i Numbered Examples: Chapters One to Eight Number and Topic Number and Topic Chapter One 4.7–1 1.1–1 1.6–1 Volume of a circular cylinder Piston motion Chapter Two 2.3–1 2.3–2 2.3–3 2.3–4 2.3–5 2.4–1 2.4–2 2.4–3 2.4–4 2.5–1 2.6–1 2.7–1 Vectors and displacement Aortic pressure model Transportation route analysis Current and power dissipation in resistors A batch distillation process Miles traveled Height versus velocity Manufacturing cost analysis Product cost analysis Earthquake-resistant building design An environmental database A student database Chapter Three 3.2–1 Optimization of an irrigation channel Chapter Four 4.3–1 4.5–1 4.5–2 4.5–3 4.5–4 4.6–1 4.6–2 4.6–3 Height and speed of a projectile Series calculation with a for loop Plotting with a for loop Data sorting Flight of an instrumented rocket Series calculation with a while loop Growth of a bank account Time to reach a speci ed height 4.9–1 4.9–2 Using the switch structure for calendar calculations A college enrollment model: Part I A college enrollment model: Part II Chapter Five 5.2–1 Plotting orbits Chapter Six 6.1–1 6.1–2 6.2–1 6.2–2 6.2–3 6.2–4 Temperature dynamics Hydraulic resistance Estimation of traf c ow Modeling bacteria growth Breaking strength and alloy composition Response of a biomedical instrument Chapter Seven 7.1–1 7.2–1 7.2–2 7.3–1 Breaking strength of thread Mean and standard deviation of heights Estimation of height distribution Statistical analysis and manufacturing tolerances Chapter Eight 8.1–1 8.2–1 8.2–2 8.2–3 8.2–4 The matrix inverse method Left division method with three unknowns Calculations of cable tension An electric resistance network Ethanol production pal34870_fm_i-xii_1.qxd 1/7/10 7:44 PM Page i Numbered Examples: Chapters Eight to Eleven Number and Topic Number and Topic 8.3–1 Chapter Ten 8.3–2 8.3–3 8.3–4 8.3–5 8.4–1 8.4–2 An underdetermined set with three equations and three unknowns A statically indeterminate problem Three equations in three unknowns, continued Production planning Traf c engineering The least-squares method An overdetermined set Chapter Nine 9.1–1 9.1–2 9.1–3 9.3–1 9.3–2 9.4–1 9.5–1 Velocity from an accelerometer Evaluation of Fresnel’s cosine integral Double integral over a nonrectangular region Response of an RC circuit Liquid height in a spherical tank A nonlinear pendulum model Trapezoidal pro le for a dc motor 10.2–1 10.2–2 10.2–3 10.3–1 10.4–1 10.4–2 10.5–1 10.6–1 # Simulink solution of y = 10 sin t Exporting to the MATLAB workspace # Simulink model for y = - 10y + f (t) Simulink model of a two-mass suspension system Simulink model of a rocket-propelled sled Model of a relay-controlled motor Response with a dead zone Model of a nonlinear pendulum Chapter Eleven 11.3–1 11.3–2 11.5–1 Intersection of two circles Positioning a robot arm Topping the Green Monster pal34870_fm_i-xii_1.qxd 1/7/10 7:44 PM Page iii Introduction to MATLAB® for Engineers William J Palm III University of Rhode Island TM pal34870_fm_i-xii_1.qxd 1/15/10 11:41 AM Page iv TM INTRODUCTION TO MATLAB® FOR ENGINEERS, THIRD EDITION Published 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 Previous editions © 2005 and 2001 No part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written consent of The McGraw-Hill Companies, Inc., including, but not limited to, in any network or other electronic storage or transmission, or broadcast for distance learning Some ancillaries, including electronic and print components, may not be available to customers outside the United States This book is printed on acid-free paper containing 10% postconsumer waste DOC/DOC ISBN 978-0-07-353487-9 MHID 0-07-353487-0 Vice President & Editor-in-Chief: Martin Lange Vice President, EDP: Kimberly Meriwether David Global Publisher: Raghu Srinivasan Sponsoring Editor: Bill Stenquist Marketing Manager: Curt Reynolds Development Editor: Lora Neyens Senior Project Manager: Joyce Watters Design Coordinator: Margarite Reynolds Cover Designer: Rick D Noel Photo Research: John Leland Cover Image: © Ingram Publishing/AGE Fotostock Production Supervisor: Nicole Baumgartner Media Project Manager: Joyce Watters Compositor: MPS Limited, A Macmillan Company Typeface: 10/12 Times Roman Printer: RRDonnelly All 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 Data Palm, 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-9 MATLAB Numerical analysis—Data processing I Title QA297.P33 2011 518.0285—dc22 2009051876 www.mhhe.com pal34870_fm_i-xii_1.qxd 1/7/10 7:44 PM Page v To my sisters, Linda and Chris, and to my parents, Lillian and William pal34870_fm_i-xii_1.qxd 1/7/10 7:44 PM Page vi ABOUT THE AUTHOR is Professor of Mechanical Engineering at the University of Rhode Island In 1966 he received a B.S from Loyola College in Baltimore, and in 1971 a Ph.D in Mechanical Engineering and Astronautical Sciences from Northwestern University in Evanston, Illinois During his 38 years as a faculty member, he has taught 19 courses One of these is a freshman MATLAB course, which he helped develop He has authored eight textbooks dealing with modeling and simulation, system dynamics, control systems, 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 fth editions 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 Robotics Research Center at the University of Rhode Island from 1985 to 1993, and is the coholder of a patent for a robot hand He served as Acting Department Chair from 2002 to 2003 His industrial experience is in automated manufacturing; modeling and simulation of naval systems, including underwater vehicles and tracking systems; and design of control systems for underwater-vehicle enginetest facilities William J Palm III vi pal34870_fm_i-xii_1.qxd 1/9/10 3:59 PM Page vii CONTENTS Preface ix CHAPTER CHAPTER Programming with MATLAB 147 An Overview of MATLAB® 1.1 MATLAB Interactive Sessions 1.2 Menus and the Toolbar 16 1.3 Arrays, Files, and Plots 18 1.4 Script Files and the Editor/Debugger 27 1.5 The MATLAB Help System 33 1.6 Problem-Solving Methodologies 38 1.7 Summary 46 Problems 47 CHAPTER Numeric, Cell, and Structure Arrays 53 2.1 One- and Two-Dimensional Numeric Arrays 54 2.2 Multidimensional Numeric Arrays 63 2.3 Element-by-Element Operations 64 2.4 Matrix Operations 73 2.5 Polynomial Operations Using Arrays 85 2.6 Cell Arrays 90 2.7 Structure Arrays 92 2.8 Summary 96 Problems 97 CHAPTER 4.1 4.2 Program Design and Development 148 Relational Operators and Logical Variables 155 4.3 Logical Operators and Functions 157 4.4 Conditional Statements 164 4.5 for Loops 171 4.6 while Loops 183 4.7 The switch Structure 188 4.8 Debugging MATLAB Programs 190 4.9 Applications to Simulation 193 4.10 Summary 199 Problems 200 CHAPTER Advanced Plotting 219 5.1 5.2 xy Plotting Functions 219 Additional Commands and Plot Types 226 5.3 Interactive Plotting in MATLAB 5.4 Three-Dimensional Plots 246 5.5 Summary 251 Problems 251 Functions and Files 113 CHAPTER 3.1 Elementary Mathematical Functions 113 3.2 User-De ned Functions 119 3.3 Additional Function Topics 130 3.4 Working with Data Files 138 3.5 Summary 140 Problems 140 6.1 Function Discovery 263 6.2 Regression 271 6.3 The Basic Fitting Interface 282 6.4 Summary 285 Problems 286 Model Building and Regression 241 263 vii pal34870_fm_i-xii_1.qxd 1/7/10 7:44 PM Page viii Contents viii CHAPTER 10.7 10.8 10.9 Statistics, Probability, and Interpolation 295 7.1 Statistics and Histograms 296 7.2 The Normal Distribution 301 7.3 Random Number Generation 307 7.4 Interpolation 313 7.5 Summary 322 Problems 324 CHAPTER Linear Algebraic Equations 331 8.1 Matrix Methods for Linear Equations 332 8.2 The Left Division Method 335 8.3 Underdetermined Systems 341 8.4 Overdetermined Systems 350 8.5 A General Solution Program 354 8.6 Summary 356 Problems 357 CHAPTER Numerical Methods for Calculus and Differential Equations 369 9.1 Numerical Integration 370 9.2 Numerical Differentiation 377 9.3 First-Order Differential Equations 382 9.4 Higher-Order Differential Equations 389 9.5 Special Methods for Linear Equations 395 9.6 Summary 408 Problems 410 CHAPTER Simulink 10.1 10.2 10.3 10.4 10.5 10.6 10 419 Simulation Diagrams 420 Introduction to Simulink 421 Linear State-Variable Models 427 Piecewise-Linear Models 430 Transfer-Function Models 437 Nonlinear State-Variable Models 441 Subsystems 443 Dead Time in Models 448 Simulation of a Nonlinear Vehicle Suspension Model 451 10.10 Summary 455 Problems 456 CHAPTER MuPAD 11 465 11.1 11.2 11.3 Introduction to MuPAD 466 Symbolic Expressions and Algebra 472 Algebraic and Transcendental Equations 479 11.4 Linear Algebra 489 11.5 Calculus 493 11.6 Ordinary Differential Equations 501 11.7 Laplace Transforms 506 11.8 Special Functions 512 11.9 Summary 514 Problems 515 APPENDIX A Guide to Commands and Functions in This Text 527 APPENDIX B Animation and Sound in MATLAB APPENDIX C Formatted Output in MATLAB 549 APPENDIX References APPENDIX D 553 E Some Project Suggestions www.mhhe.com/palm Answers to Selected Problems 554 Index 557 538 pal34870_fm_i-xii_1.qxd 1/7/10 7:44 PM Page ix P R E FA C E F ormerly used mainly by specialists in signal processing and numerical analysis, 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 in the 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 In most schools a MATLAB course has replaced the traditional Fortran course, and MATLAB is the principal computational tool used throughout the curriculum In some technical specialties, such as signal processing and control systems, it is the standard software package for analysis and design The popularity of MATLAB is partly due to its long history, and thus it is well developed and well tested People trust its answers Its popularity is also due to its user interface, which provides an easy-to-use interactive environment that includes extensive numerical computation and visualization capabilities Its compactness is a big advantage For example, you can solve a set of many linear algebraic equations with just three lines of code, a feat that is impossible with traditional programming languages MATLAB is also extensible; currently more than 20 “toolboxes” in various application areas can be used with MATLAB to add new commands and capabilities MATLAB is available for MS Windows and Macintosh personal computers and for other operating systems It is compatible across all these platforms, which enables users to share their programs, insights, and ideas This text is based on MATLAB version 7.9 (R2009b) Some of the material in Chapter is based on the control system toolbox, Version 8.4 Chapter 10 is based on Version 7.4 of Simulink® Chapter 11 is based on Version 5.3 of the Symbolic Math toolbox TEXT OBJECTIVES AND PREREQUISITES This text is intended as a stand-alone introduction to MATLAB It can be used in an introductory course, as a self-study text, or as a supplementary text The text’s material is based on the author’s experience in teaching a required two-credit semester 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 its referencing 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 ... PM Page iii Introduction to MATLAB? ? for Engineers William J Palm III University of Rhode Island TM pal34870_fm_i-xii_1.qxd 1/15/10 11:41 AM Page iv TM INTRODUCTION TO MATLAB? ? FOR ENGINEERS, THIRD... book are considered to be an extension of the copyright page Library of Congress Cataloging-in-Publication Data Palm, William J (William John), 1944– Introduction to MATLAB for engineers / William... Programming with MATLAB 147 An Overview of MATLAB? ? 1.1 MATLAB Interactive Sessions 1.2 Menus and the Toolbar 16 1.3 Arrays, Files, and Plots 18 1.4 Script Files and the Editor/Debugger 27 1.5 The MATLAB