Inserting a linear fit equation and R sq value for a scatter plot

8 vues (au cours des 30 derniers jours)
Amardeep Singh
Amardeep Singh le 29 Juin 2018
Commenté : Aries Dwi Siswanto le 23 Août 2021
I know it is very basic question. I was able to do a linear curve fit for 6 different curves but I am trying to insert the equation and R sq value in the graph, any help would be appreciated.
if true
% code
x1=[43.4 30.4 37.6 42.2 60.2 37.8 48 54 67.4 47.6 56 66.5];
y1=[6.38 4.37 4.92 4.31 6.65 5.06 6.06 6.67 8.83 6.29 7.08 7.27];
figure
%%Fit: 'NF'.
[x2, y2] = prepareCurveData( x1, y1 );
ft = fittype( 'poly1' );% Set up fittype and options.
[fitresult, gof] = fit( x2, y2, ft, 'Normalize', 'on' );% Fit model to data.
hold on
h1=scatter (x1,y1,'filled','r');
hold on
h2 = plot( fitresult,'r' );% Plot fit with data.
end

Réponse acceptée

jonas
jonas le 29 Juin 2018
Modifié(e) : jonas le 29 Juin 2018
The R^2 goodness of fit is stored in gof.rsquare. This is one way to display it in the plot together with the equation:
str=['R^2 =',num2str(gof.rsquare),newline,...
'y = ',num2str(fitresult.p1),'x+',num2str(fitresult.p2)]
annotation('textbox',[.15 .9 0 0],'string',str,'FitBoxToText','on')
  8 commentaires
Amardeep Singh
Amardeep Singh le 5 Juil 2018
Thanks for the help with little modification I was able to do everything, thanks. and my graph look like a happy puppy now.
Aries Dwi Siswanto
Aries Dwi Siswanto le 23 Août 2021
I'm wondering how to arrange the position of the legend:
red circle NF: y =0.87309x + 8.0233 R2=0.8127
etc.
I'm foolowing your code above, but I could not get the graph as your mentioned above.
Could you please help me?
Thank you very much

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Linear and Nonlinear Regression dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by