From 1b56a56e7847da33a9f043b0198c5b0ae6d245cc Mon Sep 17 00:00:00 2001
From: Johannes Pfeifer <jpfeifer@gmx.de>
Date: Thu, 12 Dec 2019 18:50:46 +0100
Subject: [PATCH] dynare_solve.m: return with valid solution even if Jacobian
 is ill-behaved

In pathological cases, the Jacobian at the initial but true steady state value is ill-behaved. In this case we should return with the valid steady state instead of trying random starting values
---
 matlab/dynare_solve.m | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/matlab/dynare_solve.m b/matlab/dynare_solve.m
index 9ba3ea3f1f..ce48f98748 100644
--- a/matlab/dynare_solve.m
+++ b/matlab/dynare_solve.m
@@ -78,6 +78,10 @@ if jacobian_flag
     wrong_initial_guess_flag = false;
     if ~all(isfinite(fvec)) || any(isinf(fjac(:))) || any(isnan((fjac(:)))) ...
             || any(~isreal(fvec)) || any(~isreal(fjac(:)))
+        if max(abs(fvec)) < tolf %return if initial value solves problem
+            info = 0; 
+            return;
+        end    
         disp('Randomize initial guess...')
         % Let's try random numbers for the variables initialized with the default value.
         wrong_initial_guess_flag = true;
-- 
GitLab