Contents | Previous | Next | Subchapters |

Syntax |
`autodiff(function ` f`, ` epsf`, ` x`, ` h`)` |

See Also | fordiff , cendiff |

Returns a central difference approximation for the derivative of the function

*f*(*y*)

evaluated at the point
specified by *f*(*y*)

and its column dimension is equal to the row dimension of

If *f*(*y*)

returns a column vector with the same type as *f*(*y*)

is the same for all
values of *f*(*y*)

.
The real, double-precision or complex column vector *f*(*y*)

is approximated.
The column vector *f*(*y*)

.
The output value 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)`

.
```
```

clear

function f(y) begin

return exp(y) * ( 1d0 + 1d-5 * rand(1, 1))

end

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)

end

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

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.