-
Notifications
You must be signed in to change notification settings - Fork 0
/
derivativeCheck.m
executable file
·42 lines (35 loc) · 1.02 KB
/
derivativeCheck.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
function diff = derivativeCheck(funObj,x,order,type,varargin)
% diff = derivativeCheck(funObj,x,order,useComplex,varargin)
%
% type = 1 (simple forward-difference)
% type = 2 (central differencing - default)
% type = 3 (complex-step deriative)
if nargin < 3
order = 1; % Only check gradient by default
if nargin < 4
type = 2; % Use central-differencing by default
end
end
if order == 2
[f,g,H] = funObj(x,varargin{:});
fprintf('Checking Hessian...\n');
[f2,g2,H2] = autoHess(x,type,funObj,varargin{:});
fprintf('Max difference between user and numerical hessian: %e\n',max(abs(H(:)-H2(:))));
if max(abs(H(:)-H2(:))) > 1e-4
H
H2
diff = abs(H-H2)
pause;
end
else
[f,g] = funObj(x,varargin{:});
fprintf('Checking Gradient...\n');
[f2,g2] = autoGrad(x,type,funObj,varargin{:});
fprintf('Max difference between user and numerical gradient: %e\n',max(abs(g-g2)));
% if max(abs(g-g2)) > 1e-4
fprintf('User NumDif:\n');
[g g2]
diff = abs(g-g2)
% pause
% end
end