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

clear

x = -pi:0.01:pi;  % plot the function over the appropriate range
f = x ; 

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) ; 

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

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


figure(2) 
