% MATH 3150 Fall 2008
% Demo for Fourier series approximation of sawtooth function
%  f(x) = pi - x for 0<=x<=2*pi
x = linspace(-2*pi,4*pi,1000);

% plot true function for refernce
hold on;
% trick to make 2*pi periodic function from fn def on [0,2*pi]
plot(x,pi-mod(x,2*pi)); 
axis([-2*pi,4*pi,-4,4]);
pause;

% loop over number of terms
Ns = [1,2,5,10,15];
cols={'g','m','c','r','k'};
for iN = 1:length(Ns),
 N=Ns(iN);

 % compute partial Fourier series
 s=zeros(size(x));
 for n=1:N,
  s=s+(2/n)*sin(n*x);
 end;

 % comparative plot
 plot(x,s,cols{iN});
 pause; % stops program until user presses any key
end;%N

