filename="h1_train_small.csv";
datatrain=csvread(filename);
xtrain=datatrain(2:rows(datatrain),1);
ytrain=datatrain(2:rows(datatrain),2);
filename="error_train_small.csv";
fidts=fopen(filename,"w");
filetest='h1_test.csv';
datatest=csvread(filetest);
xtest=datatest(2:rows(datatest),1);
ytest=datatest(2:rows(datatest),2);
filetest="error_test_small.csv";
fidtt=fopen(filetest,"w");
fileval="h1_validate.csv";
dataval=csvread(fileval);
xval=dataval(2:rows(dataval),1);
yval=dataval(2:rows(dataval),2);
errind=0;
fileval='validate_result_small.txt';
fpt=fopen(fileval,"w");
fpoly=fopen('polycoeff_small.txt',"w");

for i=[1,2,3,4,6,10]
 errortrain=0;
 errortest=0;
 errorval=0;
 savetrain=strcat('train_small_',num2str(i-1),".jpeg");
 savetest=strcat('test_small_',num2str(i-1),".jpeg");
 saveval=strcat('val_small_',num2str(i-1),".jpeg");
 p=polyfit(xtrain,ytrain,i-1);
 fprintf(fpoly, '%.02f,', p);
 fprintf(fpoly, '\n');
 for j=1:rows(xtrain)
  errortrain+=(1/2)*(ytrain(j)-polyval(p,xtrain(j)))^2;
 endfor
 errortrain=sqrt(2*errortrain/rows(xtrain));
 for j=1:rows(xtest)
  errortest+=(1/2)*(ytest(j)-polyval(p,xtest(j)))^2;
 endfor
 errortest=sqrt(2*errortest/rows(xtest));
 fputs(fidts,strcat(num2str(i-1),",",num2str(errortrain),"\n"));
 fputs(fidtt,strcat(num2str(i-1),",",num2str(errortest),"\n"));
 for j=1:rows(xval)
  errorval+=(1/2)*(yval(j)-polyval(p,xval(j)))^2;
 endfor
 errorval=sqrt(2*errorval/rows(xval));
 fputs(fpt,strcat(num2str(i-1),",",num2str(errorval),"\n"));
 if i==1
  minerr=errorval;
 else
  if errorval<minerr
   minerr=errorval;
   errind=i-1;
  endif
 endif
 xcurve=linspace(xtrain(1),xtrain(rows(xtrain)));
 ycurve=polyval(p,xcurve);
 figure;
 fig=gcf;
 plot(xtrain,ytrain,'o');
 hold on
 plot(xcurve,ycurve);
 print(fig,savetrain) -djpeg;
 hold off
 xcurve=linspace(xtest(1),xtest(rows(xtest)));
 ycurve=polyval(p,xcurve);
 figure;
 fig=gcf;
 plot(xtest,ytest,'o');
 hold on
 plot(xcurve,ycurve);
 print(fig,savetest) -djpeg;
 hold off;
 xcurve=linspace(xval(1),xval(rows(xval)));
 ycurve=polyval(p,xcurve);
 figure;
 fig=gcf;
 plot(xval,yval,'o');
 hold on
 plot(xcurve,ycurve);
 print(fig,saveval) -djpeg;
 hold off;
endfor

fclose(fidts);
fclose(fidtt);
fclose(fpoly);

fputs(fpt,strcat("The optimal degree for polynomial is :",num2str(errind),"\n"));
fclose(fpt);

datatrain=csvread(filename);
datatest=csvread(filetest);
dataval=csvread(fileval);
xerrortrain=datatrain(:,1);
yerrortrain=datatrain(:,2);
xerrortest=datatest(:,1);
yerrortest=datatest(:,2);
xerrorval=dataval(1:6,1);
yerrorval=dataval(1:6,2);
figure;
fig=gcf;
plot(xerrortrain,yerrortrain,'-ob');
hold on
print(fig,'error_vs_degree_train_small.jpeg') -djpeg;
hold off;
figure;
fig=gcf;
plot(xerrortest,yerrortest,'-or');
hold on
print(fig,'error_vs_degree_test_small.jpeg') -djpeg;
hold off;
figure;
fig=gcf;
plot(xerrorval,yerrorval,'-og');
hold on
print(fig,'error_vs_degree_val_small.jpeg') -djpeg;
hold off;



