How to plot best fit line with polyfit?

I just want to plot a best fit line based on 6 points. The coordinates are given. After entering the six points, how do I use the polyfit command? Can someone explain in detail to me?

Réponses (1)

Friedrich
Friedrich le 30 Déc 2011
Hi,
%generate some points, here of the function y = x^2
x = 1:6;
y = x.^2;
%fit second order polynomial
coefs = polyfit(x,y,2)
%plot it, use polyval to calcualte function values of that fit
plot(x,y,'*',1:0.1:6,polyval(coefs,1:0.1:6),'-')
So if you don't know which order matches best you can try it out and use the second return value if polyfit which is a struct.
x = (0: 0.1: 2.5)';
y = erf(x);
for i=1:10
[p,S] = polyfit(x,y,i);
f = polyval(p,x);
subplot(5,2,i)
plot(x,y,'*',x,f,'-')
axis([0 3 0 1.5])
title(['Order is: ',num2str(i),' norm is: ',num2str(S.normr)])
end
Smaller S.normr means better fit for the given values. But this isn't a measure for the behavior between the given points!

14 commentaires

Wong
Wong le 30 Déc 2011
Still dont get it. But I used the basic fitting tool, and tick linear under plot ticks. Now another problem is that the best fit line isn't exactly straight. It is somehow not smooth, having small curves along the line. What is the problem?
Friedrich
Friedrich le 30 Déc 2011
Can you gives us your x and y values so we can better understand your problem?
Wong
Wong le 30 Déc 2011
x=[9 16 25 49 64 80 ]
y=[18.1 22.8 30 47.5 61.2 74]
Can you give me the command for the best fit based on these values?
Friedrich
Friedrich le 30 Déc 2011
I wouldn't go for polyfit with that values, since you won't get a nice fit. Best to get with polyfit is:
>> [p,S] = polyfit(x,y,4);
>> f = polyval(p,x);
>> plot(x,y,'*',x,f,'-')
I would rather go for splines (needs curve fitting toolbox):
pp=csapi(x,y)
hold on
fnplt(pp)
plot(x,y,'r*')
hold off
Wong
Wong le 30 Déc 2011
I used the solines command you gave me
pp=csapi(x,y)
hold on
fnplt(pp)
plot(x,y,'r*')
hold off
But I still don't get a best fit line.
Friedrich
Friedrich le 30 Déc 2011
What do you expect? What is the best fit line for you?
Wong
Wong le 30 Déc 2011
Nevermind, thats okay with me. Now how do I insert the x-axis and y-axis
Friedrich
Friedrich le 30 Déc 2011
What do you mean with insert x-axis? the axis is already there. Do you mean labeling it?
pp = spapi(2,x,y);
fnplt(pp)
xlabel('text for x-axis')
ylabel('text for y-axis')
Wong
Wong le 30 Déc 2011
I mean when I set the limit of the x-axis and y-axis to less than zero, both of the axis will move to the border of the graph. I want them to intersect at the origin.
Friedrich
Friedrich le 30 Déc 2011
MATLAB doesn't provide a feature like that. Maybe this from the FileExchange helps:
http://www.mathworks.com/matlabcentral/fileexchange/3245
Wong
Wong le 30 Déc 2011
Oh, thanks. I have another problem. How do I draw more grid lines between boxes? For example, the scale of my x-axis is 1:10. So how do I insert 10 more lines between the two boxes?
Friedrich
Friedrich le 30 Déc 2011
I modified the example so it works in 2D:
x=[9 16 25 49 64 80 ]
y=[18.1 22.8 30 47.5 61.2 74]
hold on
pp = spapi(2,x,y);
fnplt(pp)
axis([-10,80,-10,80])
plot(get(gca,'XLim'),[0 0],'k');
plot([0 0],get(gca,'YLim'),'k');
X=get(gca,'Xtick');
Y=get(gca,'Ytick');
XL=get(gca,'XtickLabel');
YL=get(gca,'YtickLabel');
set(gca,'Xtick',[]);
set(gca,'Ytick',[]);
Xoff=diff(get(gca,'XLim'))./30;
Yoff=diff(get(gca,'YLim'))./30;
for i=1:length(X)
plot([X(i) X(i)],[-Yoff, Yoff],'k');
end;
for i=1:length(Y)
plot([-Xoff Xoff],[Y(i) Y(i)],'k');
end;
text(X,zeros(size(X))-5,zeros(size(X))-3.*Zoff,XL);
text(zeros(size(Y))-3.*Xoff,Y,zeros(size(Y)),YL);
hold off
Wong
Wong le 30 Déc 2011
I get this error
??? Undefined function or variable 'Zoff'.
Friedrich
Friedrich le 30 Déc 2011
Sorry small typo. instead of
text(X,zeros(size(X))-5,zeros(size(X))-3.*Zoff,XL);
use
text(X,zeros(size(X))-5,zeros(size(X))-3,XL);
Regarding the grid. There is no ML functionality available. You have to plot this lines by your own, e.g.:
plot(1:10)
hold on
for i=1:10
plot(ones(10,1)*0.1*i,1:10,'MarkerSize',4,'LineStyle',':','Color',[0 0 0]);
end
grid

Connectez-vous pour commenter.

Catégories

En savoir plus sur 2-D and 3-D Plots dans Centre d'aide et File Exchange

Question posée :

le 30 Déc 2011

Community Treasure Hunt

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

Start Hunting!

Translated by