I want to show r^2 and the poly1 ec on the plot

5 Ansichten (letzte 30 Tage)
Giulia Di Giorgio
Giulia Di Giorgio am 5 Mär. 2023
Hello, I made a poly1 ecuation with 2 column vector but I dont know how to show on the plot the R^2 value and the ecuation. I tried the text command but it's giving me an error with the () and [], help me please
h=[10;20;30;40];t1=[51.91;106.49;166.07;227.87]; t2=[51.91;54.58;59.58;61.80];
V=(pi*(dtr/2)^2.*h)/1000; %L
Q1=V./t1; Q2=V./t2; %L/s
f1=fit(t1,Q1,'poly1');
m1=f1.p1; b1=f1.p2;
fit1=m1.*t1+b1;
f2=fit(t2,Q2,'poly1');
m2=f2.p1; b2=f2.p2;
fit2=m2.*t2+b2;
subplot(2,2,1);
plot(t1,Q1,'mo',t1,fit1,'b-');
xlim([0 250]);
ylim([0.15 0.2]);
title('Caudal vs tiempos totales');
ylabel('Q (L/s)');
xlabel('t(s)');
legend('datos','ajuste',"location","best");
text(0,0.15,['R²='num2str(R_squared)])
  3 Kommentare
John D'Errico
John D'Errico am 5 Mär. 2023
Now that I can read what you wrote, where do you define the variable R_squared?
Is that intended to be one of the R^2 coefficients from one of the two polynomial fits you did?
Giulia Di Giorgio
Giulia Di Giorgio am 5 Mär. 2023
Yes, for the first plot. I didn't define it, how can I do it?

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Torsten
Torsten am 5 Mär. 2023
format short g
%datos del equipo
df=6.1; %cm
hf=25; %cm
dtr=35; %cm
htr=62; %cm
%datos del usuario
V1=60; V2=0; Vr=V1-V2; %volumen de susp filtrado (L)
m1=0.1; %kg
mr=(Vr*m1)/V1; %masa real filtrada
ma=1.912; md=1.980; %pesos antes y despues de filtrar del cartucho+filtro+agua (Kg)
P=3; %psi
h=[10;20;30;40];t1=[51.91;106.49;166.07;227.87]; t2=[51.91;54.58;59.58;61.80]; %vectores altura (cm) y tiempos totales y parciales (s)
%GRAFICA Q vs t
V=(pi*(dtr/2)^2.*h)/1000; %L
Q1=V./t1; Q2=V./t2; %L/s
[f1,R]=fit(t1,Q1,'poly1');
R_squared_char = num2str(R.rsquare);
m1=f1.p1; b1=f1.p2;
fit1=m1.*t1+b1;
f2=fit(t2,Q2,'poly1');
m2=f2.p1; b2=f2.p2;
fit2=m2.*t2+b2;
subplot(2,2,1);
plot(t1,Q1,'mo',t1,fit1,'b-');
xlim([0 250]);
ylim([0.15 0.2]);
title('Caudal vs tiempos totales');
ylabel('Q (L/s)');
xlabel('t(s)');
legend('datos','ajuste',"location","best");
text(10,0.155,strcat('R²= ',R_squared_char))
subplot(2,2,2);
plot(t2,Q2,'mo',t2,fit2,'b-');
xlim([50 65]);
ylim([0.1 0.7]);
title('Caudal vs tiempos parciales');
ylabel('Q (L/s)');
xlabel('t(s)');
legend('datos','ajuste',"location","best");

Weitere Antworten (1)

John D'Errico
John D'Errico am 5 Mär. 2023
Bearbeitet: John D'Errico am 5 Mär. 2023
Read the help for fit! Look at the second argument it can return. You don't give me enough information (what is dtr, for example?) to actually run your code, so I'll just make up some data.
x = rand(10,1);
y = rand(10,1); % random data, so R^2 should be randomly near 0.
[f1,goodness]=fit(x,y,'poly1');
goodness
goodness = struct with fields:
sse: 0.3322 rsquare: 0.0073 dfe: 8 adjrsquare: -0.1168 rmse: 0.2038
Do you see that goodness has a field named rsquare? That is the R^2 coefficient you want.
goodness.rsquare
ans = 0.0073
  3 Kommentare
John D'Errico
John D'Errico am 5 Mär. 2023
I explained what you are missing in my answer. The variable R_squared does not just appear like magic. It will not be defined just because you want it to exist.
You need to create it. In this case, you can create it from the second return argument from fit.
[f1,goodness] = fit(t1,Q1,'poly1');
R_squared = goodness.rsquare;
Having done that, NOW you can use the value. But not until.

Melden Sie sich an, um zu kommentieren.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by