filename="h1_train_big.csv";
datatrain=csvread(filename);
xtrain=datatrain(2:rows(datatrain),1);
ytrain=datatrain(2:rows(datatrain),2);
filename="error_train_big.csv";
fidts=fopen(filename,"w");
filetest='h1_test.csv';
datatest=csvread(filetest);
xtest=datatest(2:rows(datatest),1);
ytest=datatest(2:rows(datatest),2);
filetest="error_test_big.csv";
fidtt=fopen(filetest,"w");
fileval="h1_validate.csv";
dataval=csvread(fileval);
xval=dataval(2:rows(dataval),1);
yval=dataval(2:rows(dataval),2);
errind=0;
fileval='validate_result_big.txt';
fpt=fopen(fileval,"w");
fpoly=fopen('polycoeff_big.txt',"w");

for i=[1,2,3,4,6,10]
 errortrain=0;
 errortest=0;
 errorval=0;
 savetrain=strcat('train_big_',num2str(i-1),".jpeg");
 savetest=strcat('test_big_',num2str(i-1),".jpeg");
 saveval=strcat('val_big_',num2str(i-1),".jpeg");
 p=polyfit(xtrain,ytrain,i-1);
 fprintf(fpoly, '%f,', p);
 fprintf(fpoly, '\n');
 for j=1:rows(xtrain)
  errortrain+=(1/2)*(ytrain(j)-polyval(p,xtrain(j)))^2;
 endfor
 errortrain=sqrt(2*errortrain/rows(xtrain));
 for j=1:rows(xtest)
  errortest+=(1/2)*(ytest(j)-polyval(p,xtest(j)))^2;
 endfor
 errortest=sqrt(2*errortest/rows(xtest));
 fputs(fidts,strcat(num2str(i-1),",",num2str(errortrain),"\n"));
 fputs(fidtt,strcat(num2str(i-1),",",num2str(errortest),"\n"));
 for j=1:rows(xval)
  errorval+=(1/2)*(yval(j)-polyval(p,xval(j)))^2;
 endfor
 errorval=sqrt(2*errorval/rows(xval));
 fputs(fpt,strcat(num2str(i-1),",",num2str(errorval),"\n"));
 if i==1
  minerr=errorval;
 else
  if errorval<minerr
   minerr=errorval;
   errind=i-1;
  endif
 endif
 xcurve=linspace(xtrain(1),xtrain(rows(xtrain)));
 ycurve=polyval(p,xcurve);
 figure;
 fig=gcf;
 plot(xtrain,ytrain,'o');
 hold on
 plot(xcurve,ycurve);
 print(fig,savetrain) -djpeg;
 hold off
 xcurve=linspace(xtest(1),xtest(rows(xtest)));
 ycurve=polyval(p,xcurve);
 figure;
 fig=gcf;
 plot(xtest,ytest,'o');
 hold on
 plot(xcurve,ycurve);
 print(fig,savetest) -djpeg;
 hold off;
 xcurve=linspace(xval(1),xval(rows(xval)));
 ycurve=polyval(p,xcurve);
 figure;
 fig=gcf;
 plot(xval,yval,'o');
 hold on
 plot(xcurve,ycurve);
 print(fig,saveval) -djpeg;
 hold off;
endfor

fclose(fidts);
fclose(fidtt);
fclose(fpoly);

fputs(fpt,strcat("The optimal degree for polynomial is :",num2str(errind),"\n"));
fclose(fpt);

datatrain=csvread(filename);
datatest=csvread(filetest);
dataval=csvread(fileval);
xerrortrain=datatrain(:,1);
yerrortrain=datatrain(:,2);
xerrortest=datatest(:,1);
yerrortest=datatest(:,2);
xerrorval=dataval(1:6,1);
yerrorval=dataval(1:6,2);
figure;
fig=gcf;
plot(xerrortrain,yerrortrain,'-ob');
hold on
print(fig,'error_vs_degree_train_big.jpeg') -djpeg;
hold off;
figure;
fig=gcf;
plot(xerrortest,yerrortest,'-or');
hold on
print(fig,'error_vs_degree_test_big.jpeg') -djpeg;
hold off;
figure;
fig=gcf;
plot(xerrorval,yerrorval,'-og');
hold on
print(fig,'error_vs_degree_val_big.jpeg') -djpeg;
hold off;
