clear all; close all; clc

% define domain
L=pi;
N=1024; % num samples
dx=2*L/(N-1); % delta x
x=-L:dx:L; % -pi to pi


% define ____/\____ function
func = 0*x;
f1 = 2/pi*x +1;
f2 = -2/pi*x +1;
func(N/4:N/2) = f1(N/4:N/2);
func(N/2+1:3*N/4) = f2(N/2+1:3*N/4);
plot(x, func, '-k','LineWidth',3.5); hold on

figure;
n = 20;
CC=jet(n); % just getting some colormap array to visualize
A0=sum(func.*ones(size(x)))*dx/pi;
fFS = A0/2;

for k=1:n
    A(k) = sum(func.*cos(pi*k*x/L))*dx/pi;
    B(k) = sum(func.*sin(pi*k*x/L))*dx/pi;
    fFS = fFS + A(k)*cos(k*pi*x/L) + B(k)*sin(k*pi*x/L);
    plot(x, fFS,'-', 'Color', CC(k,:), 'LineWidth', 2)
    pause(0.2)
end 

