Want to fit linear curve on my data.

I have x, y data
x=[0;0.100000000000000;0.200000000000000;0.300000000000000;0.400000000000000;0.500000000000000;0.600000000000000];
y=[4.67178152947921e-06;4.67353333624452e-06;4.70560728038426e-06;4.74873086195845e-06;4.77333265701103e-06;4.84630647442201e-06;4.87015810633671e-06];
I want to plot x vs y and want to y-axis in log scale
plot(x,y)
set(gca,'YScale','log')
hold on
Note: x data starts from 0
Now I want to fit a line and show the slope of that curve fitting line + original curve
p=polyfit(x,(y),1);
q=polyval(p,x);
plot(x,q).
It seems to be not right because the fit line isn't straight ( it likes power fit or exponential) . Note log scale ( not log(data))
Please helps. Thanks

2 Kommentare

the cyclist
the cyclist am 18 Mär. 2013
It would be tremendously helpful if you included a small sample of your data that exhibits the problem, so that we could run your code and see the results. Your statement that it "seems to be not right" is not quite enough.
Daniel Shub
Daniel Shub am 18 Mär. 2013
Your x and y are not the same size ...

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Daniel Shub
Daniel Shub am 18 Mär. 2013
Bearbeitet: Daniel Shub am 20 Mär. 2013

0 Stimmen

I don't like working on log scales, I would rather take the log transform of my data
x=[0;0.100000000000000;0.200000000000000;0.300000000000000;0.400000000000000;0.500000000000000;0.600000000000000];
y=[4.67178152947921e-06;4.67353333624452e-06;4.70560728038426e-06;4.74873086195845e-06;4.77333265701103e-06;4.84630647442201e-06;4.87015810633671e-06];
xx = x;
yy = log(y);
plot(xx, yy, '*');
lsline;
p = polyfit(xx, yy,1);
text(max(xlim), max(ylim), ['Slope: ', num2str(p(1))], 'HorizontalAlignment', 'Right', 'VerticalAlignment', 'Top')
set(gca, 'YTickLabel', 10^6*exp(get(gca, 'YTick')))

4 Kommentare

Phong Pham
Phong Pham am 19 Mär. 2013
But I Really need the log scale on y. I applied the your code and it does not look good since the plot x, y does not look linear curve so the fit line is way off.
Daniel Shub
Daniel Shub am 19 Mär. 2013
Opps, I missed that. I edited the code.
Phong Pham
Phong Pham am 19 Mär. 2013
Bearbeitet: Phong Pham am 19 Mär. 2013
I dont know why but it has something weird to me. First, the y-scale has number likes 0.00344 not 10^-3 , etc .. ( log scale). Second, the slope is negative, the fitting line is straight up so it should give positive slope.
Thanks
Daniel Shub
Daniel Shub am 20 Mär. 2013
I got the output of polyfit wrong (I thought the first coefficient was the intercept). I changed the code. I also changed the scaling of the ticks. You can set them to be whatever you want.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Jan
Jan am 19 Mär. 2013

0 Stimmen

When you want a line in the logspace diagram, you need an exponential fit on the data. Or build the log of the data at first and fit the line afterwards.

Kategorien

Mehr zu Get Started with Curve Fitting Toolbox finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by