1function [HD,err,finaldelta] = hessdiag(fun,x0)
2% HESSDIAG: diagonal elements of the Hessian matrix (vector of second partials)
3% usage: [HD,err,finaldelta] = hessdiag(fun,x0)
5% When all that you want are the diagonal elements of the hessian
6% matrix, it will be more efficient to call HESSDIAG than HESSIAN.
7% HESSDIAG uses DERIVEST to provide both second derivative estimates
8% and error estimates. fun needs not be vectorized.
11% fun - SCALAR analytical function to differentiate.
12% fun must be a function of the vector or array x0.
14% x0 - vector location at which to differentiate fun
15% If x0
is an nxm array, then fun
is assumed to be
16% a function of n*m variables.
19% HD - vector of second partial derivatives of fun.
20% These are the diagonal elements of the Hessian
21% matrix, evaluated at x0.
22% HD will be a row vector of length numel(x0).
24% err - vector of error estimates corresponding to
25% each second partial derivative in HD.
27% finaldelta - vector of final step sizes chosen for
28% each second partial derivative.
32% [HD,err] = hessdiag(@(x) x(1) + x(2)^2 + x(3)^3,[1 2 3])
40% See also: derivest, gradient, gradest
43% Author: John D
'Errico
44% e-mail: woodchips@rochester.rr.com
46% Release date: 2/9/2007
48% get the size of x0 so we can reshape
52% total number of derivatives we will need to take
59 [HD(ind),err(ind),finaldelta(ind)] = derivest( ...
60 @(xi) fun(swapelement(x0,ind,xi)), ...
61 x0(ind),'deriv
',2,'vectorized
','no
');
64end % mainline function end
66% =======================================
68% =======================================
69function vec = swapelement(vec,ind,val)
70% swaps val as element ind, into the vector vec