|
|
xiii | |
Preface |
|
xix | |
|
|
1 | (6) |
|
1.1 Obtaining and installing R |
|
|
1 | (1) |
|
|
1 | (1) |
|
1.3 Learning numerical methods |
|
|
1 | (1) |
|
|
2 | (1) |
|
1.5 Augmenting R with packages |
|
|
3 | (2) |
|
1.6 Learning more about R |
|
|
5 | (2) |
|
|
5 | (1) |
|
|
5 | (2) |
|
|
7 | (30) |
|
2.1 Basic operators and functions |
|
|
7 | (1) |
|
|
8 | (1) |
|
2.3 Numerical display, round-off error, and rounding |
|
|
9 | (2) |
|
|
11 | (1) |
|
2.4.1 Listing and removing variables |
|
|
12 | (1) |
|
|
12 | (1) |
|
|
13 | (8) |
|
2.6.1 Vector elements and indexes |
|
|
13 | (1) |
|
2.6.2 Operations with vectors |
|
|
14 | (1) |
|
2.6.3 Generating sequences |
|
|
15 | (1) |
|
2.6.3.1 Regular sequences |
|
|
15 | (1) |
|
|
16 | (1) |
|
2.6.3.3 Sequences of random numbers |
|
|
16 | (1) |
|
|
17 | (1) |
|
2.6.5 Speed of forming large vectors |
|
|
18 | (1) |
|
2.6.6 Vector dot product and crossproduct |
|
|
19 | (2) |
|
|
21 | (13) |
|
|
21 | (3) |
|
2.7.2 Operations on matrices |
|
|
24 | (1) |
|
2.7.2.1 Arithmetic operations on matrices |
|
|
24 | (1) |
|
2.7.2.2 Matrix multiplication |
|
|
25 | (1) |
|
2.7.2.3 Transpose and determinant |
|
|
26 | (1) |
|
2.7.2.4 Matrix crossproduct |
|
|
26 | (1) |
|
2.7.2.5 Matrix exponential |
|
|
27 | (1) |
|
2.7.2.6 Matrix inverse and solve |
|
|
27 | (2) |
|
2.7.2.7 Eigenvalues and eigenvectors |
|
|
29 | (2) |
|
2.7.2.8 Singular value decomposition |
|
|
31 | (2) |
|
|
33 | (1) |
|
2.7.4 Additional matrix functions and packages |
|
|
34 | (1) |
|
2.8 Time and date calculations |
|
|
34 | (3) |
|
|
37 | (28) |
|
|
37 | (2) |
|
|
39 | (1) |
|
|
40 | (4) |
|
|
40 | (2) |
|
|
42 | (1) |
|
3.3.3 Box-and-whisker plots |
|
|
43 | (1) |
|
|
44 | (4) |
|
|
44 | (1) |
|
3.4.2 Axes, ticks, and par () |
|
|
44 | (2) |
|
3.4.3 Overlaying plots with graphic elements |
|
|
46 | (2) |
|
|
48 | (1) |
|
3.6 Superimposing vectors in a plot |
|
|
49 | (1) |
|
|
50 | (4) |
|
|
51 | (1) |
|
|
51 | (1) |
|
3.7.3 Incomplete axis boxes |
|
|
52 | (1) |
|
|
52 | (2) |
|
3.8 Adding text and math expressions |
|
|
54 | (2) |
|
3.8.1 Making math annotations with expression() |
|
|
55 | (1) |
|
3.9 Placing several plots in a figure |
|
|
56 | (2) |
|
3.10 Two- and three-dimensional plots |
|
|
58 | (2) |
|
|
60 | (3) |
|
3.11.1 radial.plot and polar.plot |
|
|
60 | (1) |
|
|
61 | (1) |
|
3.11.3 Error bars in plotrix |
|
|
62 | (1) |
|
|
63 | (1) |
|
3.13 Additional plotting packages |
|
|
64 | (1) |
|
4 Programming and functions |
|
|
65 | (26) |
|
4.1 Conditional execution: if and if else |
|
|
65 | (1) |
|
|
66 | (3) |
|
|
66 | (2) |
|
4.2.2 Looping with while and repeat |
|
|
68 | (1) |
|
4.3 User-defined functions |
|
|
69 | (3) |
|
|
72 | (1) |
|
4.5 Built-in mathematical functions |
|
|
73 | (2) |
|
|
73 | (1) |
|
4.5.2 Beta and gamma functions |
|
|
74 | (1) |
|
4.5.3 Binomial coefficients |
|
|
75 | (1) |
|
4.6 Special functions of mathematical physics |
|
|
75 | (3) |
|
|
75 | (1) |
|
4.6.2 Special functions in other packages |
|
|
75 | (3) |
|
4.7 Polynomial functions in packages |
|
|
78 | (8) |
|
|
79 | (4) |
|
4.7.2 orthopolynom package |
|
|
83 | (3) |
|
|
86 | (5) |
|
4.8.1 Two-dimensional random walk |
|
|
86 | (1) |
|
4.8.2 Eigenvalues of a polymer chain |
|
|
87 | (4) |
|
5 Solving systems of algebraic equations |
|
|
91 | (34) |
|
5.1 Finding the zeros of a polynomial |
|
|
91 | (1) |
|
5.2 Finding the zeros of a function |
|
|
92 | (4) |
|
|
92 | (1) |
|
|
93 | (1) |
|
5.2.3 uniroot and uniroot.all |
|
|
94 | (2) |
|
5.3 Systems of linear equations: matrix solve |
|
|
96 | (1) |
|
|
97 | (1) |
|
|
97 | (1) |
|
5.6 Overdetermined systems and generalized inverse |
|
|
98 | (1) |
|
|
99 | (5) |
|
|
99 | (2) |
|
|
101 | (1) |
|
|
102 | (2) |
|
|
104 | (5) |
|
|
105 | (1) |
|
5.8.2 Singular value decomposition |
|
|
106 | (1) |
|
|
107 | (1) |
|
|
107 | (1) |
|
5.8.5 Cholesky decomposition |
|
|
108 | (1) |
|
5.8.6 Schur decomposition |
|
|
109 | (1) |
|
5.8.7 backsolve and forwardsolve |
|
|
109 | (1) |
|
5.9 Systems of nonlinear equations |
|
|
109 | (8) |
|
5.9.1 multiroot in the rootSolve package |
|
|
109 | (2) |
|
|
111 | (1) |
|
5.9.3 BBsolve() in the BB package |
|
|
112 | (5) |
|
|
117 | (8) |
|
5.10.1 Spectroscopic analysis of a mixture |
|
|
117 | (3) |
|
5.10.2 van der Waals equation |
|
|
120 | (2) |
|
5.10.3 Chemical equilibrium |
|
|
122 | (3) |
|
6 Numerical differentiation and integration |
|
|
125 | (34) |
|
6.1 Numerical differentiation |
|
|
125 | (8) |
|
6.1.1 Numerical differentiation using base R |
|
|
125 | (1) |
|
6.1.1.1 Using the fundamental definition |
|
|
125 | (1) |
|
|
126 | (1) |
|
6.1.2 Numerical differentiation using the numDeriv package |
|
|
127 | (1) |
|
|
128 | (1) |
|
|
128 | (1) |
|
|
129 | (1) |
|
6.1.3 Numerical differentiation using the pracma package |
|
|
129 | (1) |
|
|
129 | (1) |
|
6.1.3.2 numderiv() and numdiff() |
|
|
130 | (1) |
|
6.1.3.3 grad() and gradient() |
|
|
131 | (1) |
|
|
131 | (1) |
|
|
132 | (1) |
|
|
133 | (1) |
|
6.2 Numerical integration |
|
|
133 | (19) |
|
6.2.1 integrate: Basic integration in R |
|
|
134 | (2) |
|
6.2.2 Integrating discretized functions |
|
|
136 | (1) |
|
6.2.3 Gaussian quadrature |
|
|
137 | (3) |
|
6.2.4 More integration routines in pracma |
|
|
140 | (2) |
|
6.2.5 Functions with singularities |
|
|
142 | (2) |
|
6.2.6 Infinite integration domains |
|
|
144 | (2) |
|
6.2.7 Integrals in higher dimensions |
|
|
146 | (2) |
|
6.2.8 Monte Carlo and sparse grid integration |
|
|
148 | (2) |
|
6.2.9 Complex line integrals |
|
|
150 | (2) |
|
6.3 Symbolic manipulations in R |
|
|
152 | (3) |
|
|
152 | (1) |
|
|
152 | (2) |
|
6.3.3 Polynomial functions |
|
|
154 | (1) |
|
6.3.4 Interfaces to symbolic packages |
|
|
155 | (1) |
|
|
155 | (4) |
|
6.4.1 Circumference of an ellipse |
|
|
155 | (1) |
|
6.4.2 Integration of a Lorentzian derivative spectrum |
|
|
156 | (1) |
|
6.4.3 Volume of an ellipsoid |
|
|
157 | (2) |
|
|
159 | (40) |
|
7.1 One-dimensional optimization |
|
|
159 | (3) |
|
7.2 Multi-dimensional optimization with optim() |
|
|
162 | (7) |
|
7.2.1 optim() with "Nelder-Mead" default |
|
|
163 | (2) |
|
7.2.2 optim() with "BFGS" method |
|
|
165 | (2) |
|
7.2.3 optim() with "CG" method |
|
|
167 | (1) |
|
7.2.4 optim() with "L-BFGS-B" method to find a local minimum |
|
|
167 | (2) |
|
7.3 Other optimization packages |
|
|
169 | (4) |
|
|
169 | (2) |
|
|
171 | (1) |
|
|
171 | (1) |
|
|
172 | (1) |
|
7.3.5 Derivative-free optimization algorithms |
|
|
172 | (1) |
|
7.4 Optimization with constraints |
|
|
173 | (4) |
|
7.4.1 constrOptim to optimize functions with linear constraints |
|
|
173 | (2) |
|
7.4.2 External packages alabama and Rsolnp |
|
|
175 | (2) |
|
7.5 Global optimization with many local minima |
|
|
177 | (6) |
|
7.5.1 Simulated annealing |
|
|
178 | (3) |
|
|
181 | (1) |
|
|
181 | (2) |
|
|
183 | (1) |
|
|
183 | (1) |
|
7.6 Linear and quadratic programming |
|
|
183 | (6) |
|
|
183 | (3) |
|
7.6.2 Quadratic programming |
|
|
186 | (3) |
|
7.7 Mixed-integer linear programming |
|
|
189 | (5) |
|
7.7.1 Mixed-integer problems |
|
|
189 | (1) |
|
7.7.2 Integer programming problems |
|
|
190 | (1) |
|
7.7.2.1 Knapsack problems |
|
|
191 | (1) |
|
7.7.2.2 Transportation problems |
|
|
191 | (1) |
|
7.7.2.3 Assignment problems |
|
|
192 | (1) |
|
7.7.2.4 Subsetsum problems |
|
|
193 | (1) |
|
|
194 | (5) |
|
7.8.1 Monte Carlo simulation of the 2D Ising model |
|
|
194 | (5) |
|
8 Ordinary differential equations |
|
|
199 | (50) |
|
|
200 | (5) |
|
|
201 | (2) |
|
|
203 | (2) |
|
8.2 Improved Euler method |
|
|
205 | (3) |
|
|
208 | (6) |
|
8.3.1 lsoda() and lsode() |
|
|
210 | (1) |
|
8.3.2 "adams" and related methods |
|
|
211 | (2) |
|
|
213 | (1) |
|
8.4 Matrix exponential solution for sets of linear ODEs |
|
|
214 | (1) |
|
|
215 | (5) |
|
|
220 | (1) |
|
8.7 Delay differential equations |
|
|
221 | (3) |
|
8.8 Differential algebraic equations |
|
|
224 | (3) |
|
8.9 rootSolve for steady state solutions of systems of ODEs |
|
|
227 | (3) |
|
8.10 bvpSolve package for boundary value ODE problems |
|
|
230 | (3) |
|
|
230 | (1) |
|
|
231 | (1) |
|
|
232 | (1) |
|
8.11 Stochastic differential equations: GillespieSSA package |
|
|
233 | (7) |
|
|
240 | (9) |
|
8.12.1 Launch of the space shuttle |
|
|
240 | (1) |
|
8.12.2 Electrostatic potential of DNA solutions |
|
|
241 | (3) |
|
8.12.3 Bifurcation analysis of Lotka--Volterra model |
|
|
244 | (5) |
|
9 Partial differential equations |
|
|
249 | (24) |
|
|
249 | (2) |
|
|
251 | (3) |
|
|
252 | (1) |
|
|
253 | (1) |
|
|
254 | (2) |
|
9.4 Solving PDEs with the ReacTran package |
|
|
256 | (3) |
|
|
257 | (1) |
|
|
258 | (1) |
|
|
258 | (1) |
|
9.4.4 Calling ode. 1D or steady. 1D |
|
|
259 | (1) |
|
9.5 Examples with the ReacTran package |
|
|
259 | (5) |
|
9.5.1 1-D diffusion-advection equation |
|
|
259 | (1) |
|
|
260 | (2) |
|
|
262 | (1) |
|
9.5.4 Poisson equation for a dipole |
|
|
263 | (1) |
|
|
264 | (9) |
|
9.6.1 Diffusion in a viscosity gradient |
|
|
264 | (3) |
|
9.6.2 Evolution of a Gaussian wave packet |
|
|
267 | (2) |
|
|
269 | (4) |
|
|
273 | (20) |
|
|
273 | (1) |
|
|
274 | (1) |
|
10.3 Summary statistics for a single dataset |
|
|
275 | (2) |
|
10.4 Statistical comparison of two samples |
|
|
277 | (2) |
|
10.5 Chi-squared test for goodness of fit |
|
|
279 | (1) |
|
|
280 | (1) |
|
10.7 Principal component analysis |
|
|
281 | (2) |
|
|
283 | (3) |
|
10.8.1 Using hclust for agglomerative hierarchical clustering |
|
|
283 | (1) |
|
10.8.2 Using diana for divisive hierarchical clustering |
|
|
284 | (1) |
|
10.8.3 Using kmeans for partitioning clustering |
|
|
285 | (1) |
|
10.8.4 Using pam for partitioning around medoids |
|
|
286 | (1) |
|
|
286 | (7) |
|
10.9.1 Chi square analysis of radioactive decay |
|
|
286 | (3) |
|
10.9.2 Principal component analysis of quasars |
|
|
289 | (4) |
|
11 Fitting models to data |
|
|
293 | (36) |
|
11.1 Fitting data with linear models |
|
|
293 | (3) |
|
11.1.1 Polynomial fitting with lm |
|
|
294 | (2) |
|
11.2 Fitting data with nonlinear models |
|
|
296 | (8) |
|
11.3 Inverse modeling of ODEs with the FME package |
|
|
304 | (5) |
|
11.4 Improving the convergence of series: Pade and Shanks |
|
|
309 | (2) |
|
|
311 | (5) |
|
11.5.1 Linear interpolation |
|
|
312 | (1) |
|
11.5.2 Polynomial interpolation |
|
|
313 | (1) |
|
11.5.3 Spline interpolation |
|
|
313 | (1) |
|
11.5.3.1 Integration and differentiation with splines |
|
|
314 | (1) |
|
11.5.4 Rational interpolation |
|
|
315 | (1) |
|
11.6 Time series, spectrum analysis, and signal processing |
|
|
316 | (9) |
|
11.6.1 Fast Fourier transform: fft() function |
|
|
316 | (1) |
|
11.6.2 Inverse Fourier transform |
|
|
317 | (1) |
|
11.6.3 Power spectrum: spectrum() function |
|
|
318 | (3) |
|
11.6.4 findpeaks() function |
|
|
321 | (1) |
|
|
322 | (1) |
|
11.6.5.1 Butterworth filter |
|
|
322 | (2) |
|
11.6.5.2 Savitzky--Golay filter |
|
|
324 | (1) |
|
|
324 | (1) |
|
|
325 | (4) |
|
11.7.1 Fitting a rational function to data |
|
|
325 | (2) |
|
11.7.2 Rise of atmospheric carbon dioxide |
|
|
327 | (2) |
Bibliography |
|
329 | (2) |
Index |
|
331 | |