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

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

 채택된 답변

jonas
jonas 2018년 6월 29일
편집: jonas 2018년 6월 29일
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

I am sorry, to say, but I am trying to input the code, in my code but seems not to be working. Its giving me a error, in STR line. I am learner and new user, sorry if I am asking too much.this is my code and I hope I am doing right.
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.
str=['R^2 =',num2str(gof.rsquare),newline,...
'y = ',num2str(fitresult.x2),'x+',num2str(fitresult.y2)]
annotation('textbox',[.15 .9 0 0],'string',str,'FitBoxToText','on')
end
No problem! That is probably because you changed the code. Change back x2 and x1 to p1 and p2. They are not variable names but rather the coefficients obtained from the fit.
Also, note that the equation shows the normalized coefficients. Set normalize 'off' in the fit argument or adjust the parameters if you want to change that.
If you still get an error, copy the error message here.
Error in Untitled (line 14) str=['R^2 =',num2str(gof.rsquare),newline,...
this is the error I am getting, the curve is there but nothing.
if true
% code
% 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', 'off' );% Fit model to data.
hold on
h1=scatter (x1,y1,'filled','r');
hold on
h2 = plot( fitresult,'r' );% Plot fit with data.
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')
end
jonas
jonas 2018년 7월 4일
편집: jonas 2018년 7월 4일
Right, you are using an older version of MATLAB, from before 'newline' was introduced. Try replacing newline by either char(10) or sprintf('\n'). Both should work.
I am sorry to say, but both didn't worked out. :( No luck, I tried to modify the code and it just represent R^2 but no value, here is the code.
if true
% code
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.
formatspec = 'R^2=','gof.rsquare'
str= sprintf(formatspec,(gof.rsquare))
annotation('textbox',[.15 .9 0 0],'string',str,'FitBoxToText','on')
end
Same Error
Error in Untitled (line 14)
str=['R^2 =',num2str(gof.rsquare),char(10),...
end
but what I want to do is to use this option in the given below code, I've been trying to find solution but all in vain, I will be really thankful for you any advice on this.
ORIGINAL CODE
if true
% code
%%Fill the Markers
% Create a scatter plot and fill in the markers. |scatter| fills each marker
% using the color of the marker edge
% Copyright 2015 The MathWorks, Inc.
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];
x2=[51.6 51.6 50.1 57.4 63 59.4 54.8 64.7 73.5 63.2 65.9 77.6];
y2=[7.22 6.19 5.1 6.02 7.84 8.11 7.43 7.92 9.33 8.91 8.21 8.16];
x3=[58.1 51.5 60.1 60 68.5 62.7 66.5 72.6 74.3 66 69.7 78.3];
y3=[7.27 5.68 7.74 5.94 8.57 8.36 8.44 7.89 9.71 9.28 9.33 8.07];
x4=[54.4 51.9 56.6 63.6 67 55.9 64.4 76.2 72.1 63.7 69.1 79.3];
y4=[6.76 5.95 6.23 5.63 8.21 7.75 8.23 7.16 9.45 8.72 9.3 7.57];
figure
%%scatter plot
%scatter (x1,y1,'filled');
%hold on
%scatter (x2,y2,'filled');
%hold on
%scatter (x3,y3,'filled');
%hold on
%scatter (x4,y4,'filled');
%hold on
%%Fit: 'NF'.
[x5, y5] = prepareCurveData( x1, y1 );
ft = fittype( 'poly1' );% Set up fittype and options.
[fitresult, gof] = fit( x5, y5, 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.
hold on
%%Fit: 'SFI'.
[x6, y6] = prepareCurveData( x2, y2 );
ft = fittype( 'poly1' );% Set up fittype and options.
[fitresult, gof] = fit( x6, y6, ft, 'Normalize', 'on' );% Fit model to data.
hold on
h3=scatter (x2,y2,'filled','b');
hold on
h4 = plot( fitresult,'b' );% Plot fit with data.
hold on
%%Fit: 'SFII'.
[x7, y7] = prepareCurveData( x3, y3 );
ft = fittype( 'poly1' );% Set up fittype and options.
[fitresult, gof] = fit( x7, y7, ft, 'Normalize', 'on' );% Fit model to data.
hold on
h5=scatter (x3,y3,'filled','m');
hold on
h6 = plot( fitresult,'m' );% Plot fit with data.
hold on
%%Fit: 'SC-M'.
[x8, y8] = prepareCurveData( x4, y4 );
ft = fittype( 'poly1' );% Set up fittype and options.
[fitresult, gof] = fit( x8, y8, ft, 'Normalize', 'on' );% Fit model to data.
hold on
h7=scatter (x4,y4,'filled','k');
hold on
h8 = plot( fitresult,'k' );% Plot fit with data.
hold on
axis ([25 85 4 11]);
legend ([h1 h3 h5 h7],{'NF','SFI','SFII','SC-M'}, 'location','NW','orientation','horizontal')
xlabel('Compressive Strength (MPa)') % x-axis label
ylabel('Flexural Strength (MPa)') % y-axis label
grid on
grid minor
end
Don't worry, it's a quite trivial issue. You will solve it eventually.
Next attempt, without using any newline character at all.
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.
formatspec = ['R^2 = ',num2str(gof.rsquare)]
str1 = sprintf(formatspec,(gof.rsquare))
formatspec = ['y = ',num2str(fitresult.p1),'x+',num2str(fitresult.p2)]
str2 = sprintf(formatspec,(gof.rsquare))
annotation('textbox',[.15 .9 0 0],'string',str1,'FitBoxToText','on','linestyle','none')
annotation('textbox',[.15 .85 0 0],'string',str2,'FitBoxToText','on','linestyle','none')
end
Thanks for the help with little modification I was able to do everything, thanks. and my graph look like a happy puppy now.
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

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Linear and Nonlinear Regression에 대해 자세히 알아보기

질문:

2018년 6월 29일

댓글:

2021년 8월 23일

Community Treasure Hunt

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

Start Hunting!

Translated by