필터 지우기
필터 지우기

quadratic curvature doesn't went smoothly

조회 수: 1 (최근 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 CenterFile Exchange에서 Get Started with Curve Fitting Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by