Polyfit with plot, semilogx, semilogy, and loglog scales.

22 次查看(过去 30 天)
So I have vector x and vector y, and I need to determine which scale fits the data.
x = 2004:2015;
y = [9214, 9271, 9320, 10413, 10930, 11242, 10873, 11801, 13245, 14728, 15463, 16297];
So I decided to use polyfit and use all the plots and let MATLAB show me which one best fits.
%Output:
subplot(1,3,1)
p1 = polyfit(x_new,y,1);
m1 = p1(1);
b1 = p1(2);
plot(x, y, 'o', x, m1.*x+b1), xlabel('years'), ylabel('sales'), title('years vs sales')
subplot(1,3,2)
p2 = polyfit(x_new,log(y),1);
m2 = p2(1);
b2 = 10^(p2(2));
semilogy(x, y, 'o', x, b2*10.^(m2.*x)), xlabel('years'), ylabel('sales'), title('years vs sales')
subplot(1,3,3)
p3 = polyfit(log10(x),log10(y),1);
m3 = p3(1);
b3 = 10^(p3(2));
loglog(x, y, 'o', x, b3.*x.^m3), xlabel('years'), ylabel('sales'), title('years vs sales')
And this is the graph I am getting:
I think the first one is right, and I believe that the line shouldn't look like that in the second one, and the third is just not working for some reason!!!

回答(1 个)

Prabhan Purwar
Prabhan Purwar 2020-3-2
Hi,
Try fitting a higher degree of a polynomial using polyfit function while fitting a curve to logarithmic data as it tends to be a curve rather than a straight line.
For more details refer to the following link
Hope it helps!!

类别

Help CenterFile Exchange 中查找有关 Interpolation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by