Example: stock market

Matlab: a Practical Introduction to

matlab : a Practical Introduction to Programming and Problem SolvingMatlab: a Practical Introduction to Programming and Problem SolvingByStormy AttawayCollege of Engineering, Boston UniversityBoston, MAAMSTERDAM BOSTON HEIDELBERG LONDONNEW YORK OXFORD PARIS SAN DIEGOSAN FRANCISCO SINGAPORE SYDNEY TOKYOB utterworth-Heinemann is an imprint of ElsevierThis page intentionally left blankButterworth-Heinemann is an imprint of Elsevier30 Corporate Drive, Suite 400, Burlington, MA 01803, USAL inacre House, Jordan Hill, Oxford OX2 8DP, UKCopyright 2009, Elsevier, Inc. All rights part of this publication may be reproduced or transmitted in any form or by anymeans, electronic or mechanical, including photocopy, recording, or any informationstorage and retrieval system, without permission in writing from the may be sought directly from Elsevier s Science & Technology RightsDepartment in Oxford, UK: phone: (+44) 1865 843830, fax: (+44) 1865 853333,E-mail: You may also complete your request onlinevia the Elsevier homepage ( ), by selecting Support & Contact then Copyright and Permission and then Obtaining Permissions.

a Practical Introduction to Programming and Problem Solving By Stormy Attaway College of Engineering, Boston University Boston, MA AMSTERDAM • BOSTON • HEIDELBERG • LONDON NEW YORK • OXFORD • PARIS • SAN DIEGO SAN FRANCISCO • SINGAPORE • SYDNEY • TOKYO Butterworth-Heinemann is an imprint of Elsevier

Tags:

  Introduction, Practical, Matlab, Practical introduction

Information

Domain:

Source:

Link to this page:

Please notify us if you found a problem with this document:

Other abuse

Advertisement

Transcription of Matlab: a Practical Introduction to

1 matlab : a Practical Introduction to Programming and Problem SolvingMatlab: a Practical Introduction to Programming and Problem SolvingByStormy AttawayCollege of Engineering, Boston UniversityBoston, MAAMSTERDAM BOSTON HEIDELBERG LONDONNEW YORK OXFORD PARIS SAN DIEGOSAN FRANCISCO SINGAPORE SYDNEY TOKYOB utterworth-Heinemann is an imprint of ElsevierThis page intentionally left blankButterworth-Heinemann is an imprint of Elsevier30 Corporate Drive, Suite 400, Burlington, MA 01803, USAL inacre House, Jordan Hill, Oxford OX2 8DP, UKCopyright 2009, Elsevier, Inc. All rights part of this publication may be reproduced or transmitted in any form or by anymeans, electronic or mechanical, including photocopy, recording, or any informationstorage and retrieval system, without permission in writing from the may be sought directly from Elsevier s Science & Technology RightsDepartment in Oxford, UK: phone: (+44) 1865 843830, fax: (+44) 1865 853333,E-mail: You may also complete your request onlinevia the Elsevier homepage ( ), by selecting Support & Contact then Copyright and Permission and then Obtaining Permissions.

2 Library of Congress Cataloging-in-Publication DataApplication submittedBritish Library Cataloguing-in-Publication DataA catalogue record for this book is available from the British : 978-0-75-068762-1 For information on all Butterworth-Heinemann publicationsvisit our Web site at in Canada08 09 10 9 8 7 6 5 4 3 2 1 Attribution matlab and Handle Graphics are registered trademarks of The MathWorks, book is dedicated to my two mentors at Boston University: the late Bill Henneman of the Computer Science Department, and the late Merrill Ebner of the Department of Manufacturing AttawayThis page intentionally left blankviiPreface ..xiiiPart I Programming and Problem Solving Using matlab ..1 ChAPTer 1 Introduction to matlab ..3 Getting into matlab ..4 Variables and assignment Statements ..6 Initializing, Incrementing, and Decrementing ..8 Variable Names ..8 expressions ..10 The Format function and ellipsis ..10 Operators ..11 Built-In functions and Help.

3 12 constants ..14 Types ..14 random Numbers ..16 characters and encoding ..18 Vectors and Matrices ..20 creating row Vectors ..21 creating column Vectors ..24 creating Matrix Variables ..24 Dimensions ..28 Using functions with Vectors and Matrices ..33 empty Vectors ..33 ChAPTer 2 Introduction to matlab Programming ..41 algorithms ..42 matlab Scripts ..43 Documentation ..45 Input and Output ..46 ContentsContentsviii Input function ..46 Output Statements: disp and fprintf ..48 Scripts with Input and Output ..53 Scripts to Produce and customize Simple Plots ..54 The Plot function ..54 Simple related Plot functions ..57 Introduction to file Input/Output (Load and Save) ..59 Writing Data to a file ..60 appending Data to a Data file ..61 reading from a file ..61 User-Defined functions that return a Single function Definitions ..65 calling a function ..66 calling a User-Defined function from a Script ..67 Passing Multiple arguments.

4 67 functions with Local 3 Selection Statements ..79 relational expressions ..80 The if representing Logical True and The if-else Statement ..87 Nested if-else Statements ..88 The Switch Statement ..93 The Menu function ..96 The is functions in matlab ..98 ChAPTer 4 Looping ..109 The for Loop ..110 finding Sums and Products ..111 combining for Loops with if Statements ..118 for Loops that Do Not Use the Iterator Variable in the action ..119 Input in a for Loop ..120 Nested for Loops ..122 Nested Loops and Matrices ..126 combining Nested for Loops and if Statements ..130 Vectorizing ..131 Logical Vectors ..133 Vectors and Matrices as function arguments ..140ixContents While Multiple conditions in a While Loop ..145 reading from a file in a While Loop ..145 Input in a While Loop ..147 counting in a While Loop ..148 error-checking User Input in a While Loop ..149 ChAPTer 5 matlab Programs ..161 More Types of User-Defined functions.

