How to display cfit data on plot

40 views (last 30 days)
Apurba Paul
Apurba Paul on 12 Aug 2020
Answered: Serhii Tetora on 12 Aug 2020
Is there a way to annote the fitting parameters with 95% coefficient into the plot. I am trying to do curve fit some data in a loop and want to displya the fitting results directly in the plot, may in some textbox. For example I am doing some curvefitting in the following code
%Generate data
x=-0:0.1:10;
x=transpose(x);
y=2*exp(-x/5)+0.1*rand(numel(x),1);
%curve fitting
ft=fittype('a*exp(-x/b)','independent','x','dependent','y');
opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts.Display = 'Off';
opts.StartPoint =[1,1,];
[fitresults,gof]=fit(x,y,ft,opts);
%showing plot
plot(fitresults,x,y,'.')
%display fit results
disp(fitresults)
Which generates plot like this
and the fitting results are displayed in the command window in the following way
General model:
fitresults(x) = a*exp(-x/b)
Coefficients (with 95% confidence bounds):
a = 2.031 (2.015, 2.047)
b = 5.329 (5.258, 5.4)
I want to display the fitting results inside the plot something like this
Is it possible to do using script.

Answers (1)

Serhii Tetora
Serhii Tetora on 12 Aug 2020
coeffs = coeffnames(fitresults);
coeffvals= coeffvalues(fitresults);
ci = confint(fitresults,0.95);
str1 = sprintf('\n %s = %0.3f (%0.3f %0.3f)',coeffs{1},coeffvals(1),ci(:,1));
str2 = sprintf('\n %s = %0.3f (%0.3f %0.3f)',coeffs{2},coeffvals(2),ci(:,2));
annotation('textbox',[.4 .5 .5 .2],'String',['Coefficients (with 95% confidence bounds): ', str1, str2]);

Categories

Find more on Linear and Nonlinear Regression 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!