How to use polyfit for a table of values

조회 수: 20 (최근 30일)
Daniel Ibarra
Daniel Ibarra 2019년 10월 7일
댓글: Matt J 2019년 10월 8일
I am trying to fit a curve to data i read in from a excel file. This is the code i have so far. I've been trying to debug it for some time and the line that is giving me problems is when i use polyfit. The values that it gives for constants are NAN and NAN.
cardatatable = xlsread('carmpg.csv')
mpg = cardatatable(:,1);
horsepower = cardatatable(:,4);
figure(1)
plot(mpg,horsepower,'o')
xlabel('miles per gallon')
ylabel('horsepower')
p = polyfit(mpg,horsepower,1);
mpgfit = linspace(min(mpg),max(mpg),1000);
horsepowerfit = polyval(P,mpgfit);
hold on
plot(mpgfit,horsepowerfit,1)

채택된 답변

Matt J
Matt J 2019년 10월 7일
편집: Matt J 2019년 10월 7일
That would occur if mpg or horsepower contain NaNs. You should remove them.
  댓글 수: 2
Daniel Ibarra
Daniel Ibarra 2019년 10월 8일
Thank you, this fixed the issue.
Matt J
Matt J 2019년 10월 8일
You're welcome, but please Accept-click the answer if it resolved your question.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by