Best fit line on Semi log graph

61 次查看(过去 30 天)
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

采纳的回答

Alan Stevens
Alan Stevens 2020-10-25
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')
  3 个评论
Alan Stevens
Alan Stevens 2020-10-25
The Matlab figures come from fitting Cd to lo10(Re).
The Excel figures come from fitting log10(Cd) to log10(Re).
If we modify the MATLAB program to the following
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);
logq1 = log10(q1);
pp = polyfit(logp1, logq1, 1);
qq=polyval(pp,logp1);
plot(logp1, qq,'-*',logp1,logq1,'o'),grid
xlabel('log10(Re)'),ylabel('log(Cd)')
legend('fit','data')
we get
pp =
0.1244 -0.6110
We can manipulate this as follows
% pp = 0.1244 -0.6110
% m = pp(1); c = pp(2)
% log10(Cd) = m*log10(Re) + c
% log10(Cd) = log10(Re^m) + c
% Cd = (10^c)*Re^m
% 10^c = 0.2449
% Cd = 0.2449*Re^0.1244
So the result depends on the type of fit, Cd vs log10(Re) or log10(Cd) vs log10(Re).

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Get Started with Curve Fitting Toolbox 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by