Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Learning_MATLAB.pdf
Скачиваний:
27
Добавлен:
12.03.2015
Размер:
1.02 Mб
Скачать

Learning

MATLAB

Learning

MATLAB

Tobin A. Driscoll

University of Delaware

Newark, Delaware

Society for Industrial and Applied Mathematics

Copyright ©2009 by the Society for Industrial and Applied Mathematics (SIAM)

10 9 8 7 6 5 4 3 2 1

All rights reserved. Printed in the United States of America. No part of this book may be reproduced, stored, or transmitted in any manner without the written permission of the publisher. For information, write to the Society for Industrial and Applied Mathematics, 3600 Market Street, 6th Floor, Philadelphia, PA 19104-2688 USA.

Cover photo by John Pelesko and Tobin Driscoll. Photo taken in the Modeling, Experiment, and Computation Lab at the University of Delaware (www.math.udel.edu/MECLAB).

Trademarked names may be used in this book without the inclusion of a trademark symbol. These names are used in an editorial context only; no infringement of trademark is intended.

Maple is a registered trademark of Waterloo Maple, Inc.

Mathematica is a registered trademark of Wolfram Research, Inc.

MATLAB and the Optimization Toolbox are registered trademarks of The MathWorks, Inc. For MATLAB product information, please contact The MathWorks, Inc., 3 Apple Hill Drive, Natick, MA 01760-2098 USA, 508-647-7000,

Fax: 508-647-7001, info@mathworks.com, www.mathworks.com.

Windows is a registered trademark of Microsoft Corporation in the United States and/or other countries.

Word is a trademark of Microsoft Corporation in the United States and/or other countries.

Library of Congress Cataloging-in-Publication Data

Driscoll, Tobin A. (Tobin Allen), 1969-

 

Learning MATLAB / Tobin A. Driscoll.

 

p. cm.

 

Includes index.

 

ISBN 978-0-898716-83-2

 

1. Mathematics--Data processing. 2. MATLAB. I. Title.

 

QA76.95.D75 2009

 

620.001'51--dc22

2009008516

is a registered trademark.

To Jen, Luke, and Adam,

who are my greatest teachers.

Contents

List of Figures

ix

List of Tables

 

xi

Preface

 

xiii

1

Introduction

1

 

1.1

The fifty-cent tour . . . . . . . . . . . . . . . . . . . . . . .

2

 

1.2

Graphical versus command-line usage . . . . . . . . . . . .

3

 

1.3

Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

3

 

1.4

Basic commands and syntax . . . . . . . . . . . . . . . . . .

4

 

1.5

Saving and loading work . . . . . . . . . . . . . . . . . . .

6

1.6Things about MATLAB that are very nice to know,

 

 

but which often do not come to the attention of beginners . .

7

 

Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

8

2

Arrays and Matrices

11

 

2.1

Building arrays and matrices . . . . . . . . . . . . . . . . .

12

 

2.2

Referencing elements . . . . . . . . . . . . . . . . . . . . .

14

 

2.3

Matrix operations . . . . . . . . . . . . . . . . . . . . . . .

18

 

2.4

Array operations . . . . . . . . . . . . . . . . . . . . . . . .

20

 

2.5

Sparse matrices . . . . . . . . . . . . . . . . . . . . . . . .

23

 

Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

25

3

Scripts and Functions

27

 

3.1

Using scripts effectively . . . . . . . . . . . . . . . . . . . .

28

 

3.2

Functions and workspaces . . . . . . . . . . . . . . . . . . .

29

 

3.3

Conditionals: if and switch . . . . . . . . . . . . . . . .

31

 

3.4

Loops: for and while . . . . . . . . . . . . . . . . . . . .

32

 

3.5

Debugging and profiling . . . . . . . . . . . . . . . . . . . .

33

 

Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

34

4

More on Functions

37

 

4.1

Function handles and anonymous functions . . . . . . . . . .

