问题
So i have this data
x
1.0423
2.8249
3.2016
2.0851
1.0299
4.7397
0.4104
0.5285
0.7102
0.8323
3.1048
2.8685
0.2604
4.6560
3.6433
3.6892
0.3170
4.3022
4.6720
4.9220
y
2.0529
-3.0669
-2.3631
-0.7300
1.4354
2.0260
0.5980
0.5296
1.3405
1.7361
-1.5876
-2.7872
1.0788
1.3677
-0.1355
-1.5755
0.7811
-0.8328
-0.0592
2.0927
And I tried to fit an 8th order polynomial to the data using cftool.
These are the results I get which are wrong
Linear model Poly8:
f(x) = p1*x^8 + p2*x^7 + p3*x^6 + p4*x^5 +
p5*x^4 + p6*x^3 + p7*x^2 + p8*x + p9
Coefficients (with 95% confidence bounds):
p1 = -0.05446 (-0.126, 0.01711)
p2 = 1.088 (-0.3839, 2.561)
p3 = -8.762 (-21.23, 3.706)
p4 = 36.2 (-19.89, 92.3)
p5 = -80.85 (-225.3, 63.62)
p6 = 94.32 (-119.9, 308.6)
p7 = -52.21 (-227.5, 123.1)
p8 = 12.21 (-59.01, 83.42)
p9 = -0.05814 (-10.72, 10.61)
Goodness of fit:
SSE: 4.083
R-square: 0.9225
Adjusted R-square: 0.8661
RMSE: 0.6092
This is a cftool plot of the fit which looks fine
and this is what plotting the coefficients actually looks like.
Code for plotting coefficients:
xx = 0:1/100:5;
yy = -0.05446*xx.^8 +1.088*xx.^7 -8.762*xx.^6 +36.2*xx.^5 -80.85*xx.^4 +94.32*xx.^3 -52.21*xx.^2 +12.21*xx -0.05814;
plot(xx,yy)
Does anyone know whats going on and how to get the correct coefficients of the plot that cftool produces?
回答1:
I suspect that by truncating the coefficients, you have changed the polynomial too much.
You can see the effect yourself, by examining following code (when you already have x
and y
in your workspace).
P = polyfit(x,y,8); % fitted polynomial
P2 = [-0.05446 1.088 -8.762 36.2 -80.85 94.32 -52.21 12.21 -0.0814]; % your coefficients
toString = @(x,pat)(sprintf(pat,x));
fix = @(x,pat)(str2double(toString(x,pat)));
P3 = arrayfun(@(x)(fix(x,'%3.4f')), P); % artificially truncate the coefficients
X = linspace(min(x),max(x),100);
Y = polyval(P ,X);
Y2 = polyval(P2,X);
Y3 = polyval(P3,X);
figure;
[x,i] = sort(x);
y = y(i);
plot(x,y ,'or'); hold on;
plot(X,Y ,'-b');
plot(X,Y2,'--g');
plot(X,Y3,'-c');
hold off;
What I did, was fit the polynomial of eighth order to your data, I also truncated the coefficients artificially (I don't know the exact rounding strategy of cftool
, it seems a bit more complicated than what I did).
In the figure below, you can see the outcome. The red dots are the data, the blue line is the exact polynomial as fitted with polyfit
(and probably cftool
as well). The dotted green line depicts your polynomial with truncated coefficients) and the cyan line shows my truncated polynomial. So you see that by truncating, you are changing the polynomial too much to be usable.
回答2:
I think you used x center and scale check box so you have normalized x and you have different answer when you write this yourself
来源:https://stackoverflow.com/questions/9756890/matlab-cftool-producing-wrong-coefficients