ebook img

Using R for Numerical Analysis in Science and Engineering PDF

356 Pages·2014·4.945 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 Using R for Numerical Analysis in Science and Engineering

The R Series Statistics U s i n g R Using R for Numerical f Instead of presenting the standard theoretical treatments that under- o r lie the various numerical methods used by scientists and engineers, N Using R for Numerical Analysis in Science and Engineering u Analysis in Science m shows how to use R and its add-on packages to obtain numerical e solutions to the complex mathematical problems commonly faced r by scientists and engineers. This practical guide to the capabilities ic and Engineering a of R demonstrates Monte Carlo, stochastic, deterministic, and other l A numerical methods through an abundance of worked examples and n code, covering the solution of systems of linear algebraic equations a l y and nonlinear equations as well as ordinary differential equations and s partial differential equations. It not only shows how to use R’s power- is ful graphic tools to construct the types of plots most useful in scien- i n tific and engineering work, but also S c • Explains how to statistically analyze and fit data to linear and i e nonlinear models n c • Explores numerical differentiation, integration, and optimization e • Describes how to find eigenvalues and eigenfunctions a n • Discusses interpolation and curve fitting d • Considers the analysis of time series E n Using R for Numerical Analysis in Science and Engineering pro- g i n vides a solid introduction to the most useful numerical methods for e scientific and engineering data analysis using R. e r i n g B Victor A. Bloomfield l o o m fi e K13976 l d K13976_Cover.indd 1 3/18/14 12:29 PM Using R for Numerical Analysis in Science and Engineering Victor A. Bloomfield University of Minnesota Minneapolis, USA K13976_FM.indd 1 3/24/14 11:19 AM Chapman & Hall/CRC The R Series Series Editors John M. Chambers Torsten Hothorn Department of Statistics Division of Biostatistics Stanford University University of Zurich Stanford, California, USA Switzerland Duncan Temple Lang Hadley Wickham Department of Statistics RStudio University of California, Davis Boston, Massachusetts, USA Davis, California, USA Aims and Scope This book series reflects the recent rapid growth in the development and application of R, the programming language and software environment for statistical computing and graphics. R is now widely used in academic research, education, and industry. It is constantly growing, with new versions of the core software released regularly and more than 5,000 packages available. It is difficult for the documentation to keep pace with the expansion of the software, and this vital book series provides a forum for the publication of books covering many aspects of the development and application of R. The scope of the series is wide, covering three main threads: • Applications of R to specific disciplines such as biology, epidemiology, genetics, engineering, finance, and the social sciences. • Using R for the study of topics of statistical methodology, such as linear and mixed modeling, time series, Bayesian methods, and missing data. • The development of R, including programming, building packages, and graphics. The books will appeal to programmers and developers of R software, as well as applied statisticians and data analysts in many fields. The books will feature detailed worked examples and R code fully integrated into the text, ensuring their usefulness to researchers, practitioners and students. K13976_FM.indd 2 3/24/14 11:19 AM Published Titles Using R for Numerical Analysis in Science and Engineering , Victor A. Bloomfield Event History Analysis with R, Göran Broström Computational Actuarial Science with R, Arthur Charpentier Statistical Computing in C++ and R, Randall L. Eubank and Ana Kupresanin Reproducible Research with R and RStudio, Christopher Gandrud Displaying Time Series, Spatial, and Space-Time Data with R, Oscar Perpiñán Lamigueiro Programming Graphical User Interfaces with R, Michael F. Lawrence and John Verzani Analyzing Baseball Data with R, Max Marchi and Jim Albert Growth Curve Analysis and Visualization Using R, Daniel Mirman R Graphics, Second Edition, Paul Murrell Customer and Business Analytics: Applied Data Mining for Business Decision Making Using R, Daniel S. Putler and Robert E. Krider Implementing Reproducible Research, Victoria Stodden, Friedrich Leisch, and Roger D. Peng Dynamic Documents with R and knitr, Yihui Xie K13976_FM.indd 3 3/24/14 11:19 AM MATLAB® is a trademark of The MathWorks, Inc. and is used with permission. The MathWorks does not warrant the accuracy of the text or exercises in this book. This book’s use or discussion of MATLAB® software or related products does not constitute endorsement or sponsorship by The MathWorks of a particular pedagogical approach or particular use of the MATLAB® software. CRC Press Taylor & Francis Group 6000 Broken Sound Parkway NW, Suite 300 Boca Raton, FL 33487-2742 © 2014 by Taylor & Francis Group, LLC CRC Press is an imprint of Taylor & Francis Group, an Informa business No claim to original U.S. Government works Version Date: 20140220 International Standard Book Number-13: 978-1-4398-8449-2 (eBook - PDF) This book contains information obtained from authentic and highly regarded sources. Reasonable efforts have been made to publish reliable data and information, but the author and publisher cannot assume responsibility for the validity of all materials or the consequences of their use. The authors and publishers have attempted to trace the copyright holders of all material reproduced in this publication and apologize to copyright holders if permission to publish in this form has not been obtained. If any copyright material has not been acknowledged please write and let us know so we may rectify in any future reprint. Except as permitted under U.S. Copyright Law, no part of this book may be reprinted, reproduced, transmitted, or utilized in any form by any electronic, mechanical, or other means, now known or hereafter invented, includ- ing photocopying, microfilming, and recording, or in any information storage or retrieval system, without written permission from the publishers. For permission to photocopy or use material electronically from this work, please access www.copyright.com (http://www.copyright.com/) or contact the Copyright Clearance Center, Inc. (CCC), 222 Rosewood Drive, Danvers, MA 01923, 978-750-8400. CCC is a not-for-profit organization that provides licenses and registration for a variety of users. For organizations that have been granted a photocopy license by the CCC, a separate system of payment has been arranged. Trademark Notice: Product or corporate names may be trademarks or registered trademarks, and are used only for identification and explanation without intent to infringe. Visit the Taylor & Francis Web site at http://www.taylorandfrancis.com and the CRC Press Web site at http://www.crcpress.com Contents ListofFigures xiii Preface xix 1 Introduction 1 1.1 ObtainingandinstallingR 1 1.2 LearningR 1 1.3 Learningnumericalmethods 1 1.4 Findinghelp 2 1.5 AugmentingRwithpackages 3 1.6 LearningmoreaboutR 5 1.6.1 Books 5 1.6.2 Onlineresources 5 2 Calculating 7 2.1 Basicoperatorsandfunctions 7 2.2 Complexnumbers 8 2.3 Numericaldisplay,round-offerror,androunding 9 2.4 Assigningvariables 11 2.4.1 Listingandremovingvariables 12 2.5 Relationaloperators 12 2.6 Vectors 13 2.6.1 Vectorelementsandindexes 13 2.6.2 Operationswithvectors 14 2.6.3 Generatingsequences 15 2.6.3.1 Regularsequences 15 2.6.3.2 Repeatingvalues 16 2.6.3.3 Sequencesofrandomnumbers 16 2.6.4 Logicalvectors 17 2.6.5 Speedofforminglargevectors 18 2.6.6 Vectordotproductandcrossproduct 19 2.7 Matrices 21 2.7.1 Formingmatrices 21 2.7.2 Operationsonmatrices 24 2.7.2.1 Arithmeticoperationsonmatrices 24 2.7.2.2 Matrixmultiplication 25 v vi CONTENTS 2.7.2.3 Transposeanddeterminant 26 2.7.2.4 Matrixcrossproduct 26 2.7.2.5 Matrixexponential 27 2.7.2.6 Matrixinverseandsolve 27 2.7.2.7 Eigenvaluesandeigenvectors 29 2.7.2.8 Singularvaluedecomposition 31 2.7.3 TheMatrixpackage 33 2.7.4 Additionalmatrixfunctionsandpackages 34 2.8 Timeanddatecalculations 34 3 Graphing 37 3.1 Scatterplots 37 3.2 Functionplots 39 3.3 Othercommonplots 40 3.3.1 Barcharts 40 3.3.2 Histograms 42 3.3.3 Box-and-whiskerplots 43 3.4 Customizingplots 44 3.4.1 Pointsandlines 44 3.4.2 Axes,ticks,andpar() 44 3.4.3 Overlayingplotswithgraphicelements 46 3.5 Errorbars 48 3.6 Superimposingvectorsinaplot 49 3.7 Modifyingaxes 50 3.7.1 Logarithmicaxes 51 3.7.2 Supplementaryaxes 51 3.7.3 Incompleteaxisboxes 52 3.7.4 Brokenaxes 52 3.8 Addingtextandmathexpressions 54 3.8.1 Makingmathannotationswithexpression() 55 3.9 Placingseveralplotsinafigure 56 3.10 Two-andthree-dimensionalplots 58 3.11 Theplotrixpackage 60 3.11.1 radial.plot andpolar.plot 60 3.11.2 Triangleplot 61 3.11.3 Errorbarsinplotrix 62 3.12 Animation 63 3.13 Additionalplottingpackages 64 4 Programmingandfunctions 65 4.1 Conditionalexecution:ifandifelse 65 4.2 Loops 66 4.2.1 forloop 66 4.2.2 Loopingwithwhileandrepeat 68 4.3 User-definedfunctions 69 CONTENTS vii 4.4 Debugging 72 4.5 Built-inmathematicalfunctions 73 4.5.1 Besselfunctions 73 4.5.2 Betaandgammafunctions 74 4.5.3 Binomialcoefficients 75 4.6 Specialfunctionsofmathematicalphysics 75 4.6.1 Thegslpackage 75 4.6.2 Specialfunctionsinotherpackages 75 4.7 Polynomialfunctionsinpackages 78 4.7.1 PolynomF package 79 4.7.2 orthopolynompackage 83 4.8 Casestudies 86 4.8.1 Two-dimensionalrandomwalk 86 4.8.2 Eigenvaluesofapolymerchain 87 5 Solvingsystemsofalgebraicequations 91 5.1 Findingthezerosofapolynomial 91 5.2 Findingthezerosofafunction 92 5.2.1 Bisectionmethod 92 5.2.2 Newton’smethod 93 5.2.3 unirootanduniroot.all 94 5.3 Systemsoflinearequations:matrixsolve 96 5.4 Matrixinverse 97 5.5 Singularmatrix 97 5.6 Overdeterminedsystemsandgeneralizedinverse 98 5.7 Sparsematrices 99 5.7.1 Tridiagonalmatrix 99 5.7.2 Bandedmatrix 101 5.7.3 Blockmatrix 102 5.8 Matrixdecomposition 104 5.8.1 QRdecomposition 105 5.8.2 Singularvaluedecomposition 106 5.8.3 Eigendecomposition 107 5.8.4 LUdecomposition 107 5.8.5 Choleskydecomposition 108 5.8.6 Schurdecomposition 109 5.8.7 backsolveandforwardsolve 109 5.9 Systemsofnonlinearequations 109 5.9.1 multirootintherootSolvepackage 109 5.9.2 nleqslv 111 5.9.3 BBsolve() in the BB package 112 5.10 Casestudies 117 5.10.1 Spectroscopicanalysisofamixture 117 5.10.2 vanderWaalsequation 120 5.10.3 Chemicalequilibrium 122 viii CONTENTS 6 Numericaldifferentiationandintegration 125 6.1 Numericaldifferentiation 125 6.1.1 NumericaldifferentiationusingbaseR 125 6.1.1.1 Usingthefundamentaldefinition 125 6.1.1.2 diff() 126 6.1.2 NumericaldifferentiationusingthenumDerivpackage 127 6.1.2.1 grad() 128 6.1.2.2 jacobian() 128 6.1.2.3 hessian 129 6.1.3 Numericaldifferentiationusingthepracmapackage 129 6.1.3.1 fderiv() 129 6.1.3.2 numderiv()andnumdiff() 130 6.1.3.3 grad()andgradient() 131 6.1.3.4 jacobian() 131 6.1.3.5 hessian 132 6.1.3.6 laplacian() 133 6.2 Numericalintegration 133 6.2.1 integrate:BasicintegrationinR 134 6.2.2 Integratingdiscretizedfunctions 136 6.2.3 Gaussianquadrature 137 6.2.4 Moreintegrationroutinesinpracma 140 6.2.5 Functionswithsingularities 142 6.2.6 Infiniteintegrationdomains 144 6.2.7 Integralsinhigherdimensions 146 6.2.8 MonteCarloandsparsegridintegration 148 6.2.9 Complexlineintegrals 150 6.3 SymbolicmanipulationsinR 152 6.3.1 D() 152 6.3.2 deriv() 152 6.3.3 Polynomialfunctions 154 6.3.4 Interfacestosymbolicpackages 155 6.4 Casestudies 155 6.4.1 Circumferenceofanellipse 155 6.4.2 IntegrationofaLorentzianderivativespectrum 156 6.4.3 Volumeofanellipsoid 157 7 Optimization 159 7.1 One-dimensionaloptimization 159 7.2 Multi-dimensionaloptimizationwithoptim() 162 7.2.1 optim()with“Nelder–Mead”default 163 7.2.2 optim()with“BFGS”method 165 7.2.3 optim()with“CG”method 167 7.2.4 optim()with“L-BFGS-B”methodtofindalocalminimum 167 7.3 Otheroptimizationpackages 169 7.3.1 nlm() 169 CONTENTS ix 7.3.2 ucminfpackage 171 7.3.3 BBpackage 171 7.3.4 optimx()wrapper 172 7.3.5 Derivative-freeoptimizationalgorithms 172 7.4 Optimizationwithconstraints 173 7.4.1 constrOptimtooptimizefunctionswithlinearconstraints 173 7.4.2 ExternalpackagesalabamaandRsolnp 175 7.5 Globaloptimizationwithmanylocalminima 177 7.5.1 Simulatedannealing 178 7.5.2 Geneticalgorithms 181 7.5.2.1 DEoptim 181 7.5.2.2 rgenoud 183 7.5.2.3 GA 183 7.6 Linearandquadraticprogramming 183 7.6.1 Linearprogramming 183 7.6.2 Quadraticprogramming 186 7.7 Mixed-integerlinearprogramming 189 7.7.1 Mixed-integerproblems 189 7.7.2 Integerprogrammingproblems 190 7.7.2.1 Knapsackproblems 191 7.7.2.2 Transportationproblems 191 7.7.2.3 Assignmentproblems 192 7.7.2.4 Subsetsumproblems 193 7.8 Casestudy 194 7.8.1 MonteCarlosimulationofthe2DIsingmodel 194 8 Ordinarydifferentialequations 199 8.1 Eulermethod 200 8.1.1 Projectilemotion 201 8.1.2 Orbitalmotion 203 8.2 ImprovedEulermethod 205 8.3 deSolvepackage 208 8.3.1 lsoda()andlsode() 210 8.3.2 “adams”andrelatedmethods 211 8.3.3 Stiffsystems 213 8.4 MatrixexponentialsolutionforsetsoflinearODEs 214 8.5 Eventsandroots 215 8.6 Differenceequations 220 8.7 Delaydifferentialequations 221 8.8 Differentialalgebraicequations 224 8.9 rootSolveforsteadystatesolutionsofsystemsofODEs 227 8.10 bvpSolvepackageforboundaryvalueODEproblems 230 8.10.1 bvpshoot() 230 8.10.2 bvptwp() 231 8.10.3 bvpcol() 232

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.