MATLAB: Polyfit – uncertainty on coefficient.

polyfituncertainty

Hi all,
I've got data points :
x = [0 0 5 5]; y = [0 1 4 7];
I would like to fit these data with polyfit (ax + b) and then, derive the "maximum and minimum" lines compatible with these data – that should be the uncertainty on a and b.
What I should obtain in this easy example is : y = 7/5 * x + 0 and y = 3/5 * x + 1.
I've found on this forum things about using cov and the S output of the function, but the error I got from the sqrt(…) formula doesn't give the numbers above…
Could anyone help me ?
Thanks

Best Answer

If you have the 2012b version of nlinfit, you're in luck! You can use the 'weights' option with it to weight your regression using the error bars in your data.
Error bars are usually the ‘standard errors’ or the ‘95% confidence intervals’, also based on the standard errors. Standard errors are defined as:
SE = std/(sqrt(N)
where N are the number of observations (that may change between data points). The usual weighting vector is the inverse-variance, so to get a weight vector from standard errors, the calculation becomes:
Wgt = 1./(N .* SE.^2);
Your objective function is:
f = @(B,x) B(1) + B(2).*x;
If you don't have the 2012b version, this gets slightly more complicated. You need two objective functions, one weighted (that you call in nlinfit) and your original unweighted function.
The weighted function becomes:
fw = @(B,x) Wgt .* f(B,x);
When you have fitted your function, use nlparci and nlpredci to generate the confidence intervals for your parameters (‘B’ here ) and your fitted function. NOTE that it is necessary to define Wgt and f before you define fw or call nlinfit.
Nonlinear fitting is a bit heuristic, and while your function should be easy to fit, sometimes it's necessary to use different starting guesses to get a good fit. I strongly suggest using rand(2,1) (in your current problem) rather than ever starting with a vector of zeros. Zeros can cause problems in some situations.