1function [dd,err,finaldelta] = directionaldiff(fun,x0,vec)
2% directionaldiff: estimate of
the directional derivative of a function of n variables
3% usage: [grad,err,finaldelta] = directionaldiff(fun,x0,vec)
5% Uses derivest to provide both a directional derivative
6% estimates plus an error estimates. fun needs not be vectorized.
9% fun - analytical function to differentiate. fun must
10% be a function of
the vector or array x0. Fun needs
13% x0 - vector location at which to differentiate fun
14% If x0
is an nxm array, then fun
is assumed to be
15% a function of n*m variables.
17% vec - vector defining
the line along which to take
the
18% derivative. Vec should be
the same size as x0. It
19% need not be a vector of unit length.
22% dd - scalar estimate of
the first derivative of fun
23% in
the SPECIFIED direction.
25% err - error estimate of
the directional derivative
27% finaldelta - vector of final step sizes chosen for
28% each partial derivative.
32% At
the global minimizer (1,1) of
the Rosenbrock function,
33% compute
the directional derivative in
the direction [1 2]
36% rosen = @(x) (1-x(1)).^2 + 105*(x(2)-x(1).^2).^2;
37% [dd,err] = directionaldiff(rosen,[1 1])
45% See also: derivest, gradest, gradient
48% Author: John D
'Errico
49% e-mail: woodchips@rochester.rr.com
51% Release date: 3/5/2007
53% get the size of x0 so we can make sure vec is
56if numel(x0)~=numel(vec)
57 error 'vec and x0 must be
the same sizes
'
63[dd,err,finaldelta] = derivest(@(t) fun(x0+t*vec), ...
64 0,'deriv
',1,'vectorized
','no
');
66end % mainline function end