Matlab:A Practical Introduction toProgramming and Problem Solving

Matlab:A Practical Introduction toProgramming and ProblemSolvingByStormy AttawayCollege of Engineering, Boston UniversityBoston, MAAMSTERDAM BOSTON HEIDELBERG LONDONNEW YORK OXFORD PARIS SAN DIEGOSAN FRANCISCO SINGAPORE SYDNEY TOKYOButterworth-Heinemann is an imprint of Elsevier

This page intentionally left blank

Butterworth-Heinemann is an imprint of Elsevier30 Corporate Drive, Suite 400, Burlington, MA 01803, USALinacre House, Jordan Hill, Oxford OX2 8DP, UKCopyright 2009, Elsevier, Inc. All rights reserved.No 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 publisher.Permissions may be sought directly from Elsevier’s Science & Technology RightsDepartment in Oxford, UK: phone: ( 44) 1865 843830, fax: ( 44) 1865 853333,E-mail: [email protected] You may also complete your request onlinevia the Elsevier homepage (, by selecting “Support & Contact”then “Copyright and Permission” and then “Obtaining Permissions.”Library of Congress Cataloging-in-Publication DataApplication submittedBritish Library Cataloguing-in-Publication DataA catalogue record for this book is available from the British Library.ISBN: 978-0-75-068762-1For information on all Butterworth-Heinemann publicationsvisit our Web site at www.elsevierdirect.comAttributionMATLAB and Handle Graphics are registered trademarksof The MathWorks, Inc.Printed in Canada08â ‡ 09â ‡ 10⠇⠇ 9â ‡ 8â ‡ 7â ‡ 6â ‡ 5â ‡ 4â ‡ 3â ‡ 2â ‡ 1

DedicationThis book is dedicated to my two mentors at Boston University: the lateBill Henneman of the Computer Science Department, and the late Merrill Ebnerof the Department of Manufacturing Engineering.Stormy Attaway

This page intentionally left blank

ContentsPreface. xiiiPart I rogramming and Problem SolvingPUsing MATLAB. 1Chapter 1Introduction to MATLAB. 31.1 Getting into MATLAB. 41.2 Variables and Assignment Statements. 61.2.1 Initializing, Incrementing, and Decrementing. 81.2.2 Variable Names. 81.3 Expressions. 101.3.1 The Format Function and Ellipsis. 101.3.2 Operators. 111.3.3 Built-In Functions and Help. 121.3.4 Constants. 141.3.5 Types. 141.3.6 Random Numbers. 161.4 Characters and Encoding. 181.5 Vectors and Matrices. 201.5.1 Creating Row Vectors. 211.5.2 Creating Column Vectors. 241.5.3 Creating Matrix Variables. 241.5.4 Dimensions. 281.5.5 Using Functions with Vectors and Matrices. 331.5.6 Empty Vectors. 33Chapter 2Introduction to MATLAB Programming. 412.1 Algorithms. 422.2 MATLAB Scripts. 432.2.1 Documentation. 452.3 Input and Output. 46vii

viiiContents2.3.1 Input Function. 462.3.2 Output Statements: disp and fprintf. 482.4 Scripts with Input and Output. 532.5 Scripts to Produce and Customize Simple Plots. 542.5.1 The Plot Function. 542.5.2 Simple Related Plot Functions. 572.6 Introduction to File Input/Output (Load and Save). 592.6.1 Writing Data to a File. 602.6.2 Appending Data to a Data File. 612.6.3 Reading from a File. 612.7 User-Defined Functions that Return a Single Value. 642.7.1 Function Definitions. 652.7.2 Calling a Function. 662.7.3 Calling a User-Defined Function from aScript. 672.7.4 Passing Multiple Arguments. 672.7.5 Functions with Local Variables. 68Chapter 3Selection Statements. 793.1 Relational Expressions. 803.2 The if Statement. 823.2.1 Representing Logical True and False. 863.3 The if-Else Statement. 873.4 Nested if-Else Statements. 883.5 The Switch Statement. 933.6 The Menu Function. 963.7 The is Functions in MATLAB. 98Chapter 4Looping. 1094.1 The for Loop. 1104.1.1 Finding Sums and Products. 1114.1.2 Combining for Loops with if Statements. 1184.1.3 For Loops that Do Not Use the IteratorVariable in the Action. 1194.1.4 Input in a for Loop. 1204.2 Nested for Loops. 1224.2.1 Nested Loops and Matrices. 1264.2.2 Combining Nested for Loops and ifStatements. 1304.3 Vectorizing. 1314.3.1 Logical Vectors. 1334.3.2 Vectors and Matrices as Function Arguments.140

Contents4.4 While Loops. 1434.4.1 Multiple Conditions in a While Loop. 1454.4.2 Reading from a File in a While Loop. 1454.4.3 Input in a While Loop. 1474.4.4 Counting in a While Loop. 1484.4.5 Error-Checking User Input in a While Loop. 149Chapter 5MATLAB Programs. 1615.1 More Types of User-Defined Functions. 1625.1.1 Functions that Return More thanOne Value. 1625.1.2 Functions that Accomplish a TaskWithout Returning Values. 1665.1.3 Functions that Return Values VersusPrinting. 1675.1.4 Passing Arguments to Functions. 1685.2 Matlab Program Organization. 1705.2.1 Modular Programs. 1705.2.2 Subfunctions. 1725.3 Application: Menu-Driven Modular Program. 1745.4 Variable Scope. 1825.4.1 Persistent Variables. 1845.5 Debugging Techniques. 1855.5.1 Types of Errors. 1865.5.2 Tracing. 1885.5.3 Editor/Debugger. 1895.5.4 Function Stubs. 190Chapter 6String Manipulation. 1976.1 Creating String Variables. 1986.1.1 Strings as Vectors. 1986.2 Operations on Strings. 2006.2.1 Concatenation. 2006.2.2 Creating Customized Strings. 2026.2.3 Removing Whitespace Characters. 2066.2.4 Changing Case. 2076.2.5 Comparing Strings. 2076.2.6 Finding, Replacing, and SeparatingStrings. 2096.2.7 Evaluating a String. 2136.3 The is Function for Strings. 2146.4 Converting between String and Number Types. 215ix

xContentsChapter 7Data Structures: Cell Arrays and Structures. 2237.1 Cell Arrays. 2247.1.1 Creating Cell Arrays. 2247.2 Structures. 2297.2.1 Creating and Modifying StructureVariables. 2297.2.2 Passing Structures to Functions. 2317.2.3 Related Structure Functions. 2327.2.4 Vectors of Structures. 2347.2.5 Nested Structures. 2417.2.6 Vectors of Nested Structures. 243Chapter 8Advanced File Input and Output. 2538.1 Lower Level File I/O Functions. 2548.1.1 Opening and Closing a File. 2548.1.2 Reading from Files. 2558.1.3 Writing to Files. 2628.1.4 Appending to Files. 2648.2 Writing and Reading Spreadsheet Files. 2648.3 Using MAT-Files for Variables. 2668.3.1 Writing Variables to a File. 2668.3.2 Appending Variables to a MAT-File. 2678.3.3 Reading from a MAT-File. 267Chapter 9Advanced Functions. 2739.1 Anonymous Functions. 2739.2 Uses of Function Handles. 2759.2.1 Function Functions. 2769.3 Variable Numbers of Arguments. 2789.3.1 Variable Number of Input Arguments. 2799.3.2 Variable Number of Output Arguments. 2809.4 Nested Functions. 2849.5 Recursive Functions. 287Part IIApplications. . 295Chapter 10 MATLAB Plots. 29710.1 Plot Functions. 29710.1.1 Matrix of Plots. 29810.1.2 Plot Types. 29910.2 Animation. 30210.3 Three-Dimensional Plots. 30310.4 Customizing Plots. 304

Contents10.5 Graphics Properties. 30610.6 Plot Applications. 30810.6.1 Plotting from a Function. 30810.6.2 Plotting File Data. 309Chapter 11 Solving Systems of Linear Algebraic Equations. 32111.1 Matrix Definitions.

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. This page intentionally left blank. Butterworth