MATLAB - Basic fitting tool on plot

3 次查看(过去 30 天)
Hi, I don't understand any things on interactive fitting.
When I try to fit a plot with basic fittings, the norm residual is the error of fitting? My fitting is better if norm residual is near 0?
Another question:
In basic fitting I got this data (9-th degree polynomial):
y = 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:
p1 = 1.4601e-20
p2 = -5.7732e-17
p3 = 1.0077e-13
p4 = -1.0187e-10
p5 = 6.5726e-08
p6 = -2.8044e-05
p7 = 0.0079067
p8 = -1.4194
p9 = 147.14
p10 = -6709.5
Norm of residuals =
0.078056
In the basic fitting tool, in the Find y=f(x) section, why if I insert a value, for example 250, I get as f(x) = -0.366, and instead, if I compute
y = 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
above, why I get a different result?
UPDATE: the code is
close all
clear all
filename = 'phase_diagram.xls';
T = readtable(filename);
Temp_K_Saturation = str2double((T.SaturationLine(2:length(T.SaturationLine)))');
Temp_K_Saturation(isnan(Temp_K_Saturation)) = [];
Pressure_Bar_Saturation = str2double((T.Var5(2:length(T.Var5)))');
Pressure_Bar_Saturation(isnan(Pressure_Bar_Saturation)) = [];
figure(1);
plot(Temp_K_Saturation,Pressure_Bar_Saturation)
title('Linear Phase Diagram Water - Saturation line');
xlabel('Temperature [K]');
ylabel('Pressure [Bar]');
x=Temp_K_Saturation';
y=Pressure_Bar_Saturation';
degree=4;
%POLYFIT No centering and scaling
p = polyfit(x,y,degree);
f = polyval(p,x);
figure
plot(x,y,'o')
hold on
plot(x,f,'r')
hold off
title(strcat('No Centering and Scaling: Saturation Line --> Degree = ', num2str(degree)));
T = table(x,y,f,y-f,'VariableNames',{'X','Y','Fit','FitError'})
p
%%%%%%%%%%%%%%%
%POLYFIT CENTERING AND SCALING
[p,S,mu] = polyfit(x,y,degree);
f = polyval(p,x,S,mu);
figure
plot(x,y,'o')
hold on
plot(x,f,'r')
hold off
title(strcat('Auto Centering and Scaling: Saturation Line --> Degree = ', num2str(degree)));
T = table(x,y,f,y-f,'VariableNames',{'X','Y','Fit','FitError'})
p

回答(1 个)

Steven Lord
Steven Lord 2016-2-28
  1. Just because you can use a 9th degree polynomial doesn't mean you should. Look at the coefficients -- many of the higher order ones are extremely small.
  2. The coefficients values being displayed are not displayed to their full precision. If you used the displayed values rather than the full double precision values to evaluate the fit, particularly with an x as large as 250, the small error in the coefficients you used through rounding to five significant figures could be greatly magnified. Instead, export the fit and use it directly (NOT the displayed coefficients) to evaluate the fit.
  3. If you centered and scaled the fit but didn't center and scale the data when you evaluated it manually, the results will be different.
  3 个评论
Walter Roberson
Walter Roberson 2016-2-28
You can use the three-output form of polyfit(); that tells it to re-center and rescale.
However, typically if you are using anything over 7th degree, your answers are prone to be numeric nonsense.
Antonio Voza
Antonio Voza 2016-2-28
编辑:Antonio Voza 2016-2-28
Hi Walter, thank you for the answer. I'm analyzing the function in a linear scale and semilog scale. It is the semilog scaled plot that pushed me to use 9-th degree equation... But now I think that it's wrong fitting in a semilog plot, right? Because in the linear plot I can fit with a 4-th degree polynomial. So if I start from a semilog (y logscaled) plot, how can I fit the function in Matlab? Must I convert to linear scale and fit? Last question about polyfit and three output form: If my plot start from 250, with the three output form, polyfit centers x at zero and scales it to have unit standard deviation. What does it mean for polyfit centers x at zero? In my case where my plot start for x=250 (and not 0) can be useful?
UPDATE: I pasted my code above. I performed the centering and scaling using polyfit but I noted that the error fit is a little greater in the case of No centering and scaling case. You can see the Error fit in the table T in the last pieces of code above

请先登录,再进行评论。

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by