% 
%  Complex Fourier Series
%
%  inputs:  Tlow and Thigh: The period goes from Tlow to Thigh
%           N = the number of terms to use in the Fourier series
%
function Fourier_Series(Tlow,Thigh,N)
global T m
%
%  set the fundamental period
%
  T = Thigh-Tlow;
%
%  first numerically integrate to get the dc value, 
%  don't worry about how this is being done
%
  c0 = (1/T)*quadl(@fcn,Tlow,Thigh);
%
%  numerically integrate to get the remaining coefficients,
%  don't worry about how this is being done
%
  for i = 1:N
      m = i;  
      c(i) = quadl(@integrand,Tlow,Thigh);
  end;
%
%  Now compute the true function
%  don't worry about how this is being done
%
  t = linspace(Tlow,Thigh,10000);
  true_value = feval(@fcn,t);
%
% we need to look at negative and positive values for m
% so we use peoperties of Fourier Series, 
% don't worry about how this is being done, for now....
%
% The Fourier Series estimate will end up in the variable est
%
  m = 1;
  est = T*c(1)*conj(feval(@periodic,t))+T*conj(c(1))*feval(@periodic,t);
%  
  for i = 2:N
    m = i; 
    est = est + T*c(i)*conj(feval(@periodic,t));
    est = est + T*conj(c(i))*feval(@periodic,t);
  end;
  est = est+c0;
%
%  at this point the c's are in 'c0' and in the array 'c'
%
  plot(t,true_value,'-',t,real(est),'--',t,imag(est),'.-'); grid; 
  title(['Fourier Series, Numer of Terms = ' num2str(N) ]);
  legend('True','Real Estimate', 'Imaginary Estimate');
%  
return;
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%  Integrate the function times the appropriate complex exponential,
%  don't worry about this....
%
function c = integrand(x)
  c = feval(@fcn,x).*feval(@periodic,x);
return;
%
%  Determine the proper complex exponential. In order to use this with the
%  integrand functions, the harmonic and fundamental period are global
%  parameters, don't worry about this....
%
function value = periodic(x)
global T m
  value = (1/T)*exp(-j*2*pi*m*x/T);
return;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%  This is the function we want the Fourier series of
%  You will modify this for each of your functions...
%
function value = fcn(t);
%
%  the default function
%
 interval1 = (t < 2.0);
 interval2 = (t >= 2.0) & ( t < 3.0);
 interval3 = (t >= 3) & (t < 4);
 interval4 = (t>=4);
%
%  return the function in the variable `value'
%
  value = (0.135*exp(t)).*interval1 + 1*interval2 ...
      + (4-t).*interval3 + 0*interval4;
%
  return;
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%  Your functions go down here ..................
%