From e815fb2901218f58be96b99a254b1ca6c994878e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Adjemian=20=28Ry=C3=BBk=29?= Date: Thu, 3 Mar 2022 18:19:33 +0100 Subject: [PATCH] Display exitflag returned by fsolve. When the Newton fails in the simulation of backward models. --- .../simul_backward_nonlinear_model_.m | 23 ++++++++++++++++--- matlab/dynare_solve.m | 6 +++-- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/matlab/backward/simul_backward_nonlinear_model_.m b/matlab/backward/simul_backward_nonlinear_model_.m index 31f0f7056..cc1e1698a 100644 --- a/matlab/backward/simul_backward_nonlinear_model_.m +++ b/matlab/backward/simul_backward_nonlinear_model_.m @@ -56,12 +56,12 @@ for it = initialconditions.nobs+(1:samplesize) y = y_; % A good guess for the initial conditions is the previous values for the endogenous variables. try if ismember(DynareOptions.solve_algo, [12,14]) - [DynareOutput.endo_simul(:,it), errorflag] = ... + [DynareOutput.endo_simul(:,it), errorflag, ~, ~, exitflag] = ... dynare_solve(model_dynamic_s, y, DynareOptions, ... DynareModel.isloggedlhs, DynareModel.isauxdiffloggedrhs, DynareModel.endo_names, DynareModel.lhs, ... model_dynamic, ylag, DynareOutput.exo_simul, DynareModel.params, DynareOutput.steady_state, it); else - [DynareOutput.endo_simul(:,it), errorflag] = ... + [DynareOutput.endo_simul(:,it), errorflag, ~, ~, exitflag] = ... dynare_solve(model_dynamic_s, y, DynareOptions, ... model_dynamic, ylag, DynareOutput.exo_simul, DynareModel.params, DynareOutput.steady_state, it); end @@ -130,8 +130,25 @@ for it = initialconditions.nobs+(1:samplesize) display_names_of_problematic_equations(DynareModel, residuals_evaluating_to_nan); skipline() end - % TODO Implement same checks with the jacobian matrix. + % + % Display value of exitflag if available (fsolve, solve_algo=0, only) + % + if ~isnan(exitflag) + skipline() + switch exitflag + case 0 + dprint('Returned value for exitflag is 0 (maximum number of iterations or evaluation reached).') + case -1 + dprint('Returned value for exitflag is -1 (objective function stopped algorithm).') + case -3 + dprint('Returned value for exitflag is -3 (Trust region radius became too small, trust-region-dogleg algorithm).') + case -2 + dprint('Returned value for exitflag is -2.') + end + end break + % TODO Implement same checks with the jacobian matrix. + % TODO Modify other solvers to return an exitflag. end end diff --git a/matlab/dynare_solve.m b/matlab/dynare_solve.m index 2fd4b7c9a..539eae7d7 100644 --- a/matlab/dynare_solve.m +++ b/matlab/dynare_solve.m @@ -1,4 +1,4 @@ -function [x, errorflag, fvec, fjac] = dynare_solve(f, x, options, varargin) +function [x, errorflag, fvec, fjac, exitflag] = dynare_solve(f, x, options, varargin) % Solves a nonlinear system of equations, f(x) = 0 with n unknowns % and n equations. @@ -15,7 +15,7 @@ function [x, errorflag, fvec, fjac] = dynare_solve(f, x, options, varargin) % - fvec [double] n×1 vector, function value at x (f(x), used for debugging when errorflag is true). % - fjac [double] n×n matrix, Jacobian value at x (J(x), used for debugging when errorflag is true). -% Copyright © 2001-2021 Dynare Team +% Copyright © 2001-2022 Dynare Team % % This file is part of Dynare. % @@ -32,6 +32,8 @@ function [x, errorflag, fvec, fjac] = dynare_solve(f, x, options, varargin) % You should have received a copy of the GNU General Public License % along with Dynare. If not, see . +exitflag = nan; + jacobian_flag = options.jacobian_flag; % true iff Jacobian is returned by f routine (as a second output argument). % Set tolerance parameter depending the the caller function.