Polyfit polynomial badly conditioned on Linux whereas on Mac no warnings
7 views (last 30 days)
I have a code which has to interpolate some simple data grid using polyfit.
However, when I run the code on my computer (MacBook, Matlab 2021a) there are no warnings or errors. Whereas, when I run the same identical code on the server I work with (which runs on Linux, Matlab 2019b) Matlab gives me warnings about the polynomial that is "badly conditioned".
I have also checked one-by-one the results before the polyfit and they are identical between the two systems.
Do you have any idea?
Bruno Luong on 14 Sep 2022
Moved: Bruno Luong on 14 Sep 2022
In R2022a the Vandermonde matrix V is built the condition number is aroun 1e12, and the resolution is carrired out by
[p, rankV, QRfactor, perm] = matlab.internal.math.leastSquaresFit(V,y1);
I don't have the version handly but if someone have, can you check what is the resolution engine used by R2019a by editing polyfit.m, (line #72 in R2022a)
More Answers (1)
Jan on 15 Sep 2022
The warning is serious. Although the results look find in your case, scaling the input is the way to go for a scientifically stable solution. It is cheap and easy: If the 3rd output MU is requested from polyfit, x is scaled by:
MU = [mean(x), std(x)]
x = (x - MU(1)) / MU(2);
The same is applied in polyval also before running the horner scheme.
Scaling is important in numerical maths to control the effects of the limited precision. This concerns leats square fits, integrations and optimizations. The argument "the results are okay so I do not need scaling" or "I suppress the warnings" is a bad decision and should not appear in serious scientific work.
In numerical maths the final value can be called "result" only, if the accuracy is controlled. Otherwise the program is a weak random number generator with a certian probability < 1.0 to reply a final value near to the expected one. I've seen to many PhD theses, in which method of numerical maths have been applied without knowing, how they work and which limitations have to be considered. Typical discussions contain statements like:
"I've used ODE45 because everybody uses it. Matlab is slow so I did not care that the integration runs for 2 days. What is stiffness and scaling? What is sensitivity to variation of initial values and parameters?"
A polynomial fit is instable without scaling and the residuals must be controlled and considered in the following computations. Warnings are very important. For modern polyfit implementations they appear a little bit later, but the problem is the same.