jacob_element & hess_element: remove unnecessary loops

time-shift
Houtan Bastani 2020-05-04 12:12:41 -04:00
parent 6e0f104d7e
commit 25a977f4be
No known key found for this signature in database
GPG Key ID: 000094FB955BE169
2 changed files with 21 additions and 25 deletions

View File

@ -14,7 +14,7 @@ function d=hess_element(func,element1,element2,args)
% SPECIAL REQUIREMENTS
% none
% Copyright (C) 2010-2011 Dynare Team
% Copyright (C) 2010-2020 Dynare Team
%
% This file is part of Dynare.
%
@ -31,6 +31,8 @@ function d=hess_element(func,element1,element2,args)
% You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <http://www.gnu.org/licenses/>.
assert(element1 <= length(args) && element2 <= length(args));
func = str2func(func);
h=10e-6;
@ -40,23 +42,18 @@ m10 = args;
m01 = args;
p11 = args;
m11 = args;
for i=1:size(args,2)
if i==element1
p10{i} = p10{i} + h;
m10{i} = m10{i} - h;
p11{i} = p11{i} + h;
m11{i} = m11{i} - h;
end
p10{element1} = p10{element1} + h;
m10{element1} = m10{element1} - h;
if i==element2
p01{i} = p01{i} + h;
m01{i} = m01{i} - h;
p11{element1} = p11{element1} + h;
m11{element1} = m11{element1} - h;
p01{element2} = p01{element2} + h;
m01{element2} = m01{element2} - h;
p11{i} = p11{i} + h;
m11{i} = m11{i} - h;
end
end
p11{element2} = p11{element2} + h;
m11{element2} = m11{element2} - h;
% From Abramowitz and Stegun. Handbook of Mathematical Functions (1965)
% formulas 25.3.24 and 25.3.27 p. 884
@ -75,3 +72,4 @@ else
-func(p11{:})...
-func(m11{:}))/(-2*h^2);
end
end

View File

@ -13,7 +13,7 @@ function d=jacob_element(func,element,args)
% SPECIAL REQUIREMENTS
% none
% Copyright (C) 2010-2011 Dynare Team
% Copyright (C) 2010-2020 Dynare Team
%
% This file is part of Dynare.
%
@ -30,17 +30,15 @@ function d=jacob_element(func,element,args)
% You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <http://www.gnu.org/licenses/>.
assert(element <= length(args));
func = str2func(func);
h=10e-6;
pargs=args;
margs=args;
% length(args) is used instead of size(args, 2) to avoid to transpose column vectors
for i=1:length(args)
if i==element
pargs{i} = pargs{i} + h;
margs{i} = margs{i} - h;
end
args{element} = args{element} + h;
margs{element} = margs{element} - h;
d=(func(args{:})-func(margs{:}))/(2*h);
end
d=(func(pargs{:})...
-func(margs{:}))/(2*h);