Are these polyfit curves correct?

조회 수: 1 (최근 30일)
Giovanni Virgen
Giovanni Virgen 2018년 7월 19일
댓글: Walter Roberson 2018년 7월 19일
This is what I have to do:
Create an m-file that uses the polyfit, linspace, polyval and plot functions to create a plot of the data listed below along with a least-squares line, a third-degree polynomial and a five-degree polynomial fit. Plot the curves with 100 data points (linspace default) to produce smooth fit curves.
x = [-8, -6, -4, -2, -1, 0, 1, 2, 4, 6, 8]
y = [0.1, 0.2, 0.5, 0.7, 2.2, 3.8, 5.5, 3.6, 2.8, 4.0, 3.5]
This is what I have so far.
x = linspace(-8,8);
y = linspace(0.1,3.5);
p = polyfit(x,y,3);
x2 = 0:0.1:3.1;
y2 = polyval(p,x2);
v = polyfit(x,y,5);
c = polyval(v,x2);
subplot(2,1,1)
plot(x,y,x2,y2)
legend('x,y,polyfit')
xlabel('x')
ylabel('y')
subplot(2,1,2)
plot(x,y,x2,c)
legend('x,y,polyfit')
xlabel('x')
ylabel('y')

채택된 답변

madhan ravi
madhan ravi 2018년 7월 19일
편집: madhan ravi 2018년 7월 19일
Codes are correct
%one remark
%x2=-8:0.1:8
%because the start and endpoint should be similar to x.
plot(x,y,'ob'x2,y2,'')
%use line markers to distinguish between them :)

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by