Because the syntax should be
polyfit(x,y,N)
and not
polyfit(y,x,N)
Here is some code that illustrates the fix:
x = 1:503;
y = 250 + 0.02*x + 0.005*x.^2 + 0.2*rand(1,503).*x;
figure polyfit(y,x,0); coef1 = polyfit(x,y,0); y1 = polyval(coef1,x); hold on plot(x,y,'.'); plot(x,y1); polyfit(x,y,1); coef2 = polyfit(x,y,1); y2 = polyval(coef2,x); plot(x,y2); polyfit(y,x,2); coef3 = polyfit(x,y,2); y3 = polyval(coef3,x); plot(x,y3);
