Contents Previous Next Subchapters

Numerical Derivatives With Automatic Step Size Control
Syntax autodiff(function fepsfxh)
See Also fordiff , cendiff

Returns a central difference approximation for the derivative of the function f(y) evaluated at the point specified by x. The return value has the same number of rows as f(y) and its column dimension is equal to the row dimension of x.

If y has the same type and dimension as x, f(y) returns a column vector with the same type as x. In addition, the row dimension of f(y) is the same for all values of y. The scalar epsf has the same type as x and specifies the relative accuracy in the value of f(y). The real, double-precision or complex column vector x is the point at which the derivative of f(y) is approximated. The column vector h has the same type and dimension as x The input value of h specifies the step size for this approximation of the derivative of f(y). The output value of h is a suggested step size for the next time the partials of f(y) is evaluated nearby. You may modify this value before the next call to autodiff. (for example, to ensure it is between some maximum and minimum values).

The program below approximates the derivative of the exponential function. The routine f(y) simulates evaluating this function to a relative accuracy of 1e-5 by adding a random error. The autodiff function is called four times in order to demonstrate the improvement in the derivative approximation with each call. Note that the derivative of the exponential function at x = 1 is equal to exp(1).

function f(y) begin 
     return exp(y) * ( 1d0 + 1d-5 * rand(1, 1))
format int    "10"
format double "f15.10"
epsf = 1d-5
x    = 1d0 
h    = 1d0 
print "exp(1d0) =", exp(1d0)
print align("i, h, autodiff", ",", [10, 16, 16], "right")
for i = 1 to 4 begin
     print i, h, autodiff(function f, epsf, x, h)

If the input value of h(j) + x(j) is equal to x(j), partials of f(y) with respect to x(j) are not approximated. In this case, zero is placed in the j-th column of the return value. In addition, the output value of h(j) is equal to its input value.

The step size is controlled to be optimal for the component of f(y) that has the largest value. If the components of f(y) have very different scaling, it may help to evaluate the derivative one component at a time (maintaining a different value of h for each component).

The functions autodiff, cendiff , and fordiff can be used to approximate derivatives for an optimization or zero-finding algorithm. The function autodiff requires more function evaluations than the others, but it should be more accurate.