From 1975e467b50d7bdf513ea8ddc5b2ef2966ef2905 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Sat, 30 May 2020 00:00:00 +0100 Subject: [PATCH] update jacob_element & hess_element for vector arguments --- matlab/hess_element.m | 34 ++++++++++++++++++---------------- matlab/jacob_element.m | 15 ++++++++------- 2 files changed, 26 insertions(+), 23 deletions(-) diff --git a/matlab/hess_element.m b/matlab/hess_element.m index 5af38ecd2..069a0ea9e 100644 --- a/matlab/hess_element.m +++ b/matlab/hess_element.m @@ -1,15 +1,17 @@ -function d=hess_element(func,element1,element2,args) -% function d=hess_element(func,element1,element2,args) +function d=hess_element(func,arg1,arg2,elem1,elem2,args) +% function d=hess_element(func,arg1,arg2,elem1,elem2,args) % returns an entry of the finite differences approximation to the hessian of func % % INPUTS % func [function name] string with name of the function -% element1 [int] the indices showing the element within the hessian that should be returned -% element2 [int] +% arg1 [int] the indices showing the element within the hessian that should be returned +% arg2 [int] +% elem1 [int] vector index 1 +% elem2 [int] vector index 2 % args [cell array] arguments provided to func % % OUTPUTS -% d [double] the (element1,element2) entry of the hessian +% d [double] the (arg1,arg2) entry of the hessian % % SPECIAL REQUIREMENTS % none @@ -31,7 +33,7 @@ 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 . -assert(element1 <= length(args) && element2 <= length(args)); +assert(arg1 <= length(args) && arg2 <= length(args)); func = str2func(func); @@ -43,21 +45,21 @@ m01 = args; p11 = args; m11 = args; -p10{element1} = p10{element1} + h; -m10{element1} = m10{element1} - h; +p10{arg1}(elem1) = p10{arg1}(elem1) + h; +m10{arg1}(elem1) = m10{arg1}(elem1) - h; -p11{element1} = p11{element1} + h; -m11{element1} = m11{element1} - h; - -p01{element2} = p01{element2} + h; -m01{element2} = m01{element2} - h; +p11{arg1}(elem1) = p11{arg1}(elem1) + h; +m11{arg1}(elem1) = m11{arg1}(elem1) - h; -p11{element2} = p11{element2} + h; -m11{element2} = m11{element2} - h; +p01{arg2}(elem2) = p01{arg2}(elem2) + h; +m01{arg2}(elem2) = m01{arg2}(elem2) - h; + +p11{arg2}(elem2) = p11{arg2}(elem2) + h; +m11{arg2}(elem2) = m11{arg2}(elem2) - h; % From Abramowitz and Stegun. Handbook of Mathematical Functions (1965) % formulas 25.3.24 and 25.3.27 p. 884 -if element1==element2 +if arg1==arg2 d = (16*func(p10{:})... +16*func(m10{:})... -30*func(args{:})... diff --git a/matlab/jacob_element.m b/matlab/jacob_element.m index 4f49cdbab..e2a04b426 100644 --- a/matlab/jacob_element.m +++ b/matlab/jacob_element.m @@ -1,14 +1,15 @@ -function d=jacob_element(func,element,args) -% function d=jacob_element(func,element,args) +function d=jacob_element(func,arg,elem,args) +% function d=jacob_element(func,arg,elem,args) % returns an entry of the finite differences approximation to the jacobian of func % % INPUTS % func [function name] string with name of the function -% element [int] the index showing the element within the jacobian that should be returned +% arg [int] the index showing the elem within the jacobian that should be returned +% elem [int] vector index % args [cell array] arguments provided to func % % OUTPUTS -% d [double] jacobian[element] +% d [double] jacobian[elem] % % SPECIAL REQUIREMENTS % none @@ -30,15 +31,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 . -assert(element <= length(args)); +assert(arg <= length(args)); func = str2func(func); h=1e-6; margs=args; -args{element} = args{element} + h; -margs{element} = margs{element} - h; +args{arg}(elem) = args{arg}(elem) + h; +margs{arg}(elem) = margs{arg}(elem) - h; d=(func(args{:})-func(margs{:}))/(2*h); end