Pearson Education Limited Edinburgh Gate Harlow Essex CM20 2IE England © Addison Wesley Longman Limited 1999 The rights of Adrian Biran and Moshe Breiner to be identified of authors of this work have been asserted by them in accordance with the Copyright, Designs and Patents Act 1988 All rights reserved No part ofthis publication may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, electronic, mechanical, photocopying, recording or otherwise, without prior written permission of the publisher or a license permitting restricted copying in the United Kingdom issued by the Copyright Licensing Agency Ltd., 90 Tottenham Court Road, London WIP OLP The programs in this book have been included for their instructional value They have been tested with care but are not guaranteed for any particular purpose The publisher does not offer any warranties or representations, nor does it accept any liabilities with respect to the programs Many of the designations used by the manufacturers and sellers to distinguish their products are claimed as trademarks Addison Wesley Longman has made every attempt to supply trademark information about manufacturers and their products mentioned in this book A list of the trademark designations and their owners appears below Cover designed by Designers & Partners, Oxford Cover image: two concentric lit spheres demonstrating both face and edge lighting in MATLAB Reproduced with permission from The MathWorks Typeset by 56 Printed in Great Britain by Henry Ling Ltd., at the Dorset Press, Dorchester, Dorset First edition published 1995 Second edition published 1999, reprinted 1999 ISBN 0-201-36043-8 British Library Cataloguing in Publication Data A catalogue record for this book is available Library of Congress Cataloging in Publication Data Trademark notice DEC and VAX are trademarks of Digital Equipment Corporation KEDIT is a trademark of Mansfield Software Group TEX is a trademark of American Mathematical Society Simulink and Spline Toolbox are registered trademarks of The Math Works Incorporated SUN is a trademark of Sun Microsystems Incorporated Preface to MATLAB 5for Engineers Preface to MATLAB for Engineers MATLAB in a nutshell Desktop calculations 1.1 1.1.1 Getting started 1.1.2 Arithmetic operations 1.1.3 Last-line editing 1.1.4 A few built-in functions 1.1.5 Naming constants and variables 1.1.6 I'ormat 1.1.7 Diary One-dimensional arrays and graphics 1.2 1.2.1 Arrays - elementary operations 1.2.2 More operations on arrays 1.2.3 Row vectors and column vectors 1.2.4 The scalar product 1.2.5 Simple plots Matrices 1.3 1.3.1 Defining a matrix 1.3.2 Elementary operations with matrices 1.3.3 Printing a table Complex numbers 1.4 1.4.1 Complex numbers - elementary operations 1.4.2 Two notes on the use of i and j 1.4.3 Plotting a complex number 1.4.4 Trigonometric and exponential representations 1.4.5 I'unctions of complex variables 1.4.6 Arrays of complex numbers Systems of linear equations 1.5 1.5.1 Determinants xi xiii 1 3 10 10 12 13 14 15 16 16 19 20 21 21 22 23 24 26 26 27 27 vi I CONTENTS 1.5.2 Cramer's rule 1.5.3 Matrix inversion 1.5.4 Solving systems of linear equations in MATLAB 1.6 Polynomials 1.6.1 Polynomial roots 1.6.2 Retrieving polynomial coefficients from polynomial roots 1.6.3 Polynomial evaluation 1.6.4 Multiplication and division of polynomials Programming in MATLAB 1.7 1.7.1 Programming a function 1.7.2 Repetitive control structures - FOR loops 1.7.3 Conditional control structures 1.7.4 Repetitive control structures - WHILE loops External files and programs 1.8 1.8.1 Saving and loading data 1.8.2 The CUMSUM and DlFF functions 1.8.3 Reading data from external files 1.8.4 Issuing commands to the operating system 1.8.5 Writing output to external files 1.9 Regression and interpolation 1.9.1 Tables with one entry 1.9.2 Tables with two entries 1.9.3 Polynomial fit and polynomial interpolation 1.9.4 Spline interpolation 1.10 More about plotting 1.10.1 Function evaluation 1.10.2 Histograms 1.10.3 Polar plots 1.10.4 A more elaborate polar plot 1.10.5 Three-dimensional plots 1.11 More about interpolation and 3D plots 1.11.1 More interpolating functions 1.11.2 The GRIDDATA function and 3D plots 1.11.3 The plot of a 3D line 28 30 30 31 31 31 32 33 34 35 36 37 39 41 41 42 44 44 45 46 46 47 48 50 51 51 52 53 53 54 56 56 58 61 MATLAB - AN INTRODUCTION FOR ENGINEERS 63 Desktop calculations with graphics 2.1 Introduction 2.2 Computer-aided mistakes 2.3 Computer representation of numbers 2.4 Roundoff 2.5 Roundoff errors The set of machine numbers 2.6 2.7 Vectors 65 65 65 66 69 70 72 74 CONTENTS 2.8 2.9 2.10 2.ll 2.12 2.13 2.14 2.15 Column vectors A spreadsheet Geometrical significance of the scalar product Graphical solution of equations Summary Examples Exercises Appendix - a note on Fourier series Two-dimensional arrays and matrices 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 3.10 Building a matrix out of vectors Vector-by-matrix multiplication Matrix-by-matrix multiplication Array division Matrix addition Multidimensional arrays Summary Examples Exercises Where to find more examples and exercises Complex numbers Introduction 4.1 4.2 The introduction of complex numbers 4.3 Operations with complex numbers 4.4 Geometric representation Trigonometric representation 4.5 4.6 Functions of complex variables 4.7 Mapping by functions of complex variables 4.8 Conformal mapping 4.9 Harmonic motion - phasors 4.10 A simple MATLAB program - visualizing a rotating vector 4.11 Summary 4.12 Examples 4.13 Exercises 4.14 Where to find more examples 4.15 Appendix 4.15.1 Operations with complex numbers 4.15.2 AC circuits - phasors in electrical engineering Geometric calculations Introduction 5.1 5.2 Rotation of coordinate axes in the plane Matrix inversion 5.3 5.4 Programming a function Euler's angles 5.5 vii 76 77 78 80 84 86 108 121 123 123 126 128 130 131 132 135 136 148 161 163 163 164 165 167 169 172 175 178 181 186 189 190 210 215 215 215 216 219 219 220 221 223 224 viii CONTENTS 5.6 5.7 5.8 5.9 5.10 5.11 Determinants Summary Affine transformations Inverse affine transformations Examples Exercises Solving equations 6.1 Systems of linear equations - introduction 6.2 Inhomogeneous linear equations 6.3 Homogeneous linear equations 6.4 MATLAB's solution of linear systems 6.5 Overdetermined systems -least-squares solution 6.6 III-conditioned systems 6.7 Polynomial equations - introduction 6.8 Finding roots of polynomials in MATLAB 6.9 Retrieving polynomial coefficients from roots 6.10 Summary 6.11 Examples - linear equations 6.12 Examples - polynomial equations 6.13 Exercises 6.14 Appendix -least squares fit Programming in MATLAB 7.1 Introduction 7.2 Conditional branching 7.3 WHILE loops 7.4 Iterative solution of equations 7.5 The Newton-Raphson method 7.6 Recursion 7.7 A note on complexity 7.8 Summary 7.9 Examples 7.10 Exercises 7.11 Appendix - the contractive Lipschitz condition External files 8.1 Introduction 8.2 A log-book of calculations 8.3 Reading data from M-files 8.4 Linear interpolation 8.4.1 Tables with one entry 8.4.2 Tables with two entries 8.5 Switching to the operating system 8.6 Writing output to external files 8.7 Additional input/output functions 228 231 232 238 239 243 247 247 248 251 254 257 260 262 263 264 264 266 287 298 308 311 311 311 315 317 320 328 330 333 335 346 349 353 353 353 355 356 356 358 362 362 364 CONTENTS 8.8 8.9 8.10 Summary Examples Exercises 10 More 10.1 10.2 10.3 10.4 10.5 10.6 10.7 10.8 10.9 n 364 366 371 Introduction Polynomial fit Polynomial interpolation Assessing the quality of a regression Power regression Exponential regression Spline interpolation Summary Examples Exercises 377 377 378 381 384 386 389 392 394 396 411 about plotting Introduction Histograms Polar plots Three-dimensional plots Animation Additive colour mixing Examples Exercises Appendix - the equation of orbits of planets and satellites 415 415 416 419 421 421 422 424 436 441 Regression and interpolation 9.1 9.2 9.3 9.4 9.5 9.6 9.7 9.8 9.9 9.10 IX MORE APPLICATIONS 445 11 Numerical integration 11.1 Introduction 11.2 The trapezoidal rule 11.3 Error of integration by the trapezoidal rule 11.4 Simpson's rule 11.5 Error of integration by Simpson's rule 11.6 MATLAB QUAD and QUAD8 functions 11.7 Examples 11.8 Exercises 447 447 449 452 455 459 460 461 470 12 Graph theory 12.1 Definitions 12.2 Graph of a railway network 12.3 Sparse matrix 12.4 MATLAB new features 12.5 Exercises 475 475 478 482 483 485 x CONTENTS 13 Dimensional analysis 13.1 Introduction · 13.2 Dimensional analysis 13.3 Examples 13.4 Summary 13.5 Exercises 489 489 489 493 504 505 14 System modelling and simulation 14.1 Introduction · 14.2 Five ways of modelling dynamic systems 14.2.1 Transfer functions 14.2.2 Zero-pole-gain models 14.2.3 Partial fractions 14.2.4 State space 14.2.5 Cauchy form 14.3 Numerical solution of ordinary differential equations 14.3.1 Specifying the times of the solution 14.3.2 Using alternative odesolvers 14.3.3 Passing parameters to the model 14.4 Alternative strategies to solve ordinary differential equations 14.4.1 Runge-Kutta methods 14.4.2 Predictor-corrector methods 14.4.3 Stiff systems 14.5 Conclusion: how to choose the odesolver 14.6 Exercises 507 507 508 508 509 510 511 512 513 515 516 517 520 523 525 527 533 534 15 Control 15.1 Introduction · 15.2 Root locus design 15.3 Design in the frequency domain 15.4 Optimal design 15.5 Exercises 543 543 544 546 556 559 16 Signal processing 16.1 Signals and signal processing 16.2 The FILTER function 16.3 Calling the filter with initial conditions 16.4 Design of digital filters 16.4.1 Definitions 16.4.2 Defining the frequency characteristics of the filter 16.4.3 The Tustin bilinear transformation 16.5 The Discrete Fourier Transform 16.6 A short introduction to the DFT 16.7 The power spectrum 16.8 Trigonometric expansion of a signal 16.9 High frequency signals and aliasing 567 567 568 574 578 578 578 583 586 588 591 596 598 CONTENTS 16.10 Exercises 17 Case studies in mechanical engineering 17.1 Bending moment caused by a moving vehicle 17.2 One-dimensional heat conduction 17.3 Using a sparse matrix 17.4 Operations on unit-affected quantities 17.4 Introduction 17.4.2 Transition tables 17.4.3 Multiplication 17.4.4 Electrical units 17.5 Exercises Amwers to selected exercises Bibliography Index Xl 604 609 609 613 618 621 621 623 624 628 628 635 673 679 Preface to MATLAB for Engineers This second edition of the book MATLAB for Engineers is adapted to version of MATLAB The new features of MATLAB include powerful program-development tools, new data types and structures, more graphic and visualization features, additional mathematical functions and major improvements to MATLAB application toolboxes, among them the introduction of SIMULINK All these new features make programming easier, especially for complex applications On the other hand, the number of new features is so large that it is impossible to describe all of them adequately in a one-volume textbook To keep the introductory character of the text, and a reasonable book size, we limited the extent of updating as described below We deleted all references to MATLAB 3.5 Where MATLAB commands are different from MATLAB commands, we updated them to version A few new functions and graphic facilities that enhance programming power and graph readability are introduced Titles, labels and text added to graphs are really improved by using the subset ofIbTE;Xcommands admitted by MATLAB For instance, instead of writing alpha as previously, we can now write \al pha and obtain the Greek letter Ci From the new data types and structures we chose multidimensional arrays, structures and cells because we found them useful even in simple engineering calculations Thus, in Chapter we extended the spreadsheet model to represent it by a multidimensional array, and in Chapter 10 we used three-dimensional arrays for the RGB model of additive colour mixing In Chapter 12 we introduced the new MATLAB structure facility to define graphs by a single data structure In Chapter 17 we used structures to store values of constants together with their units, and we introduced new functions operating on character strings and cells to operate on those constants and obtain the values and the units of the results A major updating is that of the material related to the integration of ordinary differential equations - (ODEs) MATLAB introduces new integration routines that solve more difficult problems than those that could be treated in MATLAB Examples of such problems are stiff equations and dynamical systems exhibiting discontinuities Therefore, Chapter 14 underwent an extensive updating A few new exercises were distributed among all chapters MathWorks, Inc took care to provide software usable after the year 2000 While preparing this new edition we received essential help from several people at Math Works, from Baruch Pekelman and Oren Merom of Omikron Delta, the agents of Math Works in Israel, and from Emma Mitchell, Bridget Allen and Michael Strang of Addison Wesley Longman 674 BIBLIOGRAPHY - 10% rhodium/platinum thermocouples - Type S London: British Standards Institution BSI (1982) BS 6323: Seamless and welded steel tubes for automobile, mechanical and general engineering - Part i-General requirement, London: British Standards Institution BSI (1984) BS i904 -industrial platinum resistance thermometer London: British Standards Institution Bu-qing S and Ding-yuan L (1989) Computational Geometry - Curve and Surface Modeling Boston, MA: Academic Press, Inc Carlson A B and Gisser D G (1990) Electrical Engineering - Concepts and Applications, 2nd edn Reading, MA: Addison-Wesley Publishing Company Catchpole J and Fulford G D (1987) Dimensionless Groups New York: McGrawHill Encyclopedia of Science & Technology Chapra S C and Canale R P (1989) Numerical Methods for Engineers, 2nd edn New York: McGraw-Hill International Editions Chertov, A.G (1997), Units of physical measure, in Grigoriev, I.S, and Meilikhov, E.Z., editors, Handbook of physical quantities, Boca Raton: CRC Press Churchill R V and Brown J W (1990) Complex Variables and Applications, 5th edn New York: McGraw-Hill Publishing Company Collin R E (1985) Antennas and Radiowave Propagation New York: McGraw-Hill Book Company Cooley J W and Tukey W (1965) An algorithm for the machine computation of complex Fourier series Mathematics of Computation, 19, April, 297-301 Corge C (1975) Elements d'informatique -informatique et demarche de l'esprit Paris: Librairie Larousse Coulon F de and Jufer M (1978) Introduction a l'electrotechnique St-Saphorin, Switzerland: Editions Giorgi Coxeter H S M (1980) Introduction to Geometry, 2nd edn New York: John Wiley & Sons Czichos H., ed (1989) HOITE - Die Grundlagen der Ingenieurwissenschaften, 29th completely revised edn Berlin: Springer-Verlag De Facia B (1992) Beats McGraw-Hill Encyclopedia of Science and Technology, 7th edn, volume New York: McGraw-Hill Dierker P F and Voxman W L (1986) Discrete Mathematics San Diego, CA: Harcourt Brace Jovanovich Publishers Dietrich G and Stahl H (1965) Grundzuge der Matrizenrechnung, 3rd edn Leipzig: VEB FachbuchverIag Leipzig Dieudonne (1992) Mathematics - The Music of Reason Berlin: Springer-Verlag French original (1987) Pour l'honneur de I 'esprit humain Paris: Hachette Dimarogonas A D (1989) Computer Aided Machine Design New York: PrenticeHall DIN (1985) DIN i072: Road andfoot bridge - Design loads English translation of DIN 1072: Straj3en und Wegbrucken - Lastnahmen Berlin: Verlag Beuth DIN (1097) DIN 4710: Meteorologische Daten zur Berechnung des Energieverbrauches von raumluftechnischen Anlagen; Lufttemperatur-Luftfeuchte nach Monatsummen Berlin: Verlag Beuth BIBLIOGRAPHY 675 DorfR C (1992) Modem Control Systems, 6th edn Reading, MA: Addison-Wesley Publishing Company Eckert E R G and Drake R M Jr (1972) Analysis of Heat and Mass Transfer, International student edition New York: McGraw-Hill Kogakusha Ltd Edwards H W (1964) Analytic and Vector Mechanics New York: Dover Publications Elgerd O I (1967) Control Systems Theory New York: McGraw-Hill Book Company, and Kogakusha Company, Ltd Fahidy T Z and Quairashi M S (1986) Principles of dimensional analysis In Encyclopedia of Fluid Mechanics, Vol (Cheremisinoff N.P., ed.) Houston: Gulf Publishing Company Farin G (1993) Curves and Surfaces for Computer Aided Geometric Design - A Practical Guide, 3rd edn Boston, MA: Academic Press, Inc Fehlberg E (1970) Klassische Runge-Kutta Formeln vierter und niedriger Ordnung mit Schrittweiten- Kontrolle und ihre Anwendung auf Warmeleitungsprobleme, Computing, 6, 61-71 Fraleigh J B and Beauregard R A (1990) Linear Algebra (with historical notes by Katz V J.), 2nd edn Reading, MA: Addison-Wesley Publishing Company Franklin G E, Powell J D and Workman M L (1990) Digital control of Dynamic Systems, 2nd edn Reading, MA: Addison-Wesley Fuller G and Tarwater D (1992) Analytic Geometry Reading, MA: Addison-Wesley Publishing Company Gabel R and Roberts R (1973) Signals and Linear Systems New York: Wiley Genta G (1982) Lezioni di meccanica dell'autoveicolo Torino: Libreria Editrice Universitaria Levrotto & Bella Gerald C E and Wheatley P O (1994) Applied Numerical Analysis, 5th ed Reading, MA: Addison-Wesley Giardina B (1970) Introduzione metodi statistici Milano: Edizioni di Comunita Giedt W H (1987) Convection (heat) In McGraw-Hill Encyclopedia of Science & Technology, Vol 4, 6th edn New York: McGraw-Hill Gillies R B (1993) Instrumentation and Measurements for Electronic Technicians 2nd edn New York: Merrill Giordano E R and Weir M D (1991) Differential Equations - A Modeling Approach Reading, MA: Addison-Wesley Goossens, M., Mittelbach, E, and Samarin, A (1994), The BTEXcompanion, Reading, MA: Addison-Wesley Publishing Company Goldner H et at (1979) Lehrbuch Hjjhere Festigkeitslehre, Vol Weinheim: Physik Verlag Grober H., Erk S and Grigull U (1961) Die Grundgesetze der Wiirmeubertragung New edition prepared by Grigull Ulrich Berlin: Springer-Verlag, English translation (1961) Fundamentals of heat transfer New York: McGraw-Hill Guggenheimer H W (1977) Differential Geometry New York: Dover Publications H & B (1961) Elektrische und Wiirmetechnische Messungen, 10th edn Frankfurt/Main: Hartmann & Braun AG Hartley P and Wynn-Evans A (1979) A Structured Introduction to Numerical MnthoWlnt;rc rhpltpnh