Debian Codegraph @

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

raw
1 function [g, badg] = numgrad(fcn,x,varargin)
2 % function [g badg] = numgrad(fcn,xvarargin)
3 %
4 % Copyright (C) 1997-2012 Tao Zha
5 %
6 % This free software: you can redistribute it and/or modify
7 % it under the terms of the GNU General Public License as published by
8 % the Free Software Foundation, either version 3 of the License, or
9 % (at your option) any later version.
10 %
11 % It is distributed in the hope that it will be useful,
12 % but WITHOUT ANY WARRANTY; without even the implied warranty of
13 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 % GNU General Public License for more details.
15 %
16 % If you did not received a copy of the GNU General Public License
17 % with this software, see <http://www.gnu.org/licenses/>.
18 %
19
20 %
21 delta = 1e-6; % works for a general case
22 %delta = 1e-2; % bigger step -- works for a flat peak
23 %delta=1e-04;
24
25 n=length(x);
26 tvec=delta*eye(n);
27 g=zeros(n,1);
28 %--------------------old way to deal with variable # of P's--------------
29 %tailstr = ')';
30 %stailstr = [];
31 %for i=nargin-2:-1:1
32 % tailstr=[ ',P' num2str(i) tailstr];
33 % stailstr=[' P' num2str(i) stailstr];
34 %end
35 %f0 = eval([fcn '(x' tailstr]); % Is there a way not to do this?
36 %---------------------------------------------------------------^yes
37 f0 = eval([fcn '(x,varargin{:})']);
38 % disp(' first fcn in numgrad.m ------------------')
39 %home
40 % disp('numgrad.m is working. ----') % Jiinil on 9/5/95
41 % sizex=size(x),sizetvec=size(tvec),x, % Jinill on 9/6/95
42 badg=0;
43 for i=1:n
44 scale=1; % originally 1
45 % i,tveci=tvec(:,i)% ,plus=x+scale*tvec(:,i) % Jinill Kim on 9/6/95
46 if size(x,1)>size(x,2)
47 tvecv=tvec(i,:);
48 else
49 tvecv=tvec(:,i);
50 end
51 g0 = (eval([fcn '(x+scale*tvecv'', varargin{:})']) - f0) ...
52 /(scale*delta);
53 % disp(' fcn in the i=1:n loop of numgrad.m ------------------')% Jinill 9/6/95
54 % disp(' and i is') % Jinill
55 % i % Jinill
56 % fprintf('Gradient w.r.t. %3d: %10g\n',i,g0) %see below Jinill 9/6/95
57 % -------------------------- special code to essentially quit here
58 % absg0=abs(g0) % Jinill on 9/6/95
59 if abs(g0)< 1e15
60 g(i)=g0;
61 % disp('good gradient') % Jinill Kim
62 else
63 disp('bad gradient ------------------------') % Jinill Kim
64 % fprintf('Gradient w.r.t. %3d: %10g\n',i,g0) %see above
65 g(i)=0;
66 badg=1;
67 % return
68 % can return here to save time if the gradient will never be
69 % used when badg returns as true.
70 end
71 end
72 %-------------------------------------------------------------
73 % if g0 > 0
74 % sided=2;
75 % g1 = -(eval([fcn '(x-scale*tvec(:,i)''' tailstr]) - f0) ...
76 % /(scale*delta);
77 % if g1<0
78 % scale = scale/10;
79 % else
80 % break
81 % end
82 % else
83 % sided=1;
84 % break
85 % end
86 % end
87 % if sided==1
88 % g(i)=g0;
89 % else
90 % if (g0<1e20)
91 % if (g1>-1e20)
92 % g(i)=(g0+g1)/2;
93 % else
94 % g(i)=0;
95 % badg=1;
96 % disp( ['Banging against wall, parameter ' int2str(i)] );
97 % end
98 % else
99 % if g1>-1e20
100 % if g1<0
101 % g(i)=0;
102 % badg=1;
103 % disp( ['Banging against wall, parameter ' int2str(i)] );
104 % else
105 % g(i)=g1;
106 % end
107 % else
108 % g(i)=0;
109 % badg=1;
110 % disp(['Valley around parameter ' int2str(i)])
111 % end
112 % end
113 % end
114 %end
115 %save g.dat g x f0
116 %eval(['save g g x f0 ' stailstr]);