quadratic curvature doesn't went smoothly

3 ビュー (過去 30 日間)
nirwana
nirwana 2023 年 10 月 24 日
編集済み: John D'Errico 2023 年 10 月 24 日
Anyone can help me?
I am trying to fit my data with first and second order polynomial, but the result of quadratic curvature doesn't smoothly, seems it repeat several time in certain point. Here is my code
%Loads the vectors x and y.
load steam
% Fit a first degree polynomial to the data.
[p s]= polyfit(x,y,2);
y1=polyval(p,x);
% PLOTTING DATA
plot(x,y,'o',MarkerFaceColor='k',MarkerEdgeColor='none')
hold on
plot(x,y1,'--')
steam data is

採用された回答

John D'Errico
John D'Errico 2023 年 10 月 24 日
編集済み: John D'Errico 2023 年 10 月 24 日
load steam
% Fit a first degree polynomial to the data.
[p s]= polyfit(x,y,2);
y1=polyval(p,x);
% PLOTTING DATA
plot(x,y,'o',MarkerFaceColor='k',MarkerEdgeColor='none')
hold on
plot(x,y1,'--')
What is the problem? You are confused by the line that is plotted, I know. That just means your data was not sorted. Plot connects the dots. But if the dots are not in sequential order for x, then it back tracks.
diff(x)
ans = 1×24
-5.6000 1.1000 28.0000 2.6000 9.9000 3.1000 2.3000 -6.0000 -13.2000 -11.1000 -17.5000 -0.8000 11.0000 7.7000 1.7000 10.8000 10.7000 0 4.5000 -2.4000 -14.0000 -13.5000 -11.2000 -4.8000
Do you see some the elements of x are not uniformly increasing? The negative elements in the diff tell you that.
[xs,tags] = sort(x);
ys = y(tags);
yshat = polyval(p,xs);
figure
plot(x,y,'o',MarkerFaceColor='k',MarkerEdgeColor='none')
hold on
plot(xs,yshat,'--')
There is no problem when you plot only the points as dots, but when you connect the dots, watch out!!!!!!!

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangePolynomials についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by