Uncertainties of coefficients in linear fit I'm trying to calculate the uncertainty of the coefficients in a linear fit, but I'm not sure how to do it.
on the one hand, I can extract the error with matlab statistical tools but on the other hand I need to take into account the uncertainty of my measurement (Let's say I measure pressure in a chamber and I have some degree of uncertainty that based on the measurement tool), and I dont know how to put it in my statistical calculations in matlab.
the code that I'm using now to evaluate the uncertainties is:
[z,s]=polyfit(t,lnP,1);
ste =sqrt(diag(inv(s.R)*inv(s.R')).*s.normr.^2./s.df);
Thank you for your help!
 A: let's assume you have N data points of the form $(x_i,y_i\pm s_i)$, and you want some function of x to approximate y.  I'll assume a more general form for the fitting function, but it will be applicable (and much simpler) in the case of a linear fit.
$$f(x)=\sum_{i=1}^n c_i x^{e_i}$$
For a linear fit, the exponents ${e_i}$ will just be zero and one, and you'll need to optimize the coefficients ${c_i}$.
I assume you're doing a least-squares fit, so the quantity you'll want to minimize is $\chi^2$:
$$\chi^2=\sum_{i=1}^N\left(\frac{y_i-f(x_i)}{s_i}\right)^2$$
To minimize this with respect to $c_i$, you'll need to find a stationary point such that $\frac{\partial\chi^2}{\partial c_i}=0$ for all $c_i$.
This can be cast as a system of linear equations:
$$\frac{\partial\chi^2}{\partial c_k}=2\sum_{i=1}^N\left(\frac{y_i-f(x_i)}{s_i}\right)\left(-\frac{x_i^{e_k}}{s_i}\right)=0$$
$$\sum_{i=1}^N\left(\frac{y_i-\sum_{j=1}^n c_j x_i^{e_j}}{s_i}\right)\left(-\frac{x_i^{e_k}}{s_i}\right)=0$$
$$\sum_{i=1}^N\left(\frac{x_i^{e_k}\sum_{j=1}^n c_j x_i^{e_j}}{s_i^2}\right)=\sum_{i=1}^N\left(\frac{y_i x_i^{e_k}}{s_i^2}\right)$$
$$\sum_{i=1}^N\sum_{j=1}^n\left(\frac{x_i^{e_k+e_j}}{s_i^2}\right)c_j=\sum_{i=1}^N\left(\frac{y_i x_i^{e_k}}{s_i^2}\right)$$
$$\sum_{j=1}^n A_{kj}c_j=b_k$$
$$c_k=\sum_{j=1}^n A_{kj}^{-1}b_j$$
Where
$$A_{kj}=\sum_{i=1}^N\left(\frac{x_i^{e_k+e_j}}{s_i^2}\right)$$
$$b_k=\sum_{i=1}^N\left(\frac{y_i x_i^{e_k}}{s_i^2}\right)$$
The variance $\sigma_i^2$ of each coefficient $c_i$ can be estimated by (see equation 9)
\begin{align*}
\sigma_i^2 &= \sum_{j=1}^N s_j^2 \left(\frac{\partial c_i}{\partial y_j}\right)^2\\
&= \sum_{j=1}^N s_j^2 \left(\sum_{k=1}^n\frac{\partial A_{ik}^{-1}b_k}{\partial y_j}\right)^2\\
&= \sum_{j=1}^N s_j^2 \left(\sum_{k=1}^n A_{ik}^{-1}\frac{\partial b_k}{\partial y_j}\right)^2\\
&= \sum_{j=1}^N s_j^2 \left(\sum_{k=1}^n A_{ik}^{-1}\frac{x_j^{e_k}}{s_j^2}\right)^2
\end{align*}
So as long as $A$ is invertible, you can use the procedure above to find uncertainties in your coefficients.
edit:
the $\sigma_i^2$ actually simplify a bit further:
\begin{align*}
\sigma_i^2 &= \sum_{j=1}^N s_j^2 \left(\sum_{k=1}^n A_{ik}^{-1}\frac{x_j^{e_k}}{s_j^2}\right)^2\\
&=\sum_{j=1}^N s_j^2 \left(\sum_{k=1}^n A_{ik}^{-1}\frac{x_j^{e_k}}{s_j^2}\right)\left(\sum_{l=1}^n A_{il}^{-1}\frac{x_j^{e_l}}{s_j^2}\right)\\
&=\sum_{k=1}^n\sum_{l=1}^n A_{ik}^{-1} A_{il}^{-1}\sum_{j=1}^N\frac{x_j^{e_l+e_k}}{s_j^2}\\
&=\sum_{k=1}^n\sum_{l=1}^n A_{ik}^{-1} A_{il}^{-1}A_{lk}\\
&=\sum_{k=1}^n A_{ik}^{-1} \delta_{ik}\\
\sigma_i^2&=A_{ii}^{-1}
\end{align*}
So your function of x is now 
$$f(x)=\sum_{i=1}^n (c_i \pm \sigma_i)x^{e_i}$$
The uncertainty in the evaluation of the function at some point $x_j$ can be estimated similarly to how the uncertainty in coefficients was calculated.
\begin{align*}
\sigma_{f(x_j)}^2 &=\sum_{i=1}^n\left(\frac{\partial f(x_j)}{\partial c_i}\right)^2 \sigma_i^2\\
&=\sum_{i=1}^n\left(x_j^{e_i}\right)^2 \sigma_i^2
\end{align*}
Given some point $x_i$, the value given by the fit will be $f(x_i)$, and the uncertainty (one std. deviation) will be $\sigma_{f(x_i)}$ (as defined above)
