diff --git a/matlab/optimization/solvopt.m b/matlab/optimization/solvopt.m
index a069e6dc17ac1f760b3004e53bf1cf2d3be43f3d..56050b07400f615efca75f94856d956c40f1eb9e 100644
--- a/matlab/optimization/solvopt.m
+++ b/matlab/optimization/solvopt.m
@@ -678,6 +678,9 @@ while 1
                 end
                 if ksm || kc>=mxtc
                     exitflag=-3;
+                    % don't return with NaN or Inf despite error code
+                    x=x1;
+                    f=f1;                  
                     if trx
                         x=x';
                     end