Correlation Coefficient in Legend
5 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hi im new to Matlab, Im having trouble knowing how to display my correlation coefficient on my legend, i tried using : text(x, y, sprintf('R = %.2f', R))
but this does not work because it does not include it in the legend. Also how can I make Matlab not round up my correaltion coefficient number? My correlation coefficient number is: 0.999259 but when it shows up in the graph it rounds up to 1
here is my code :
close all;
clc
hT=[400
350
300
250
200
150
100
50 ];
h2=[64
56
46
40
30
22
14
4 ];
plot(hT,h2,'k.','markersize',16)
axis([0 400 0 70]);
%% least square fit
X =[ones(size(hT)),hT];
z=X'*h2;
S=X'*X;
U= chol(S);
w=U'\z;
c=U\w;
q=50:5:400;
fit=c(1)+c(2)*q; % coefficints
hold on
plot(q,fit,'b', 'linewidth',1); % linear least square fit
%%
R= corr(hT,h2)
title('\Deltah_{1-2} VS \Deltah_{1-T}','BackgroundColor','y')
legend('Data', 'Least square fit')
xlabel('\Deltah_{1-T} (mm)')
ylabel('\Deltah_{1-2} (mm)')
grid on
0 comentarios
Respuestas (2)
Star Strider
el 2 de Oct. de 2020
Change the legend call to:
legend('Data', sprintf('Least square fit R = %.2f', R))
.
0 comentarios
VBBV
el 8 de Nov. de 2023
Use the sprintf command in legend function just like the text, and include a higher precision specifier for decimals e.g,. 5 for 5 place decimals to avoid roundoff of numbers in legend appearance
close all;
clc
hT=[400
350
300
250
200
150
100
50 ];
h2=[64
56
46
40
30
22
14
4 ];
plot(hT,h2,'k.','markersize',16)
axis([0 400 0 70]);
%% least square fit
X =[ones(size(hT)),hT];
z=X'*h2;
S=X'*X;
U= chol(S);
w=U'\z;
c=U\w;
q=50:5:400;
fit=c(1)+c(2)*q; % coefficints
hold on
plot(q,fit,'b', 'linewidth',1); % linear least square fit
%%
R= corr(hT,h2);
title('\Deltah_{1-2} VS \Deltah_{1-T}','BackgroundColor','y')
legend('Data', sprintf('Least square fit, R = %0.5f',R)) % use the sprintf command with higher precison specifier
xlabel('\Deltah_{1-T} (mm)')
ylabel('\Deltah_{1-2} (mm)')
grid on
0 comentarios
Ver también
Categorías
Más información sobre Discrete Data Plots en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
