% fourier plot
close all; clear all; clc;

load exam_2020.mat

pos = [50 100 600 400]; % [left bottom width height]
figure('Position',pos);
%%
for N = 1:20,
    hold off;
    plot(x, f, 'k', 'linewidth', 1.2)
    grid;
    A0 = sum(f.*ones(size(x))) * dx * 2/L;
    fFS = A0/2;
    for k = 1:N
        Ak = sum(f.*cos(2*pi*k*x/L))*dx*2/L;
        Bk = sum(f.*sin(2*pi*k*x/L))*dx*2/L;
        fFS = fFS + Ak*cos(2*k*pi*x/L) + Bk*sin(2*k*pi*x/L);
    end
    hold on;
    plot(x,fFS, 'r-', 'linewidth', 1.2);
    axis([-0.1,2*pi+0.1,-1.7, 1.7]);
    xlabel('Time','interpreter', 'latex','fontsize', 14);
    xticks([0, pi, 2*pi]);
    xticklabels({'-\pi', '0', '\pi'});
    a = get(gca, 'XTickLabel');
    set(gca,'xticklabel',a,'fontsize', 14);
    drawnow;
    text(3*L/4,1.5,strcat('No.Terms:'," ",num2str(N)));
    pause(0.1);
end

