MATLAB: Surface fitting issue when exporting polynomial

exporting equationpolyonomial

Hello there,
I'm using matlab to fit some experimental data. In this case I have the temperature and the pressure as x and y, and I have measured the liquid phase concentration as z, so my data are:
T= [4.4;4.6;4.8;5;5.2;5.4;5.5;5.6;5.7;5.8;]
p= [3;3.2;3.4;3.6;3.8;4;4.2;4.4;4.6;4.8;5;5.2;5.4;5.6;5.8;6;]
and
Z= [0.972604477 0.925657091 0.872451415 0.809828448 0.730361186 0.608428527 0.498661917 0.333396162 0.181062214 0.065747859
0.969339971 0.923431583 0.871870319 0.812179155 0.739118958 0.638373974 0.564120655 0.455628826 0.316002614 0.184750708
0.965913105 0.920933789 0.870794311 0.813502473 0.745169504 0.656675875 0.59785416 0.520540508 0.416267407 0.294691062
0.96234282 0.918201278 0.869307645 0.814026608 0.749361355 0.669013857 0.618744244 0.557085041 0.478281715 0.379522293
0.958645013 0.915264434 0.867474761 0.813910905 0.752195615 0.677731146 0.63293655 0.580321632 0.516453915 0.437614269
0.954833164 0.912148192 0.865346097 0.813271236 0.753993832 0.684017651 0.643058849 0.596310988 0.541630142 0.476299938
0.950918799 0.90887328 0.862961903 0.812194602 0.754975065 0.688559969 0.65045739 0.607835941 0.559271998 0.502902027
0.946911856 0.905457112 0.860354829 0.81074806 0.755295472 0.691790211 0.655909544 0.616361752 0.572149793 0.52197214
0.942820964 0.901914471 0.857551736 0.80898442 0.755070399 0.693997987 0.659903511 0.622744115 0.581790512 0.536103499
0.93865366 0.898258009 0.854574999 0.806946038 0.754387525 0.6953869 0.662765477 0.627522829 0.589106398 0.546816041
0.934416569 0.894498642 0.851443452 0.804667418 0.753315091 0.696105556 0.664723976 0.631059008 0.594679763 0.555049002
0.930115545 0.890645853 0.848173108 0.802177049 0.75190727 0.696265748 0.66594525 0.633605578 0.598902619 0.561413731
0.925755784 0.886707934 0.844777691 0.799498733 0.7502078 0.695953696 0.666553955 0.635346315 0.602050416 0.566326603
0.921341921 0.882692177 0.841269057 0.796652559 0.748252509 0.695237295 0.666645941 0.636418812 0.60432359 0.570082274
0.916878106 0.878605026 0.837657516 0.793655645 0.746071116 0.694170969 0.666296475 0.636928686 0.605872276 0.57289609
0.912368069 0.874452209 0.833952092 0.790522691 0.743688551 0.692799013 0.665565734 0.636958726 0.606811692 0.574929782]
we have a small system where we need to implement a simple fitting function up to degree 5, and cannot use other functions like sin,cos, exp…only simple operations, so we decided to fit the dada with a polynomial function of degree 5 for x and degree 4 for y.
I used cftool, selected "polynomial" and the degrees 5(for x) and 4(for y); Robust "off".
My problem starts when I try to export/use the "fitting polynomial" in the form of f = p00+ p01*x….etc. I mean, if I save it to workspace and use it as a matlab function, I have no problems and passing a value it returns the correct result, but if I want to use the polynomial and its coeff. on their own the results are totally wrong….for instance if I write x=4.8 and y=3 the result I have using the polynomial is 25.88…the real result should be z=0.8724;
Does anyone of you have an idea?
Thanks, Flavio.
The results are these:
Linear model Poly54:
f(x,y) = p00 + p10*x + p01*y + p20*x^2 + p11*x*y + p02*y^2 + p30*x^3 + p21*x^2*y
+ p12*x*y^2 + p03*y^3 + p40*x^4 + p31*x^3*y + p22*x^2*y^2
+ p13*x*y^3 + p04*y^4 + p50*x^5 + p41*x^4*y + p32*x^3*y^2
+ p23*x^2*y^3 + p14*x*y^4
Coefficients (with 95% confidence bounds):
p00 = -375 (-842, 91.94)
p10 = 402.5 (-48.9, 853.9)
p01 = -32.2 (-74.96, 10.57)
p20 = -168.3 (-343.5, 6.955)
p11 = 20.3 (-8.7, 49.31)
p02 = 2.671 (-2.521, 7.863)
p30 = 34.44 (0.336, 68.54)
p21 = -4.569 (-12.37, 3.233)
p12 = -1.261 (-3.393, 0.8696)
p03 = -0.1928 (-0.697, 0.3114)
p40 = -3.401 (-6.727, -0.07578)
p31 = 0.1891 (-0.7798, 1.158)
p22 = 0.5681 (0.2349, 0.9013)
p13 = -0.2035 (-0.3335, -0.07347)
p04 = 0.0762 (0.05197, 0.1004)
p50 = 0.1246 (-0.005309, 0.2546)
p41 = 0.0365 (-0.01007, 0.08307)
p32 = -0.08888 (-0.1089, -0.0689)
p23 = 0.05173 (0.042, 0.06146)
p14 = -0.01618 (-0.02082, -0.01154)
Goodness of fit: SSE: 0.005731 R-square: 0.9988 Adjusted R-square: 0.9986 RMSE: 0.006398

Best Answer

Oh, too bad! You are the 1000001'th person to make this mistake. The person before you got an all expense paid trip to Newark, New Jersey. Ok, most of their expenses were paid. Maybe some of them were paid? :)
Seriously, this is a very common mistake that people make with polynomials, or with curve fitting in general.
You see those coefficients written out to 4 digits, so you try to use them. Bad idea. A really bad idea. For example, the p50 coefficient is 0.1246. Or is it? Not exactly. In fact, there are some more digits past the 4th decimal place that are important.
When you try to evaluate that function at x = 4.8, remember that you are forming terms like this:
(0.1246 + delta)*4.8^5
for some small error delta, on the order of +/- 0.0001. Since
4.8^5
ans =
2548
is moderately big, when you multiply that by the error you make by representing the coefficient by exactly 0.1246, instead of perhaps 0.1245785675446642 (or whatever it truly was) this introduces a serious amount of error into your prediction.
Now, do that for EVERY coefficient in that model. What happens is instead of a valid prediction, you get random garbage.
You need to use the EXACTLY estimated coefficients in the polynomial model. Not just some 4 digit approximation as it was written in the command window.
Next, I would point out that your predictions with this model are probably worth very little. Look at the predicted uncertainties in those coefficients. For example, consider the constant term.
p00 = -375 (-842, 91.94)
Yep, -375, plus or minus something well over 400. So in fact, that constant term could easily have been zero. Or it might have been -800. Your z values were all between 0 and 1. In fact, just the error that you make by representing p00 as exactly 375, instead of some floating point number that is close to 375 is as large as the entire variation in your data.
This is just one of the problems with using high order polynomials in curve and surface fitting. And this is a moderately high order polynomial. Most of those coefficients are barely significantly different from zero, if at all. What that means is even if the model does predict your data reasonably well, it has very little meaning, and it will certainly do poorly in trying to extrapolate beyond the data.