Learn R Programming

pracma (version 1.1.6)

complexstep: Complex Step Derivation

Description

Complex step derivatives of real-valued functions.

Usage

complexstep(f, x0, h = 1e-20, test = FALSE, ...)

complexstepJ(f, x0, h = 1e-20, ...)

Arguments

f
Function that is to be differentiated.
x0
Point at which to differentiate the function.
h
Step size to be applied; shall be very small.
test
logical; if TRUE the complex step derivative will be compared to the central derivative formula and a warning issued if the difference is too large.
...
Additional variables to be passed to f.

Value

  • complexstep(f, x0) returns the derivative $f'(x_0)$ of $f$ at $x_0$. The function is vectorized in x0.

Details

Complex step derivation is a fast and highly exact way of numerically differentiating a function. If the following conditions are satisfied, there will be no loss of accuracy between computing a function value and computing the derivative at a certain point.
  • fmust have an analytical (i.e., complex differentiable) continuation into an open neighborhood ofx0.
  • x0andf(x0)must be real.
  • his real andverysmall:0 < h << 1.

References

Martins, J. R. R. A., P. Sturdza, and J. J. Alonso (2003). The Complex-step Derivative Approximation. ACM Transactions on Mathematical Software, Vol. 29, No. 3, pp. 245--262.

See Also

jacobian

Examples

Run this code
##  Example from Martins et al.
f <- function(x) exp(x)/sqrt(sin(x)^3 + cos(x)^3)  # derivative at x0 = 1.5
# central diff formula    # 4.05342789402801, error 1e-10
# numDeriv::grad(f, 1.5)  # 4.05342789388197, error 1e-12  Richardson
# pracma::numderiv        # 4.05342789389868, error 5e-14  Richardson
complexstep(f, 1.5)       # 4.05342789389862, error 1e-15
# Symbolic calculation:   # 4.05342789389862

complexstepJ(f, 1.5)

Run the code above in your browser using DataLab