linear fit of a semilog graph

조회 수: 16 (최근 30일)
Daniel
Daniel 2014년 11월 19일
댓글: Daniel 2014년 11월 19일
Hello,
I have my data as follows with F1, F2, F3, N1, N2 and N3. I want to do a linear fit of my data and plot that. I tried polyfit as seen in my code. But it just plots a horizontal line...? I tried adding my vectors to a Ntot and Ftot as seen.
What should I do? Thank you.
F1=[200 200 200];
N1=[10000 15000 20000];
F2=[300 300 300];
N2=[8000 7000 7500];
F3=[100 100 100];
N3=[120000 140000 80000];
Ftot=[F3 F2 F1];
Ntot=[N3 N2 N1];
figure
semilogx(N1,F1,'o')
hold on
semilogx(N2,F2,'o')
semilogx(N3,F3,'o')
xlabel('N');
ylabel('F');
grid on
P = polyfit(Ntot,Ftot,1);
yfit = P(1)*Ftot+P(2);
plot(Ntot,yfit,'r-.');
xlim([1000 500000])
ylim([0 max(Ftot)+50])
  댓글 수: 2
Torsten
Torsten 2014년 11월 19일
According to your call to polyfit, the next line must read
yfit = P(1)*Ntot+P(2);
instead of
yfit = P(1)*Ftot+P(2);
Best wishes
Torsten.
Star Strider
Star Strider 2014년 11월 19일
The other option, of course, is to use the polyval function.

댓글을 달려면 로그인하십시오.

채택된 답변

Erik
Erik 2014년 11월 19일
편집: Erik 2014년 11월 19일
You should sort the values in
Ntot
using
[Ntot,i] = sort(Ntot);
and sort
Ftot
accordingly using
Ftot = Ftot(i);
Then you can do the
polyfit(...)
and as Torsten mentions, you should use
yfit = P(1)*Ntot+P(2);
Then plot it using
semilogx(Ntot,yfit,'r-')
although
plot(...)
also works. If you want the semilogarithmic plot to have a straight line, then use
log10(Ntot)
instead of
Ntot
in the line containing the
polyfit(...)
and the line containing
yfit = ...
  댓글 수: 1
Daniel
Daniel 2014년 11월 19일
Thank you. Okey, but now I have a plot like the attached picture.
Can I do so a straight line goes from the data at 300 at the y-axis to the data at 100 on the y-axis?
Is it some way to do an approximation for that?

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Linear and Nonlinear Regression에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by