Debian Codegraph @

contrib/ms-sbvar/TZcode/MatlabFiles/fn_hesscd.m

raw
1 function grdd = fn_hesscd(fcn,x0,grdh,P1,P2,P3,P4,P5,P6,P7,P8,P9,...
2 P10,P11,P12,P13,P14,P15,P16,P17,P18,P19,P20)
3 % Computing numerical hessian using a central difference with
4 % function grdd = hesscd(fcn,x0,grdh,Passed variables1)
5 %
6 % fcn: a string naming the objective function.
7 % x0: a column vector n*1, at which point the hessian is evaluated.
8 % grdh: step size.
9 % grdd: hessian matrix (second derivative), n*n.
10 %
11 % Written by Tao Zha, May 1998. Revised June 1002.
12 %
13 % Copyright (C) 1997-2012 Tao Zha
14 %
15 % This free software: you can redistribute it and/or modify
16 % it under the terms of the GNU General Public License as published by
17 % the Free Software Foundation, either version 3 of the License, or
18 % (at your option) any later version.
19 %
20 % It is distributed in the hope that it will be useful,
21 % but WITHOUT ANY WARRANTY; without even the implied warranty of
22 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 % GNU General Public License for more details.
24 %
25 % If you did not received a copy of the GNU General Public License
26 % with this software, see <http://www.gnu.org/licenses/>.
27 %
28
29
30 stps = eps^(1/3);
31 % eps: floating point relative accuracy or machine precision: 2.22e-16
32 % stps: step size recommended by Dennis and Schnabel: 6.006e-6
33
34 x0 = x0(:);
35 tailstr = ')';
36 for i=nargin-3:-1:1
37 tailstr=[ ',P' num2str(i) tailstr];
38 end
39 f0 = eval([fcn '(x0' tailstr]);
40
41 % ** initializations
42 k = length(x0);
43 grdd = zeros(k);
44
45 % ** Computation of stepsize (dh)
46 if all(grdh)
47 dh = grdh;
48 else
49 ax0 = abs(x0);
50 if all(x0)
51 dax0 = x0 ./ ax0;
52 else
53 dax0 = 1;
54 end
55 dh = stps * (max([ax0 (1e-2)*ones(k,1)]'))' .* dax0;
56 end
57
58 xdh = x0 + dh;
59 dh = xdh - x0; % This increases precision slightly
60 dhm = dh(:,ones(k,1));
61 ee = eye(k) .* dhm;
62
63 i = 1;
64 while i <= k
65 j = i;
66 while j <= k
67
68 fune1 = eval([fcn '(x0 + ee(:,i) + ee(:,j)' tailstr]);
69 fune2 = eval([fcn '(x0 - ee(:,i) + ee(:,j)' tailstr]);
70 fune3 = eval([fcn '(x0 + ee(:,i) - ee(:,j)' tailstr]);
71 fune4 = eval([fcn '(x0 - ee(:,i) - ee(:,j)' tailstr]);
72 grdd(i,j) = (fune1 - fune2 - fune3 + fune4) / (4 * dh(i) * dh(j));
73
74 if i ~= j
75 grdd(j,i) = grdd(i,j);
76 end
77
78 j = j+1;
79 end
80 i = i+1;
81 end
82