Display exitflag returned by fsolve.

When the Newton fails in the simulation of backward models.
trustregion
Stéphane Adjemian (Ryûk) 2022-03-03 18:19:33 +01:00
parent e6592b3943
commit e815fb2901
Signed by: stepan
GPG Key ID: 295C1FE89E17EB3C
2 changed files with 24 additions and 5 deletions

View File

@ -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

View File

@ -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 <https://www.gnu.org/licenses/>.
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.