MATLAB: Are these polyfit curves correct

linspaceMATLABpolyfitpolyval

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')

Best Answer

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 :)