MATLAB: How to judge polyfit output is correct or not

MATLABpolyfit

a=polyfit(x,y,1);
X=(y-a(2))/a(1);
all of all data:X-x=0?

Best Answer

Today my crystal ball is very clear (head not) so I see a good answer to this age old question.
Since you have a very large number of data points y with acceptable estimates of their "measurement noise" fortunately are caused by normal-distributed random processes your question have a rather good answer. The polynomial fit (or really any type fitting curve) should ideally be a good guess of the expected y-value at each point. That makes it possible to check the distribution of the residuals:
dy = y - a;
subplot(3,1,1)
hist(dy,100)
From that histogram it should be possible to check that the residuals have a normal distribution with expected width and zero (at least approximately) expected value.
Further there should be no systematic variation of dy, so we have to check that too:
subplot(3,1,2)
plot(x,dy)
subplot(3,1,3)
plot(abs(fftshift(fft(dy))))
To summarize: Check the distribution of your residuals and that they are in reasonable agreement with your expected measurement uncertainty. Check that there your resudials have no systematic patterns, if you pass those checks then you're OK - but dont extrapolate far anyway.
HTH