Remove["Global`*"];

n=10;
sigma=1/4;
f[t_] = Sin[2 π t];
times=Table[i/n,{i,0,n}];
testTimes=Table[(1/20)+i/n,{i,0,n}];
data=Table[{times[[i]], f[times[[i]]]+RandomReal[NormalDistribution[0,sigma]]},{i,1,n+1}];
testData=Table[{testTimes[[i]], f[testTimes[[i]]]+RandomReal[NormalDistribution[0,sigma]]},{i,1,n+1}];
curveFit[m_, data_] := Module[{modelParameters,y,sumOfSquares,leastSqrsSoln},
modelParameters=Table[Symbol["w"<>ToString[i-1]],{i,m}];
y=Table[modelParameters[[i]] x^(i-1),{i,1,m}];
y=Total[y];
sumOfSquares =Sum[((y/.x->data[[i,1]])-data[[i,2]])^2,{i,1,n+1}]/n;
leastSqrsSoln=NMinimize[sumOfSquares,modelParameters,AccuracyGoal->16,PrecisionGoal->16][[2]];
y /. leastSqrsSoln
];
marker1=Graphics[{Red,Circle[]}];
marker2=Graphics[{Green,Circle[]}];
g1=ListPlot[{data, testData}, PlotRange->{-1.5,1.5}, PlotMarkers->{{marker1,0.05},{marker2,0.05}}, DataRange->{0,1.05},PlotStyle->Thick ];
g2 = Plot[f[x], {x,0,1.05}, PlotRange->{-1.5,1.5}, PlotStyle->Thick];

Show[g1,g2]

Manipulate[Show[Plot[Evaluate[{f[x],curveFit[m,data] }] , {x,0,1}, PlotRange->{-1.5,1.5}, PlotStyle->Thick], g1],{m,1,11,1}]

trainingError=Table[{i,0},{i,1,n+1}];
testError=Table[{i,0},{i,1,n+1}];
For[m=1,m<n+1,m++,
y= curveFit[m,data];
trainingError[[m,2]] =Sum[((y/.x->data[[i,1]])- data[[i,2]])^2,{i,1,n+1}]/n;
testError[[ m,2]]=Sum[((y/.x->testData[[i,1]])-testData[[i,2]])^2,{i,1,n+1}]/n;
];

ListLogPlot[{trainingError, testError},  PlotMarkers->{{marker1,0.05},{marker2,0.05}}, PlotRange->{{0.0,10.5},{0,Max[testError[[All,2]]]+1}},PlotStyle->Thick, Frame->{True,True,False,False}, FrameLabel->{"Order of Polynomial, m","Error"} ]