MATLAB doing wrong polynomial fitting

7 次查看(过去 30 天)
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 个评论
Pedro Rafael Guaraldi da Silva
Hello Walter, here are the data that generated the fitting:
X data (or Temperature):
273.15
275
280
285
290
295
300
305
310
315
320
325
330
335
340
345
350
355
360
365
370
373.15
375
380
385
390
400
410
420
430
440
450
460
470
480
490
500
510
520
530
540
550
560
570
580
590
600
610
620
625
630
635
640
645
647.3
Y data (or Density):
1
1
1
1
0.999000999000999
0.998003992015968
0.997008973080758
0.995024875621891
0.993048659384310
0.991080277502478
0.989119683481701
0.987166831194472
0.984251968503937
0.982318271119843
0.979431929480901
0.976562500000000
0.973709834469328
0.970873786407767
0.967117988394584
0.963391136801541
0.960614793467820
0.957854406130268
0.956937799043062
0.953288846520496
0.949667616334283
0.945179584120983
0.937207122774133
0.928505106778087
0.919117647058824
0.909918107370337
0.900900900900901
0.890471950133571
0.879507475813544
0.868055555555556
0.856898029134533
0.844594594594595
0.831255195344971
0.818330605564648
0.803858520900322
0.788643533123028
0.772797527047913
0.755857898715042
0.738007380073801
0.718390804597701
0.697836706210747
0.674763832658570
0.648929266709929
0.620347394540943
0.586510263929619
0.562429696287964
0.538793103448276
0.516795865633075
0.481927710843373
0.425350914504466
0.315457413249211
Thank you in advance, Guaraldi

请先登录,再进行评论。

采纳的回答

emehmetcik
emehmetcik 2015-12-12
编辑:emehmetcik 2015-12-12
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 个)

类别

Help CenterFile Exchange 中查找有关 Polynomials 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by