Polyfit polynomial badly conditioned on Linux whereas on Mac no warnings

2 visualizaciones (últimos 30 días)
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 comentarios
Luigi De Maria
Luigi De Maria el 15 de Sept. de 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
Luigi De Maria
Luigi De Maria el 11 de Dic. de 2022
As a conclusion, if you experience the same problem, consider to call all three outputs of polyfit as adviced in the documentation. This will automatically introduce a proper scaling and recentering of the nodes.

Iniciar sesión para comentar.

Respuesta aceptada

Jan
Jan el 15 de Sept. de 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.
  2 comentarios
Luigi De Maria
Luigi De Maria el 15 de Sept. de 2022
I am totally aware of what you are talking about. I had to simplify what I wrote in the question and comments because the code behind it is much bigger and I wanted to focus the attention on polyfit.
However, I implemented the scaling months ago because at the beginning the warnings were also on my Matlab 2021a and I spent days trying different solutions. The current scaling works fine.
How can I say it works fine?
Because I am reproducing a test case to validate the code (which will be applied on a new case). Moreover, this code is inside a Monte-Carlo simulation. I don't have just the results of this MC but I also have a Subset Simulation, a Gaussian Mixtures Simulation and another method (called Chan).
So I decided to just turn off warning not because "the results are okay so I do not need scaling", but because:
  • I already checked the code (and hence polynomial fitting part) using other similar method (which do not involve polyfit)
  • I am analyzing a test case which is certified and the results have always been identical to it
  • I had already introduced a proper scaling months ago
  • Residuals have always been very low (about 1e-12)
I thank you for all your advices and care, but don't worry if I am not 99.999% sure the code is mathematically correct and computationally fine, I never trust it.
Jan
Jan el 15 de Sept. de 2022
Editada: Jan el 15 de Sept. de 2022
Fine. The correct application of numerical maths is not trivial.

Iniciar sesión para comentar.

Más respuestas (1)

Bruno Luong
Bruno Luong el 14 de Sept. de 2022
Movida: Bruno Luong el 14 de Sept. de 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)
  5 comentarios
Bruno Luong
Bruno Luong el 15 de Sept. de 2022
Editada: Bruno Luong el 15 de Sept. de 2022
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.
Luigi De Maria
Luigi De Maria el 15 de Sept. de 2022
Thank you very much to all of you for your kind replies. It has been a very formative discover this difference.

Iniciar sesión para comentar.

Categorías

Más información sobre Programming en Help Center y File Exchange.

Productos


Versión

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by