If you have the curve fitting toolbox installed, you can use fit to determine the uncertainty of the slope
a and the y-intersect
b of a linear fit.
y have to be column vectors for this example to work.
cf = fit(x,y,'poly1');
'poly1' tells the fit function to perform a linear fit. The output is a "fit object". You can access the fit results with the methods
confint. Assuming that the confidence intervals are symmetrically spaced around the fitted values (which in my experience is true in all reasonable cases), you can use the following code:
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;
One note of caution: The errors of
b will generally be correlated, which makes them unnecessarily big. You can reduce this correlation by subtracting the mean x-value of your data before fitting. My Statistics skills aren't good enough to provide a solid explanation on the reasons for that - hopefully one of the more seasoned statistics experts can edit my answer (or provide their own and delete mine) to give details on this side-note.