From 0db6463703c36b4f74b05cbb7dcbf631a2944d93 Mon Sep 17 00:00:00 2001
From: Johannes Pfeifer <jpfeifer@gmx.de>
Date: Tue, 5 Feb 2019 10:00:54 +0100
Subject: [PATCH] mr_hessian.m: prevent infinite loop

Make sure hessian is not 0 and add counter.
Fixes #1636

(cherry picked from commit d5e25c8f436e88f2bc3dbba0237cdcf3f245d80f)
---
 matlab/optimization/mr_hessian.m | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/matlab/optimization/mr_hessian.m b/matlab/optimization/mr_hessian.m
index b234df9b7c..ec8ac1e900 100644
--- a/matlab/optimization/mr_hessian.m
+++ b/matlab/optimization/mr_hessian.m
@@ -125,17 +125,20 @@ while i<n
         end
         if abs(dx(it))>(3*hess_info.htol)
             hess_info.h1(i)= hess_info.htol/abs(dx(it))*hess_info.h1(i);
+            hess_info.h1(i) = max(hess_info.h1(i),1e-10);
             xh1(i)=x(i)+hess_info.h1(i);
             try
                 [fx,exit_flag,ffx]=penalty_objective_function(xh1,func,penalty,varargin{:});
             catch
                 fx=1.e8;
             end
-            while (fx-f0)==0
+            iter=0;
+            while (fx-f0)==0 && iter<50
                 hess_info.h1(i)= hess_info.h1(i)*2;
                 xh1(i)=x(i)+hess_info.h1(i);
                 [fx,exit_flag,ffx]=penalty_objective_function(xh1,func,penalty,varargin{:});
                 ic=1;
+                iter=iter+1;
             end
         end
         it=it+1;
-- 
GitLab