Debian Codegraph @

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

raw
1 function [grdd, badg] = numgrad(fcn,x0,varargin)
2 %function grdd = fn_gradcd(fcn,x0,varargin)
3 % computes numerical gradient of a single-valued function or Jacobian
4 % matrix of a vector-valued function using a central difference with
5 % function grdd = gradcd(fcn,x0,grdh)
6 %
7 % fcn: a string naming a vector-valued function (f:n-by-1 -> k-by-1).
8 % x0: a column vector n-by-1, at which point the hessian is evaluated.
9 % grdh: step size, n*1. Set as follows
10 % step = eps^(1/3);
11 % %step = 1e-04;
12 % grdh = step * (max([abs(x) ones(length(x),1)]'))' .* (abs(x) ./ x);
13 %--------------------
14 % grdd: n-by-k Jacobian matrix (gradients).
15 %
16 % Written by Tao Zha, 2002.
17 %
18 % Copyright (C) 1997-2012 Tao Zha
19 %
20 % This free software: you can redistribute it and/or modify
21 % it under the terms of the GNU General Public License as published by
22 % the Free Software Foundation, either version 3 of the License, or
23 % (at your option) any later version.
24 %
25 % It is distributed in the hope that it will be useful,
26 % but WITHOUT ANY WARRANTY; without even the implied warranty of
27 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 % GNU General Public License for more details.
29 %
30 % If you did not received a copy of the GNU General Public License
31 % with this software, see <http://www.gnu.org/licenses/>.
32 %
33
34
35 stps = eps^(1/3);
36 % eps: floating point relative accuracy or machine precision: 2.22e-16
37 % stps: step size recommended by Dennis and Schnabel: 6.006e-6
38
39 x0 = x0(:);
40 %tailstr = ')';
41 %for i=nargin-3:-1:1
42 % tailstr=[ ',P' num2str(i) tailstr];
43 %end
44 f0 = eval([fcn '(x0,varargin{:})']);
45 %f0 = eval([fcn '(x0' tailstr]);
46
47 % ** initializations
48 n = length(x0);
49 k = length(f0); % dimension of "fcn"
50
51 % ** Computation of stepsize (dh)
52 if (0)
53 dh = 1.0e-06; %grdh;
54 else
55 ax0 = abs(x0);
56 if all(x0)
57 dax0 = x0 ./ ax0;
58 else
59 dax0 = 1;
60 end
61 dh = stps * (max([ax0 ones(n,1)]'))' .* dax0;
62 end
63
64 xdh = x0 + dh;
65 dh = xdh - x0; % This increases precision slightly
66 %
67 argplus = x0(:,ones(n,1));
68 argminus = argplus;
69 dnum = 1:n+1:n^2; % positions of diagonals in vec(argplus).
70 argplus(dnum) = xdh; % replace the diagonals of "argplus" by "xdh".
71 argminus(dnum) = x0-dh; % replace the diagonals of "argplus" by "xdh".
72
73 grdd = zeros(k,n); % preallocate to speed the loop.
74 badg=0;
75 i = 0;
76 while i ~= n
77 i = i+1;
78 fp = eval([fcn '(argplus(:,i),varargin{:})']);
79 fm = eval([fcn '(argminus(:,i),varargin{:})']);
80 % fp = eval([fcn '(argplus(:,i)' tailstr]);
81 % fm = eval([fcn '(argminus(:,i)' tailstr]);
82 g0 = fp - fm;
83
84 if abs(g0)< 1e15
85 grdd(:,i)=g0;
86 % disp('good gradient')
87 else
88 disp('bad gradient ------------------------')
89 % fprintf('Gradient w.r.t. %3d: %10g\n',i,g0) %see above
90 grdd(:,i)=0;
91 badg=1;
92 % return
93 % can return here to save time if the gradient will never be
94 % used when badg returns as true.
95 end
96
97 end
98 dhm = dh(:,ones(k,1));
99 dhm = dhm'; % k*n
100 grdd = grdd ./ (2*dhm); % k-by-n.
101 grdd = grdd'; % n-by-k.
102