Why the optimization results of lsqnonlin are different in R2026a and R2025a?
184 views (last 30 days)
Show older comments
I used the following code to solve a nonlinear optimization problem. All random seeds and optimization options are the same, but the result in the R2026a pre-release is complex-valued, whereas in R2025a or older releases the result is real-valued. All code and data are attached.
% R2025b or earier is real-base solution
% R2026a is complex-base solution?
R2025b:
load temp.mat
rng default;
options = optimoptions('lsqnonlin', 'Algorithm','levenberg-marquardt', 'Display','final',...
'MaxFunEvals',2000, 'MaxIter',1e3, 'TolFun',1e-6, 'TolX',1e-6, 'Jacobian','off');
[x,resnorm,~,exitflag,output] = lsqnonlin(@(p)residual_KR_robust(double(X1_ok), double(X2_ok), imsize1, imsize2, p, 1000), [1000 1000 0 0 0],...
[],[])
R2026a:

2 Comments
Torsten
on 10 Nov 2025 at 15:44
Starting from real-valued initial guesses for the parameters, do you have a line in your code that could produce complex results for an element of "err" ? I cannot find one - thus I have no explanation why you could end up with complex-valued parameters at all.
dpb
on 10 Nov 2025 at 16:14
Edited: dpb
on 10 Nov 2025 at 16:36
type residual_KR_robust
type residual_H
The backslash solve might under some circumstances, couldn't it?
Setting a breakpoint on condition ~isreal(errH) would be able to discover when it first occurs and let figure out what might have happened and (maybe then) why.
I don't suppose there's anything about lsqnonlin in the release notes...
Accepted Answer
Matt J
on 10 Nov 2025 at 20:31
Edited: Matt J
on 11 Nov 2025 at 2:53
There appears to be a new (and buggy) implementation of expm.m in R2026a, resulting in incorrectly complex results for skew symmetric matrices:
>> A=zeros(3); A(3,2)=1; A(2,3)=-1
A =
0 0 0
0 0 -1
0 1 0
>> B=expm(A)
B =
1.000000000000000 + 0.000000000000000i 0.000000000000000 + 0.000000000000000i 0.000000000000000 + 0.000000000000000i
0.000000000000000 + 0.000000000000000i 0.540302305868140 - 0.000000000000000i -0.841470984807896 - 0.000000000000000i
0.000000000000000 + 0.000000000000000i 0.841470984807896 + 0.000000000000000i 0.540302305868140 - 0.000000000000000i
>> imag(B)==0
ans =
3×3 logical array
1 1 1
1 0 0
1 0 0
I have submitted a bug report.
19 Comments
dpb
on 11 Nov 2025 at 18:55
if ~any(imag(A),'all')
will only be true if all(imag(A))==0 identically; even the LSB in one element will fail such that if A got converted from intended real to complex owing to some computational gaff like under discussion here, should it really be complex with rounding error complex part or real?
Matt J
on 13 Nov 2025 at 22:35
Tech support has informed me that they are aware of the issue, and that it will be fixed in the R2026a general release.
More Answers (0)
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!