


FDJAC Computes two-sided finite difference Jacobian USAGE fjac = fdjac(f,x,P1,P2,...) INPUTS f : name of function of form fval = f(x) x : evaluation point P1,P2,... : additional arguments for f (optional) OUTPUT fjac : finite differnce Jacobian Copyright (C) 2010 Dynare Team 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 along with Dynare. If not, see <http://www.gnu.org/licenses/>.


0001 function fjac = fjaco(f,x,varargin) 0002 0003 % FDJAC Computes two-sided finite difference Jacobian 0004 % USAGE 0005 % fjac = fdjac(f,x,P1,P2,...) 0006 % INPUTS 0007 % f : name of function of form fval = f(x) 0008 % x : evaluation point 0009 % P1,P2,... : additional arguments for f (optional) 0010 % OUTPUT 0011 % fjac : finite differnce Jacobian 0012 % 0013 % Copyright (C) 2010 Dynare Team 0014 % 0015 % This file is part of Dynare. 0016 % 0017 % Dynare is free software: you can redistribute it and/or modify 0018 % it under the terms of the GNU General Public License as published by 0019 % the Free Software Foundation, either version 3 of the License, or 0020 % (at your option) any later version. 0021 % 0022 % Dynare is distributed in the hope that it will be useful, 0023 % but WITHOUT ANY WARRANTY; without even the implied warranty of 0024 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0025 % GNU General Public License for more details. 0026 % 0027 % You should have received a copy of the GNU General Public License 0028 % along with Dynare. If not, see <http://www.gnu.org/licenses/>. 0029 0030 ff=feval(f,x,varargin{:}); 0031 0032 tol = eps.^(1/3); 0033 h = tol.*max(abs(x),1); 0034 xh1=x+h; xh0=x-h; 0035 h=xh1-xh0; 0036 fjac = NaN(length(ff),length(x)); 0037 for j=1:length(x); 0038 xx = x; 0039 xx(j) = xh1(j); f1=feval(f,xx,varargin{:}); 0040 xx(j) = xh0(j); f0=feval(f,xx,varargin{:}); 0041 fjac(:,j) = (f1-f0)/h(j); 0042 end 0043 0044 feval(f,x,varargin{:});