How to plot best fit line with polyfit?
Afficher commentaires plus anciens
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
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
le 30 Déc 2011
Friedrich
le 30 Déc 2011
Can you gives us your x and y values so we can better understand your problem?
Wong
le 30 Déc 2011
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
le 30 Déc 2011
Friedrich
le 30 Déc 2011
What do you expect? What is the best fit line for you?
Wong
le 30 Déc 2011
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
le 30 Déc 2011
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
le 30 Déc 2011
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
le 30 Déc 2011
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
Catégories
En savoir plus sur 2-D and 3-D Plots 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!