MATLAB doing wrong polynomial fitting
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Pedro Rafael Guaraldi da Silva
el 12 de Dic. de 2015
Editada: emehmetcik
el 12 de Dic. de 2015
Hello guys,
I have been doing some fittings lately. For this case I have a set of 55 points from which I'd like to obtain a good fitting. For simplicity I was trying a polynomial fitting with degree = 9. The graph matlab generated was this:
And the coefficients were these:
Linear model Poly9:
f(x) = p1*x^9 + p2*x^8 + p3*x^7 + p4*x^6 +
p5*x^5 + p6*x^4 + p7*x^3 + p8*x^2 + p9*x + p10
Coefficients (with 95% confidence bounds):
p1 = -5.012e-21 (-8.177e-21, -1.848e-21)
p2 = 2.014e-17 (7.065e-18, 3.322e-17)
p3 = -3.562e-14 (-5.94e-14, -1.184e-14)
p4 = 3.637e-11 (1.14e-11, 6.135e-11)
p5 = -2.363e-08 (-4.032e-08, -6.943e-09)
p6 = 1.013e-05 (2.772e-06, 1.749e-05)
p7 = -0.002864 (-0.005003, -0.0007243)
p8 = 0.5149 (0.1193, 0.9105)
p9 = -53.42 (-95.63, -11.21)
p10 = 2437 (457.4, 4417)
Goodness of fit:
SSE: 0.003537
R-square: 0.9978
Adjusted R-square: 0.9974
RMSE: 0.008865
So far, so good, the results should be between 1 and 0.3, however when i tested a random value in the polynomial itself, like 300, the result was above 3, indicating the fitting was wrong. Here is the graph generated using Maple with the coefficients from Matlab:
As we can see, the graphs are totally not connected. Honestly, I have never seen something like this and i have spent all day trying to figure this out. The fitting was done with Robust = off and no center and scale (I've tried that too, experiencing the same problems). I hope you guys can help me.
2 comentarios
Respuesta aceptada
emehmetcik
el 12 de Dic. de 2015
Editada: emehmetcik
el 12 de Dic. de 2015
It is about the numerical precision of the polynomial coefficients. When I use polyfit function (to fit a 9th degree polynomial to the data you provided), I get the following coefficients (using "longe" format):
p = polyfit(x, y, 9)'
-5.012224980469582e-21
2.014161401489308e-17
-3.561946886075925e-14
3.637480005367969e-11
-2.363394090375955e-08
1.012995390520798e-05
-2.863843661446328e-03
5.148924388805175e-01
-5.341667014988152e+01
2.437220199802428e+03
which have the following differences with the coefficients you provided:
2.249804695825785e-25
-1.614014893081597e-21
-5.311392407456585e-19
-4.800053679690507e-15
3.940903759549705e-12
4.609479202040336e-11
-1.563385536719301e-07
7.561119482524248e-06
-3.329850118483080e-03
-2.201998024279419e-01
These differences yield the problem you encountered.
Also notice that the polyfit function gives the following warning:
Warning: Polynomial is badly conditioned. Add points with distinct X values, reduce the degree of the polynomial, or try centering and scaling as
described in HELP POLYFIT.
Which basically says that using the following form would provide better numerical properties:
[p, ~, mu] = polyfit(x, y, 9);
The resulting polynomial coefficients and the centering and scaling values:
p = [-2.708845185841449e-02
1.392369931258979e-02
1.281073313665780e-01
-4.680674330342817e-02
-2.058795803488430e-01
3.743623355367855e-02
1.181616990828998e-01
-4.142032762357658e-02
-1.408639397299561e-01
9.011201272506475e-01];
mu = [4.396109090909091e+02
1.206193744383505e+02];
Note that to use the above values you need to evaluate the polynomial at the scaled and centered x points:
yi = polyval(p, (x-mu(1))/mu(2));
This can also be done with the following:
yi = polyval(p, x, [], mu);
0 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Polynomials en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!