% P1
% Tests the function TrigFit

  close all
  P = 3;
  N = 7;
  m = 1000;
  t = linspace(0,10,1000)';
  tVals = (2*pi/P)*t;
  y = (2*cos(1*tVals)   - 5*sin(1*tVals))/1 +...
      (7*cos(2*tVals)   + 3*sin(2*tVals))/100 +...
      (2*cos(3*tVals)   - 9*sin(3*tVals))/2 +...
      (9*cos(4*tVals)   + 1*sin(4*tVals))/10 +...
      (4*cos(5*tVals)   - 3*sin(5*tVals))/1000 +...
      (5*cos(6*tVals)   - 1*sin(6*tVals))/7 +...
      .3*randn;
  
  plot(t,y)
  
  [A,B] = TrigFit(t,y,N,P);
  
  clc
  for n=1:N
      a = A(1:n,n); 
      b = B(1:n,n);
      fprintf('\n\nn = %1d\n',n)
      disp(['  a:'  sprintf(' %8.4f',a)])
      disp(['  b:'  sprintf(' %8.4f',b)])
      yn = zeros(m,1);
      for j=1:n
          yn = yn + a(j)*cos((2*pi*j/P)*t) + b(j)*sin((2*pi*j/P)*t);
      end
      figure
      plot(t,y,'k',t,yn,'r')
      title(sprintf('n = %1d  DO NOT TURN IN THIS PLOT!',n))
      shg 
      pause
  end