%get data
myf=@(x) 2*sin(1.5*x);
xeq=linspace(0,5,100);
x25=5*rand(25,1);
e25=randn(25,1);
y25=myf(x25)+e25;
x75=5*rand(75,1);
e75=randn(75,1);
y75=myf(x75)+e75;
degree=1;
V=ones(25,degree+1);
V(:,2)=x25;
A=V'*V;
b=V'*y25;
w=A\b;
%disp(w)
fit=V*w;
Ein(degree)=(fit-y25)'*(fit-y25);
D=ones(75,degree+1);
D(:,2)=x75;
valfit=D*w;
Eval(degree)=(valfit-y75)'*(valfit-y75);
degree=2;
V=ones(25,degree+1);
V(:,2)=x25;
V(:,3)=V(:,2).*x25;
A=V'*V;
b=V'*y25;
w=A\b;
%disp(w)
fit=V*w;
Ein(degree)=(fit-y25)'*(fit-y25);
D=ones(75,degree+1);
D(:,2)=x75;
D(:,3)=D(:,2).*x75;
valfit=D*w;
Eval(degree)=(valfit-y75)'*(valfit-y75);
degree=3;
V=ones(25,degree+1);
V(:,2)=x25;
V(:,3)=V(:,2).*x25;
V(:,4)=V(:,3).*x25;
A=V'*V;
b=V'*y25;
w=A\b;
%disp(w)
fit=V*w;
Ein(degree)=(fit-y25)'*(fit-y25);
D=ones(75,degree+1);
D(:,2)=x75;
D(:,3)=D(:,2).*x75;
D(:,4)=D(:,3).*x75;
valfit=D*w;
Eval(degree)=(valfit-y75)'*(valfit-y75);
degree=4;
V=ones(25,degree+1);
V(:,2)=x25;
V(:,3)=V(:,2).*x25;
V(:,4)=V(:,3).*x25;
V(:,5)=V(:,4).*x25;
A=V'*V;
b=V'*y25;
w=A\b;
%disp(w)
fit=V*w;
Ein(degree)=(fit-y25)'*(fit-y25);
D=ones(75,degree+1);
D(:,2)=x75;
D(:,3)=D(:,2).*x75;
D(:,4)=D(:,3).*x75;
D(:,5)=D(:,4).*x75;
valfit=D*w;
Eval(degree)=(valfit-y75)'*(valfit-y75);
degree=5;
V=ones(25,degree+1);
V(:,2)=x25;
V(:,3)=V(:,2).*x25;
V(:,4)=V(:,3).*x25;
V(:,5)=V(:,4).*x25;
V(:,6)=V(:,5).*x25;
A=V'*V;
b=V'*y25;
w=A\b;
%disp(w)
fit=V*w;
Ein(degree)=(fit-y25)'*(fit-y25);
D=ones(75,degree+1);
D(:,2)=x75;
D(:,3)=D(:,2).*x75;
D(:,4)=D(:,3).*x75;
D(:,5)=D(:,4).*x75;
D(:,6)=D(:,5).*x75;
valfit=D*w;
Eval(degree)=(valfit-y75)'*(valfit-y75);
plot(Ein)
hold on
plot(Eval)
hold off
x100=x25;
y100=y25;
for i=1:75
    x100(i+25)=x75(i);
    y100(i+25)=y75(i);
end
V=ones(100,4);
V(:,2)=x100;
V(:,3)=V(:,2).*x100;
V(:,4)=V(:,3).*x100;
A=V'*V;
b=V'*y100;
w=A\b;
%disp(w)
bestfit=V*w;
ybar=sum(y100)/100;
Ein0=(y100-ybar)'*(y100-ybar);
bestEin=(bestfit-y100)'*(bestfit-y100);
rsq=1-(bestEin/Ein0);
disp(rsq)
figure(2)
plot(xeq,myf(xeq))
hold on
plot(x100,bestfit,'b*')
plot(x100,y100,'r+')
hold off