numderiv: Richardson's Numerical Derivative

Description

Richardson's method applied to the computation of the numerical derivative.

Usage

 ```1 2 3``` ```numderiv(f, x0, maxiter = 16, h = 1/2, ..., tol = .Machine\$double.eps) numdiff(f, x, maxiter = 16, h = 1/2, ..., tol = .Machine\$double.eps) ```

Arguments

 `f` function to be differentiated. `x0, x` point(s) at which the derivative is to be computed. `maxiter` maximum number of iterations. `h` starting step size, should be the default `h=0.5`. `tol` relative tolerance. `...` variables to be passed to function `f`.

Details

`numderiv` returns the derivative of `f` at `x0`, where `x0` must be a single scalar in the domain of the function.

`numdiff` is a vectorized form of `numderiv` such that the derivatives will be returned at all points of the vector `x`.

Value

Numeric scalar or vector of approximated derivatives.

Note

See `grad` in the ‘numDeriv’ package for another implementation of Richardson's method in the context of numerical differentiation.

References

Mathews, J. H., and K. D. Fink (1999). Numerical Methods Using Matlab. Third Edition, Prentice Hall.

`fderiv`, `complexstep`

Examples

 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20``` ```# Differentiate an anti-derivative function f <- function(x) sin(x)*sqrt(1+sin(x)) F <- function(x) integrate(f, 0, x, rel.tol = 1e-12)\$value x0 <- 1 dF0 <- numderiv(F, x0, tol = 6.5e-15) #=> 1.141882942715462 f(x0) # 1.141882942715464 true value # fderiv(F, x0) # 1.141882942704476 # numDeriv::grad(F, x0) # 1.141882942705797 # Compare over a whole period x <- seq(0, 2*pi, length.out = 11) max(abs(numdiff(sin, x) - cos(x))) #=> 3.44e-15 # max(abs(numDeriv::grad(sin, x) - cos(x))) # 7.70e-12 # Example from complex step f <- function(x) exp(x) / sqrt(sin(x)^3 + cos(x)^3) x0 <- 1.5 numderiv(f, x0) # 4.05342789389876, error 0.5e-12 # 4.053427893898621... true value ```

Search within the pracma package
Search all R packages, documentation and source code

Questions? Problems? Suggestions? or email at ian@mutexlabs.com.

Please suggest features or report bugs with the GitHub issue tracker.

All documentation is copyright its authors; we didn't write any of that.