jacob_element & hess_element: remove unnecessary loops
parent
6e0f104d7e
commit
25a977f4be
|
@ -14,7 +14,7 @@ function d=hess_element(func,element1,element2,args)
|
||||||
% SPECIAL REQUIREMENTS
|
% SPECIAL REQUIREMENTS
|
||||||
% none
|
% none
|
||||||
|
|
||||||
% Copyright (C) 2010-2011 Dynare Team
|
% Copyright (C) 2010-2020 Dynare Team
|
||||||
%
|
%
|
||||||
% This file is part of Dynare.
|
% 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
|
% You should have received a copy of the GNU General Public License
|
||||||
% along with Dynare. If not, see <http://www.gnu.org/licenses/>.
|
% along with Dynare. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
assert(element1 <= length(args) && element2 <= length(args));
|
||||||
|
|
||||||
func = str2func(func);
|
func = str2func(func);
|
||||||
|
|
||||||
h=10e-6;
|
h=10e-6;
|
||||||
|
@ -40,23 +42,18 @@ m10 = args;
|
||||||
m01 = args;
|
m01 = args;
|
||||||
p11 = args;
|
p11 = args;
|
||||||
m11 = 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;
|
p10{element1} = p10{element1} + h;
|
||||||
m11{i} = m11{i} - h;
|
m10{element1} = m10{element1} - h;
|
||||||
end
|
|
||||||
|
|
||||||
if i==element2
|
p11{element1} = p11{element1} + h;
|
||||||
p01{i} = p01{i} + h;
|
m11{element1} = m11{element1} - h;
|
||||||
m01{i} = m01{i} - h;
|
|
||||||
|
|
||||||
p11{i} = p11{i} + h;
|
p01{element2} = p01{element2} + h;
|
||||||
m11{i} = m11{i} - h;
|
m01{element2} = m01{element2} - h;
|
||||||
end
|
|
||||||
end
|
p11{element2} = p11{element2} + h;
|
||||||
|
m11{element2} = m11{element2} - h;
|
||||||
|
|
||||||
% From Abramowitz and Stegun. Handbook of Mathematical Functions (1965)
|
% From Abramowitz and Stegun. Handbook of Mathematical Functions (1965)
|
||||||
% formulas 25.3.24 and 25.3.27 p. 884
|
% formulas 25.3.24 and 25.3.27 p. 884
|
||||||
|
@ -75,3 +72,4 @@ else
|
||||||
-func(p11{:})...
|
-func(p11{:})...
|
||||||
-func(m11{:}))/(-2*h^2);
|
-func(m11{:}))/(-2*h^2);
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
|
@ -13,7 +13,7 @@ function d=jacob_element(func,element,args)
|
||||||
% SPECIAL REQUIREMENTS
|
% SPECIAL REQUIREMENTS
|
||||||
% none
|
% none
|
||||||
|
|
||||||
% Copyright (C) 2010-2011 Dynare Team
|
% Copyright (C) 2010-2020 Dynare Team
|
||||||
%
|
%
|
||||||
% This file is part of Dynare.
|
% 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
|
% You should have received a copy of the GNU General Public License
|
||||||
% along with Dynare. If not, see <http://www.gnu.org/licenses/>.
|
% along with Dynare. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
assert(element <= length(args));
|
||||||
|
|
||||||
func = str2func(func);
|
func = str2func(func);
|
||||||
|
|
||||||
h=10e-6;
|
h=10e-6;
|
||||||
pargs=args;
|
|
||||||
margs=args;
|
margs=args;
|
||||||
% length(args) is used instead of size(args, 2) to avoid to transpose column vectors
|
|
||||||
for i=1:length(args)
|
args{element} = args{element} + h;
|
||||||
if i==element
|
margs{element} = margs{element} - h;
|
||||||
pargs{i} = pargs{i} + h;
|
|
||||||
margs{i} = margs{i} - h;
|
d=(func(args{:})-func(margs{:}))/(2*h);
|
||||||
end
|
end
|
||||||
end
|
|
||||||
d=(func(pargs{:})...
|
|
||||||
-func(margs{:}))/(2*h);
|
|
||||||
|
|
Loading…
Reference in New Issue