From 882091cc97d8eed031d8e6d9d865a287898acd41 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 22:30:38 +0100 Subject: [PATCH] Return more info about nonlinear solver failures (solve1). --- .../simul_backward_nonlinear_model_.m | 21 ++++++++--- matlab/dynare_solve.m | 4 +-- matlab/solve1.m | 36 +++++++++++++------ 3 files changed, 42 insertions(+), 19 deletions(-) diff --git a/matlab/backward/simul_backward_nonlinear_model_.m b/matlab/backward/simul_backward_nonlinear_model_.m index cc1e1698a..bd87796bd 100644 --- a/matlab/backward/simul_backward_nonlinear_model_.m +++ b/matlab/backward/simul_backward_nonlinear_model_.m @@ -137,13 +137,25 @@ for it = initialconditions.nobs+(1:samplesize) skipline() switch exitflag case 0 - dprint('Returned value for exitflag is 0 (maximum number of iterations or evaluation reached).') + disp('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).') + if options.solve_algo==0 + disp('Returned value for exitflag is -1 (objective function stopped algorithm).') + elseif options.solve_algo==1 + disp('Returned value for exitflag is -1 (objective function cannot be evaluated at the initial guess).') + end case -3 - dprint('Returned value for exitflag is -3 (Trust region radius became too small, trust-region-dogleg algorithm).') + if options.solve_algo==0 + disp('Returned value for exitflag is -3 (Trust region radius became too small, trust-region-dogleg algorithm).') + elseif options.solve_algo==1 + disp('Returned value for exitflag is -3 (Convergence too slow).') + end case -2 - dprint('Returned value for exitflag is -2.') + if options.solve_algo==0 + disp('Returned value for exitflag is -2.') + elseif options.solve_algo==1 + disp('Returned value for exitflag is -2 (Spurious convergence).') + end end end break @@ -155,7 +167,6 @@ end ysim = DynareOutput.endo_simul(1:DynareModel.orig_endo_nbr,:); xsim = DynareOutput.exo_simul; - function display_names_of_problematic_equations(DynareModel, TruthTable) for i=1:DynareModel.orig_endo_nbr if TruthTable(i) diff --git a/matlab/dynare_solve.m b/matlab/dynare_solve.m index e38863dee..024485500 100644 --- a/matlab/dynare_solve.m +++ b/matlab/dynare_solve.m @@ -229,9 +229,7 @@ if options.solve_algo == 0 errorflag = true; end elseif options.solve_algo==1 - [x, errorflag] = solve1(f, x, 1:nn, 1:nn, jacobian_flag, options.gstep, ... - tolf, tolx, ... - maxit, [], options.debug, arguments{:}); + [x, errorflag, exitflag] = solve1(f, x, 1:nn, 1:nn, jacobian_flag, options.gstep, tolf, tolx, maxit, [], options.debug, arguments{:}); elseif options.solve_algo==9 [x, errorflag] = trust_region(f,x, 1:nn, 1:nn, jacobian_flag, options.gstep, ... tolf, tolx, maxit, ... diff --git a/matlab/solve1.m b/matlab/solve1.m index 173ada50b..7637f057f 100644 --- a/matlab/solve1.m +++ b/matlab/solve1.m @@ -1,4 +1,4 @@ -function [x, errorflag] = solve1(func, x, j1, j2, jacobian_flag, gstep, tolf, tolx, maxit, fake, debug, varargin) +function [x, errorflag, exitflag] = solve1(func, x, j1, j2, jacobian_flag, gstep, tolf, tolx, maxit, fake, debug, varargin) % Solves systems of non linear equations of several variables % @@ -61,6 +61,7 @@ idCpx = ~isreal(fvec); if any(idInf) disp('SOLVE1: during the resolution of the non-linear system, the evaluation of the following equation(s) resulted in a non-finite number:') disp(j1(idInf)') + exitflag = -1; errorflag = true; return end @@ -68,6 +69,7 @@ end if any(idNan) disp('SOLVE1: during the resolution of the non-linear system, the evaluation of the following equation(s) resulted in a nan:') disp(j1(idNan)') + exitflag = -1; errorflag = true; return end @@ -75,6 +77,7 @@ end if any(idNan) disp('SOLVE1: during the resolution of the non-linear system, the evaluation of the following equation(s) resulted in a complex number:') disp(j1(idCpx)') + exitflag = -1; errorflag = true; return end @@ -83,6 +86,7 @@ f = 0.5*(fvec'*fvec); if max(abs(fvec))