From 234aaacf92d204ed0cb26e482d875eca93c700a9 Mon Sep 17 00:00:00 2001 From: Marco Ratto Date: Thu, 23 Mar 2017 00:01:11 +0100 Subject: [PATCH] factorized display of critical values. --- matlab/perfect-foresight-models/sim1.m | 56 +++++++++++++++----------- 1 file changed, 33 insertions(+), 23 deletions(-) diff --git a/matlab/perfect-foresight-models/sim1.m b/matlab/perfect-foresight-models/sim1.m index bb93691ed..3d86e4923 100644 --- a/matlab/perfect-foresight-models/sim1.m +++ b/matlab/perfect-foresight-models/sim1.m @@ -165,6 +165,11 @@ for iter = 1:options.simul.maxit dy = -lin_solve( A, res, verbose ); end end + if (any(~isreal(dy)) || any(isnan(dy)) || any(isinf(dy)) , + if verbose + display_critical_variables(reshape(dy,[ny periods])', M); + end + end Y(i_upd) = Y(i_upd) + dy; end @@ -184,33 +189,12 @@ if stop if verbose skipline() disp(sprintf('Total time of simulation: %s.', num2str(etime(clock,h1)))) - dyy=reshape(dy,[size(endogenousvariables,1) periods])'; if ~isreal(res) || ~isreal(Y) disp('Simulation terminated with imaginary parts in the residuals or endogenous variables.') - if any(~isreal(dy)) - indx = find(any(~isreal(dyy))); - endo_names=cellstr(M.endo_names(indx,:)); - disp('Newton algorithm provided complex number for variables:') - fprintf('%s, ', endo_names{:}); - skipline() - end else disp('Simulation terminated with NaN or Inf in the residuals or endogenous variables.') - if any(isnan(dy)) - indx = find(any(isnan(dyy))); - endo_names=cellstr(M.endo_names(indx,:)); - disp('Newton algorithm provided NaN for variables:') - fprintf('%s, ',endo_names{:}); - skipline() - end - if any(isinf(dy)) - indx = find(any(isinf(dyy))); - endo_names=cellstr(M.endo_names(indx,:)); - disp('Newton algorithm diverged (Inf) for variables:') - fprintf('%s, ',endo_names{:}); - skipline() - end end + display_critical_variables(reshape(dy,[ny periods])', M); disp('There is most likely something wrong with your model. Try model_diagnostics or another simulation method.') printline(105) end @@ -322,4 +306,30 @@ function [ x, flag, relres ] = lin_solve_robust( A, b , verbose) flag = relres > 1e-6; if flag ~= 0 && verbose fprintf( 'WARNING : Failed to find a solution to the linear system\n' ); - end \ No newline at end of file + end + +function display_critical_variables(dyy, M) + + if any(isnan(dyy)) + indx = find(any(isnan(dyy))); + endo_names=cellstr(M.endo_names(indx,:)); + disp('Last iteration provided NaN for the following variables:') + fprintf('%s, ',endo_names{:}), + fprintf('\n'), + end + if any(isinf(dyy)) + indx = find(any(isinf(dyy))); + endo_names=cellstr(M.endo_names(indx,:)); + disp('Last iteration diverged (Inf) for the following variables:') + fprintf('%s, ',endo_names{:}), + fprintf('\n'), + end + if any(~isreal(dyy)) + indx = find(any(~isreal(dyy))); + endo_names=cellstr(M.endo_names(indx,:)); + disp('Last iteration provided complex number for the following variables:') + fprintf('%s, ',endo_names{:}), + fprintf('\n'), + end + + \ No newline at end of file