Computing variance of a variable after linear fit using lsqcurvefit function
Show older comments
I just want to confirm whether the follwings are correct or not. I have two data sets and I want to linealy fit these data sets using the "lsqcurvefit" function. The linear fit equation is given by :
. Following is the code, which also computes the covariance matrix of the parametrs (a,b):
clear all;
xdata = [-0.41095 -0.410820 -0.41074 -0.41068 -0.41063 -0.41058 -0.41055 -0.41052 -0.41049 -0.41047];
ydata = [0.166666666666667 0.142857142857143 0.125000000000000 0.111111111111111 0.100000000000000 0.090909090909091 0.083333333333333 0.076923076923077 0.071428571428571 0.066666666666667];
fun = @(A,xdata)(A(1)+A(2)*xdata);
A0 = [1,-1];
A = lsqcurvefit(fun,A0,xdata,ydata);
[A,resnorm,residual,exitflag,output,lambda,J]= lsqcurvefit(fun,A0,xdata,ydata);
ACovariance = inv(J.'*J)*var(residual) %computes the covariance matrix of the parameters a,b in the equation y=a+bx
Is the above code correct for computing the covariance matrix of the fitting parameters?
Now I want to compute the variance of "y" at some point let's say at
. Is the following equation correct for computing the variance of
:
. Where,
are the variance of a and b , which are equal to i think the diagonal elements of the matrix "ACovariance" in the code, and
is the covariance of the parametrs a,b , which is equal to i think offdiagonal elements of "ACovariance". Following is a derivation of the above equation:

Now variance

where, variance
.
Is the above way of computing variance of y at a point
, is correct? If the above are incorrect, could you please, point out how to find variance of the variable "y" extrapolated to some arbitary point, after linear fitting the given data sets.
1 Comment
Answers (1)
Matt J
on 26 Jun 2023
It would be,
yvariance = [1,x0]*ACovariance*[1;x0]
7 Comments
P Rakesh Kumar Dora
on 26 Jun 2023
Matt J
on 26 Jun 2023
Does that mean your question has been answered? If so, please Accept-click the answer.
P Rakesh Kumar Dora
on 26 Jun 2023
P Rakesh Kumar Dora
on 27 Jun 2023
Matt J
on 27 Jun 2023
Well, the std deviation of your residuals is orders of magnitude smaller than that,
>> std(residual)
ans =
4.8895e-06
I don't see how extrapolating to x0=0 ends up magnifying that 1000 times.
Categories
Find more on Get Started with Curve Fitting Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!