jacob_element & hess_element: remove unnecessary loops

parent 6e0f104d
......@@ -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
......@@ -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);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment