%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Homework problem #14 (a) on page 34 of text.
% Fourier partial sums for f(t) = t^2.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

clear;        % clear any previous variables
K=100;J=20;   % set # of subdivisions, # of terms in series
dt=2*pi/K;    % compute delta t

% compute t_k's, and array for f(t)
t=-pi:dt:pi; f=t.^2;

% initialize array for partial sums
S_0 = pi^2/3 * ones(size(t));
S(1,:)=S_0; 

% set up variable for alternating signs (-1)^j
sgn=1;

% build partial sum approximations S_j(t) for all t
% as j loops from 1 to J 
for j=1:J;
    sgn = -sgn;
    % add next term (vector) to partial sum
    S(j+1,:)=S(j,:)+ 4*sgn*cos(j*t)/j^2;
    % plot (first clear, set axis, plot f and S_{j+1} against t)
    clf;axis([-pi,pi,-10,10]);hold on
    plot(t,f,t,S(j+1,:));pause;hold off;
  end;