37

 

4.2

Subfunctions and nested functions . . . . . . . . . . . . . .

39

vii

viii

 

 

 

Contents

 

4.3

Errors and warnings . . . . . . . . . . . . . . . . . . .

. . . 41

 

4.4

Input and output arguments, revisited . . . . . . . . . .

. . . 41

 

Exercises . . . .

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

. . . 42

5

Graphics

 

45

 

5.1

Data plots versus function plots . . . . . . . . . . . . .

. . . 45

 

 

5.1.1

ez plots . . . . . . . . . . . . . . . . . . . .

. . . 46

 

 

5.1.2

Two-dimensional data plots . . . . . . . . . .

. . . 48

 

 

5.1.3

Three-dimensional data plots . . . . . . . . .

. . . 49

 

5.2

Annotation . . . . . . . . . . . . . . . . . . . . . . . .

. . . 50

 

5.3

Handles and properties . . . . . . . . . . . . . . . . .

. . . 51

 

5.4

Color . .

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

. . . 52

 

5.5

Saving and exporting figures . . . . . . . . . . . . . .

. . . 53

 

5.6

Other common graphics techniques . . . . . . . . . . .

. . . 55

 

Exercises . . . .

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

. . . 56

6

Advanced Techniques

59

 

6.1

Memory preallocation . . . . . . . . . . . . . . . . . .

. . . 59

 

6.2

Vectorization . . . . . . . . . . . . . . . . . . . . . . .

. . . 60

 

6.3

Masking

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

. . . 63

 

6.4

Scoping exceptions . . . . . . . . . . . . . . . . . . .

. . . 64

 

6.5

Strings .

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

. . . 65

 

6.6

Cell arrays . . . . . . . . . . . . . . . . . . . . . . . .

. . . 67

 

6.7

Structures . . . . . . . . . . . . . . . . . . . . . . . .

. . . 70

 

Exercises . . . .

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

. . . 72

7

Scientific Computing

75

 

7.1

Linear algebra . . . . . . . . . . . . . . . . . . . . . .

. . . 75

 

7.2

Iterative linear algebra . . . . . . . . . . . . . . . . . .

. . . 78

 

7.3

Rootfinding . . . . . . . . . . . . . . . . . . . . . . .

. . . 79

 

7.4

Optimization . . . . . . . . . . . . . . . . . . . . . . .

. . . 80

 

7.5

Data fitting and interpolation . . . . . . . . . . . . . .

. . . 81

 

7.6

Integration . . . . . . . . . . . . . . . . . . . . . . . .

. . . 84

 

7.7

Initial-value problems . . . . . . . . . . . . . . . . . .

. . . 85

 

7.8

Boundary-value problems . . . . . . . . . . . . . . . .

. . . 86

 

7.9

Time-dependent partial differential equations . . . . . .

. . . 88

 

Exercises . . . .

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

. . . 90

Afterword

 

 

93

Index

 

 

 

95

List of Figures

1.1Initial MATLAB desktop window. (Microsoft® Windows® XP

version shown.) . . . . . . . . . . . . . . . . . . . . . . . . . .

2

3.1Debugging tools in the MATLAB Editor. . . . . . . . . . . . . 33

5.1A partial graphics object hierarchy. Except for the group/series

case, each name is a graphics object type as well as a low-level command name. . . . . . . . . . . . . . . . . . . . . . . . . . 46

5.2 Result of an ezplot command. . . . . . . . . . . . . . . . . . 47 5.3 Semilog plot of U.S. census data, and an extrapolation. . . . . . 49

7.1Example of a least-squares fit to data. . . . . . . . . . . . . . . 82

7.2One-dimensional interpolation. . . . . . . . . . . . . . . . . . 83

7.3Solution of the Allen–Cahn equation by bvp4c. . . . . . . . . 88

7.4Method of lines result for a PDE in one dimension. . . . . . . . 89

ix

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]