%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                            Michael Pokojovy                             % 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function main;
    figure(1);

    set(gcf, 'PaperUnits', 'centimeters');
    xSize = 20; ySize = 16;
    xLeft = (21 - xSize)/2; yTop = (30 - ySize)/2;
    set(gcf,'PaperPosition', [xLeft yTop xSize ySize]);
    set(gcf,'Position',[0 0 xSize*50 ySize*50]);

    N = 1000;
    x = linspace(-pi, pi, N);

    M = 11;

    delay = 0.2;

    for i = 1:M
        clf;
        hold on;

        t = title(['Die ersten $n = ', num2str(i - 1), '$ Glieder in der Fourierentwicklung der Funktion ', '$f \colon [-\pi, \pi] \ni x \mapsto x^{2}$'], 'interpreter', 'latex');
        set(t, 'FontSize', 16);    

        plot(x, x.^2, 'r');
        plot(x, f(x, i - 1), 'b');
        axis([-pi pi 0 pi^2 + 2]);
        
        xlabel('x');
        ylabel('f(x)');
        
        hleg = legend('$x \mapsto x^{2}$','$x \mapsto \frac{\pi^{2}}{3} + \sum\limits_{k = 1}^{n} \frac{4 (-1)^{k}}{k^{2}} \cos(kx)$');
        set(hleg, 'interpreter', 'latex');
        set(hleg, 'FontSize', 16);  
        
        pause(delay);
    end    

    function y = f(x, n)
        y = zeros(size(x));

        y = y + pi^2/3;

        I = ones(size(x));

        for k = 1:n
            y = y + 4*(-I).^(-k)./(k*I).^2.*cos(k*x);
        end
    end
end