
% Fourier series for f(x) = pi^2-t^2 on -pi < x < pi 

clear

x = -pi:0.01:pi;      % plot the function over the appropriate range
f = pi^2-x.^2 - 2*pi^2/3 ; 

figure(1)
hold on 
%subplot(2,2,1)
plot(x,f, 'b') 

x = -10:0.01:10; 

sx = length(x) ; 

F5  = zeros(1,sx) ; % create empty arrays to hold the approximations
F10 = zeros(1,sx) ; 
F20 = zeros(1,sx) ; 

F5  =  F5  ; % load up the constant terms 
F10 =  F10  ; 
F20 =  F20  ; 

for j = 1:5                   % sum the series to the appropriate level
    F5 = F5    + 4 * j^(-2) * (-1)^(j+1) * cos (j*x) ; 
    figure(2)
    subplot(5,1,j)
    plot(x,F5, 'b',x, 4 * j^(-2) * (-1)^(j+1) * cos (j*x), 'r') 
end
for j = 1:10
    F10 = F10  + 4 * j^(-2) * (-1)^(j+1) * cos (j*x) ;
end
for j = 1:20
    F20 = F20  + 4 * j^(-2) * (-1)^(j+1) * cos (j*x) ;
end

figure(1)
plot(x, F5, 'r', x, F10, 'm', x, F20, 'g')   % plot these 
title('f, F5, F10, F20')

figure(2)

