LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
hessdiag.m
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)
4%
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.
9%
10% arguments: (input)
11% fun - SCALAR analytical function to differentiate.
12% fun must be a function of the vector or array x0.
13%
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.
17%
18% arguments: (output)
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).
23%
24% err - vector of error estimates corresponding to
25% each second partial derivative in HD.
26%
27% finaldelta - vector of final step sizes chosen for
28% each second partial derivative.
29%
30%
31% Example usage:
32% [HD,err] = hessdiag(@(x) x(1) + x(2)^2 + x(3)^3,[1 2 3])
33% HD =
34% 0 2 18
35%
36% err =
37% 0 0 0
38%
39%
40% See also: derivest, gradient, gradest
41%
42%
43% Author: John D'Errico
44% e-mail: woodchips@rochester.rr.com
45% Release: 1.0
46% Release date: 2/9/2007
47
48% get the size of x0 so we can reshape
49% later.
50sx = size(x0);
51
52% total number of derivatives we will need to take
53nx = numel(x0);
54
55HD = zeros(1,nx);
56err = HD;
57finaldelta = HD;
58for ind = 1:nx
59 [HD(ind),err(ind),finaldelta(ind)] = derivest( ...
60 @(xi) fun(swapelement(x0,ind,xi)), ...
61 x0(ind),'deriv',2,'vectorized','no');
62end
63
64end % mainline function end
65
66% =======================================
67% sub-functions
68% =======================================
69function vec = swapelement(vec,ind,val)
70% swaps val as element ind, into the vector vec
71vec(ind) = val;
72
73end % sub-function end
74
75
76