%%% change as appropriate for your file names  
load 'xdata.txt'
load 'ydata.txt'


numdatapts = length(xdata);
clf
x=xdata;
y=ydata;
h = plot(x,y,'ro'); hold on; 
totsqerr=0

for i=1:numdatapts  % leave out each datapoint in turn
  if i==1
    xuse=x(2:numdatapts);
    yuse=y(2:numdatapts);
  elseif i==numdatapts
    xuse=x(1:numdatapts-1);
    yuse=y(1:numdatapts-1);
  else
    xuse=x([1:i-1, i+1:numdatapts]);
    yuse=y([1:i-1, i+1:numdatapts]);
  end
  xleftout=x(i);
  
  xuse=xuse';
  yuse=yuse';
  
  length(xuse)
  length(yuse)
    
  start = [1];
  options = optimset('TolX',0.1);  % termination tolerance on x 
  estimated_c = fminsearch(@(xx) hw4fitfun(xx,xuse,yuse,h),start,options)

  A=[xuse  sin(estimated_c*xuse) ones(length(xuse),1)];
  abd=A\yuse
  
  xp=0:.1:6;
  %%%%%%%% write next line to compute yp

  
  %%%%%%%%%
  plot(xp,yp,'k')

  %%%%%%%%% write next line to compute  yleftout (you can write it in one
  %%%%%%%%% line of code  (you don't need to write  a predictyval function

  
  %%%%%%%%%
  %%% compute squared error for left out point
  sqerr=(yleftout-y(i))^2;
  totsqerr=sqerr+totsqerr;
end
rootmeansqerr=sqrt(totsqerr/numdatapts)
