| 1 |
function [grdd, badg] = numgradcd(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 |
%stps = 1.0e-02; |
| 37 |
% eps: floating point relative accuracy or machine precision: 2.22e-16 |
| 38 |
% stps: step size recommended by Dennis and Schnabel: 6.006e-6 |
| 39 |
|
| 40 |
x0 = x0(:); |
| 41 |
%tailstr = ')'; |
| 42 |
%for i=nargin-3:-1:1 |
| 43 |
% tailstr=[ ',P' num2str(i) tailstr]; |
| 44 |
%end |
| 45 |
if ischar(fcn) |
| 46 |
f0 = eval([fcn '(x0,varargin{:})']); |
| 47 |
else |
| 48 |
f0 = fcn(x0,varargin{:}); |
| 49 |
end |
| 50 |
%f0 = eval([fcn '(x0' tailstr]); |
| 51 |
|
| 52 |
% ** initializations |
| 53 |
n = length(x0); |
| 54 |
k = length(f0); % dimension of "fcn" |
| 55 |
|
| 56 |
% ** Computation of stepsize (dh) |
| 57 |
if (0) |
| 58 |
dh = 1.0e-06; %grdh; |
| 59 |
else |
| 60 |
ax0 = abs(x0); |
| 61 |
if all(x0) |
| 62 |
dax0 = x0 ./ ax0; |
| 63 |
else |
| 64 |
dax0 = 1; |
| 65 |
end |
| 66 |
dh = stps * (max([ax0 ones(n,1)]'))' .* dax0; |
| 67 |
end |
| 68 |
|
| 69 |
xdh = x0 + dh; |
| 70 |
dh = xdh - x0; % This increases precision slightly |
| 71 |
% |
| 72 |
argplus = x0(:,ones(n,1)); |
| 73 |
argminus = argplus; |
| 74 |
dnum = 1:n+1:n^2; % positions of diagonals in vec(argplus). |
| 75 |
argplus(dnum) = xdh; % replace the diagonals of "argplus" by "xdh". |
| 76 |
argminus(dnum) = x0-dh; % replace the diagonals of "argplus" by "xdh". |
| 77 |
|
| 78 |
grdd = zeros(k,n); % preallocate to speed the loop. |
| 79 |
badg=0; |
| 80 |
i = 0; |
| 81 |
while i ~= n |
| 82 |
i = i+1; |
| 83 |
if ischar(fcn) |
| 84 |
|
| 85 |
fp = eval([fcn '(argplus(:,i),varargin{:})']); |
| 86 |
fm = eval([fcn '(argminus(:,i),varargin{:})']); |
| 87 |
else |
| 88 |
|
| 89 |
fp = fcn(argplus(:,i),varargin{:}); |
| 90 |
fm = fcn(argminus(:,i),varargin{:}); |
| 91 |
end |
| 92 |
% fp = eval([fcn '(argplus(:,i)' tailstr]); |
| 93 |
% fm = eval([fcn '(argminus(:,i)' tailstr]); |
| 94 |
g0 = fp - fm; |
| 95 |
|
| 96 |
if abs(g0)< 1e15 |
| 97 |
grdd(:,i)=g0; |
| 98 |
% disp('good gradient') |
| 99 |
else |
| 100 |
disp('bad gradient ------------------------') |
| 101 |
% fprintf('Gradient w.r.t. %3d: %10g\n',i,g0) %see above |
| 102 |
grdd(:,i)=0; |
| 103 |
badg=1; |
| 104 |
% return |
| 105 |
% can return here to save time if the gradient will never be |
| 106 |
% used when badg returns as true. |
| 107 |
end |
| 108 |
|
| 109 |
end |
| 110 |
dhm = dh(:,ones(k,1)); |
| 111 |
dhm = dhm'; % k*n |
| 112 |
grdd = grdd ./ (2*dhm); % k-by-n. |
| 113 |
grdd = grdd'; % n-by-k. |
| 114 |
|