From ee2c30fd97c76b64cfa1f6c3fbef3d2add7e1fd3 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Adjemian=20=28Charybdis=29?=
 <stephane.adjemian@univ-lemans.fr>
Date: Wed, 22 Mar 2017 14:04:35 +0100
Subject: [PATCH] Partialm cherry-pick of
 28e95d817a181f758dab595d6a14de0ff348bed5

Give more informations in case of NaN, Inf or non real variables.

Closes PR #1385.
---
 matlab/perfect-foresight-models/sim1.m | 28 +++++++++++++++++++++++++-
 1 file changed, 27 insertions(+), 1 deletion(-)

diff --git a/matlab/perfect-foresight-models/sim1.m b/matlab/perfect-foresight-models/sim1.m
index 05ef5b1867..bb93691ed5 100644
--- a/matlab/perfect-foresight-models/sim1.m
+++ b/matlab/perfect-foresight-models/sim1.m
@@ -184,7 +184,33 @@ if stop
         if verbose
             skipline()
             disp(sprintf('Total time of simulation: %s.', num2str(etime(clock,h1))))
-            disp('Simulation terminated with NaN or Inf in the residuals or endogenous variables.')
+            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
             disp('There is most likely something wrong with your model. Try model_diagnostics or another simulation method.')
             printline(105)
         end
-- 
GitLab