quadratic curvature doesn't went smoothly

4 views (last 30 days)
nirwana
nirwana on 24 Oct 2023
Edited: John D'Errico on 24 Oct 2023
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

Answers (1)

John D'Errico
John D'Errico on 24 Oct 2023
Edited: John D'Errico on 24 Oct 2023
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!!!!!!!

Community Treasure Hunt

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

Start Hunting!