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

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

% compute t_i's, and array for f(t)
t=-1.1*pi:dt:1.1*pi; f=t.^3;

% initialize array for partial sums
S_0 = zeros(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,:)+ sgn*(-2*pi^2/j+12/j^3)*sin(j*t);
    % plot (first clear, set axis, plot f and S_{j+1} against t)
    clf;axis([-1.1*pi,1.1*pi,-35,35]);hold on
    plot(t,f,'k',t,S(j+1,:),'b');pause;hold off;
  end;

