MATLAB: Best fit line on Semi log graph

semi log best fit

Hello, I'm new to matlab. i want to plot a graph where x axis is in log scale and y axis is linear. The graph beow though shows straight line but I know the equation is not correct because It didn't pass throgh the scatter points
here is the code. Please help me, how can these line pass through the points and also how can i get the equation?
data=[0.891 35784.525582
0.91 39142.72
0.815 17679.26
0.891 25582
0.8861 30168.052]
p1=data(:,2); %Re
q1=data(:,1); %Cd
scatter(p1,q1)
set(gca,'XScale','log');
pp = polyfit(log(p1), q1, 1)
qq=exp(polyval(pp,log(p1)))
hold on
plot(p1, qq)
hold off

Best Answer

Like so
data=[0.891 35784.525582
0.91 39142.72
0.815 17679.26
0.891 25582
0.8861 30168.052];
p1=data(:,2); %Re
q1=data(:,1); %Cd
logp1 = log10(p1);
pp = polyfit(logp1, q1, 1);
qq=polyval(pp,logp1);
plot(logp1, qq,'-*',logp1,q1,'o'),grid
xlabel('log10(p1)'),ylabel('q1')
legend('fit','data')