Polyfit() does not seem to work well for some straight lines. Is there a good alternative?
Afficher commentaires plus anciens
I am currently in need of a function that fits a series of 2D points to a straight line and returns the gradient, the y intercept and a measure of how well the line fits.
I used the following code to test the suitability of polyfit() by applying to some of my typical values.
x=[4.4698,4.4793,4.4784,4.4672,4.4757];
y=[0.0390,0.0781,0.1172,0.1560,0.1954];
[p,s]=polyfit(x,y,1);
figure(1);
plot(x,y,'r');
xTest=-5:0.1:5;
yTest=xin*p(1)+p(2);
hold on;
plot(xin,yin);
It created the following figure:

The line created by polyfit does not look any thing like the line created by the data. Is there a good alternative? Or am I using polyfit incorrectly?
Thank you in advance.
1 commentaire
Image Analyst
le 2 Oct 2013
As Wayne pointed out, the code you posted does not even run. Here is the fixed code that will actually run:
x=[4.4698,4.4793,4.4784,4.4672,4.4757];
y=[0.0390,0.0781,0.1172,0.1560,0.1954];
[p,s]=polyfit(x,y,1);
figure(1);
plot(x,y,'ro', 'LineWidth', 2);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
uiwait(msgbox('This is the training data. Click OK to see the fit'));
xFit=-5:0.1:5;
yFit=xFit *p(1)+p(2);
hold on;
plot(xFit,yFit, 'b-', 'LineWidth', 2);
grid on;
Of course it still has the problems that everyone explained to you.
Réponses (2)
Wayne King
le 2 Oct 2013
Your x-vector is not monotonic, that is one major issue.
Then you create this vector, xTest, which runs from -5 to 5 when the fit you are getting does not even approach that range (your data x only has a range of 0.01 from 4.6 to 4.7. Although I'm not even sure you end up using xTest, because you then go on to use xin, a variable, you do not even show us.
x=[4.4698,4.4793,4.4784,4.4672,4.4757];
y=[0.0390,0.0781,0.1172,0.1560,0.1954];
[xnew,idx] = sort(x);
ynew = y(idx);
[p,s] = polyfit(xnew,ynew,1);
xin = 4.45:0.001:4.48;
yhat = p(1)*xin+p(2);
plot(xin,yhat,'r','linewidth',2)
hold on;
plot(xnew,ynew,'k*')
Note that your y-data don't come close to approximating a linear relationship for the monotonic x values, so it's not surprising that a linear fit is not good.
Jos (10584)
le 2 Oct 2013
0 votes
Tip: always plot your data to see if a model would fit. In your case a linear model is simply a very bad choice, even more so when you extrapolate it so much like you do ...
See this how dangerous extrapolation can be

1 commentaire
Jan
le 2 Oct 2013
It is funny that "total" and "functional non-redundant" genome is still distinguished. How likely is this theory that we carry so much highly expensive garbage with us??
The origin of earth cannot be explained well by a vertical line.
Btw., the maximal accepted exposure to radioactivity and trans fat has been determined by such a linear interpolation also, in the 2nd case using rats.
Catégories
En savoir plus sur Dimensionality Reduction and Feature Extraction dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!