Professional Documents
Culture Documents
Download textbook Computational Methods For Numerical Analysis With R 1St Edition James Patrick Howard Ii ebook all chapter pdf
Download textbook Computational Methods For Numerical Analysis With R 1St Edition James Patrick Howard Ii ebook all chapter pdf
https://textbookfull.com/product/spatial-analysis-with-r-
statistics-visualization-and-computational-methods-second-
edition-oyana/
https://textbookfull.com/product/teaching-and-learning-
mathematics-online-1st-edition-james-p-howard-ii-editor/
https://textbookfull.com/product/numerical-methods-of-
exploration-seismology-with-algorithms-in-matlab-r-gary-f-
margrave/
https://textbookfull.com/product/computational-stem-cell-biology-
methods-and-protocols-patrick-cahan/
Using R for Numerical Analysis in Science and
Engineering 1st Edition Victor A. Bloomfield
https://textbookfull.com/product/using-r-for-numerical-analysis-
in-science-and-engineering-1st-edition-victor-a-bloomfield/
https://textbookfull.com/product/statistical-analysis-of-
financial-data-with-examples-in-r-1st-edition-james-gentle/
https://textbookfull.com/product/computational-methods-for-next-
generation-sequencing-data-analysis-1st-edition-ion-mandoiu/
https://textbookfull.com/product/computational-methods-for-
single-cell-data-analysis-guo-cheng-yuan/
https://textbookfull.com/product/numerical-methods-in-physics-
with-python-1st-edition-alex-gezerlis/
Computational Methods for
Numerical Analysis with R
CHAPMAN & HALL/CRC
Numerical Analysis and Scientific Computing
Editors
Choi-Hong Lai Frédéric Magoulès
School of Computing and Applied Mathematics and
Mathematical Sciences Systems Laboratory
University of Greenwich Ecole Centrale Paris
Proposals for the series should be submitted to one of the series editors above or directly to:
CRC Press, Taylor & Francis Group
3 Park Square, Milton Park
Abingdon, Oxfordshire OX14 4RN
UK
Published Titles
Classical and Modern Numerical Analysis: Theory, Methods and Practice
Azmy S. Ackleh, Edward James Allen, Ralph Baker Kearfott, and Padmanabhan Seshaiyer
Cloud Computing: Data-Intensive Computing and Scheduling Frédéric Magoulès, Jie Pan,
and Fei Teng
Computational Fluid Dynamics Frédéric Magoulès
Computational Methods for Numerical Analysis with R James P. Howard, II
A Concise Introduction to Image Processing using C++ Meiqing Wang and Choi-Hong Lai
Coupled Systems: Theory, Models, and Applications in Engineering Juergen Geiser
Decomposition Methods for Differential Equations: Theory and Applications Juergen Geiser
Designing Scientific Applications on GPUs Raphaël Couturier
Desktop Grid Computing Christophe Cérin and Gilles Fedak
Discrete Dynamical Systems and Chaotic Machines: Theory and Applications
Jacques M. Bahi and Christophe Guyeux
Discrete Variational Derivative Method: A Structure-Preserving Numerical Method for
Partial Differential Equations Daisuke Furihata and Takayasu Matsuo
Grid Resource Management: Toward Virtual and Services Compliant Grid Computing
Frédéric Magoulès, Thi-Mai-Huong Nguyen, and Lei Yu
Fundamentals of Grid Computing: Theory, Algorithms and Technologies Frédéric Magoulès
Handbook of Sinc Numerical Methods Frank Stenger
Introduction to Grid Computing Frédéric Magoulès, Jie Pan, Kiat-An Tan, and Abhinit Kumar
Iterative Splitting Methods for Differential Equations Juergen Geiser
Mathematical Objects in C++: Computational Tools in a Unified Object-Oriented Approach
Yair Shapira
Numerical Linear Approximation in C Nabih N. Abdelmalek and William A. Malek
Numerical Methods and Optimization: An Introduction Sergiy Butenko and Panos M. Pardalos
Numerical Methods for Fractional Calculus Changpin Li and Fanhai Zeng
Numerical Techniques for Direct and Large-Eddy Simulations Xi Jiang and Choi-Hong Lai
Parallel Algorithms Henri Casanova, Arnaud Legrand, and Yves Robert
Parallel Iterative Algorithms: From Sequential to Grid Computing Jacques M. Bahi,
Sylvain Contassot-Vivier, and Raphaël Couturier
Particle Swarm Optimisation: Classical and Quantum Perspectives Jun Sun, Choi-Hong Lai,
and Xiao-Jun Wu
XML in Scientific Computing C. Pozrikidis
Computational Methods for
Numerical Analysis with R
James P. Howard, II
The Johns Hopkins University
Applied Physics Laboratory
Laurel, Maryland, USA
CRC Press
Taylor & Francis Group
6000 Broken Sound Parkway NW, Suite 300
Boca Raton, FL 33487-2742
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, including 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
To Nina, Chase (see Figure 4.13),
and Ducky (see Figure 4.14)
Contents
List of Tables xv
Preface xix
2 Error Analysis 29
ix
x Contents
2.4 Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
2.4.1 Simple Division Algorithms . . . . . . . . . . . . . . . 53
2.4.2 Binary Long Division . . . . . . . . . . . . . . . . . . 55
Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
3 Linear Algebra 59
Index 253
List of Figures
5.1 Derivative of sin x at x = π/4 for step sizes h = {1, 0.5, 0.01} 135
5.2 The midpoint rule for f (x) = x2 with m = 2 and m = 4 . . 140
5.3 The trapezoid rule for f (x) = x2 with√m = 2 and m = 4 . . 142
5.4 The Monte Carlo method for f (x) = 1 − x2 with m = 20 . 160
5.5 Cylinder as a rotated solid around the x-axis and y-axis . . 165
5.6 Rotated volume around the x-axis . . . . . . . . . . . . . . . 166
5.7 Rotated volume around the y-axis . . . . . . . . . . . . . . . 167
5.8 Lorenz curve . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
xiii
xiv Computational Methods for Numerical Analysis with R
xv
List of R Functions
xvii
xviii Computational Methods for Numerical Analysis with R
xix
xx Computational Methods for Numerical Analysis with R
Each section of this text present a type of problem and a numerical solu-
tion. The problem is described briefly and the numerical solution is explained.
Then the R code implementing that solution is described, along with any
specific implementation details. From there examples are given. Where ap-
propriate, examples showing where the algorithm can fail are provided along
with suggestions for where robust implementations are available as part of the
standard R distributions, or one of the many packages available from cran.
Because this book focuses on working code, this book is not meant to be
a textbook. However, its structure mirrors the structure of an undergraduate
computational methods course. As such, this text would be appropriate an
textbook for courses entitled Numerical Analysis, Computational Methods,
Scientific Computing, or similar courses, if the instructor is willing to forego
rigours analytical approaches. cmna would better serve as a secondary text
for one of these courses when the text provides weaker examples, or in an
inaccessible manner, such as Fortran or matlab® .
I would like to thank the following Rob Calver, Lara Spieker, and many
others at CRC Press for seeing this book through to completion. I would also
like to thank Marck Vaisman, Elizabeth Shope, Kelley O’Neal, James Long,
and Casey Driscoll, who all offered great comments at one point or another.
I also want to thank two anonymous reviewers for extensive and insightful
comments on the text. Any errors remaining are only my responsibility.
1
Introduction to Numerical Analysis
1
2 Computational Methods for Numerical Analysis with R
R Function 1.1
The Fibonacci sequence
reader to open the text and read functions. The small size is designed to
help example implementations fit in the head of the reader. As a result, error
handling and potential evaluation options are not included, unless convenient
or necessary. In some cases, smaller independent components were moved into
new functions. This provides additional flexibility to the implementation and
improves the readability of the functions.
The functions included in this book, and some others, are bundled into a
package called cmna, standing for Computational Methods for Numerical Anal-
ysis. This package provides the algorithms, implementations, documentation,
and testing. The package is available through standard distribution channels.
> library(cmna)
The call to library makes the library available for use. Despite bundling
these functions into a single package, the functions provided do not provide
a unified whole. That is, the functions provide a baseline for understanding
the imeplementation and are designed to provide a comprehensive solution for
numerical applications. Where possible, functions with similar applications are
given similar interfaces. This is not to say the functions were not designed.
But they are not designed to work together in any meaningful way.
Because of these design choices, it is also important to remember the func-
tions implemented here are not robust. That is, they do not perform basic
error handling and they are unlikely to fail gracefully when presented with
a problem. This is the tradeoff from the simple implementations given. They
explain the fundamental method from a numerical standpoint. Handling un-
usual circumstances or errors, should be a routine part of any program, but
because they are not included here, some implementations may not fail well.
As such, error handling is addressed when necessary, such as when a function
could silently return an incorrect answer, rather than simply terminate.
The functions in cmna are not robust and should not be used for actual ap-
plied numerical analysis. Applied numerical analysis, such as an architectural
model, relies on well-tested and well-understood numerical models that handle
errors. R includes robust and tested functions for numerical analysis. Because
the functions we develop are not robust enough for everyday use, when appro-
priate, these functions are described and demonstrated to show how to solve
actual numerical analysis problems in R. This allows you to move along and
actually use the demonstrated solution rather than fixing the cmna version to
meet more demanding requirements.
1.1.3 Efficiency
The first constraint we will look at is efficiency. Programmers generally work
very hard to make sure their computers do not have to. Every gain in effi-
ciency enables more interesting problems to be solved. Because programs for
numerical analysis are run so many times, increased efficiency decreases the
6 Computational Methods for Numerical Analysis with R
overall runtime, as well. Efficiency comprises two areas. The first is time com-
plexity, focusing on how long it takes for a program to execute. Programs that
require more steps for larger inputs are said to have higher time complexity.
The second is space complexity, measuring how much storage, even intermedi-
ate, is necessary to execute the program. Programs that require more memory
for larger inputs are said to have higher space complexity.1 We are going to
use less formal methods focusing on the count of operations a function must
perform to complete and return a value. For instance, examine this loop:
> count <- 0
> m <- 10
> for(i in 1:m)
+ count <- count + 1
> count
[1] 10
This is a fancy way to perform a loop 10 times. The function loops from 1 to
m times. This loop’s internals execute 10 times. To verify this, we increment
the count variable and print the value, which is m. Of course, we can also nest
loops, and this increases the number of times the loop’s internals, the parts
within the innermost loop, run:
> count <- 0
> m <- 10
> n <- 7
> for(i in 1:m)
+ for(j in 1:n)
+ count <- count + 1
> count
[1] 70
In this case, we have two loops. The outer loop runs from 1 to m and the
inner loop executes from 1 to n. The inner loop executes n times for each time
through the outer loop. Therefore, the loop’s innermost internals execute mn
times. These are straightforward examples. Slightly less straightforward is an
example where the outer loop executes from 1 to m. The inner loop executes
from 1 to i where i represents the iteration of the outer loop:
> count <- 0
> m <- 10
> for(i in 1:m)
+ for(j in 1:i)
+ count <- count + 1
> count
[1] 55
1 For a textbook treatment, I recommend Introduction to Algorithms by Cormen et al.
(2009) or Algorithms by Sedgewick and Wayne (2011). For a less formal approach, an
Internet search for “time complexity,” “space complexity,” or “big-O notation” is sufficient.
Introduction to Numerical Analysis 7
for ( i in 2: sqrt ( n ) )
if ( n %% i == 0)
return ( FALSE )
return ( TRUE )
}
R Function 1.2
Test an integer for primality
The inner loop’s internals execute 1 time the first time through the outer loop,
2 times the second time, and so on up to m times the mth time Pmthrough the
loop. Accordingly, the inner loop executes k times where k = i=1 i.
Each of these grow at least as quickly as the input does. However, that
is not always the case for a function. Consider the function isPrime, listed
in Function 1.2. This function performs a basic primality test by testing the
input for division by a successive number of potential divisors.
When testing for primality, it is only necessary
√ to test the integers from
2 to the last integer less than or equal to n. This function still grows, but
at a much slower rate. Further, the relative
√ rate of increase decreases as n
increases. Also, in the case of isPrime, n is the maximum number of √ times
through the loop. If the number is not prime, an integer less than n will
successfully divide n and the loop will terminate early.
Of course, within a loop, it is possible for complex operations to be less
obvious. In this example,
> count <- 0
> m <- 10
> x <- 1:100
> y <- rep(1, 100)
> for(i in 1:m) {
+ y <- y * x
+ count <- count + 1
+ }
> count
[1] 10
the value of count is 10. However, many more multiplications happened within
the loop. The loop itself contains an element-wise multiplication. This is a
multiplication for each element of the vectors x and y. As each are 100 elements
long, this loop actually contains 1000 multiplications. Despite there being only
8 Computational Methods for Numerical Analysis with R
one for loop within the example as stated, it includes more operations than
the last example, with its explictly nested loops.
Understanding what is happening inside a loop is critical to estimating
how long it will take to execute. If the loop calls other external functions,
each of those will have their operations to perform. Likely, these functions are
opaque to us, and we will not necessarily know what they are doing and how
they do it.
Despite these speed analyses, R is not a fast programming language. Its
internal operations are fast enough for most purposes. If speed were the most
important consideration, we would use c or Fortan. As compiled languages,
they execute as machine code directly on the hardware. R programs, like
Python, matlab® , and other interpreted programming languages, do not run
directly on the hardware, but are instead translated and executed by the
language’s runtime engine.
But we use languages like R, matlab® , or Python for other reasons and
in the case of R, the reasons might be access to libraries created by other
programmers, the statistical facilities, or its accessibility. And understanding
how to estimate the number of operations necessary to complete a task is
language-neutral. So if one implemented
√ a c version of the prime tester in
Function 1.2, it would still require n tests.
De altos espíritus es
Aspirar á empresas altas,
Y ver con dolor las faltas
De los que muerden sus pies.
Es Copacavana un pueblo
Que casi igualmente dista,
En la provincia que llaman
Chucuitos, las propias millas
De la ciudad de la Paz
Y Potosí. Sus campiñas
Son fértiles, sus ganados
Muchos, y sus alquerías
De frutas, pescas y cazas
Abundantes siempre y ricas:
Cuya opulencia, en su lengua,
Á la nuestra traducida,
Copacavana lo mismo
Que piedra preciosa explica.
Pero aunque pudiera ser
Por esto grande su estima,
Lo hizo mayor que en sus montes
Yace aquella peña altiva
Que adoratorio del Sol
Fué un tiempo, por ser su cima
Donde diabólico impulso
Hizo creer que el Sol podía
Dar á su hijo para que
Los mande, gobierne y rija.
Á esta causa, entre la peña
Y la procelosa orilla
De gran laguna, que hace
El medio contorno isla,
Se construyó templo al sol,
En cuyas aras impías
Faubro al ídolo llamaron
Superior, que significa
Mes Santo, y mientras el cielo
No nos revele el enigma,
Ocioso es que discurramos
Ahora en su etimología.
En él, por los reservados
Juicios de Dios, las insidias
Del antiguo áspid, y en otros
Oráculos, respondían
Inspirando abominables
Ritos, cuya hidropesía
De sangre, mal apagada
Con la de las brutas vidas,
Pasó á beber la de humanas
Virgenes sacerdotisas.
En fin, siendo como era
Copacavana la hidra
De tantas cabezas cuantas
El padre de la mentira
En cada garganta mueve,
En cada anhélito inspira,
Fué la primera en quien Dios
Logró la feliz semilla
De su fe, siendo primeros
Obreros de su doctrina
De Domingo y de Agustino
Las dos sagradas familias.
Roma de América hay
Quien piadoso la publica;
Pues bien, como Roma, siendo
Donde más vana tenía
La gentilidad su trono,
Fué donde puso su silla
Triunfante la Iglesia; así
Donde más la idolatría
Reinaba, puso la fe
Su española monarquía.
Mostrando cuán docta siempre
La eterna sabiduría,
Donde ocurre el mayor daño,
El mayor remedio aplica.