diff --git a/matlab/+estimate/nls.m b/matlab/+estimate/nls.m index be511202023f43a07b97fd2085cec5f685f586aa..96009ab7d80e4ddb4194735e12b1872f944eb459 100644 --- a/matlab/+estimate/nls.m +++ b/matlab/+estimate/nls.m @@ -136,10 +136,27 @@ else end end -% Remove residuals from the equation. Note that a plus or minus will -% remain in the equation, but this seems to be without consequence. +% Remove residuals from the equation. Note that a plus or minus will remain in the equation rhs = regexprep(rhs, rname, ''); +% FIXME The JSON output for rhs (with aux variables substitutions) is not always +% the same regarding to the position of the residual. If the residual appears at +% the end of the equation, after the removal of rname the rhs will end with a + +% symbol which will result in a crash later when evaluating the sum of square +% residuals. If the residual appears at the begining of the equation, after the +% removal of rname the rhs will begin with a + symbol which is just awful (but +% will not cause any trouble). + +% Remove trailing + (if any, introduced when removing residual) +if isequal(rhs(end), '+') + rhs = rhs(1:end-1); +end + +% Remove leading + (if any, introduced when removing residual) +if isequal(rhs(1), '+') + rhs = rhs(2:end); +end + % % Rewrite and print the equation. %