Best fit line on Semi log graph
60 views (last 30 days)
Show older comments
Maiesha Nujhat
on 25 Oct 2020
Commented: Maiesha Nujhat
on 25 Oct 2020
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
0 Comments
Accepted Answer
Alan Stevens
on 25 Oct 2020
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 Comments
Alan Stevens
on 25 Oct 2020
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).
More Answers (0)
See Also
Categories
Find more on Get Started with Curve Fitting Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!