5 162 functions that return More than One Value ..162 functions that accomplish a Task Without returning Values ..166 functions that return Values Versus Printing ..167 Passing arguments to functions ..168 matlab Program Organization ..170 Modular Programs ..170 Subfunctions ..172 application: Menu-Driven Modular Program ..174 Variable Scope ..182 Persistent Variables ..184 Debugging Techniques ..185 Types of errors ..186 Tracing ..188 function Stubs ..190 ChAPTer 6 String Manipulation ..197 creating String Variables ..198 Strings as Vectors ..198 Operations on Strings ..200 concatenation ..200 creating customized Strings ..202 removing Whitespace characters ..206 changing case ..207 comparing Strings ..207 finding, replacing, and Separating Strings ..209 evaluating a String ..213 The is function for Strings ..214 converting between String and Number Types ..215 ContentsxChAPTer 7 Data Structures: cell arrays and Structures.

6 223 cell arrays ..224 creating cell arrays ..224 Structures ..229 creating and Modifying Structure Variables ..229 Passing Structures to functions ..231 related Structure functions ..232 Vectors of Nested Structures ..241 Vectors of Nested Structures ..243 ChAPTer 8 advanced file Input and Output ..253 Lower Level file I/O functions ..254 Opening and closing a file ..254 reading from files ..255 Writing to files ..262 appending to Writing and reading Spreadsheet Using MaT-files for Variables ..266 Writing Variables to a file ..266 appending Variables to a MaT-file ..267 reading from a MaT-file ..267 ChAPTer 9 advanced functions ..273 anonymous functions ..273 Uses of function Handles ..275 function functions ..276 Variable Numbers of arguments ..278 Variable Number of Input arguments ..279 Variable Number of Output arguments ..280 Nested functions ..284 recursive II Applications ..295 ChAPTer 10 matlab Plots.

7 297 Plot functions ..297 Matrix of Plots ..298 Plot Types ..299 animation ..302 Three-Dimensional Plots ..303 customizing Plots ..304xiContents Graphics Properties ..306 Plot applications ..308 Plotting from a function ..308 Plotting file Data ..309 ChAPTer 11 Solving Systems of Linear algebraic equations ..321 Matrix Definitions ..322 Matrix Square Matrices ..324 Matrix Operations ..328 Matrix Multiplication ..330 Vector Operations ..334 Matrix Solutions to Systems of Linear algebraic equations ..336 Solving 2 2 Systems of equations ..338 Gauss, Gauss-Jordan elimination ..341 reduced row echelon form ..347 finding a Matrix Inverse by reducing an augmented Matrix ..348 Symbolic Mathematics ..348 Symbolic Variables and expressions ..349 Simplification functions ..350 Displaying expressions ..351 Solving 12 Basic Statistics, Searching, and Sorting ..361 Statistical functions ..362 Mean ..363 Variance and Standard Deviation.

8 366 Mode ..367 Median ..368 Set Operations ..368 Sorting ..372 Sorting Vectors of Structures ..374 Sorting Strings ..377 Indexing ..379 Indexing into Vectors of Structures ..381 Searching ..382 Sequential Search ..382 Binary Search ..383 ChAPTer 13 Sights and Sounds ..393 Sound files ..393 Contentsxii Introduction to Handle Graphics ..395 Graphics Objects and Their Properties ..395 Image Processing ..400 Introduction to Graphical User Interfaces ..405 ChAPTer 14 advanced Mathematics ..421 fitting curves to Data ..421 Polynomials ..422 curve Interpolation and extrapolation ..423 Least Squares ..426 complex Numbers ..429 equality for complex Numbers ..431 adding and Subtracting complex Numbers ..431 Multiplying complex Numbers ..432 complex conjugate and absolute Value ..433 complex equations represented as Polynomials ..433 Polar form ..434 Plotting ..434 calculus: Integration and Differentiation ..435 Trapezoidal rule.

9 435 calculus in Symbolic Math Toolbox ..438 Index ..447xiiiMotivationThe purpose of this book is to teach basic programming concepts and skills needed for basic problem solving, all using matlab as the vehicle. matlab is a powerful software package that has built-in functions to accomplish a diverse range of tasks, from mathematical operations to three-dimensional imaging. Additionally, matlab has a complete set of programming constructs that allows users to customize programs to their own are many books that introduce matlab . There are two basic flavors of these books: those that demonstrate the use of the built-in functions in matlab , with a chapter or two on some programming concepts, and those that cover only the programming constructs without mentioning many of the built-in functions that make matlab efficient to use. Someone who learns just the built-in functions will be well-prepared to use matlab , but would not under-stand basic programming concepts.

10 That person then would not be able to learn a language such as C++ or Java without taking another introductory course, or reading another book, on the programming concepts. Conversely, anyone who learns only programming concepts first (using any language) would tend to write highly inefficient code using control statements to solve problems, not realizing that in many cases these are not necessary in book instead takes a hybrid approach, introducing both the programming and the efficient uses. The challenge for students is that it is nearly impossi-ble to predict whether they will in fact need to know programming concepts later on or whether a software package such as matlab will suffice for their careers. Therefore, the best approach for beginning students is to give them both the programming concepts and the efficient built-in functions. Since matlab is very easy to use, it is a perfect platform for this approach to teaching programming and problem programming concepts are critically important to this book, emphasis is not placed on the time-saving features that evolve with every new matlab release.


Related search queries