I'm getting strange results plotting a 4th degree polynomial.
Here is the resultant graph:
I don't understand why there appear to be 2 lines. The first degree polynomial is just a single line, and 2nd degree and beyond comes up with the two lines like this graph.
The data is a 420x3 matrix attached.
here is the code:
p1=polyfit(tp(:,1),tp(:,2),4); %4th degree polynomial
f=polyval(p1,tp(:,1));
figure(1)
plot(tp(:,1),tp(:,2),'*',tp(:,1),f)

回答 (1 件)

Steven Lord
Steven Lord 2020 年 7 月 29 日

2 投票

It looks to me like tp(:, 1) is not sorted. You can check this with issorted. If it's not sorted, MATLAB may draw a line from around x = 27 back to x = 13 then have to draw a straight line back to x = 25.
Try sorting tp(:, 1) before using it to evaluate the fitted polynomial and before plotting.

4 件のコメント

John D'Errico
John D'Errico 2020 年 7 月 29 日
編集済み: John D'Errico 2020 年 7 月 29 日
The data may be sordid, but it is definitely not sorted. :) In fact, I did check the file, and tp(:,1) is not in a sorted sequence.
Terence O'Brien
Terence O'Brien 2020 年 7 月 29 日
Awesome, thanks so much.
The data is not sorted. I tried that with sort(tp) yesterday and I end up with this:
So now my data is inverted. I'm sure there is an easy fix... might you have the answer?
Terence O'Brien
Terence O'Brien 2020 年 7 月 29 日
I did try sort(tp,'descend') and it is the same
John Jr
John Jr 2024 年 10 月 28 日
I have the same problem, were you able to solve it, how did you do it?

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangePolynomials についてさらに検索

質問済み:

2020 年 7 月 29 日

コメント済み:

2024 年 10 月 28 日

Community Treasure Hunt

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

Start Hunting!

Translated by