% Fourier Cosine Series for the Delta Function, Animation adds a new term per frame.
% David Bloore, August 9, 2011

ccc=input('Case?');

switch ccc;
    case 1
        lb=-pi/2;
        ub=pi/2;
        s=10000;
        t=2000;
    case 2
        lb=-pi/4;
        ub=pi/4;
        s=10000;
        t=2000;
end

x=linspace(lb,ub,s);

y=1/(2*pi)+1/pi*cos(x);
n=1;

p=plot(x,y);
legend('Truncated Fourier Series Terms','Location','best');
title('Truncated Fourier Cosine Series for the Delta Function');
xlabel([sprintf('%d',lb) ' \leq x \leq ' sprintf('%d',ub)]);
ylabel('y');

% Very interesting things are seen at different limit settings

% ylim([-2 2]); % for case 1
ylim([-1 1]);
% ylim([-1.5 1.5]);
% xlim([-0.4 0.4]);

grid on;
set(gcf,'Position',[0 356 1440 900]);

while n<t+1
    n=n+1;
    for cc=1:s;
        y(cc)=y(cc)+1/pi*cos(n*x(cc));
    end
    set(p,'YData',y);
    drawnow
end