quadratic curvature doesn't went smoothly

3 次查看(过去 30 天)
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 中查找有关 Polynomials 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by