Polyfit polynomial badly conditioned on Linux whereas on Mac no warnings
5 ビュー (過去 30 日間)
古いコメントを表示
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?
8 件のコメント
採用された回答
Jan
2022 年 9 月 15 日
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.
2 件のコメント
その他の回答 (1 件)
Bruno Luong
2022 年 9 月 14 日
移動済み: Bruno Luong
2022 年 9 月 14 日
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)
5 件のコメント
Bruno Luong
2022 年 9 月 15 日
編集済み: Bruno Luong
2022 年 9 月 15 日
The function matlab.internal.math.leastSquaresFit uses QR decomposition WITH permutation, so somewhat R is slighly better conditionned. It is also solved with specified rank so the warning kicks in a little later.
To me it is a slightly better algorithm, but honestly the non-permutation QR used by polyfit in older version should work OK.
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!