uncertainty in linear fit

39 views (last 30 days)
Thar
Thar on 6 Feb 2018
Commented: Liviu Ivanescu on 9 Jan 2020
Hi all!
I want to ask about the uncertainty in the slope and in the constant term in a linear fit.
I found two function:
[p,S,mu] = polyfit(x,y,1);
[ynew,delta]= polyval(p,x,S,mu);
and
cf = fit(x,y,'poly1');
cf_coeff = coeffvalues(cf);
cf_confint = confint(cf);
a = cf_coeff(1);
b = cf_coeff(2);
a_uncert = (cf_confint(2,1) - cf_confint(1,1))/2;
b_uncert = (cf_confint(2,2) - cf_confint(1,2))/2;
when i apply the two functions in my x,y data i find different coefficient. What is the different between the two functions and how can i apply them in x{a},y{a} data?
Thank you!
  1 Comment
Liviu Ivanescu
Liviu Ivanescu on 9 Jan 2020
polyval gives errors on y, not on slope or intercept.

Sign in to comment.

Answers (1)

Jos (10584)
Jos (10584) on 7 Feb 2018
Edited: Jos (10584) on 7 Feb 2018
Unfortunately, the first output of the function polyfit, being the coefficients of the fit changes, with the requested number of outputs. As an example:
x = 1:10 ;
y = 2*x + 3 ;
p1 = polyfit(x,y,1)
[p2, S] = polyfit(x,y,1)
[p3, S, mu] = polyfit(x,y,1) % p3 is different!
This is only very cryptically mentioned in the documentation and is easily overlooked. I am not sure why this behaviour is implemented, but The Mathworks is likely to have its reasons ...
I suggest you to use the function fit to estimate the uncertainty in the parameters, as these are easy to retrieve. If you only need the coefficients, p = polyfit(x,y,n) will do, of course.
  1 Comment
Liviu Ivanescu
Liviu Ivanescu on 9 Jan 2020
[p3, S, mu] = polyfit(x,y,1)
uses a scaled version of x, see docs on polyfit for details ; that's why one gets different results.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!