%% How to use a training and test set % % First, the original data: N=80; %Number of points overall xorig=linspace(-4,7,N); xorig=xorig(:); yorig=sin(xorig)+0.1*randn(size(xorig)); % Set up the training and testing sets: We'll take the test set to be 80 % percent of the data. k=randperm(N); k1=round(0.8*N); xtrain=xorig(k(1:k1)); xtest=xorig(k(k1+1:N)); ytrain=yorig(k(1:k1)); ytest=yorig(k(k1+1:N)); tt=linspace(-4,7,100); %For plotting only tt=tt'; %% Training and Testing % % Only use the data in xtrain to build the polynomial for j=1:length(xtrain) V=vander1(xtrain,j); a=V\ytrain; % yy=polyval(a,tt); % plot(xtrain,ytrain,'r*',tt,yy); % pause z=polyval(a,xtest); Error(j)=sum((z-ytest).^2); end