How to display cfit data on plot

조회 수: 40 (최근 30일)
Apurba Paul
Apurba Paul 2020년 8월 12일
답변: Serhii Tetora 2020년 8월 12일
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.

답변 (1개)

Serhii Tetora
Serhii Tetora 2020년 8월 12일
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]);

카테고리

Help CenterFile Exchange에서 Linear and Nonlinear Regression에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by