How to find 95% confidence lines?

12 views (last 30 days)
Ganesh
Ganesh on 10 Jul 2012
I have been using polyfit and polyval for estimating and plotting polynomial fit along with the 95% confidence lines (which is 2*stdev). However, now I want to fit the data using exponentially increasing function of the form say, y = 100 - A*exp(Bx). I could do this using fitcurvedemo(). How do I find the confidence interval lines for this new fit model?
Thanks, Ganesh

Answers (1)

Star Strider
Star Strider on 10 Jul 2012
Several nonlinear curve fitting functions (‘lsqcurvefit’, ‘nlinfit’ and probably others) will give you the necessary output data to use ‘nlpredci’ to plot the confidence limits on the fitted data. You can also use them with ‘nlparci’ to calculate confidence intervals on the parameters. If you want more statistics on the fit and parameters, see ‘NonLinearModel.fit’.
  3 Comments
Star Strider
Star Strider on 10 Jul 2012
Not that I'm aware of. Sorry. If you have the Statistics Toolbox, you can use some of those nonlinear parameter estimation functions. (This is different from the Curve Fitting Toolbox.)
The only other alternative I can suggest is to log-transform you data and then fit to a linear model. The problem with that is that it also transforms the errors, and renders the confidence limits on the fit essentially useless.
Star Strider
Star Strider on 11 Jul 2012
I figured out how to do the nonlinear regression with the ‘fminsearch’ function that is part of basic MATLAB. You will still have to calculate the statistics, but this code will give you good parameter estimates. I am listing it with my development efforts to make it easier to follow:
% USING ‘FMINSEARCH’ TO SOLVE A NONLINEAR LEAST SQUARES CURVE
% FITTING PROBLEM
x = [0: 0.33: 5]';
K0 = 10;
K1 = -5;
K3 = -0.75;
y = K0 + K1*exp(K3*x); % Generate baseline data
Uer = 0.5*(0.5-rand(size(x))); % Generate noise
statnoise = [mean(Uer) std(Uer)];
ye = y + Uer; % Generate data to be fitted
LSQmin1 = @(P) sum((ye - (P(1).*ones(size(x)) + P(2).*exp(P(3).*x))).^2, 1); % Sum along columns
fcntest = LSQmin1([5 1 1]);
[B,fval,exitflag] = fminsearch(LSQmin1, [15 1 -1]', optimset('TolX',1E-10, 'TolFun', 1E-10))
fcnfit = B(1) + B(2).*exp(B(3).*x);
figure (508)
plot(x, ye, 'or')
hold on
plot(x, fcnfit, '-b')
hold off
text(max(x)*0.3, max(y)*.75, sprintf('\\itf(t) = %7.3f %+7.3f · e^{%7.3f · t}', B))
grid
You should be able to find methods for calculating the statistics online if not in your library.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!