You did it correctly. The curve is linear between the points because that is how MATLAB plots these things. If you zoom in high enough, there are lines connecting the points even on the curves that look smooth.
If you want your polynomial to look smooth, make the spacing smaller:
t = 0:0.001:1;
k = sin(2*pi*t);
x = linspace(0,1,10);
y = sin(2*pi*x);
r = randn(1,10);
y_noise = y + r;
p = polyfit(x,y,9);
x2 = x;
y2 = polyval(p,x2);
x3 = linspace(0,1,100);
y3 = polyval(p,x3);
plot(x,y,'ob',x3,y3,'r',t,k,'k')
grid on
RMSE = sqrt(mean((y-y2).^2+(x-x2).^2));
.
.
.
EDIT
Notice that what you probably meant to do was something like this, which shows why higher order polynomial fitting is not always a good idea. As N gets larger, the fit gets better:
N = 10;
t = 0:0.001:1;
k = sin(2*pi*t);
x = linspace(0,1,10);
y = sin(2*pi*x);
r = randn(1,10)/N;
y_noise = y + r;
p = polyfit(x,y_noise,9);
x2 = x;
y2 = polyval(p,x2);
x3 = linspace(0,1,100);
y3 = polyval(p,x3);
plot(x,y_noise,'ob',t,k,'k',x3,y3,'r')
legend({'Noisy Sin(2\pix)';'six(2\pix)';'Polyfit'})
grid on
RMSE = sqrt(mean((y-y2).^2+(x-x2).^2));
Best Answer