comphy is an R package accompanying the book This package
accompanies the book
Computational
Physics with R. It collects all the functions described and
used throughout the book, providing ready-to-use implementations of
numerical algorithms in computational physics.
The package includes routines for:
Numerical differentiation and integration
Solvers for ordinary differential equations
Sturm–Liouville eigenvalue problems
Monte Carlo methods
Other algorithms relevant to physics and applied mathematics
You can install the development version from GitHub with:
# install.packages("devtools")
devtools::install_github("jfoadi/comphy")This is a simple example where a numerical derivative of sin(x) is computed using comphy:
library(comphy)
# Grid of known values
x <- seq(0,pi,length.out=11)
# Grid of values at which derivative must be calculated
# Same as x
x0 <- x
# Function known at tabulated points
f <- sin(x)+cos(x)
# Obtain a numerical derivative at those points
# using central differences (discard first and last value)
df <- deriv_reg(x0,x,f)
# Print derivative values
print(df)
#> [1] NA 0.6315304 0.2176105 -0.2176105 -0.6315304 -0.9836316
#> [7] -1.2394482 -1.3739389 -1.3739389 -1.2394482 NAThe two NA’s are there because the derivative is
calculated using a centred difference.
All exported functions include documentation and usage examples. For a comprehensive discussion and context, see the book Computational Physics with R.
GPL (>= 2)