Tải bản đầy đủ (.pdf) (448 trang)

MATLAB what every engineer sh adrian b brian

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (28.01 MB, 448 trang )


WHAT EVERY ENGINEER
SHOULD KNOW ABOUT

MATLAB®
®
and Simulink


WHAT EVERY ENGINEER SHOULD KNOW
A Series
Series Editor*

Phillip A. Laplante
Pennsylvania State University

1. What Every Engineer Should Know About Patents,
William G. Konold, Bruce Tittel, Donald F. Frei,
and David S. Stallard
2. What Every Engineer Should Know About Product Liability,
James F. Thorpe and William H. Middendorf
3. What Every Engineer Should Know About Microcomputers:
Hardware/Software Design, A Step-by-Step Example,
William S. Bennett and Carl F. Evert, Jr.
4. What Every Engineer Should Know About Economic Decision
Analysis, Dean S. Shupe
5. What Every Engineer Should Know About Human Resources
Management, Desmond D. Martin and Richard L. Shell
6. What Every Engineer Should Know About Manufacturing Cost
Estimating, Eric M. Malstrom
7. What Every Engineer Should Know About Inventing,


William H. Middendorf
8. What Every Engineer Should Know About Technology Transfer
and Innovation, Louis N. Mogavero and Robert S. Shane
9. What Every Engineer Should Know About Project Management,
Arnold M. Ruskin and W. Eugene Estes
10. What Every Engineer Should Know About Computer-Aided
Design and Computer-Aided Manufacturing: The CAD/CAM
Revolution, John K. Krouse
11. What Every Engineer Should Know About Robots,
Maurice I. Zeldman
12. What Every Engineer Should Know About Microcomputer
Systems Design and Debugging, Bill Wray and Bill Crawford
13. What Every Engineer Should Know About Engineering
Information Resources, Margaret T. Schenk
and James K. Webster
14. What Every Engineer Should Know About Microcomputer
Program Design, Keith R. Wehmeyer
*Founding Series Editor: William H. Middendorf


15. What Every Engineer Should Know About Computer Modeling
and Simulation, Don M. Ingels
16. What Every Engineer Should Know About Engineering
Workstations, Justin E. Harlow III
17. What Every Engineer Should Know About Practical CAD/CAM
Applications, John Stark
18. What Every Engineer Should Know About Threaded Fasteners:
Materials and Design, Alexander Blake
19. What Every Engineer Should Know About Data
Communications, Carl Stephen Clifton

20. What Every Engineer Should Know About Material
and Component Failure, Failure Analysis, and Litigation,
Lawrence E. Murr
21. What Every Engineer Should Know About Corrosion,
Philip Schweitzer
22. What Every Engineer Should Know About Lasers, D. C. Winburn
23. What Every Engineer Should Know About Finite Element
Analysis, John R. Brauer
24. What Every Engineer Should Know About Patents:
Second Edition, William G. Konold, Bruce Tittel, Donald F. Frei,
and David S. Stallard
25. What Every Engineer Should Know About Electronic
Communications Systems, L. R. McKay
26. What Every Engineer Should Know About Quality Control,
Thomas Pyzdek
27. What Every Engineer Should Know About Microcomputers:
Hardware/Software Design, A Step-by-Step Example.
Second Edition, Revised and Expanded, William S. Bennett,
Carl F. Evert, and Leslie C. Lander
28. What Every Engineer Should Know About Ceramics,
Solomon Musikant
29. What Every Engineer Should Know About Developing Plastics
Products, Bruce C. Wendle
30. What Every Engineer Should Know About Reliability and Risk
Analysis, M. Modarres
31. What Every Engineer Should Know About Finite Element
Analysis: Second Edition, Revised and Expanded,
John R. Brauer
32. What Every Engineer Should Know About Accounting and
Finance, Jae K. Shim and Norman Henteleff

33. What Every Engineer Should Know About Project Management:
Second Edition, Revised and Expanded, Arnold M. Ruskin
and W. Eugene Estes
34. What Every Engineer Should Know About Concurrent
Engineering, Thomas A. Salomone


35. What Every Engineer Should Know About Ethics,
Kenneth K. Humphreys
36. What Every Engineer Should Know About Risk Engineering
and Management, John X. Wang and Marvin L. Roush
37. What Every Engineer Should Know About Decision Making
Under Uncertainty, John X. Wang
38. What Every Engineer Should Know About Computational
Techniques of Finite Element Analysis, Louis Komzsik
39. What Every Engineer Should Know About Excel, Jack P. Holman
40.

