Polyfit polynomial badly conditioned on Linux whereas on Mac no warnings

4 views (last 30 days)
Luigi De Maria on 14 Sep 2022
Edited: Jan on 15 Sep 2022
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?
Luigi De Maria on 15 Sep 2022
@Walter Roberson the command gives:
Matlab 2019b: Intel(R) Math Kernel Library Version 2018.0.3 Product Build 20180406 for Intel(R) 64 architecture applications, CNR branch auto
Matlab 2021a: Intel(R) Math Kernel Library Version 2019.0.3 Product Build 20190125 for Intel(R) 64 architecture applications, CRN branch AVX2

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)
Luigi De Maria on 15 Sep 2022
Thank you very much to all of you for your kind replies. It has been a very formative discover this difference.

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.
Jan on 15 Sep 2022
Edited: Jan on 15 Sep 2022
Fine. The correct application of numerical maths is not trivial.

Categories

Find more on Time Series Objects in Help Center and File Exchange

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by