LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
directionaldiff.m
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)
4%
5% Uses derivest to provide both a directional derivative
6% estimates plus an error estimates. fun needs not be vectorized.
7%
8% arguments: (input)
9% fun - analytical function to differentiate. fun must
10% be a function of the vector or array x0. Fun needs
11% not be vectorized.
12%
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.
16%
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.
20%
21% arguments: (output)
22% dd - scalar estimate of the first derivative of fun
23% in the SPECIFIED direction.
24%
25% err - error estimate of the directional derivative
26%
27% finaldelta - vector of final step sizes chosen for
28% each partial derivative.
29%
30%
31% Example:
32% At the global minimizer (1,1) of the Rosenbrock function,
33% compute the directional derivative in the direction [1 2]
34% It should be 0.
35%
36% rosen = @(x) (1-x(1)).^2 + 105*(x(2)-x(1).^2).^2;
37% [dd,err] = directionaldiff(rosen,[1 1])
38%
39% dd =
40% 0
41% err =
42% 0
43%
44%
45% See also: derivest, gradest, gradient
46%
47%
48% Author: John D'Errico
49% e-mail: woodchips@rochester.rr.com
50% Release: 1.0
51% Release date: 3/5/2007
52
53% get the size of x0 so we can make sure vec is
54% the same shape.
55sx = size(x0);
56if numel(x0)~=numel(vec)
57 error 'vec and x0 must be the same sizes'
58end
59vec = vec(:);
60vec = vec/norm(vec);
61vec = reshape(vec,sx);
62
63[dd,err,finaldelta] = derivest(@(t) fun(x0+t*vec), ...
64 0,'deriv',1,'vectorized','no');
65
66end % mainline function end
67
68