Best fit line in log-log scale

Hi, I want to create a straight best fit line in the first portion of the graph and I don't want a curve best fit line, what should I do? Thank you.
rho=1000; %[kg/m3]
D=12.6*10^-3; %[m]
L=1.5; %[m]
miu=0.001; %[Pas]
g=9.81;
A=pi*D^2/4;
Q0=[1600,1500,1400,1300,1200,1100,1000,900,800,700,600,500,400,300,240,220,...
200,180,160,140,120,100,80,70,70,60,50,40,30,20,10];%[L/hr]
Q=Q0/(1000*3600);
%Wet-wet digital gauge
P_dpg=[20.1,17.5,15.7,13.1,11.6,9.3,8,6.5,5.3,4.1,3,2.1,1.3,0.8]; %[kPa]
%Inverted manometer
h=[6.9,5.9,5,4.1,3.2,2.6,1.8,1.3,0.7,0.5]; %[cm]
h_m=h/100;
P_mtr=rho*g*h_m;
%Capsuhelic gauge
P_cpg=[33,31,28,23,17,12,8];
P=[P_dpg.*1000,P_mtr,P_cpg];
V=Q/A;
Re=rho*V*D/miu;
f=P*D./(2*rho*L*V.^2);
X=Re(25:31);
Y=f(25:31);
p=polyfit(X,Y,1);
y=polyval(p,X);
figure(1)
loglog(Re,f,'x','LineWidth',1)
hold on
loglog(X,y,'--')
grid on
xlim([10^2 10^5])
ylim([0.001 0.1])
xlabel('Reynolds number Re')
ylabel('Friction factor f')
title('f vs Re')

 Accepted Answer

Like this?
p=polyfit(log(X),log(Y),1);
y=polyval(p,log(X));
figure(1)
loglog(Re,f,'x','LineWidth',1)
hold on
loglog(X,exp(y),'--')
grid on

2 Comments

Yes. It is. Thank you so much. Could you briefly explain why would you code it like that?
Simply because you are using loglog scale, so you need the equation:
(log y) = m(log x) + c for function polyfit to fit into a straight line.
This is same for polyval where variable y (but not log(y)) is calculated from (log x) so you need to convert it using exponential in the figure.

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!