ebook img

Insight through computing: a MATLAB introduction to computational science and engineering PDF

437 Pages·2010·6.503 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Insight through computing: a MATLAB introduction to computational science and engineering

Insight Through Computing Insight Through Computing A MATLAB Introduction to Computational Science and Engineering Charles F. Van Loan K.-Y. Daisy Fan Cornell University Ithaca, New York Society for Industrial and Applied Mathematics Copyright © 2010by 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, [email protected], 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. To our families (cid:2) Contents Preface xi ProgrammingTopics xv Software xvii 1 From Formula to Program 1 1.1 JustPlugItIn! 3 SurfaceAreaofaSphere Matlab: Arithmeticexpressions,assignment,input,output 1.2 CheckandEvaluate 14 MinimumofaQuadraticonanInterval Matlab: Booleanexpressions,conditionals 2 Limits and Error 27 2.1 TilingaDisk 29 Summation Matlab: Thefor-loop 2.2 Inside/OutsidePolygons 36 Sequences Matlab: Thewhile-loop 3 Approximation with Fractions 45 3.1 22/7thsandCounting 47 ProximitytoPi Matlab: Nestedloops,benchmarking 3.2 NotQuitePerfect 56 FibonacciQuotientsandtheGoldenRatio Matlab: Morecomplicatedwhile-loops 4 The Discrete versus the Continuous 63 4.1 ConnecttheDots 65 PlottingContinuousFunctions Matlab: Vectors,elementarygraphics 4.2 FromCyantoMagenta 79 ColorComputations Matlab: rgb 4.3 OneThirdPlusOneThirdIsNotTwoThirds 87 TheFloatingPointEnvironment Matlab: eps,inf,NaN vii viii Contents 5 Abstraction 95 5.1 ReshapingRectangles 97 ASquareandaRoot Matlab: Simplefunctions 5.2 OvalOdometer 109 EllipsePerimeter Matlab: Functionswithmultipleinputparameters 5.3 TheBetsyRossProblem 118 DesignParameters Matlab: Graphicsfunctions 6 Randomness 129 6.1 SafetyinNumbers 131 MonteCarloSimulation Matlab: Morepracticewithbooleanexpressions 6.2 DiceandCompass 143 RandomWalks Matlab: Morepracticewithwhile-loops 6.3 OrderfromChaos 149 PolygonAveraging Matlab: Morepracticewithgraphicsandvectors 7 The Second Dimension 155 7.1 FromHeretoThere 157 TransitionMatrices Matlab: Two-dimensionalarrays 7.2 ContoursandCrossSections 166 VisualizingF(x,y) Matlab: Contourplotting 7.3 CoolIt! 171 SimulationonaGrid Matlab: A(i,j)updating 8 Reordering 179 8.1 CutandDeal 181 ThePerfectShuffle Matlab: Morepracticewithvectorsandsubscripting 8.2 SizePlace 188 Sorting Matlab: sort Contents ix 9 Search 197 9.1 PatternsinProteins 199 LinearSearch Matlab: Characterarrays 9.2 ARomanNumeralPhoneBook 208 BinarySearch Matlab: Cellarrays 9.3 ChangingSign 219 BisectingforRoots Matlab: Functionsasparameters 10 Points, Polygons, and Circles 227 10.1 HowFar? 229 DistanceMetrics Matlab: Simplestructures 10.2 FencedinTwice? 237 Intersection Matlab: Morecomplicatedstructures,boolean-valuedfunctions 10.3 NotPerfect? 244 NearnessinShape Matlab: Practicewithstructures 11 Text File Processing 255 11.1 LatitudeandDaylight 257 DataAcquisitionandConversion Matlab: Readingdatafromatextfile 11.2 NearbyMillions 268 WritingandRepresentation Matlab: Creating.datand.binfiles 12 The Matrix: Part II 283 12.1 SavingPrivateRainbows 285 LinearInterpolationandColorMapping Matlab: row-by-rowmatrixsetup 12.2 KnownontheCorner 292 BilinearInterpolationandShading Matlab: fromf(x,y)toF(i,j) 12.3 Seven-by-Five 299 ImageDigitization Matlab: Cellarraysofmatrices 12.4 PictureThis 306 WorkingwithImageDataFiles Matlab: imread,imwrite,morepracticewithmatrices x Contents 13 Acoustic File Processing 319 13.1 TheClockStrikes 321 AcquisitionandPlayback Matlab: wavread,sound,wavwrite 13.2 DialNforNoise 326 FrequencyandSampling Matlab: Morepracticewithvectors 14 Divide and Conquer 335 14.1 PatternswithinPatterns 337 RecursiveTiling Matlab: Recursivefunctions 14.2 NandHalfN 344 MergeSort Matlab: Morepracticewithrecursion 14.3 LookingforTrouble 354 AdaptiveInterpolation Matlab: Stillmorepracticewithrecursion 15 Optimization 363 15.1 ShortestRoute 365 TheCombinatoricExplosion Matlab: Morepracticewitharrays 15.2 BestBike 372 ConstraintsandObjectiveFunctions Matlab: Morecomplicatednestedloops 15.3 MostLikelyOrbit 381 ModelBuilding Matlab: Interactivesearch AppendixA. Refined Graphics 389 Appendix B. Mathematical Facts 411 Appendix C.MATLAB,Java, and C 417 Appendix D. Exit Interview 423 Index 425 Preface Asthetitlesuggests, thisbookisbothanintroductiontoMatlab(cid:1)r programmingandto thecomputationalsideofscienceandengineering. Wetargetcollegefreshmanintending tomajorinengineering(includingcomputerscience)oranaturalscience(includingmath- ematics). Given the quantitative abilities of this group of students, we do not shy away fromtrigonometryandelementarynotionsofapproximationasseeninCalculusI.Indeed, itisagainstthegrainofliberaleducationnottointermingleintroductoryprogrammingwith continuousmathematicsifthestudentclienteleiscapableofhandlingthemix. Liberaledu- cationisallaboutacquiringanappreciationfordifferentmodesofthought. Whysquander theopportunitytocontrastdigitalthinkingwithcontinuousthinking? Ourapproachissimple. Eachsectionbeginswiththeposingofaproblemthatpoints tosomelargercomputationalstory. Thesolutioniscarefullyderivedandalongthewaywe introduce whatever new Matlab is required. This is followed by a brief “talking point” thatemphasizessomeaspectofthelargerstory. Thispatternresonateswithourbeliefthata firstcourseinprogrammingshouldbetaughtthroughexamples. Everysectionculminates intheproductionofaworkingMatlabscriptand(usually)afewMatlabfunctions. The sectionexercisesincludestraightforward“M-problems”thatfocusonthedevelopedcode and whatever new Matlab is developed. More involved “P-problems” are designed to reinforcethesection’scomputationalmessage. We use the Matlab environment because of its friendliness to the first-time pro- grammer and because it supports the idea of playing with computational ideas through experimentation. Thisiscentraltothedevelopmentofcomputationalintuition. Playingwithprogramsbuildscomputationalintuition. Intuitionisasenseofdirectionnodifferentfromthesenseofdirectionthatenablesyouto findyourwayaroundanoldchildhoodneighborhoodwithoutamap. Thekeyisthatyou havebeentherebefore. Ifintuitionisasenseofdirection,thencomputationalintuitionisa senseofcomputationaldirection. Thosewhohaveitwillbeabletofindtheirwayaround scienceandengineeringinthe21stcentury. Navigationrequiresfivekeensenses. Through examplesandproblemsweaimtodothefollowing: 1. Develop eyes for the geometric. The ability to visualize is very important to the computationalscientist. Ofcourse,computergraphicsplaysatremendousrolehere, butthevisualizationtoolsthatitoffersdonotobviatetheneedtoreasoningeometric terms. It is critical to be familiar with sines and cosines, polygons and polyhedra, metricsandproximity,etc. 2. Developanearthatcanhearthe“combinatoricexplosion.” Manydesignandopti- mizationproblemsinvolvehugesearchspaceswithanexponentialnumberofpossi- bilities. Itisimportanttoanticipatethiscomplexityandtohavethewherewithalto handleitwithintelligentheuristics. xi xii Preface 3. Developatastefortherandom. Scienceandengineeringispopulatedwithprocesses thathavearandomcomponent. Havingasenseofprobabilityandtheabilitytogather andinterpretstatisticswiththecomputerisvital. 4. Developanosefordimension. Simulationismuchmorecomputationallyintensive inthreedimensionsthanintwodimensions—ahardfactoflifethatisstaringmany computationalscientistsrightintheface. Anaccurateimpressionofhowcomputers assist in the understanding of the physical world requires an appreciation of this point. Moreover, being able to think at the array level is essential for effective, high-performancecomputing. 5. Developatouchforwhatisfinite,inexact,andapproximate. Roundingerrorsattend floatingpointarithmetic,computerdisplaysaregranular,analyticderivativesareap- proximatedwithdivideddifferences,apolynomialisusedinlieuofthesinefunction, andthedataacquiredinalabmayonlybecorrecttothreesignificantdigits. Lifein computationalscienceislikethisandthepractitionermustberesoluteenoughtoface suchuncertainties. Steadyfootworkisrequiredonthebalancebeamthatseparates thecontinuousandthediscrete. While the development of these five senses is an explicit priority, our overarching ambitionistocommunicatetheexcitementofcomputingtogetherwithanappreciationfor itsconstraintsanditsconnectionstoothermethodologies. Theinterplaybetweencomputing, theory,andexperimentationisparticularlyimportant. Computation Theory Experiment Each vertex represents a style of research and provides a window through which we can viewscienceandengineeringinthelarge. Thevibrancyofwhatweseeinsidethetriangle dependsupontheideasthatflowarounditsedges. Agoodtheorycouchedinthelanguage ofmathematicsmayberealizedintheformofacomputerprogram,perhapsjusttoaffirm its correctness. Running the program results in a simulation that may suggest a physical experiment. The experiment in turn may reveal a missed parameter in the underlying mathematicalmodel,andaroundwegoagain. There are also interesting dynamics in the other direction. Aphysical experiment mayberestrictedinscopeforreasonsofbudgetorsafety,sothesceneshiftstocomputer simulation. Theactofwritingtheprogramtoperformthesimulationwillmostlikelyhave aclarifyinginfluence, promptingsomenewmathematicalpursuit. Innovativemodelsare discovered,leadingtoamodificationoftheinitialsetofexperiments,andsoforth. Inthinkingaboutthesecriticalinteractionsweareremindedofthegreatmathematical scientist Richard Hamming, who stated in the 1960s that “the purpose of computing is insight,notnumbers.” Weareinobviousagreementwiththispointofview. Thetakeaway

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.