From 4cccdd9074da473ba58eecf026f1f47045abf1b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Adjemian=20=28Ry=C3=BBk=29?= Date: Fri, 25 Mar 2022 19:28:32 +0100 Subject: [PATCH] Bug fix. Revert to previous Jacobian matrix if updated one is ill-behaved. --- matlab/trust_region.m | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/matlab/trust_region.m b/matlab/trust_region.m index 8fcaaaa66..728ef5ac3 100644 --- a/matlab/trust_region.m +++ b/matlab/trust_region.m @@ -109,7 +109,7 @@ catch end if any(isnan(fval)) || any(isinf(fval)) || any(~isreal(fval)) || any(isnan(fjac(:))) || any(isinf(fjac(:))) || any(~isreal(fjac(:))) - % System of equations is ill-behaved at the initial guess. + % System of equations is ill-behaved at the initial guess. return end @@ -251,6 +251,7 @@ while iter<=maxiter && ~info continue end % Compute the jacobian for the next iteration. + fjac0 = fjac; if jacobianflag try [~, fjac] = objfun(x, varargin{:}); @@ -258,6 +259,7 @@ while iter<=maxiter && ~info catch % If evaluation of the Jacobian matrix returns an error, then restart but with a smaller radius of the trust region. x = x0; + fjac = fjac0; delta = delta*radiusfactor; continue end @@ -273,6 +275,7 @@ while iter<=maxiter && ~info if any(isnan(fjac(:))) || any(isinf(fjac(:))) || any(~isreal(fjac(:))) % If evaluation of the Jacobian matrix returns NaNs, an infinite numbers or a complex numbers, then restart but with a smaller radius of the trust region. x = x0; + fjac = fjac0; delta = delta*radiusfactor; end end