MATLAB: Curve Fitting Polynomial (Plot not matching original data) (Normalisation?)

MATLABpolynomial curve fittingsurfsurface fitting

Hi All,
I am trying to generate a basic equation to be used as a quick and dirty input/output function block in a model. I'll drop the data and some code below, but first Ill try to explain what Ive tried and where I'm at.
The data x,y,z are from a data sheet and have been put into the curve fitting tool/app. The polynomial generated looks like a reasonable fit so I've copied the function and coefficients into a new script. I am now trying to plot a surf() to check it generates appropriate results (rough is fine).
For whatever reason I cant get the surf plot to even resemble the original data or the curve fitting plot.
From digging around the net, my understanding is that the curve fitting tool normalises the initial data and therefore, the equation I've copied requires some form of pre and post standardisation (regardless of data being Centered and Scaled in the curve fitting tool).
Translating x,y and z with regards to mean() and std() don't seem to solve the problem – is there something I'm missing?
Any assistance would be much appreciated, the plot i expect would look similar to this image;
thanks in advance.
*******Data Sheet Data*********
x = [0 0 0 0 0 0 5 5 5 5 5 5 10 10 10 10 10 10 15 15 15 15 15 15 20 20 20 20 20 20 ...
25 25 25 25 25 25 30 30 30 30 30 30 35 35 35 35 35 35 40 40 40 40 40 40 ...
45 45 45 45 45 45 50 50 50 50 50 50];
Xm = mean(x);
Xs = std(x);
y = [1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 ...
1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6];
Ym = mean(y);
Ys = std(y);
z = [271.2 541.7 811.1 1078.8 1344.1 1606.5 104.4 348.3 590.8 ...
831.3 1069.5 1304.9 21.9 240.8 458.1 673.5 886.5 1096.7 ...
-27.2 168.3 362 553.8 743.3 930 -61.9 111.5 283.2 ...
452.9 620.2 785 -90.3 62.4 213.3 362.2 508.9 653 -115.9 ...
17.2 148.6 277.9 405 529.6 -140.5 -25.8 87 197.8 ...
306.4 412.6 -164.9 -67.8 27.4 120.7 211.7 300.5 -187.4 ...
-109.1 -30.6 45.9 120.2 192.3 -214.3 -150 -87.6 -27.1 31.2 87.4];
Zm = mean(z);
Zs = std(z);
***************Curve Fitting Results from Matlab Tool***************
Centered & Scaled - Checked
Linear model Poly33:
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
where x is normalized by mean 25 and std 15.93
and where y is normalized by mean 3.5 and std 1.721
Coefficients (with 95% confidence bounds):

p00 = 276.5 (270.3, 282.6)
p10 = -230.4 (-239.6, -221.2)
p01 = 256.3 (246.5, 266.2)
p20 = 62.51 (58.8, 66.22)
p11 = -112.6 (-115.9, -109.3)
p02 = -3.079 (-6.912, 0.7531)
p30 = -35.28 (-39.68, -30.87)
p21 = 10.49 (6.754, 14.23)
p12 = 0.08993 (-3.772, 3.952)
p03 = -0.2879 (-5.294, 4.718)
Goodness of fit:
SSE: 9597
R-square: 0.9991
Adjusted R-square: 0.999
RMSE: 13.09
%***************Curve Fitting Results from Matlab Tool***************
%Centered & Scaled - Not Checked
Linear model Poly33:
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
Coefficients (with 95% confidence bounds):
p00 = -15.95 (-54.59, 22.69)
p10 = -24.53 (-26.98, -22.08)
p01 = 272.2 (236.4, 307.9)
p20 = 0.8164 (0.7281, 0.9046)
p11 = -5.32 (-6.046, -4.595)
p02 = -0.4944 (-11.09, 10.1)
p30 = -0.008723 (-0.009812, -0.007633)
p21 = 0.02402 (0.01546, 0.03258)
p12 = 0.001906 (-0.07994, 0.08375)
p03 = -0.05648 (-1.039, 0.9258)
Goodness of fit:
SSE: 9597
R-square: 0.9991
Adjusted R-square: 0.999
RMSE: 13.09
%************Surf Plot***********
%Polynomial
p00 = 276.5;
p10 = -230.4;
p01 = 256.3;
p20 = 62.51;
p11 = -112.6;
p02 = -3.079;
p30 = -35.28;
p21 = 10.49;
p12 = 0.08993;
p03 = -0.2879;
Figure_1 = figure;
hold on;
Xm = 25;
Xs = 15.93;
Ym = 3.5 ;
Ys = 1.721;
Zm = 334.9864;
Zs = 415.4339;
resolution = 10;
[x,y] = meshgrid(0:50/resolution:50,0:6/resolution:6);
%Normalise Input Variables for equation
x = transpose((x-Xm)/Xs);
y = transpose((y-Ym)/Ys);
%Pass inputs through data (normalised output expeccted)
z = 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;
%Normalise Output Data for Plot
%z = Zs*z+Zm;
x = Xs*x+Xm
y = Ys*y+Ym
surf(x,y,z);
view([-30,20]);
grid on
%xlim([0,50]);
%ylim([0,6]);
%zlim([0,2000]);
xlabel('X');
ylabel('Y');
zlabel('Z');

Best Answer

Congratulations! You are the 10 millionth person to make this mistake!
Your reward will be a commensurate one, where you will get a free trip to someplace that you don't really want or need to go. In fact, we won't even tell you where that will be! Just someplace random, with no return ticket provided of course. ;-)
Why do I say this trip would be a reasonable reward compared to what you did? Because you did something wrong in MATLAB, getting a result that was not what you wanted or expected, pretty much random.
Ok, seriously, you are indeed the zillionth person to make this mistake. what you did was copy the polynomial coefficients, but ONLY to a few digits of precision.
MATLAB shows a coefficient like this:
p03 = -0.2879 (-5.294, 4.718)
But, in reality, that is not the exact number. In fact, it has roughly 10 more digits, at least the first few of which are seriously important to be able to reproduce that fit. For high degree polynomials, they are probably ALL important.
mdl = fit([x',y'],z','poly33')
Linear model Poly33:
mdl(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
Coefficients (with 95% confidence bounds):
p00 = -15.95 (-54.59, 22.69)
p10 = -24.53 (-26.98, -22.08)
p01 = 272.2 (236.4, 307.9)
p20 = 0.8164 (0.7281, 0.9046)
p11 = -5.32 (-6.046, -4.595)
p02 = -0.4944 (-11.09, 10.1)
p30 = -0.008723 (-0.009812, -0.007633)
p21 = 0.02402 (0.01546, 0.03258)
p12 = 0.001906 (-0.07994, 0.08375)
p03 = -0.05648 (-1.039, 0.9258)
coeffvalues(mdl)
ans =
Columns 1 through 7
-15.9519463869469 -24.5274287490288 272.156214109965 0.816362470862472 -5.32047404262406 -0.49436327561336 -0.00872253302253303
Columns 8 through 10
0.0240213120213121 0.00190584415584616 -0.0564814814814792
So, I fit your data with a polynomial model, then extracted the coefficients as a vector.
STILL, don't copy and paste those numbers!!!!!!!!!
PC = coeffvalues(mdl);
p00 = PC(1)
p00 =
-15.9519463869469
Instead, extract them as the exact numbers that MATLAB returned, as I did above.