1function fjac = fjaco(f,x,varargin) 2 3% FJACO Computes two-sided finite difference Jacobian 4% USAGE 5% fjac = fjaco(f,x,P1,P2,...) 6% INPUTS 7% f : name of function of form fval = f(x) 8% x : evaluation point 9% P1,P2,... : additional arguments for f (optional) 10% OUTPUT 11% fjac : finite difference Jacobian 12% 13% Copyright (C) 2010-2017,2019-2020 Dynare Team 14% 15% This file is part of Dynare. 16% 17% Dynare is free software: you can redistribute it and/or modify 18% it under the terms of the GNU General Public License as published by 19% the Free Software Foundation, either version 3 of the License, or 20% (at your option) any later version. 21% 22% Dynare is distributed in the hope that it will be useful, 23% but WITHOUT ANY WARRANTY; without even the implied warranty of 24% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 25% GNU General Public License for more details. 26% 27% You should have received a copy of the GNU General Public License 28% along with Dynare. If not, see <http://www.gnu.org/licenses/>. 29 30ff=feval(f,x,varargin{:}); 31 32tol = eps.^(1/3); %some default value 33if strcmp(func2str(f),'get_perturbation_params_derivs_numerical_objective') || strcmp(func2str(f),'identification_numerical_objective') 34 tol= varargin{5}.dynatol.x; 35end 36h = tol.*max(abs(x),1); 37xh1=x+h; xh0=x-h; 38h=xh1-xh0; 39fjac = NaN(length(ff),length(x)); 40for j=1:length(x) 41 xx = x; 42 xx(j) = xh1(j); f1=feval(f,xx,varargin{:}); 43 if isempty(f1) && (strcmp(func2str(f),'get_perturbation_params_derivs_numerical_objective') || strcmp(func2str(f),'identification_numerical_objective') ) 44 [~,info]=feval(f,xx,varargin{:}); 45 disp_info_error_identification_perturbation(info,j); 46 end 47 xx(j) = xh0(j); f0=feval(f,xx,varargin{:}); 48 if isempty(f0) && (strcmp(func2str(f),'get_perturbation_params_derivs_numerical_objective') || strcmp(func2str(f),'identification_numerical_objective') ) 49 [~,info]=feval(f,xx,varargin{:}); 50 disp_info_error_identification_perturbation(info,j) 51 end 52 fjac(:,j) = (f1-f0)/h(j); 53end 54 55feval(f,x,varargin{:}); 56 57%Auxiliary functions 58function disp_info_error_identification_perturbation(info,j) 59 % there are errors in the solution algorithm 60 probl_par = get_the_name(j,varargin{5}.TeX,varargin{3},varargin{2},varargin{5}); 61 skipline() 62 message = get_error_message(info,varargin{5}); 63 fprintf('Parameter error in numerical two-sided difference method:\n') 64 fprintf('Cannot solve the model for %s (info = %d, %s)\n', probl_par, info(1), message); 65 fprintf('Possible solutions:\n') 66 fprintf(' -- check your mod file, calibration and steady state computations carefully\n'); 67 fprintf(' -- use analytic derivatives, i.e. set analytic_derivation_mode=0\n'); 68 fprintf(' -- use an estimated_params block without %s or change its value\n', probl_par); 69 fprintf(' -- change numerical tolerance level in fjaco.m (you can tune ''options_.dynatol.x'' or change fjaco.m function directly)\n'); 70 error('fjaco.m: numerical two-sided difference method yields errors in solution algorithm'); 71end 72 73end %main function end