# Error in the intercept

조회 수: 11(최근 30일)
I think this might be a dumb question, but how do you find the error of the intercept for a straigh line graph that I have fittded using polyfit? and polyval. I got the error for the slope by doing something like this.
[p2,s2] = polyfit(A2,B2,1);
[f2,delta] = polyval(p2,x,s2);
deltaf2=s2.normr/sqrt(s2.df);
C2=deltaf2^2*inv(s2.R)*inv(s2.R)';
deltap2=sqrt(diag(C2));
ok
##### 댓글 수: 3표시 이전 댓글 수: 2숨기기 이전 댓글 수: 2
Ganessen Moothooveeren 2013년 3월 14일
you used this to find error in slope but which variable is the error in slope??..is it deltaf2?? [p2,s2] = polyfit(A2,B2,1); [f2,delta] = polyval(p2,x,s2); deltaf2=s2.normr/sqrt(s2.df); C2=deltaf2^2*inv(s2.R)*inv(s2.R)'; deltap2=sqrt(diag(C2));

댓글을 달려면 로그인하십시오.

### 채택된 답변

I think I have cracked it if anyone is interested.
Fairly simple if you use nlinfit. (I would be interested to know if you can get the same results from lsqcurvefit though if anyone has the answer).
So what you so is
function F = myfun(p,X)
F = (p(1)*X)+p(2); end
!sepearte file!
X = [1:4]; Y = [5:8]; % or something like this
[beta,resid,J,COVB,mse] = nlinfit(X,Y,@myfun2,[1;1]) ; [ci se] = nlparci(beta,resid,'covar',COVB);
%beta(1) is coefficient p(1)(the gradient) and beta(2) is the intercept. % se(1) is the error on the gradient and se(2) is the error of the intercept.
Easy, i feel a little ashamed..
oh yeah, you also had to alter nlparci
by going
open nlparci in matlab
then changing on the first line
function ci = nlparci(beta,resid,varargin)
to
function [ci se] = nlparci(beta,resid,varargin)
and thats it...
Thanks
##### 댓글 수: 1표시 없음숨기기 없음
Star Strider 2012년 7월 4일
I believe both 'nlinfit' and 'lsqcurvefit' can give you the information you need to give to 'nlparci' to calculate the parameter confidence intervals. The principal difference between 'nlinfit' and 'lsqcurvefit' is that 'lsqcurvefit' allows parameter constraints. Both will give you either the covariance matrix or the jacobian as well as the other results that 'nlparci' can use as arguments.
You don't have to alter 'nlparci' to get the standard errors, since 'nlinfit' gives you the ability to calculate those from the 'COVB' matrix it returns. You already calculated the standard errors as 'deltap2' from the covariance matrix you calculated as 'C2' in your original code. I used your results to calculate the 'CI95' matrix.

댓글을 달려면 로그인하십시오.

### 추가 답변(1개)

Star Strider 2012년 7월 3일
Not dumb at all. The problem is that if you want confidence limits on the estimated parameters, the 'polyfit' and 'polyval' functions won't get you there.
If you have the Statistics or Optimization Toolboxes, you can fit your model with 'lsqcurvefit' or 'nlinfit' respectively, then use 'nlparci' to get the confidence limits on the parameters. (Use 'nlpredci' to get confidence limits on the fitted data.)
If you don't have access the these, 'lscov' will likely give you what you need to calculate the confidence intervals yourself.
##### 댓글 수: 3표시 이전 댓글 수: 2숨기기 이전 댓글 수: 2
I was just looking in to finding errors using lsqcurvefit, but it ssems that you have to use nlinfit. Is this right? It also seems to suggest that you have to change some of the code in nparci in order to get the actual error on the parameters?
Can you help with that at all.
Thanks very much for the help so far by the way

댓글을 달려면 로그인하십시오.

### 범주

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!