2021-01-12 16:48:49 +01:00
|
|
|
function d=hess_element(func,element1,element2,args)
|
|
|
|
% function d=hess_element(func,element1,element2,args)
|
2010-02-22 17:33:38 +01:00
|
|
|
% returns an entry of the finite differences approximation to the hessian of func
|
|
|
|
%
|
|
|
|
% INPUTS
|
2010-10-07 14:15:56 +02:00
|
|
|
% func [function name] string with name of the function
|
2021-01-12 16:48:49 +01:00
|
|
|
% element1 [int] the indices showing the element within the hessian that should be returned
|
|
|
|
% element2 [int]
|
2010-02-22 17:33:38 +01:00
|
|
|
% args [cell array] arguments provided to func
|
|
|
|
%
|
|
|
|
% OUTPUTS
|
2021-01-12 16:48:49 +01:00
|
|
|
% d [double] the (element1,element2) entry of the hessian
|
2010-02-22 17:33:38 +01:00
|
|
|
%
|
|
|
|
% SPECIAL REQUIREMENTS
|
|
|
|
% none
|
|
|
|
|
2020-05-04 18:12:41 +02:00
|
|
|
% Copyright (C) 2010-2020 Dynare Team
|
2010-02-22 17:33:38 +01:00
|
|
|
%
|
|
|
|
% This file is part of Dynare.
|
|
|
|
%
|
|
|
|
% Dynare is free software: you can redistribute it and/or modify
|
|
|
|
% it under the terms of the GNU General Public License as published by
|
|
|
|
% the Free Software Foundation, either version 3 of the License, or
|
|
|
|
% (at your option) any later version.
|
|
|
|
%
|
|
|
|
% Dynare is distributed in the hope that it will be useful,
|
|
|
|
% but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
% GNU General Public License for more details.
|
|
|
|
%
|
|
|
|
% You should have received a copy of the GNU General Public License
|
2021-06-09 17:33:48 +02:00
|
|
|
% along with Dynare. If not, see <https://www.gnu.org/licenses/>.
|
2010-02-22 17:33:38 +01:00
|
|
|
|
2021-01-12 16:48:49 +01:00
|
|
|
assert(element1 <= length(args) && element2 <= length(args));
|
2020-05-04 18:12:41 +02:00
|
|
|
|
2010-10-07 14:15:56 +02:00
|
|
|
func = str2func(func);
|
|
|
|
|
2020-07-06 18:42:21 +02:00
|
|
|
h=1e-6;
|
2010-02-22 17:33:38 +01:00
|
|
|
p10 = args;
|
|
|
|
p01 = args;
|
|
|
|
m10 = args;
|
|
|
|
m01 = args;
|
|
|
|
p11 = args;
|
|
|
|
m11 = args;
|
|
|
|
|
2021-01-12 16:48:49 +01:00
|
|
|
p10{element1} = p10{element1} + h;
|
|
|
|
m10{element1} = m10{element1} - h;
|
2010-02-22 17:33:38 +01:00
|
|
|
|
2021-01-12 16:48:49 +01:00
|
|
|
p11{element1} = p11{element1} + h;
|
|
|
|
m11{element1} = m11{element1} - h;
|
|
|
|
|
|
|
|
p01{element2} = p01{element2} + h;
|
|
|
|
m01{element2} = m01{element2} - h;
|
2010-02-22 17:33:38 +01:00
|
|
|
|
2021-01-12 16:48:49 +01:00
|
|
|
p11{element2} = p11{element2} + h;
|
|
|
|
m11{element2} = m11{element2} - h;
|
2010-02-22 17:33:38 +01:00
|
|
|
|
|
|
|
% From Abramowitz and Stegun. Handbook of Mathematical Functions (1965)
|
|
|
|
% formulas 25.3.24 and 25.3.27 p. 884
|
2021-01-12 16:48:49 +01:00
|
|
|
if element1==element2
|
2010-02-22 17:33:38 +01:00
|
|
|
d = (16*func(p10{:})...
|
2011-02-04 17:17:48 +01:00
|
|
|
+16*func(m10{:})...
|
|
|
|
-30*func(args{:})...
|
|
|
|
-func(p11{:})...
|
|
|
|
-func(m11{:}))/(12*h^2);
|
2010-02-22 17:33:38 +01:00
|
|
|
else
|
|
|
|
d = (func(p10{:})...
|
2011-02-04 17:17:48 +01:00
|
|
|
+func(m10{:})...
|
|
|
|
+func(p01{:})...
|
|
|
|
+func(m01{:})...
|
|
|
|
-2*func(args{:})...
|
|
|
|
-func(p11{:})...
|
|
|
|
-func(m11{:}))/(-2*h^2);
|
2010-02-22 17:33:38 +01:00
|
|
|
end
|
2020-05-04 18:12:41 +02:00
|
|
|
end
|