Insight
Through
Computing
Downloaded 13 May 2012 to 202.201.0.66. Redistribution subject to SIAM license or copyright; see http://www.siam.org/journals/ojsa.php
Insight
Through
Computing
A MATLAB Introduction to
Computational Science and Engineering
Charles F. Van Loan
K.-Y. Daisy Fan
Cornell University
Ithaca, New York
Downloaded 13 May 2012 to 202.201.0.66. Redistribution subject to SIAM license or copyright; see http://www.siam.org/journals/ojsa.php
Society for Industrial and Applied Mathematics
Copyright © 2010 by the Society for Industrial and Applied Mathematics
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.
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.
Java is a trademark of Sun Microsystems, Inc. in the United States and other countries.
MATLAB is a registered trademark 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.
Big Ben image used courtesy of Wikimedia Commons.
Cover image and Figure 12.13 used with permission of Cornell University Photography.
Library of Congress Cataloging-in-Publication Data
Van Loan, Charles F.
Insight through computing : a MATLAB introduction to computational science and
engineering / Charles F. Van Loan, K.-Y. Daisy Fan.
p. cm.
Includes index.
ISBN 978-0-898716-91-7
1. Numerical analysis--Data processing. 2. Science--Computer simulation.
3. Engineering mathematics--Data processing. 4. MATLAB. I. Fan, K.-Y. Daisy. II. Title.
QA297.V25 2010
005.1--dc22 2009030277
is a registered trademark.
Downloaded 13 May 2012 to 202.201.0.66. Redistribution subject to SIAM license or copyright; see http://www.siam.org/journals/ojsa.php
To our families
Downloaded 13 May 2012 to 202.201.0.66. Redistribution subject to SIAM license or copyright; see http://www.siam.org/journals/ojsa.php
Contents
xi
Preface
Programming Topics
Software
xvii
xv
1
27
45
63
1
1.1
From Formula to Program
Just Plug It In!
3
Surface Area of a Sphere
Matlab: Arithmetic expressions, assignment, input, output
1.2
Check and Evaluate
14
Minimum of a Quadratic on an Interval
Matlab: Boolean expressions, conditionals
2
2.1
Limits and Error
Tiling a Disk
Summation
Matlab: The for-loop
29
2.2
Inside/Outside Polygons
36
Sequences
Matlab: The while-loop
3
3.1
Approximation with Fractions
22/7ths and Counting
47
Proximity to Pi
Matlab: Nested loops, benchmarking
3.2
Not Quite Perfect
56
Fibonacci Quotients and the Golden Ratio
Matlab: More complicated while-loops
4
4.1
4.2
The Discrete versus the Continuous
Connect the Dots
65
Plotting Continuous Functions
Matlab: Vectors, elementary graphics
From Cyan to Magenta
Color Computations
Matlab: rgb
79
4.3 One Third Plus One Third Is Not Two Thirds
87
The Floating Point Environment
Matlab: eps, inf, NaN
vii
Downloaded 13 May 2012 to 202.201.0.66. Redistribution subject to SIAM license or copyright; see http://www.siam.org/journals/ojsa.php
viii
5
5.1
Abstraction
Reshaping Rectangles
A Square and a Root
Matlab: Simple functions
97
5.2 Oval Odometer
109
Ellipse Perimeter
Matlab: Functions with multiple input parameters
5.3
The Betsy Ross Problem
118
Design Parameters
Matlab: Graphics functions
6
6.1
6.2
Randomness
Safety in Numbers
131
Monte Carlo Simulation
Matlab: More practice with boolean expressions
Dice and Compass
Random Walks
Matlab: More practice with while-loops
143
6.3 Order from Chaos
149
Polygon Averaging
Matlab: More practice with graphics and vectors
7
The Second Dimension
7.1
From Here to There
157
Transition Matrices
Matlab: Two-dimensional arrays
7.2
Contours and Cross Sections
166
Visualizing F (x, y)
Matlab: Contour plotting
7.3
Cool It!
171
Simulation on a Grid
Matlab: A(i,j) updating
8
Reordering
8.1
Cut and Deal
181
The Perfect Shuffle
Matlab: More practice with vectors and subscripting
8.2
188
Size Place
Sorting
Matlab: sort
Contents
95
129
155
179
Downloaded 13 May 2012 to 202.201.0.66. Redistribution subject to SIAM license or copyright; see http://www.siam.org/journals/ojsa.php
Contents
9
9.1
9.2
9.3
Search
Patterns in Proteins
199
Linear Search
Matlab: Character arrays
A Roman Numeral Phone Book
Binary Search
Matlab: Cell arrays
219
Changing Sign
Bisecting for Roots
Matlab: Functions as parameters
208
Points, Polygons, and Circles
10
10.1 How Far?
229
Distance Metrics
Matlab: Simple structures
10.2 Fenced in Twice?
237
Intersection
Matlab: More complicated structures, boolean-valued functions
10.3 Not Perfect?
244
Nearness in Shape
Matlab: Practice with structures
11
Text File Processing
11.1 Latitude and Daylight
257
Data Acquisition and Conversion
Matlab: Reading data from a text file
11.2 Nearby Millions
268
Writing and Representation
Matlab: Creating .dat and .bin files
The Matrix: Part II
12
12.1 Saving Private Rainbows
285
Linear Interpolation and Color Mapping
Matlab: row-by-row matrix setup
12.2 Known on the Corner
292
Bilinear Interpolation and Shading
Matlab: from f (x, y) toF(i,j)
12.3 Seven-by-Five
299
ix
197
227
255
283
Image Digitization
Matlab: Cell arrays of matrices
12.4 Picture This
306
Working with Image Data Files
Matlab: imread, imwrite, more practice with matrices
Downloaded 13 May 2012 to 202.201.0.66. Redistribution subject to SIAM license or copyright; see http://www.siam.org/journals/ojsa.php
x
13 Acoustic File Processing
321
13.1 The Clock Strikes
Acquisition and Playback
Matlab: wavread, sound, wavwrite
13.2 Dial N for Noise
326
Frequency and Sampling
Matlab: More practice with vectors
14 Divide and Conquer
14.1 Patterns within Patterns
337
Recursive Tiling
Matlab: Recursive functions
14.2 N and Half N
Merge Sort
Matlab: More practice with recursion
344
14.3 Looking for Trouble
354
Adaptive Interpolation
Matlab: Still more practice with recursion
15 Optimization
15.1 Shortest Route
365
The Combinatoric Explosion
Matlab: More practice with arrays
15.2 Best Bike
372
Constraints and Objective Functions
Matlab: More complicated nested loops
15.3 Most Likely Orbit
Model Building
Matlab: Interactive search
381
Appendix A. Refined Graphics
Appendix B. Mathematical Facts
Appendix C. MATLAB, Java, and C
Appendix D. Exit Interview
Index
Contents
319
335
363
389
411
417
423
425
Downloaded 13 May 2012 to 202.201.0.66. Redistribution subject to SIAM license or copyright; see http://www.siam.org/journals/ojsa.php