What Every Engineer Should Know About Software
Engineering, Phillip A. Laplante

41.

What Every Engineer Should Know About Developing
Real-Time Embedded Products, Kim R. Fowler

42.

What Every Engineer Should Know About Business
Communication, John X. Wang


43.

What Every Engineer Should Know About Career Management,
Mike Ficco

44.

What Every Engineer Should Know About Starting a High-Tech
Business Venture, Eric Koester

45.

What Every Engineer Should Know About MATLAB®
and Simulink®, Adrian B. Biran with contributions
by Moshe Breiner


WHAT EVERY ENGINEER
SHOULD KNOW ABOUT

MATLAB
®
and Simulink
®

Adrian B. Biran
With contributions by Moshe Breiner

Boca Raton London New York


CRC Press is an imprint of the
Taylor & Francis Group, an informa business


MATLAB® and Simulink® are trademarks of The MathWorks, Inc. and are used with permission. The MathWorks does not warrant the accuracy of the text of exercises in this book. This book’s use or discussion
of MATLAB® and Simulink® software or related products does not constitute endorsement or sponsorship
by The MathWorks of a particular pedagogical approach or particular use of the MATLAB® and Simulink®
software.

CRC Press
Taylor & Francis Group
6000 Broken Sound Parkway NW, Suite 300
Boca Raton, FL 33487-2742
© 2010 by Taylor & Francis Group, LLC
CRC Press is an imprint of Taylor & Francis Group, an Informa business
No claim to original U.S. Government works
Version Date: 20140514
International Standard Book Number-13: 978-1-4398-1023-1 (eBook - PDF)
This book contains information obtained from authentic and highly regarded sources. Reasonable efforts
have been made to publish reliable data and information, but the author and publisher cannot assume
responsibility for the validity of all materials or the consequences of their use. The authors and publishers
have attempted to trace the copyright holders of all material reproduced in this publication and apologize to
copyright holders if permission to publish in this form has not been obtained. If any copyright material has
not been acknowledged please write and let us know so we may rectify in any future reprint.
Except as permitted under U.S. Copyright Law, no part of this book may be reprinted, reproduced, transmitted, or utilized in any form by any electronic, mechanical, or other means, now known or hereafter invented,
including photocopying, microfilming, and recording, or in any information storage or retrieval system,
without written permission from the publishers.
For permission to photocopy or use material electronically from this work, please access www.copyright.
com ( or contact the Copyright Clearance Center, Inc. (CCC), 222 Rosewood

Drive, Danvers, MA 01923, 978-750-8400. CCC is a not-for-profit organization that provides licenses and
registration for a variety of users. For organizations that have been granted a photocopy license by the CCC,
a separate system of payment has been arranged.
Trademark Notice: Product or corporate names may be trademarks or registered trademarks, and are used
only for identification and explanation without intent to infringe.
Visit the Taylor & Francis Web site at

and the CRC Press Web site at



Dedicated to Suzi, Mihal, Paul and Zohar



Contents

Preface

I

xv

Introducing MATLAB

1 Introduction to MATLAB
1.1 Starting MATLAB . . . . . . . . . . . .
1.2 Using MATLAB as a simple calculator .
1.3 How to quit MATLAB . . . . . . . . . .
1.4 Using MATLAB as a scientific calculator

1.4.1 Trigonometric functions . . . . .
1.4.2 Inverse trigonometric functions .
1.4.3 Other elementary functions . . .
1.5 Arrays of numbers . . . . . . . . . . . .
1.6 Using MATLAB for plotting . . . . . . .
1.6.1 Annotating a graph . . . . . . .
1.7 Format . . . . . . . . . . . . . . . . . . .
1.8 Arrays of numbers . . . . . . . . . . . .
1.8.1 Array elements . . . . . . . . . .
1.8.2 Plotting resolution . . . . . . . .
1.8.3 Array operations . . . . . . . . .
1.9 Writing simple functions in MATLAB .
1.10 Summary . . . . . . . . . . . . . . . . .
1.11 Examples . . . . . . . . . . . . . . . . .
1.12 More exercises . . . . . . . . . . . . . . .

1
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

3
3
5
9
10
10
13
15
16
18
20
21
22

22
23
24
27
31
34
42

2 Vectors and matrices
2.1 Vectors in geometry . . . . . . . . . . . . . . . . . .
2.1.1 Arrays of point coordinates in the plane . . .
2.1.2 The perimeter of a polygon – for loops . . . .
2.1.3 Vectorization . . . . . . . . . . . . . . . . . .
2.1.4 Arrays of point coordinates in solid geometry
2.1.5 Geometrical interpretation of vectors . . . . .
2.1.6 Operating with vectors . . . . . . . . . . . . .
2.1.7 Vector basis . . . . . . . . . . . . . . . . . . .
2.1.8 The scalar product . . . . . . . . . . . . . . .
2.2 Vectors in mechanics . . . . . . . . . . . . . . . . . .

.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.
.
.

47
48
48
52
55
56
61
63
65
66
69

.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

ix



x

What every engineer should know about MATLAB and Simulink
2.2.1 Forces. The resultant of two or more forces . . .
2.2.2 Work as a scalar product . . . . . . . . . . . . .
2.2.3 Velocities. Composition of velocities . . . . . . .
Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.3.1 Introduction – the matrix product . . . . . . . .
2.3.2 Determinants . . . . . . . . . . . . . . . . . . . .
Matrices in geometry . . . . . . . . . . . . . . . . . . . .
2.4.1 The vector product. Parallelogram area . . . . .
2.4.2 The scalar triple product. Parallelepiped volume
Transformations . . . . . . . . . . . . . . . . . . . . . . .
2.5.1 Translation — Matrix addition and subtraction .
2.5.2 Rotation . . . . . . . . . . . . . . . . . . . . . . .
2.5.3 Homogeneous coordinates . . . . . . . . . . . . .
Matrices in Mechanics . . . . . . . . . . . . . . . . . . .
2.6.1 Angular velocity . . . . . . . . . . . . . . . . . .
2.6.2 Center of mass . . . . . . . . . . . . . . . . . . .
2.6.3 Moments as vector products . . . . . . . . . . . .
Summary . . . . . . . . . . . . . . . . . . . . . . . . . .
More exercises . . . . . . . . . . . . . . . . . . . . . . . .

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

69
72
72
73
73
77
78

78
80
82
82
83
84
88
88
89
91
93
98

3 Equations
3.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . .
3.2 Linear equations in geometry . . . . . . . . . . . . . . .
3.2.1 The intersection of two lines . . . . . . . . . . . .
3.2.2 Cramer’s rule . . . . . . . . . . . . . . . . . . . .
3.2.3 MATLAB’s solution of linear equations . . . . .
3.2.4 An example of an ill-conditioned system . . . . .
3.2.5 The intersection of three planes . . . . . . . . . .
3.3 Linear equations in statics . . . . . . . . . . . . . . . . .
3.3.1 A simple beam . . . . . . . . . . . . . . . . . . .
3.4 Linear equations in electricity . . . . . . . . . . . . . . .
3.4.1 A DC circuit . . . . . . . . . . . . . . . . . . . .
3.4.2 The method of loop currents . . . . . . . . . . .
3.5 On the solution of linear equations . . . . . . . . . . . .
3.5.1 Homogeneous linear equations . . . . . . . . . . .
3.5.2 Overdetermined systems — least-squares solution
3.5.3 Underdetermined system . . . . . . . . . . . . . .

3.5.4 A singular system . . . . . . . . . . . . . . . . . .
3.5.5 Another singular system . . . . . . . . . . . . . .
3.6 Summary 1 . . . . . . . . . . . . . . . . . . . . . . . . .
3.7 More exercises . . . . . . . . . . . . . . . . . . . . . . . .
3.8 Polynomial equations . . . . . . . . . . . . . . . . . . . .
3.8.1 MATLAB representation of polynomials . . . . .
3.8.2 The MATLAB root function . . . . . . . . . . .
3.8.3 The MATLAB function conv . . . . . . . . . . .

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

103
103
103
103
104
105

107
109
109
109
112
112
114
116
116
119
123
126
128
132
134
135
135
135
137

2.3

2.4

2.5

2.6

2.7
2.8



Table of Contents

xi

3.9

Iterative solution of equations . . . . . . . . . . . . .
3.9.1 The Newton-Raphson method . . . . . . . . .
3.9.2 Solving an equation with the command fzero
3.10 Summary 2 . . . . . . . . . . . . . . . . . . . . . . .
3.11 More exercises . . . . . . . . . . . . . . . . . . . . . .
4 Processing and publishing the results
4.1 Copy and paste . . . . . . . . . . . . . . . .
4.2 Diary . . . . . . . . . . . . . . . . . . . . . .
4.3 Exporting and processing figures . . . . . .
4.4 Interpolation . . . . . . . . . . . . . . . . .
4.4.1 Interactive plotting and curve fitting
4.5 The MATLAB spline function . . . . . . .
4.6 Importing data from Excel – histograms .
4.7 Summary . . . . . . . . . . . . . . . . . . .
4.8 Exercises . . . . . . . . . . . . . . . . . . . .

II

.
.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.

.
.
.

.
.
.
.
.

.
.
.
.
.

143
143
147
148
149

.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.
.

151
151
152
152
153
153
157
165
167
169

Programming in MATLAB

5 Some facts about numerical computing
5.1 Introduction . . . . . . . . . . . . . . . . .
5.2 Computer-aided mistakes . . . . . . . . .
5.2.1 A loop that does not stop . . . . .
5.2.2 Errors in trigonometric functions .

5.2.3 An unexpected root . . . . . . . .
5.2.4 Other unexpected roots . . . . . .
5.2.5 Accumulating errors . . . . . . . .
5.3 Computer representation of numbers . . .
5.4 The set of computer numbers . . . . . . .
5.5 Roundoff . . . . . . . . . . . . . . . . . . .
5.6 Roundoff errors . . . . . . . . . . . . . . .
5.7 Computer arithmetic . . . . . . . . . . . .
5.8 Why the examples in Section 5.2 failed . .
5.8.1 Absorbtion . . . . . . . . . . . . .
5.8.2 Correcting a non-terminating loop
5.8.3 Second-degree equation . . . . . .
5.8.4 Unexpected polynomial roots . . .
5.9 Truncation error . . . . . . . . . . . . . .
5.10 Complexity . . . . . . . . . . . . . . . . .
5.10.1 Definition, examples . . . . . . . .
5.11 Horner’s scheme . . . . . . . . . . . . . . .
5.12 Problems that cannot be solved . . . . . .
5.13 Summary . . . . . . . . . . . . . . . . . .
5.14 More examples . . . . . . . . . . . . . . .
5.15 More exercises . . . . . . . . . . . . . . . .

171
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

173
173
174
175
176
176
178
179
180
184
186
187
191
193
193
194
194
196
199
202
202

205
206
208
209
211


xii

What every engineer should know about MATLAB and Simulink

6 Data types and object-oriented programming
6.1 Structures . . . . . . . . . . . . . . . . . . . .
6.1.1 Where structures can help . . . . . . .
6.1.2 Working with structures . . . . . . . .
6.2 Cell arrays . . . . . . . . . . . . . . . . . . . .
6.3 Classes and object-oriented programming . .
6.3.1 What is object-oriented programming?
6.3.2 Calculations with units . . . . . . . . .
6.3.3 Defining a class . . . . . . . . . . . . .
6.3.4 Defining a subclass . . . . . . . . . . .
6.3.5 Calculating with electrical units . . . .
6.4 Summary . . . . . . . . . . . . . . . . . . . .
6.5 Exercises . . . . . . . . . . . . . . . . . . . . .

III

.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.

Progressing in MATLAB

.
.
.
.
.
.
.
.
.
.
.
.

215
216
216
217

219
221
221
222
224
229
233
238
240

243

7 Complex numbers
7.1 The introduction of complex numbers . . . . . . . . . . . .
7.2 Complex numbers in MATLAB . . . . . . . . . . . . . . . .
7.3 Geometric representation . . . . . . . . . . . . . . . . . . .
7.4 Trigonometric representation . . . . . . . . . . . . . . . . .
7.5 Exponential representation . . . . . . . . . . . . . . . . . .
7.6 Functions of complex variables . . . . . . . . . . . . . . . .
7.7 Conformal mapping . . . . . . . . . . . . . . . . . . . . . .
7.8 Phasors . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
7.8.1 Phasors . . . . . . . . . . . . . . . . . . . . . . . . .
7.8.2 Phasors in mechanics . . . . . . . . . . . . . . . . . .
7.8.3 Phasors in electricity . . . . . . . . . . . . . . . . . .
7.9 An application in mechanical engineering — a mechanism .
7.9.1 A four-link mechanism . . . . . . . . . . . . . . . . .
7.9.2 Displacement analysis of the four-link mechanism . .
7.9.3 A MATLAB function that simulates the motion of the
four-link mechanism . . . . . . . . . . . . . . . . . .
7.9.4 Animation . . . . . . . . . . . . . . . . . . . . . . . .

7.9.5 A variant of the function FourLink . . . . . . . . . .
7.10 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . .
7.11 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

.
.
.
.
.
.
.
.
.
.
.
.
.
.

245
245
245
248
250
250
253
255
259
259
261

265
271
271
272

.
.
.
.
.

274
277
278
281
283

8 Numerical integration
8.1 Introduction . . . . . . . . . . . . . . .
8.2 The trapezoidal rule . . . . . . . . . .
8.2.1 The formula . . . . . . . . . . .
8.2.2 The MATLAB trapz function
8.3 Simpson’s rule . . . . . . . . . . . . . .
8.3.1 The formula . . . . . . . . . . .

.
.
.
.
.

.

287
287
288
288
289
290
290

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.


.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.

.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.

.
.
.


Table of Contents
.
.
.
.
.

.
.
.
.
.

292
293
295
297
298

9 Ordinary differential equations
9.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . .
9.2 Numerical solution of ordinary differential equations . . . .
9.2.1 Cauchy form . . . . . . . . . . . . . . . . . . . . . .
9.3 Numerical solution of ordinary differential equations . . . .
9.3.1 Specifying the times of the solution . . . . . . . . . .

9.3.2 Using alternative odesolvers . . . . . . . . . . . . . .
9.3.3 Passing parameters to the model . . . . . . . . . . .
9.4 Alternative strategies to solve ordinary differential equations
9.4.1 Runge–Kutta methods . . . . . . . . . . . . . . . . .
9.4.2 Predictor-corrector methods . . . . . . . . . . . . . .
9.4.3 Stiff systems . . . . . . . . . . . . . . . . . . . . . . .
9.5 Conclusion: How to choose the odesolver . . . . . . . . . . .
9.6 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

.
.
.
.
.
.
.
.
.
.
.
.
.

301
301
301
301
302
305
306

306
310
312
315
316
323
324

10 More graphics
10.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . .
10.2 Drawing at scale . . . . . . . . . . . . . . . . . . . . . .
10.3 The cone surface and conic sections . . . . . . . . . . . .
10.3.1 The cone surface . . . . . . . . . . . . . . . . . .
10.3.2 Conic sections . . . . . . . . . . . . . . . . . . . .
10.3.3 Developing the cone surface . . . . . . . . . . . .
10.3.4 A helicoidal curve on the cone surface . . . . . .
10.3.5 The listing of functions developed in this section
10.4 GUIs - graphical user interfaces . . . . . . . . . . . . . .
10.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . .
10.6 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . .

.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

327
327
327

330
330
332
336
337
338
343
355
356

11 An introduction to Simulink
11.1 What is simulation? . . . . . . . . . . . . . . . . . .
11.2 Beats . . . . . . . . . . . . . . . . . . . . . . . . . . .
11.3 A model of the momentum law . . . . . . . . . . . .
11.4 Capacitor discharge . . . . . . . . . . . . . . . . . . .
11.5 A mass–spring–dashpot system . . . . . . . . . . . .
11.6 A series RLC circuit . . . . . . . . . . . . . . . . . .
11.7 The pendulum . . . . . . . . . . . . . . . . . . . . .
11.7.1 The mathematical and the physical pendulum
11.7.2 The phase plane . . . . . . . . . . . . . . . .
11.7.3 Running the simulation from a script file . . .

.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

359
359
360
366
370

376
380
383
383
387
391

8.4
8.5
8.6
8.7

8.3.2 A function that implements Simpson’s rule
The MATLAB quadl function . . . . . . . . . . . .
Symbolic calculation of integrals . . . . . . . . . .
Summary . . . . . . . . . . . . . . . . . . . . . . .
Exercises . . . . . . . . . . . . . . . . . . . . . . . .

xiii
.
.
.
.
.

.
.
.
.
.


.
.
.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.

.
.
.
.


11.8 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393
12 Applications in the frequency domain
12.1 Introduction . . . . . . . . . . . . . .
12.2 Signals . . . . . . . . . . . . . . . . .
12.3 A short introduction to the DFT . .
12.4 The power spectrum . . . . . . . . .
12.5 Trigonometric expansion of a signal .
12.6 High frequency signals and aliasing .
12.7 Bode plot . . . . . . . . . . . . . . .
12.8 Summary . . . . . . . . . . . . . . .
12.9 Exercises . . . . . . . . . . . . . . . .

.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.

395
395
395
398
400
407
410
412
414
415

Answers to selected exercises

417

Bibliography

423

Index

427


xiv


Preface

Target audience
This book is primarily intended for students of mechanical and electrical engineering; it can also be useful to students of civil engineering, to students of
computer sciences, and to practicing engineers who want to learn MATLAB
and Simulink . Moreover, the book can be used by students of physics, and
in some European countries by high-school students in their last year before
the maturity examinations, and by technicians.

Objectives and general approach
The general objective is to teach MATLAB by showing that it is good for
engineering students and for practicing engineers. We do this by examples in
which MATLAB yields easy and elegant solutions of problems in mathematics
and mechanical and electrical engineering. In each group of applications we
start with geometrical examples because we consider geometry as essential
in mechanical and civil engineering. It is easier to understand many mathematical concepts by showing their geometrical interpretation. Moreover, to
understand and experimentally solve a problem of geometry one may need
as few tools as a ruler and compass, while experiments in mechanics or electricity require a laboratory. Geometry is the basis of computer graphics that
are extensively used in the design of machine parts, of car, airplane and ship
surfaces, and of building and architectural structures. Last, but not least, we
often appeal to geometry to visualize phenomena that belong to various fields
of engineering or science. MATLAB was initially developed as software for
linear algebra. There is a one-to-one correspondence between objects of linear
algebra and those of geometry. Showing this can give an important insight
into the nature of problems and their solutions.
The geometrical applications are followed by examples in those basic fields

of mechanical engineering that are taught in the first university courses. The
examples belong to mechanics, that is, statics, kinematics, dynamics and

xv


xvi

What every engineer should know about MATLAB and Simulink

mechanisms, where MATLAB data types, operations, and functions fit in
a most natural manner.
We end each group of applications with examples in electrical engineering.
Again, the examples come from fields that are taught in the first basic courses.
Often we deliberately introduce the same MATLAB concept more than
once, for instance, the first time by a geometrical example, the second time
time, by a mechanical example, and the third time, by an electrical example.
In this way we allow students to concentrate on those examples that belong
to their own sphere of interest. We also show in this way that the same
MATLAB facilities can be used in many branches of engineering or science.
We use examples to introduce MATLAB data types, operations, and functions, but we do not limit ourselves to isolated applications. We also try to
teach the reader general methods and algorithms for solving problems, how to
solve them in an efficient way, and how to avoid or minimize computer errors.
In the third part of the book we introduce the reader to Simulink . Throughout the book we prefer applications in which MATLAB and Simulink simplify
the formulation and make computing more elegant, and applications for which
MATLAB provides spectacular visualization. These applications belong to
disciplines taught in basic courses.

The contents of the book
The book is divided into three parts. In Part I, Introducing MATLAB, we

teach the reader how to start using MATLAB for solving problems and how
to process and present calculation and experimental results. As this part
contains the basics of working in MATLAB, its chapters are more detailed
and extensive than those of other parts. Part II, Programming in MATLAB,
shows that the computer has its limitations that one must take into account
when programming and when evaluating results. Part II also contains an introduction to object-oriented programming (OOP). In Part III, Progressing in
MATLAB, we introduce the reader to more advanced features of the software.
By the end of this part the reader should be able to solve most problems encountered in basic engineering courses. The knowledge acquired in this stage
will also allow the reader to go further to specialized MATLAB toolboxes.
In this part we introduce Simulink and a few very elementary applications of
MATLAB in control and signal processing.
Chapter 1, Introduction, shows that anything a calculator can do, MATLAB
can do better.
Chapter 2, Vectors and matrices, shows how MATLAB arrays can be used
to represent in a most natural and elegant way geometrical, mechanical and
electrical objects, and to perform meaningful operations on them. In this


Preface

xvii

chapter the reader also discovers that programming can help in solving tasks
that otherwise would be so tedious and time consuming that many users would
even choose not to deal with them. Beginning with this chapter we introduce
the readers to the subjects of program structuring and checking.
Chapter 3, Equations, shows how to treat this ubiquitous subject in MATLAB and what the limitations of this treatment are. To better understand
the subject we use concrete examples of intersecting lines and planes. Doing
so we can also give geometric interpretations of the notions of ill-conditioned
systems and of least-squares solutions. Given a system of linear equations,

MATLAB always yields an answer. This answer, however, can be a unique
solution, a particular solution, a solution in the least-squares sense, or no solution at all. We show how to distinguish among these cases. This chapter
continues with polynomial equations. The reader is warned that the function
root can yield erroneous results and we give some hints on how to detect and
correct errors. The chapter ends by describing an iterative method for solving
transcendental equations.
Students and engineers must also deliver reports of calculations. The results of laboratory experiments must be processed, visualized in graphs, interpreted, and presented in reports. Scientists publish papers describing such
results. In Chapter 4, Processing and publishing the results, we describe the
diary option that allows the user to record full working sessions. As these
records are in plain ASCII code, they can be further modified in a word processor or a typesetter. The data can be processed by interpolation and curve
fitting and presented in 3-D plots, histograms or other plots. MATLAB provides functions for connection with Excel and for producing figures that can
be readily inserted into Word or Tex files.
Part II starts with Chapter 5, Some facts about numerical computing. Not
a few students and engineers believe that once they use a computer they need
not care much about how to supply the input data, how to write a program
or how reliable are the results. The computer is supposed to be infallible and
capable of solving any problem. To prove that such beliefs are misconceptions
we describe the sources of numerical errors, we define computing complexity
and show how to improve it. Part of this chapter is based on Chapter 2 of
Biran and Breiner (2002); the rest is new.
Chapter 6, Data types and object-oriented programming, defines structures,
cell arrays and classes as MATLAB building blocks. This chapter includes
an elementary introduction to object-oriented programming (OOP) with an
application to calculating with units. We reuse in this chapter material from
Chapter 18 of Biran and Breiner (2002).
Part III starts with Chapter 7, Complex numbers. Once considered impossible, later a mathematical curiosity, complex numbers are now an indispensable
tool in the study of oscillations, electrical circuits and control engineering. We
show that in MATLAB complex numbers can be dealt with as easily as real
numbers can. The chapter ends with applications to a simple mechanism.
Chapter 8 is about Numerical integration. We reuse part of Chapter 11



xviii

What every engineer should know about MATLAB and Simulink

in Biran and Breiner (2002) and add some newer features introduced with
MATLAB 7. In this book we continue our policy of referring only to the basic
MATLAB package and not to toolboxes. However, in this chapter we make
an exception and give a few simple examples of symbolic integration with the
help of the Symbolic Math Toolbox TM . We do this to show the reader one
possible direction of going beyond the treatment exposed in this book.
Chapter 9, Ordinary differential equations, discusses the numerical integration of ordinary differential equations (ODEs). Several methods of integration
are described as well as the corresponding MATLAB functions. The material
is based on Chapter 14 of Biran and Breiner (2002).
Chapter 10, More graphics, continues what we have begun in the previous
chapters. The last part of the chapter is an introduction to graphical user
interfaces (GUIs).
In Chapter 11, An introduction to Simulink, we begin the description of this
important toolbox that is also part of the MATLAB for Students package. We
start with the simple example of the addition of two waves and continue with
examples requiring the integration of differential equations. Progressively
we go from first- to second-order linear equations and end with nonlinear
equations. The chapter contains examples in both mechanical and electrical
engineering.
Chapter 12, Applications in the frequency domain, is dedicated to some basic methods of signal processing and contains also an introduction to the Bode
diagram. The chapter contains slightly updated versions of several sections
of Biran and Breiner (2002).

The philosophy of the book

Let us begin by quoting from the Preface to Biran and Breiner (2002).
“The third edition of our book is meant to include some of the powerful
improvements introduced in MATLAB 6. Additionally, we are aware that
with this release the software grew to such an extent that the danger appears
of not being able to see the forest because of the trees. Often, MATLAB 6
provides several possibilities of performing the same task and the beginner
may get lost when faced with such a wide choice. Therefore, we think that
an important task of the book is to guide the reader through the MATLAB 6
forest and choose a sufficient set of commands and functions that enable the
completion of most engineering tasks.
The help facilities of MATLAB 6 contain excellent reference material. As
the reader can easily access that help, we feel no necessity to compete in
that direction, but leave our book as a tutorial, as it was conceived from
the beginning. We also continue our policy of introducing notions in small


Preface

xix

portions, dispersed throughout the book.”
If the above paragraphs were true for MATLAB 6, a fortiori they are valid
for the MATLAB versions that followed. There is no need to copy the manuals
and the help provided by the MathWorksTM . Further, we do not want to write
a reference book, because the reference manuals provided by The MathWorks
are excellent. In reference books each chapter is dedicated to one subject
and exhausts it. Then, for example, one has to learn everything on matrices
before learning how to do a simple plot. Our philosophy is to teach the reader
a bit of A, then a bit of B, and so on, and return to more of A, more of
B and so on. This is the difference between a book on the grammar of a

foreign language, and a book for learning the same language. In the latter,
the reader learns a bit about nouns, then a bit about verbs, enough to build
a very simple sentence. Returning to learn more about nouns, more about
verbs, and adding some knowledge about adjectives and adverbs, the reader
can now build a more complex sentence. This is the philosophy that led us
when we wrote our previous books, and this is the philosophy that guides us
in this book.
It is common knowledge that students learn easier when they are motivated.
In our book we explain the engineering importance, significance and implications of the calculations performed. Seeing that MATLAB can help them in
their current, immediate tasks can enhance the students’ wish to learn and
use the software.

Notations
To distinguish between explanations and what should appear on the computer
screen, we present the latter in gray frames, for example
4 - 2
ans =
2
Emboldened words indicate a key term being defined for the first time in
the text, for example array of numbers. Italics are used to emphasize, for
instance command history. Boldface letters are also used to name vectors or
matrices, where it is usual to write so:
P=

5
7

Typewriter characters are used for the names of MATLAB and Simulink
functions and commands (e.g., Delete), program and function listings and



xx

What every engineer should know about MATLAB and Simulink

names of files (e.g., F2C). Sometimes we also use boldface letters for the same
purpose.
MATLAB and Simulink are registered trademarks of The MathWorks,
Inc. For product information please contact:
The MathWorks, Inc.
3 Apple Hill Drive
Natick, MA, 01760-2098 USA
Tel: 508-647-7000
Fax: 508-647-7001
E-mail:
Web: www.mathworks.com

Companion software
Companion software can found at a site provided by The MathWorks
/>and one provided by the publisher
/>
Acknowledgments
We thank Simon Lake, Tracey Cummins, Nikki Bamister, Owen King, and
Laurence Gaillard of Pearson Education, UK, for permission to reuse extensive
parts of Biran and Breiner (2002).
For our first books, Courtney Esposito, of The MathWorks, and Baruch
and Abigail Pekelman of Omikron Delta provided us with the newest versions
of MATLAB and advice. Their work was continued by Naomi Fernandez and
the MathWorks Book Program.
We acknowledge the professional help of Tim Pitts and Karen Mosman who

were the editors of the first edition, MATLAB for Engineers, in 1995.
Irina Abramovici, of Technion’s Taub Computer Center, provided help in
LATEX for our previous books and continued to do so for this book.
We thank Allison Shatkin, Kari Budyk, Karen Simon, Shashi Kumar, James
Miller, and Joel Schwarz of Taylor & Francis for their contributions in editing
and producing the book.
Finally, many thanks to my wife, Suzi, for her patience, encouragement and
continuous support.


Part I

Introducing MATLAB

1



1
Introduction to MATLAB

We begin this chapter by showing how to start MATLAB and use it as a
calculator. Next, we show how to use MATLAB as a scientific calculator.
We begin to discover the superiority of MATLAB by storing numerical values
under the names of constants, by storing several numerical values in arrays
and by calling these values by name, for use in various operations. We further
learn how to plot simple functions, built in to MATLAB, and how to program
simple functions.
By the end of this chapter the reader will be able to perform in MATLAB
all the calculations previously carried on with the hand-held computer, and

will know how to plot the results and how to program simple functions that
may speed up repetitive calculations.

1.1

Starting MATLAB

We assume that you installed MATLAB according to the instructions you received and that you are familiar with your computer and operating system. To
start, double-click on the MATLAB icon, in Windows, or type the instruction
matlab, in Unix. One or more windows appear on your screen. The configuration depends on your MATLAB version and on the configuration you used
when closing the last working session. The default desktop corresponding to
version 7.7 (R2008b), is shown in Figure 1.1.
The various windows are identified by the names appearing in their title
bars: Command Window, marked 1 in Figure 1.1, Workspace, marked 2,
Command History, marked 3, and Current Directory, marked 4. For the
moment we are interested only in the Command Window. You may close the
other windows by clicking on the Close icon, that is on the ‘X’ located in
the upper, right-hand corner of each window. You can change the size of any
window by dragging the bars that separate it from other windows. Also try
the icons situated in the toolbar of each window, at the left of the Close icon.
Finally, you may click on Desktop in the main toolbar and choose the option
you are interested in. To restore the screen to the configuration shown in
Figure 1.1 click Desktop → Desktop Layout → Def ault. If necessary, it is
possible to enlarge the command window by clicking on the square situated

3


4


What every engineer should know about MATLAB and Simulink

2

4

5

1

3

FIGURE 1.1: The MATLAB desktop

two places at the left of the Close icon.
For multimedia introductions to MATLAB click on one of the blue items
listed in the information line situated at the top of the Command Window.
Another help, introduced in 2008, is the f x icon marked 5 in Figure 1.1;
clicking on it opens a box in which you can browse lists of the many built-in
functions provided by MATLAB.
The symbol
appears in the Command Window ; it prompts you to enter
a command. In the next subsection you’ll learn the simplest ones. For the
moment try
ver

to get details about the MATLAB version, the computer and the operating
system you are using. The display will also include a list of the MATLABrelated toolboxes installed on the computer.



×