MATLAB: Polyfit is not showing the expected fit

curve fittingMATLABplottingpolyfit

Hi, I'm trying to fit a collection of data points to a line, but when I call polyfit, it returns a completely unexpected result for both the slope and the y-intercept. I checked the help section and documentation but I don't know what I'm doing wrong…
I even tried finding the fit for a line, and it failed too. This is the code I'm using:
t = linspace(0,2);
x = 2*t+3;
[p,S,mu] = polyfit(t,x,1);
polyval(p,t,S);
plot(t,x)
hold on
[y,delta] = polyval(p,t,S);
errorbar(t,y,delta)
legend('line','fit')
And this is the plot I get:
polyfit.png
Please let me know if I'm doing anything incorrectly.
Thanks in advance.

Best Answer

You forgot to include ‘mu’ in your polyval argument list. If you ask for it in polyfit, you need to include it in polyval:
[y,delta] = polyval(p,t,S,mu);
Related Question